Java code only 5 lines, beat 100%


  • 0
        public String longestCommonPrefix(String[] strs) {
            String res = strs.length == 0 ? "" : strs[0];
            for(int i = 1; i < strs.length; i++)
                while(strs[i].indexOf(res) != 0) 
                    res=res.substring(0, res.length()-1);
            return res;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.