Skip to content

Commit f80d373

Browse files
committed
Add solution 014
1 parent 0a0d11c commit f80d373

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
<a href="https://github.com/yanglbme/leetcode"><img src="https://img.shields.io/badge/Lang-Java%2FPython%2FJS%2FCPP%2FGo%2F...-blue.svg" alt="Language"></a>
88
</p>
99

10-
<h2 align="center">LeetCode Solution</h2>
11-
10+
## Introduction
1211
Complete solutions to Leetcode problems, updated daily.
1312

1413
## Solution List
@@ -20,6 +19,7 @@ Complete solutions to Leetcode problems, updated daily.
2019
| 001 | [Two Sum](https://github.com/yanglbme/leetcode/tree/master/solution/001.Two%20Sum) | `Array`, `Hash Table` |
2120
| 007 | [Reverse Integer](https://github.com/yanglbme/leetcode/tree/master/solution/007.Reverse%20Integer) | `Math` |
2221
| 013 | [Roman to Integer](https://github.com/yanglbme/leetcode/tree/master/solution/013.Roman%20to%20Integer) | `Math`, `String` |
22+
| 014 | [Longest Common Prefix](https://github.com/yanglbme/leetcode/tree/master/solution/014.Longest%20Common%20Prefix) | `String` |
2323
| 021 | [Merge Two Sorted Lists](https://github.com/yanglbme/leetcode/tree/master/solution/021.Merge%20Two%20Sorted%20Lists) | `Linked List` |
2424
| 053 | [Maximum Subarray](https://github.com/yanglbme/leetcode/tree/master/solution/053.Maximum%20Subarray) | `Array`, `Divide and Conquer`, `Dynamic Programming` |
2525
| 070 | [Climbing Stairs](https://github.com/yanglbme/leetcode/tree/master/solution/070.Climbing%20Stairs) | `Dynamic Programming` |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
## 最长公共前缀
2+
### 题目描述
3+
4+
编写一个函数来查找字符串数组中的最长公共前缀。
5+
6+
如果不存在公共前缀,返回空字符串 ""。
7+
8+
示例 1:
9+
```
10+
输入: ["flower","flow","flight"]
11+
输出: "fl"
12+
```
13+
14+
示例 2:
15+
```
16+
输入: ["dog","racecar","car"]
17+
输出: ""
18+
解释: 输入不存在公共前缀。
19+
```
20+
21+
说明:
22+
23+
所有输入只包含小写字母 a-z 。
24+
25+
### 解法
26+
取字符串数组第一个元素,遍历每一个字符,与其他每个字符串的对应位置字符做比较,如果不相等,退出循环,返回当前子串。
27+
28+
注意:其他字符串的长度可能小于第一个字符串长度,所以要注意数组越界异常。
29+
30+
```java
31+
class Solution {
32+
public String longestCommonPrefix(String[] strs) {
33+
if (strs == null || strs.length == 0) {
34+
return "";
35+
}
36+
if (strs.length == 1) {
37+
return strs[0];
38+
}
39+
40+
char[] chars = strs[0].toCharArray();
41+
int i = 0;
42+
boolean flag = true;
43+
for (; i < chars.length; ++i) {
44+
char ch = chars[i];
45+
46+
for (int j = 1; j < strs.length; ++j) {
47+
if (strs[j].length() <= i) {
48+
flag = false;
49+
break;
50+
}
51+
if (strs[j].charAt(i) != ch) {
52+
flag = false;
53+
break;
54+
}
55+
56+
}
57+
if (!flag) {
58+
break;
59+
}
60+
}
61+
return strs[0].substring(0, i);
62+
63+
64+
}
65+
}
66+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public String longestCommonPrefix(String[] strs) {
3+
if (strs == null || strs.length == 0) {
4+
return "";
5+
}
6+
if (strs.length == 1) {
7+
return strs[0];
8+
}
9+
10+
char[] chars = strs[0].toCharArray();
11+
int i = 0;
12+
boolean flag = true;
13+
for (; i < chars.length; ++i) {
14+
char ch = chars[i];
15+
16+
for (int j = 1; j < strs.length; ++j) {
17+
if (strs[j].length() <= i) {
18+
flag = false;
19+
break;
20+
}
21+
if (strs[j].charAt(i) != ch) {
22+
flag = false;
23+
break;
24+
}
25+
26+
}
27+
if (!flag) {
28+
break;
29+
}
30+
}
31+
return strs[0].substring(0, i);
32+
33+
34+
}
35+
}

0 commit comments

Comments
 (0)