码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode练习题Longest Common Prefix

时间:2019-12-18 22:08:32      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:练习   common   字符   write   case   indexof   图片   通过   array   

Question:

Longest Common Prefix

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

If there is no common prefix, return an empty string "".

Example 1:

Input: ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: ["dog","racecar","car"]
Output: ""

Explanation: There is no common prefix among the input strings.
Note:

All given inputs are in lowercase letters a-z.

我的解法思路是:

首先比较第一个和第二个字符的第i字母,如果第一个和第二个字符的第i个字母相同,则比较第二个和第三个的第i个字母,如果都相等,则将第i个字母加入最后的str中,i的大小从0开始,直到循环最后。

存在的问题是:

class Solution {
    public String longestCommonPrefix(String[] strs) {
      
        int Strslength = strs.length;


        if(Strslength>1)
        {

            int minLength = strs[0].length();
            //O(n)
            for(String str:strs)
            {
                int strLength = str.length();
                if(minLength > strLength)
                {
                    minLength = strLength;
                }
            }

            //Store output ;
            String str = "";


           label:for(int i=0;i<minLength;i++)
            {

                for(int j=1;j<strs.length;j++)
                {

                    if((strs[j-1].charAt(i))!=(strs[j].charAt(i)))
                    {

                            break label;
                        
                    }

                }

            str+= strs[0].charAt(i);
            }
            return str;

        }
        else if(Strslength==1)
        {
            return strs[0];
        }
        else
        {
            return "";
        }

       

       
    }
}

技术图片

其他人的解法:

技术图片

在这个解法中,作者主要通过找到两个string中相等的那个值,然后返回结果作为字串继续寻找使用indexOf函数,找到第一个相等的位置。

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

            return prefix;
    }

Leetcode练习题Longest Common Prefix

标签:练习   common   字符   write   case   indexof   图片   通过   array   

原文地址:https://www.cnblogs.com/zhichun/p/12063586.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!