【问题标题】:Comparing 2 integers and checking if the digits are the same比较2个整数并检查数字是否相同
【发布时间】:2025-12-23 15:35:11
【问题描述】:

问题:每当我运行我的代码时,结果总是显示为 false。

我写了一个比较两个值大于10的整数的代码,如果整数上的位数相同,结果应该返回true。

示例:11 和 41 = 真,26 和 45 = 假

但是,每当我运行我的代码时,结果总是错误的。

代码:

import java.util.ArrayList;

class SharedDigit {

public static boolean hasSharedDigit(int number1, int number2){
    
    boolean result = false;
    
    //If the number is a single digit or negative ,return false
    if (number1 < 9 || number2 < 9) {
        return false;
    }
    
    
    //Get 2 ArrayList for each number
    ArrayList<Integer> firstNumberArray = new ArrayList<Integer>();
    ArrayList<Integer> secondNumberArray = new ArrayList<Integer>();
    
    
    //Get each digit of each digit of number1 and store in the array
    while (number1 > 0) {
            
        //Get the last digit of the number by modulating by 10
        int lastDigit = number1 % 10;
        
        //Assign the reverse value to the ArrayList
        firstNumberArray.add(lastDigit);
        
        //Remove the last digit of number1
        number1 = number1 / 10;
    }
    
    //Get each digit of each digit of number2 and store in the array
    while (number2 > 0) {
        
        //Get the last digit of the number by modulating by 10
        int lastDigit = number2 % 10;
        
        //Assign the reverse value to the ArrayList
        firstNumberArray.add(lastDigit);
        
        //Remove the last digit of number2
        number2 = number2 / 10;
    }
    
    //Get the highest count of the array and compare each integer to check if they are the same
    int highestcount;
    int lowestcount;
    
    //If the first number array size is the highest
    if (firstNumberArray.size() > secondNumberArray.size()) {
        
        highestcount = firstNumberArray.size();
        lowestcount = secondNumberArray.size();
        
        //Compare each array and see if the integer is the same
        for (int i = 0; i < highestcount; i++) {
            
            for (int j = 0; j < lowestcount; j++) {
                
                if (firstNumberArray.get(i) == secondNumberArray.get(j)) {
                    result = true;
                }
            }
        }
    }
    
    //If the second number array size is the highest
    else if (secondNumberArray.size() > firstNumberArray.size()) {
        
        highestcount = secondNumberArray.size();
        lowestcount = firstNumberArray.size();
        
        //Compare each array and see if the integer is the same
        for (int i = 0; i < highestcount; i++) {
            
            for (int j = 0; j < lowestcount; j++) {
                
                if (firstNumberArray.get(j) == secondNumberArray.get(i)) {
                    result = true;
                }
            }
        }
        
    }
    
    //If both array size is the same
    else {
        highestcount = firstNumberArray.size();
        lowestcount = firstNumberArray.size();
        
        //Compare each array and see if the integer is the same
        for (int i = 0; i < highestcount; i++) {
            
            for (int j = 0; j < lowestcount; j++) {
                
                if (firstNumberArray.get(i) == secondNumberArray.get(j)) {
                    result = true;
                }
            }
        }
    }
    
    //If no integer is the same, return false
    return result;
}
}


public class testing
{
public static void main(String[] args)
{
    
    //Call the class into the main subroutine
    SharedDigit test = new SharedDigit();
    
    //Test this two numbers: It should return true as both numbers has 9
    boolean testing = test.hasSharedDigit(29, 99);
    
    //Test this two numbers: It should return true as both numbers has 5
    boolean testing2 = test.hasSharedDigit(35, 58);
    
    //Test this two numbers: It should return false as one of the numbers is less than 10
    boolean testing3 = test.hasSharedDigit(8, 84);
    
    //Test this two numbers: It should return false as both numbers has no same digits
    boolean testing4 = test.hasSharedDigit(57, 49);
    
    System.out.println("29 and 99 result is: " + testing);
    
    System.out.println("35 and 58 result is: " + testing2);
    
    System.out.println("8 and 84 result is: " + testing3);
    
    System.out.println("57 and 49 result is: " + testing4);
    
    
    
    
}
}

输出:

29 and 99 result is: false
35 and 58 result is: false
8 and 84 result is: false
57 and 49 result is: false

感谢任何回复和任何建议以改进我的编码。谢谢!

【问题讨论】:

  • 能多给点测试用例吗?在你给出的 2 个中,只有 1 个是需要的,所以有很多假设。

标签: java arraylist numbers compare


【解决方案1】:

因为原因是您要在两个 while 循环中添加数据和单个 arrayList,即 firstNumberArray

将第二个 while 循环更改为

//Get each digit of each digit of number2 and store in the array
        while (number2 > 0) {
            
            //Get the last digit of the number by modulating by 10
            int lastDigit = number2 % 10;
            
            //Assign the reverse value to the ArrayList
            secondNumberArray.add(lastDigit);
            
            //Remove the last digit of number2
            number2 = number2 / 10;
        }

希望能解决问题

【讨论】:

  • 不确定只是将答案分发给 101 级 java 编码员是最好的策略,下次再做。
  • 非常感谢!我不知道我怎么没看到那个错字。
【解决方案2】:

这将是一个更快的解决方案。

public class SharedDigit {
    
    // O(N) time, O(N) space, where N = max(numOfDigits(number1), numOfDigits(number2))
    public static boolean hasSharedDigit(int number1, int number2) {
        if (number1 < 10 || number2 < 10) {
            return false;
        }

        HashSet<Integer> set = new HashSet<>();

        do {
            set.add(number1 % 10);
            number1 /= 10;
        } while (number1 > 0);

        do {
            if (set.contains(number2 % 10)) {
                return true;
            }
            number2 /= 10;
        } while (number2 > 0);

        return false;
    }

    public static void main(String ...args) {
        
        int[][] tests = {
            { 26, 45 }, // false
            { 123, 437 },   // true
            { 12, 35 }, // false
            { 9515, 68 },   // false
            { 5832, 6501 }  // true
        };

        for (int testNo = 0; testNo < tests.length; testNo++) {
            System.out.format(
                "hasSharedDigit(%d, %d) -> %b\n",
                tests[testNo][0],
                tests[testNo][1],
                hasSharedDigit(tests[testNo][0], tests[testNo][1])
            );
        }

    }
}

【讨论】:

  • 感谢您的反馈!我会看看代码并尝试一下。