题解 | #最长公共前缀#
矩阵的最小路径和
http://www.nowcoder.com/practice/7d21b6be4c6b429bb92d219341c4f8bb
import java.util.*;
public class Solution {
/**
*
* @param strs string字符串一维数组
* @return string字符串
*/
public String longestCommonPrefix (String[] strs) {
if(strs.length == 0){
return "";
}
if(strs.length == 1){
return strs[0];
}
int min = strs[0].length();
for(int i = 1; i < strs.length; i++){
min = Math.min(min, strs[i].length());
min = subStr(strs[i-1], strs[i],min);
}
if(min == 0) return "";
return strs[0].substring(0,min);
}
public int subStr(String str1, String str2, int n){
for(int i = 1; i <= n; i++){
if(str1.charAt(i-1) != str2.charAt(i-1)){
return i-1;
}
}
return n;
}
}