每天刷一道牛客题霸-第26天- 最长公共前缀
题目
import java.util.*; public class Solution { /** * * @param strs string字符串一维数组 * @return string字符串 */ public String longestCommonPrefix (String[] str) { StringBuilder result = new StringBuilder(); int index = 0; if(str.length == 0){ return result.toString(); } boolean count = true; char item; while (count){ if (str[0].length() == 0){ return result.toString(); } if (index >= str[0].length()){ break; }else { item = str[0].charAt(index); } for (int i = 1 ; i < str.length ; i++){ if (index >= str[i].length()){ count = false;; break; } if (item != str[i].charAt(index)){ count = false; break; } } if (count){ result.append(item); } index++; } return result.toString(); // write code here } }#牛客题霸##题解#