赞
踩
import java.util.*; class Solution { public String longestCommonPrefix(String[] strs) { if(strs.length ==0) return ""; String res = strs[0]; for(int i =1; i<strs.length; i++){ //找出res和strs[i]的最长前缀 int j; //迭代出最长子串的结束位置 for(j=0; j<Math.min(res.length(),strs[i].length()); j++){ if(res.charAt(j) != strs[i].charAt(j)) break; } //更新结果字符串 res = res.substring(0,j); if(res == null) return ""; } return res; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。