Skip to content

Commit 8b6ff30

Browse files
committed
feat: add 014
1 parent 42404ed commit 8b6ff30

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
|8|[String to Integer (atoi)][008]|Math, String|
1212
|9|[Palindrome Number][009]|Math|
1313
|13|[Roman to Integer][013]|Math, String|
14+
|14|[Longest Common Prefix][014]|String|
1415

1516

1617
## Medium
@@ -36,3 +37,4 @@
3637
[008]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/008/README.md
3738
[009]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/009/README.md
3839
[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

note/013/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Input is guaranteed to be within the range from 1 to 3999.
99
**Tags:** Math, String
1010

1111

12-
## 思路0
12+
## 思路
1313

1414
题意是罗马数字转整型数,范围从1到3999,查看下百度百科的罗马数字介绍如下,
1515

note/014/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

0 commit comments

Comments
 (0)