14. Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.

Subscribe to see which companies asked this question

  

}public class Solution {
    public String longestCommonPrefix(String[] strs) {
        if (strs.length == 0)                //想换成字符数组来着,这样空间代价有点大的感觉,直接暴力法了。。。
			return "";
		String str = strs[0];
		int mark = 0;
		int count=str.length();
		while (mark != strs.length && count>=0) {
			mark = 0;
			str=str.substring(0, count--);              //傻逼怎么能String,记住用StringBuilder
for (int i = 0; i < strs.length; i++) { if (strs[i].startsWith(str)) mark++; } } return str; }

  

原文地址:https://www.cnblogs.com/kydnn/p/5160478.html