题目:获取公共子串
eg.
Input: [“flower”,“flow”,“flight”]
Output: “fl”
Input: [“dog”,“racecar”,“car”]
Output: “”
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) return "";
String res = new String();
for (int j = 0; j < strs[0].length(); ++j) {
char c = strs[0].charAt(j);
for (int i = 1; i < strs.length; ++i) {
if (j >= strs[i].length() ||
strs[i].charAt(j) != c) {
return res;
}
}
res += Character.toString(c);//c转换成String类型
}
return res;
}
}