字符串数组的最长公共前缀
2013-12-31 10:08
701 查看
题目原型:
Write a function to find the longest common prefix string amongst an array of strings.
分别字符串数组中的每个字符串进行扫描对比,设置一个临时变量保存每次的比较的结果。
Write a function to find the longest common prefix string amongst an array of strings.
分别字符串数组中的每个字符串进行扫描对比,设置一个临时变量保存每次的比较的结果。
//取得两个字符串的最长公共前缀 public String getLongestStr(String one , String two) { //考虑为空的情况 if(one.length()==0||two.length()==0) return ""; String common = ""; for(int i =0 ;i<one.length()&&i<two.length();i++) { if(one.substring(0, i+1).equals(two.substring(0, i+1))) { common = one.substring(0, i+1); } } return common; } public String longestCommonPrefix(String[] strs) { //考虑为空的情况 if(strs.length==0) return ""; boolean flag = false; for(String s : strs) { if(s.length()==0||s==null||s.equals("")) flag = true; } if(flag) return ""; String longestCommStr = null; longestCommStr = strs[0]; for(String str : strs) { if(longestCommStr.equals("")) return ""; longestCommStr = getLongestStr(longestCommStr, str); } return longestCommStr; }
相关文章推荐
- longest-common-prefix 求字符串数组的最长公共前缀
- 查找字符串数组中最长的公共前缀字符串。
- 找出一个字符串数组中最长公共前缀字符串
- leetcode-14. Longest Common Prefix(寻找字符串数组最长公共前缀)
- Lettcode_14_Longest Common Prefix——查找字符串数组中最长公共前缀字符串
- 算法#26--查找字符串数组中最长的公共前缀
- leetcode-14-求字符串数组最长公共前缀
- PHP实现一个函数查找字符串数组中最长的公共前缀字符串
- 寻找一个字符串数组的最长公共前缀
- 字符串数组最长公共前缀
- LeetCode -- 求字符串数组中的最长公共前缀
- LeetCode -- 求字符串数组中的最长公共前缀
- LeetCode 14. Longest Common Prefix--字符串数组元素的最长公共前缀
- 求字符串数组最长公共前缀
- LeetCode 找到一个字符串数组的最长公共前缀
- 14. Longest Common Prefix最长公共字符串前缀
- 扩展KMP--求字符串S的所有后缀和字符串T的最长公共前缀
- 每次询问求出两个字符串的最长公共前缀的长度 后缀数组+RMQ+lcp UVA 12338 - Anti-Rhyme Pairs
- 算法6:返回一组字符串的最长公共前缀
- 寻找若干个字符串的最长公共前缀