Write a function to find the longest common prefix string amongst an
array of strings.If there is no common prefix, return an empty string "".app
Example 1:ui
Input: ["flower","flow","flight"] Output: "fl" Example 2:code
Input: ["dog","racecar","car"] Output: "" Explanation: There is no
common prefix among the input strings. Note:inputAll given inputs are in lowercase letters a-z.
比較簡單的一道題 按正向思惟就可解string
public String longestCommonPrefix(String[] strs) { if(strs.length<=0) return ""; List<Integer> list=new ArrayList(); for(String s:strs) list.add(s.length()); int min=Collections.min(list); StringBuilder builder=new StringBuilder(); for(int i=0;i<min;i++){ char c=strs[0].charAt(i); for(int j=1;j<strs.length;j++){ if(c!=strs[j].charAt(i)) return builder.toString(); } builder.append(c); } return builder.toString(); }