author: Blankj blog : http://blankj.com time : 2017/04/26 desc :
| 9 | * </pre> |
| 10 | */ |
| 11 | public class Solution { |
| 12 | public String longestCommonPrefix(String[] strs) { |
| 13 | int len = strs.length; |
| 14 | if (len == 0) return ""; |
| 15 | int minLen = 0x7fffffff; |
| 16 | for (String str : strs) minLen = Math.min(minLen, str.length()); |
| 17 | for (int j = 0; j < minLen; ++j) |
| 18 | for (int i = 1; i < len; ++i) |
| 19 | if (strs[0].charAt(j) != strs[i].charAt(j)) |
| 20 | return strs[0].substring(0, j); |
| 21 | return strs[0].substring(0, minLen); |
| 22 | } |
| 23 | |
| 24 | public static void main(String[] args) { |
| 25 | Solution solution = new Solution(); |
| 26 | System.out.println(solution.longestCommonPrefix(new String[]{"abc", "abcd", "ab"})); |
| 27 | } |
| 28 | } |
nothing calls this directly
no outgoing calls
no test coverage detected