File tree 4 files changed +63
-1
lines changed
project/LeetCode/leetcode/src/main/java/com/blankj/easy/_014
4 files changed +63
-1
lines changed Original file line number Diff line number Diff line change 11
11
| 8| [ String to Integer (atoi)] [ 008 ] | Math, String|
12
12
| 9| [ Palindrome Number] [ 009 ] | Math|
13
13
| 13| [ Roman to Integer] [ 013 ] | Math, String|
14
+ | 14| [ Longest Common Prefix] [ 014 ] | String|
14
15
15
16
16
17
## Medium
36
37
[ 008 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/008/README.md
37
38
[ 009 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/009/README.md
38
39
[ 013 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/013/README.md
40
+ [ 014 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/014/README.md
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ Input is guaranteed to be within the range from 1 to 3999.
9
9
** Tags:** Math, String
10
10
11
11
12
- ## 思路0
12
+ ## 思路
13
13
14
14
题意是罗马数字转整型数,范围从1到3999,查看下百度百科的罗马数字介绍如下,
15
15
Original file line number Diff line number Diff line change
1
+ # [ Longest Common Prefix] ( https://leetcode.com/problems/longest-common-prefix/ )
2
+
3
+ ## Description
4
+
5
+ Write a function to find the longest common prefix string amongst an array of strings.
6
+
7
+ ** Tags:** String
8
+
9
+
10
+ ## 思路
11
+
12
+ 题意是让你从字符串数组中找出公共前缀,我的想法是找出最短的那个字符串的长度` minLen ` ,然后在` 0...minLen ` 的范围比较所有字符串,如果比较到有不同的字符,那么直接返回当前索引长度的字符串即可,否则最后返回最短的字符串即可。
13
+
14
+ ``` java
15
+ public class Solution {
16
+ public String longestCommonPrefix (String [] strs ) {
17
+ int len = strs. length;
18
+ if (len == 0 ) return " " ;
19
+ int minLen = 0x7fffffff ;
20
+ for (String str : strs) minLen = Math . min(minLen, str. length());
21
+ for (int j = 0 ; j < minLen; ++ j)
22
+ for (int i = 1 ; i < len; ++ i)
23
+ if (strs[0 ]. charAt(j) != strs[i]. charAt(j))
24
+ return strs[0 ]. substring(0 , j);
25
+ return strs[0 ]. substring(0 , minLen);
26
+ }
27
+ }
28
+ ```
Original file line number Diff line number Diff line change
1
+ package com .blankj .easy ._014 ;
2
+
3
+ import java .util .Arrays ;
4
+ import java .util .HashMap ;
5
+
6
+ /**
7
+ * <pre>
8
+ * author: Blankj
9
+ * blog : http://blankj.com
10
+ * time : 2017/04/21
11
+ * desc :
12
+ * </pre>
13
+ */
14
+
15
+ public class Solution {
16
+ public String longestCommonPrefix (String [] strs ) {
17
+ int len = strs .length ;
18
+ if (len == 0 ) return "" ;
19
+ int minLen = 0x7fffffff ;
20
+ for (String str : strs ) minLen = Math .min (minLen , str .length ());
21
+ for (int j = 0 ; j < minLen ; ++j )
22
+ for (int i = 1 ; i < len ; ++i )
23
+ if (strs [0 ].charAt (j ) != strs [i ].charAt (j ))
24
+ return strs [0 ].substring (0 , j );
25
+ return strs [0 ].substring (0 , minLen );
26
+ }
27
+
28
+ public static void main (String [] args ) {
29
+ Solution solution = new Solution ();
30
+ System .out .println (solution .longestCommonPrefix (new String []{"abc" , "abcd" , "ab" }));
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments