Skip to content

Commit 11e0db9

Browse files
committed
feat: add solutions to lc problem: No.0014
No.0014.Longest Common Prefix
1 parent ffe34f1 commit 11e0db9

File tree

3 files changed

+37
-66
lines changed

3 files changed

+37
-66
lines changed

solution/0000-0099/0014.Longest Common Prefix/README.md

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,20 @@
4040

4141
<!-- 这里可写通用的实现逻辑 -->
4242

43+
**方法一:字符比较**
44+
45+
我们以第一个字符串 $strs[0]$ 为基准,依次比较后面的字符串的第 $i$ 个字符是否与 $strs[0]$ 的第 $i$ 个字符相同,如果相同则继续比较下一个字符,否则返回 $strs[0]$ 的前 $i$ 个字符。
46+
47+
遍历结束,说明所有字符串的前 $i$ 个字符都相同,返回 $strs[0]$ 即可。
48+
49+
时间复杂度 $(n \times m)$,其中 $n$ 和 $m$ 分别为字符串数组的长度以及字符串的最小长度。空间复杂度 $O(1)$。
50+
4351
<!-- tabs:start -->
4452

4553
### **Python3**
4654

4755
<!-- 这里可写当前语言的特殊实现逻辑 -->
4856

49-
```python
50-
class Solution:
51-
def longestCommonPrefix(self, strs: List[str]) -> str:
52-
for l in range(len(strs[0]), -1, -1):
53-
if all(len(s) >= l and s[:l] == strs[0][:l] for s in strs):
54-
return strs[0][:l]
55-
return ''
56-
```
57-
5857
```python
5958
class Solution:
6059
def longestCommonPrefix(self, strs: List[str]) -> str:
@@ -123,26 +122,17 @@ func longestCommonPrefix(strs []string) string {
123122
### **C#**
124123

125124
```cs
126-
using System.Text;
127-
using System.Linq;
128-
129125
public class Solution {
130126
public string LongestCommonPrefix(string[] strs) {
131-
if (strs.Length == 0) return string.Empty;
132-
var sb = new StringBuilder();
133-
for (var i = 0; i < strs[0].Length; ++i)
134-
{
135-
var ch = strs[0][i];
136-
if (strs.All(str => str.Length > i && str[i] == ch))
137-
{
138-
sb.Append(ch);
139-
}
140-
else
141-
{
142-
break;
127+
int n = strs.Length;
128+
for (int i = 0; i < strs[0].Length; ++i) {
129+
for (int j = 1; j < n; ++j) {
130+
if (i >= strs[j].Length || strs[j][i] != strs[0][i]) {
131+
return strs[0].Substring(0, i);
132+
}
143133
}
144134
}
145-
return sb.ToString();
135+
return strs[0];
146136
}
147137
}
148138
```

solution/0000-0099/0014.Longest Common Prefix/README_EN.md

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,18 @@
3535

3636
## Solutions
3737

38+
**Approach 1: Character Comparison**
39+
40+
We take the first string $strs[0]$ as the benchmark, and compare the $i$th character of the string after it with the $i$th character of $strs[0]$. If it is the same, continue to compare the next character, otherwise return the first $i$ characters of $strs[0]$.
41+
42+
After the traversal is over, it means that the first $i$ characters of all strings are the same, and $strs[0]$ is returned.
43+
44+
Time complexity $(n \times m)$, where $n$ and $m$ are the length of the string array and the minimum length of the string respectively. The space complexity is $O(1)$.
45+
3846
<!-- tabs:start -->
3947

4048
### **Python3**
4149

42-
```python
43-
class Solution:
44-
def longestCommonPrefix(self, strs: List[str]) -> str:
45-
for l in range(len(strs[0]), -1, -1):
46-
if all(len(s) >= l and s[:l] == strs[0][:l] for s in strs):
47-
return strs[0][:l]
48-
return ''
49-
```
50-
5150
```python
5251
class Solution:
5352
def longestCommonPrefix(self, strs: List[str]) -> str:
@@ -114,26 +113,17 @@ func longestCommonPrefix(strs []string) string {
114113
### **C#**
115114

116115
```cs
117-
using System.Text;
118-
using System.Linq;
119-
120116
public class Solution {
121117
public string LongestCommonPrefix(string[] strs) {
122-
if (strs.Length == 0) return string.Empty;
123-
var sb = new StringBuilder();
124-
for (var i = 0; i < strs[0].Length; ++i)
125-
{
126-
var ch = strs[0][i];
127-
if (strs.All(str => str.Length > i && str[i] == ch))
128-
{
129-
sb.Append(ch);
130-
}
131-
else
132-
{
133-
break;
118+
int n = strs.Length;
119+
for (int i = 0; i < strs[0].Length; ++i) {
120+
for (int j = 1; j < n; ++j) {
121+
if (i >= strs[j].Length || strs[j][i] != strs[0][i]) {
122+
return strs[0].Substring(0, i);
123+
}
134124
}
135125
}
136-
return sb.ToString();
126+
return strs[0];
137127
}
138128
}
139129
```
Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
1-
using System.Text;
2-
using System.Linq;
3-
41
public class Solution {
52
public string LongestCommonPrefix(string[] strs) {
6-
if (strs.Length == 0) return string.Empty;
7-
var sb = new StringBuilder();
8-
for (var i = 0; i < strs[0].Length; ++i)
9-
{
10-
var ch = strs[0][i];
11-
if (strs.All(str => str.Length > i && str[i] == ch))
12-
{
13-
sb.Append(ch);
14-
}
15-
else
16-
{
17-
break;
3+
int n = strs.Length;
4+
for (int i = 0; i < strs[0].Length; ++i) {
5+
for (int j = 1; j < n; ++j) {
6+
if (i >= strs[j].Length || strs[j][i] != strs[0][i]) {
7+
return strs[0].Substring(0, i);
8+
}
189
}
1910
}
20-
return sb.ToString();
11+
return strs[0];
2112
}
2213
}

0 commit comments

Comments
 (0)