14. 最长公共前缀
题解
执行用时:1 ms, 在所有 Java 提交中击败了75.20%的用户 内存消耗:36.5 MB, 在所有 Java 提交中击败了63.64%的用户
class Solution {
public String longestCommonPrefix(String[] strs) {
StringBuilder sb = new StringBuilder();
int idx = 0;
while (true) {
char tempChar = ' ';
for (String str : strs) {
if (str.length() - 1 < idx) return sb.toString();
if (tempChar == ' ') {
tempChar = str.charAt(idx);
} else if (tempChar != str.charAt(idx)) {
return sb.toString();
}
}
sb.append(tempChar);
idx ++;
}
}
}