美文网首页程序员的修养
LeetCode:Longest Common Prefix[E

LeetCode:Longest Common Prefix[E

作者: voidsky_很有趣儿 | 来源:发表于2016-04-17 18:49 被阅读45次

    我现在在做一个叫《leetbook》的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看
    地址:https://github.com/hk029/leetcode
    这个是书的地址:https://hk029.gitbooks.io/leetbook/

    这里写图片描述

    014.Longest Common Prefix[E]

    问题

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

    Subscribe to see which companies asked this question

    思路

    这个没啥思路的,怎么都要两重循环,因为是最长公共子串,随便找一个(一般是第一个作为基准),然后拿拿的首部慢慢去匹配后面的字符串就行了。

    public class Solution {
        public String longestCommonPrefix(String[] strs) {
            String s = "";
            if(strs.length == 0)
                return "";
            for(int i = 0; i < strs[0].length();i++)
            {
                char c = strs[0].charAt(i);
                for(int j = 1;j < strs.length;j++)
                {
                    if(i >= strs[j].length() || strs[j].charAt(i) != c)
                        return s;
                }
                s += c;
            }
            return s;
        }
    }
    

    相关文章

      网友评论

        本文标题:LeetCode:Longest Common Prefix[E

        本文链接:https://www.haomeiwen.com/subject/qyiolttx.html