【问题标题】:Longest Common Prefix in String Array in JavaJava中字符串数组中的最长公共前缀
【发布时间】:2020-12-12 10:52:52
【问题描述】:

我正在尝试使用 java 在字符串数组中查找最长的公共前缀。以下是我的解决方案,但是当我在 leetcode 上上传它时,它失败了,我不明白哪个测试用例失败了。我测试过的所有测试用例都运行良好。

我的方法是匹配字符串数组中所有单词的第一个字符,如果所有单词的第一个字符相似,则移动到第二个字符,否则函数返回字符串。

如果有人帮助我确定我的代码失败的测试用例,我将非常感激。以下是我写的代码:

public static String LongCommonPrefix(String[] strs)
    {
        String commonPrefix="";
        int count=0, k=0;
        
        if(strs.length>0)
        {
            for(int i=0; i<strs[0].length(); i++)
            {
                int j=1;
                while(j<strs.length)
                {
                    if(strs[0].charAt(k)==strs[j].charAt(k))
                    {
                        count++;
                        j++;
                    }
                    else
                        break;
                }
                if(count==strs.length-1)
                {
                    commonPrefix+=strs[0].charAt(k);
                    count=0;
                    k++;

                }
                else
                {
                    return commonPrefix;
                }

            }
        }

        return commonPrefix;
    }

【问题讨论】:

    标签: java arrays string character prefix


    【解决方案1】:

    代码中的错误在于您使用的部分没有检查变量 (K) 是否大于数组的字符串长度 (j)。 为了解决这个问题,在使用变量(K)之前添加一个条件语句就足够了。 祝你好运

    【讨论】:

    • 您能否建议我的代码失败的输入?因为那样我会调试它并解决问题。
    • String[]array={"farzad","fa","fardfbvdb"};
    • 是的,它失败了,但现在我修复了它。非常感谢。
    【解决方案2】:

    试试这个方法。

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

    我已经检查过:

    String[] arr = {"giorgi", "gio", "gior", "giorg", "gior"};
    System.out.println(longestCommonPrefix(arr));
    

    通过打印gio,它做得很好。

    【讨论】:

    • 是的,它有帮助。我的意思是您提供的输入,它不适用于我的代码。所以现在我要调试它并解决导致错误的原因。
    • 我使用的方法与您尝试的方法非常相似;但是,我以更简洁的方式编写了正确的版本。因此,我将留给您将您的代码与工作代码进行比较,并确定原始代码中的问题(我认为您已经确定了它)。通常,最好的做法是对有帮助的答案进行投票。这将有助于社区确定正确答案。
    【解决方案3】:

    试试这个:

    private static String findPrefix(String[] prefixes) {
            String res = "";
    
            if (prefixes.length == 0) return res;
    
            for (int i = 0; i < prefixes.length; i++) {
                char[] chars1 = prefixes[i].toCharArray();
    
                for (int j = i+1; j < prefixes.length; j++) {
                    //break if itself
                    if (i == j) continue;
                    char[] charsMatch = null;
                    char[] chars2 = prefixes[j].toCharArray();
    
                    if (chars1.length > chars2.length) {
                        for (int y = 0; y < chars2.length; y++) {
                            if (chars2[y] == chars1[y]) {
                                if (charsMatch == null) {
                                    charsMatch = new char[chars2.length];
                                }
                                charsMatch[y] = chars1[y];
                            }
                        }
                    } else {
                        for (int y = 0; y < chars1.length; y++) {
                            if (chars2[y] == chars1[y]) {
                                if (charsMatch == null) {
                                    charsMatch = new char[chars1.length];
                                }
                                charsMatch[y] = chars1[y];
                            }
                        }
                    }
    
                    if (charsMatch != null && res.length() < charsMatch.length) {
                        res = new String(charsMatch);
                    }
                }
            }
    
            return res;
        }
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2012-01-24
    • 2011-10-21
    • 2012-02-01
    • 2020-11-13
    • 2018-05-07
    • 2020-06-06
    • 2021-02-14
    • 2014-10-13
    • 1970-01-01
    相关资源
    最近更新 更多