1ms java simple code


  • 2
    J

    public class Solution {
    public String longestCommonPrefix(String[] strs) {
    if(strs.length==0||strs==null) return "";
    String prefix = strs[0];
    for(int i = 1;i<strs.length;i++){
    if(strs[i].equals(prefix)) continue;
    int k = 0;
    while(k<prefix.length()&&k<strs[i].length()&&prefix.charAt(k)==strs[i].charAt(k)){
    k++;
    }
    prefix = prefix.substring(0,k);
    }
    return prefix;
    }
    }

    • list item

Log in to reply
 

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