【问题标题】:Compares two strings lexicographically using charAt() and without using compareTo() method使用 charAt() 和不使用 compareTo() 方法按字典顺序比较两个字符串
【发布时间】:2013-12-02 12:50:42
【问题描述】:

如果第一个字符串按字典顺序大于第二个字符串,则返回 1,如果相等则返回 0,否则返回 -1。在某些情况下正确返回 1,-1,0,但对于此 str1 和 str2,返回结果与期望的输出相反。

public class StringCompare {

    static String testcase1 = "helloworld";
    static String testcase2 = "hellojavaworld";

    public static void main(String args[]) {
        StringCompare testInstance = new StringCompare();
        int result = testInstance.newCompare(testcase1, testcase2);
        System.out.println("Result : " + result);
    }

    // write your code here
    public int newCompare(String str1, String str2) {

        int l1 = str1.length();
        int l2 = str2.length();
        int max = 0;
        if (l1 <= l2) {
            max = l1;
        }
        else
            max = l2;
        int count = 0;

        for (int i = 0; i < max; i++) {
            char ch1 = str1.charAt(i);
            char ch2 = str2.charAt(i);

            if (str2.charAt(i) > str1.charAt(i)) {
                return - 1;
            }

            if (str1.charAt(i) > str2.charAt(i)) {
                return 1;

            }
            if (l1 == l2) {
                if (ch1 == ch2) {
                    count++;
                }
                if (count == max) {
                    return 0;
                }
            }

        }
        if (l1 == l2) return 0;
        if (l1 > l2)
            return 1;
        else
            return - 1;

    }

}

【问题讨论】:

  • 缩进....
  • 我在提交之前按 ctrl+k...
  • 你试过调试了吗?
  • 如果你能指出什么 cales 给出了不正确的输出,那就太好了。
  • 您在问题中给出的代码在字典顺序方面是正确的;看来您对“所需输出”的定义不匹配。 hello &lt; hi 因为e &lt; i。换句话说,比较hellohi 得出的结果与比较hehi 的结果相同。这是正确的。

标签: java


【解决方案1】:

这是一个简化的答案

public class TestStrings {

    public static void main(String[] args) {

        System.out.println(compare("Mike", "Mike"));    // returns 0
        System.out.println(compare("Mikee", "Mike"));   // returns 1
        System.out.println(compare("Mike", "Mikee"));   // returns -1
    }

    public static int compare(String s1, String s2) {
        for (int i = 0; i < Math.min(s1.length(), s2.length()); i++) {
            char c1 = s1.charAt(i);
            char c2 = s2.charAt(i);

            if (c1 > c2) {
                return 1;
            } else if (c2 > c1) {
                return -1;
            }
        }

        if (s2.length() > s1.length()) {
            return -1;
        } else if (s1.length() > s2.length()){
            return 1;
        } else {
            return 0;
        }
    }
}

我使用了一个循环,停止条件为最短单词的长度。如果在最短单词的长度之后单词相等,则较长的单词自动变大。这就是底部的 if 语句的用途。

【讨论】:

  • System.out.println(compare("aabzzzzzzzz", "b")); //返回-1
  • 它应该是这样工作的。这就是 compareTo 方法将返回的结果,一个负数,意味着 b 大于 aabzzzzz。 compareTo 将比较直到字符不匹配,然后将返回正数或负数。它不会比较剩余的字母
  • 试试System.out.println("aabzzzzzzzz".compareTo("b"));。它返回 -1
  • 是的,这就是我根据词典比较告诉我的代码是正确的,但是 op 需要一些其他输出,因为 op 的逻辑是正确的(除了缩进,他可以让它更小)和他得到的输出是正确的,但也许他想要别的东西,并认为他想要的输出就是字典比较。
  • @Apan,我同意,OP应该解释让他认为aabbzzzzz大于b的逻辑。那么也许他可以对他正在寻找的东西找到更好的答案
【解决方案2】:

你可以试试:

public class StringCompare {
    static String testcase1 = "helloworld";
    static String testcase2 = "hellojavaworld";

    public static void main(String args[]){
        StringCompare testInstance = new StringCompare();
        int result = testInstance.newCompare(testcase1,testcase2);
        System.out.println("Result : "+result);
    }

    //write your code here
    public int newCompare(String str1, String str2){
        int l1=str1.length();
        int l2=str2.length();
        int max=0;
        if(l1<=l2)
        {
            max =l1;
        }
        else
        max=l2;
        int count=0;


        for (int i =0;i<max;i++) {
            char ch1=str1.charAt(i);
            char ch2=str2.charAt(i);

            if(str2.charAt(i)>str1.charAt(i))
            {
                return -1;
            }
            if(str1.charAt(i)>str2.charAt(i))
            {
                return 1;
            }
        }
        if(l1==l2)
        {
            return 0;
        }else if (l1 < l2){
            return -1;
        }else{
            return 1;
        }

     }                

【讨论】:

  • 您的答案只是问题代码的副本。如果您认为您修复了某些问题,您应该解释是什么以及为什么。
  • 这不是副本。我删除了 for 中的最后一个 if。首先,我们比较询问每个字符是否相等的字符串。循环仅对较短字符串的长度进行。那么如果两个字符串的长度相同,则意味着字符串相同,但我在函数中返回 -1 或 1 是最长的字符串。
  • 您刚刚在评论中解释了问题的代码。你确实没有解释为什么你删除了一段代码以及为什么它应该回答这个问题。
【解决方案3】:

在这种情况下 String testcase1 = "helloworld" 和 String testcase2= "hellojavaworld" 您的 for 循环将从 char 'h' 运行到 char 'o'(i=0 到 i=4) 并且您的 for 循环中的任何 if 条件都不会满意,一旦我增加到 5

 //str1.charAt(i)='w' and str2.charAt(i)=j
 if(str1.charAt(i)>str2.charAt(i))   //w(ASCII=119) > j(ASCII=106)
 {
        return 1;                    //return 1 and control return to the calling function

  }

所以 result=1。或者简而言之,您的代码工作正常。 你可以指定你想要的输出。

【讨论】:

  • 根据第一个不匹配的字符返回结果正是字典比较的内容。我真的很想听听你的替代方案……
  • 第一个不匹配的字符是需要的,因为那样只能比较
  • @Holger 是的,这就是我所说的这个程序工作正常,也许操作需要一些其他输出,因为根据 “词典比较” 点它是正确的查看
  • @user3057435 for str1="hello" and str2="hi" 它返回 -1,根据词典比较,这是真的
  • “我告诉你这个程序工作正常”如果你告诉你应该把它写到你的答案中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-03
  • 1970-01-01
  • 1970-01-01
  • 2020-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多