Мой ответ
общественная Строка longestCommonPrefix (String [] СПО) { Строка longestCommonPrefix = "" ; для ( INT J = 0; J <strs.length; j ++ ) { если (J == 0 ) { longestCommonPrefix = СПО [J]; продолжить ; } Если (longestCommonPrefix.length ()> СПО [J] .length ()) { longestCommonPrefix = СПО [J]; } } Если (longestCommonPrefix.length () == 0) возвращение "" ; longestCommonPrefix= СПО [0] .substring (0 , longestCommonPrefix.length ()); для (String строки: STRS) { Строка perfix = строки; если (longestCommonPrefix.length () <= String.length ()) { perfix = String.substring (0 , longestCommonPrefix.length ()); } Если (longestCommonPrefix.equals (perfix)) { продолжить ; } Еще { в то время как (! LongestCommonPrefix.equals (perfix)) { INT длина =longestCommonPrefix.length (); если (longestCommonPrefix.length () == 1 ) { perfix = perfix.substring (0, 1 ); longestCommonPrefix = longestCommonPrefix.substring (0, 1 ); если (! perfix.equals (longestCommonPrefix)) { longestCommonPrefix = "" ; } Перерыв ; } Иначе { perfix = perfix.substring (0, длина - 1 ); longestCommonPrefix = longestCommonPrefix.substring (0, длина - 1 ); } } } } Вернуть longestCommonPrefix; }