Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8b58ed1

Browse files
Solved 14
1 parent 2a88194 commit 8b58ed1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package leetcode.editor.en;
2+
3+
import java.util.*;
4+
5+
class LongestCommonPrefix {
6+
7+
//leetcode submit region begin(Prohibit modification and deletion)
8+
class Solution {
9+
public String longestCommonPrefix(String[] strs) {
10+
for (int i = 0 ;; i++) {
11+
if(i >= strs[0].length()) return strs[0].substring(0, i);
12+
char c = strs[0].charAt(i);
13+
for (int j = 1; j < strs.length; j++) {
14+
String s = strs[j];
15+
if(i >= s.length() || c != s.charAt(i)) return s.substring(0, i);
16+
}
17+
}
18+
}
19+
}
20+
//leetcode submit region end(Prohibit modification and deletion)
21+
22+
23+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /