LeetCode – 14. Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.

没啥难度,不多说了

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

This site Original article All followed" Attribution—NonCommercial—ShareAlike 4.0 (CC BY-NC-SA 4.0) ”。 Please keep the following marks for sharing and interpretation:

Original author: Jake Tao Source: 「LeetCode – 14. Longest Common Prefix」

Praise 132
0 0 132

Further reading

Post a reply

Log in can only be commented on later
Share this page
Back to top