Java code runtime 1 ms from top bar


  • 2
    Y
    public class Solution {
    public String longestCommonPrefix(String[] strs) {
        if(strs == null || strs.length == 0)
            return "";
        String ret = strs[0];
        int length = ret.length();
        for(int i = 1; i < strs.length; i++){
            length = Math.min(length, strs[i].length());
            while(!strs[i].substring(0, length).equals(ret.substring(0, length)) && length > 0)
                length --;
            if(length == 0){
                ret = "";
                break;
            }
            ret = strs[0].substring(0, length).toString();
        }
        return ret;
    }
    }

Log in to reply
 

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