标签:style blog class c code java
问题:寻找最长公共前缀
思路:就是逐一检查每个string中的每一位,碰到不相等的时候,结束;每个string中这一位都相等,加入到common prefix中~
public String longestCommonPrefix(String[] strs) { int len = strs.length; if(len == 0) return ""; String commonPrefix = ""; for(int j = 0; ; j++){ if(j >= strs[0].length()) break; int i; for(i = 1 ; i < len ; i++){ if(j >= strs[i].length() || strs[i].charAt(j) != strs[0].charAt(j)) break; } if(i == len) commonPrefix += strs[0].charAt(j); else break; } return commonPrefix; }
[leetcode]_Longest Common Prefix,布布扣,bubuko.com
[leetcode]_Longest Common Prefix
标签:style blog class c code java
原文地址:http://www.cnblogs.com/glamourousGirl/p/3731634.html