有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java在数组中比较字符串

给定一个字符串数组,返回数字最多的字符串,后跟字母x。如果两个字符串的数字相同,则返回索引最低的字符串

gooD({"1x","123456789","1y3534ssf","4hsd73s"}) → "1x"
gooD({"1xs3x3412fgxx6","1x+4x=5x","x5x"}) → "1x+4x=5x"
gooD({"3x2y11x3gx5x","","232","2x2xx3x3x"}) → "2x2xx3x3x"

我完全不明白为什么我的代码不起作用。为什么会这样

public String gooD(String arr[]) {
    String digits="0123456789";
    int low=0;
    int check=0;
    String s="";
    String d="";
    for (int i=0; i<arr.length; i++) {
        s=arr[i];
        for (int j=0; j<s.length()-1; j++) {
            if (digits.indexOf(s.substring(j,j+1))!=-1) {
                    if (s.substring(j+1,j+2).equals("x")) {
                        d+=s.substring(j,j+1);
                    }
            }
        }
        check=d.length();
        if (check<low) low=check;
        d="";
    }
    for (int i=0; i<arr.length; i++) {
        s=arr[i];
        for (int j=0; j<s.length()-1; j++) {
            if (digits.indexOf(s.substring(j,j+1))!=-1) {
                    if (s.substring(j+1,j+2).equals("x")) {
                        d+=s.substring(j,j+1);
                    }
            }
        }
        if (d.length()==low) return d;
        d="";
    }
    return d;
}

共 (3) 个答案

  1. # 1 楼答案

    首先,您必须逆转您的测试:

        if (check<low) low=check;
    

    应该是:

        if (check>low) low=check;
    

    您的代码正在寻找最小值而不是最大值

    接下来,您将不返回输入数组,而只返回直接后跟x的数字

    保留您的方法,一个可能的实现可能是:

    public String gooD(String arr[]) {
        String digits = "0123456789";
        int low = 0;
        int ilow = 0; // first string by default
        int check = 0;
        String s;
        String d = "";
        for (int i = 0; i < arr.length; i++) {
            s = arr[i];
            check = 0;
            for (int j = 0; j < s.length() - 1; j++) {
                if (digits.indexOf(s.substring(j, j + 1)) != -1) {
                    if (s.substring(j + 1, j + 2).equals("x")) {
                        check += 1;
                    }
                }
            }
            if (check > low) { // only is strictly longer
                low = check;
                ilow = i; // keep the index of first longer string
            }
        }
        return arr[ilow]; // return the string
    }
    
  2. # 2 楼答案

    劳恩说了些什么。无论如何,这将是更有效的

    public String gooD(String arr[]) {
        Pattern pattern = Pattern.compile("\\dx");
    
        String candidate = "";
        int max = 0;
        for (String s : arr){
            Matcher matcher = pattern.matcher(s);
            int current = 0;
            while (matcher.find()){
                current++;
            }
            if (current > max){
                max = current;
                candidate = s;
            }
        }
        return candidate;
    }
    
  3. # 3 楼答案

    一个错误是

    if (check<low) low=check;
    

    应该是哪一个

    if (check > low) low = check;
    

    因为你正在寻找一个最大值

    相当简单的是:

    public static String gooD(String... arr) {
        int max = 0;
        String best = "";
        for( String s: arr ){
            String t = s.replaceAll( "\\dx", "" );
            int d = s.length() - t.length();
            if( d > max ){
                max = d;
                best = s;
            }
        }
        return best;
    }