标签:
问题描述:Write a function to find the longest common prefix stringamongst an array of strings.
问题分析:
代码:
public class Solution { public String longestCommonPrefix(String[] strs) { if(strs == null) return null; if(strs.length < 1) return ""; if(strs.length == 1) return strs[0]; StringBuffer result = new StringBuffer(); int min_length = Integer.MAX_VALUE; //找到最短的字符长度 for(int i = 0; i < strs.length; i++) { min_length = min_length < strs[i].length() ? min_length : strs[i].length(); } char temp_char; for(int i = 0; i < min_length; i++) { temp_char = strs[0].charAt(i); for(int j = 1; j < strs.length; j++) { if(temp_char != strs[j].charAt(i)) return result.toString(); } result.append(temp_char); } return result.toString(); } }
leetcode-14 Longest Common Prefix
标签:
原文地址:http://blog.csdn.net/woliuyunyicai/article/details/44984505