mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-18 00:57:17 +00:00
Initial commit
This commit is contained in:
21
Easy/longest-common-prefix/Solution.java
Normal file
21
Easy/longest-common-prefix/Solution.java
Normal file
@@ -0,0 +1,21 @@
|
||||
class Solution {
|
||||
public String longestCommonPrefix(String[] strs) {
|
||||
int len = strs.length;
|
||||
|
||||
if (len == 0)
|
||||
return "";
|
||||
|
||||
if (len == 1)
|
||||
return strs[0];
|
||||
|
||||
Arrays.sort(strs);
|
||||
|
||||
int end = Math.min(strs[0].length(), strs[len - 1].length());
|
||||
|
||||
int i = 0;
|
||||
while (i < end && strs[0].charAt(i) == strs[len - 1].charAt(i))
|
||||
i++;
|
||||
|
||||
return strs[0].substring(0, i);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user