Java整数共享数字检测代码始终返回false问题排查及优化建议求助
Hey there! Let's break down why all your test cases are returning false—I spotted a critical bug right away, plus some easy fixes and optimizations to make your code work better.
First, the Root Cause of All false Results
Take a close look at the loop where you process number2:
while (number2 > 0) { int lastDigit = number2 % 10; firstNumberArray.add(lastDigit); // ❌ This is the big mistake! number2 = number2 / 10; }
You’re adding every digit from number2 to firstNumberArray instead of secondNumberArray! That means secondNumberArray stays completely empty the whole time. When you go to compare the two lists later, you’re checking against an empty collection—so no matches are ever found, hence every test returns false.
Fix that line to:
secondNumberArray.add(lastDigit);
Next, Fix the Input Validation Logic
Your current check if (number1 < 9 || number2 < 9) doesn’t align with your intended rules:
- You want to reject single-digit numbers (less than 10) and negative values, but
number1 <9would incorrectly allow 9 (a single-digit number) and doesn’t explicitly block negatives. Plus, your problem description specifies numbers greater than 10, so we should also reject 10 itself.
Update the validation to:
if (number1 <= 10 || number2 <= 10 || number1 < 0 || number2 < 0) { return false; }
This correctly handles single-digit numbers, negatives, and values equal to 10.
Cleanup & Efficiency Improvements
Storing digits in ArrayLists and running nested loops works, but it’s not the most efficient approach. Here are two simpler, faster alternatives:
Option 1: Use a Set for Instant Lookups
Store all digits of the first number in a Set, then check each digit of the second number against the set. We can return true as soon as we find a match to avoid unnecessary work:
import java.util.HashSet; import java.util.Set; class SharedDigit { public static boolean hasSharedDigit(int number1, int number2) { // Input validation if (number1 <= 10 || number2 <= 10 || number1 < 0 || number2 < 0) { return false; } Set<Integer> digits = new HashSet<>(); int temp = number1; // Add all digits of number1 to the set while (temp > 0) { digits.add(temp % 10); temp /= 10; } // Check each digit of number2 against the set temp = number2; while (temp > 0) { int digit = temp % 10; if (digits.contains(digit)) { return true; // Found a match, exit early } temp /= 10; } return false; // No matches found } }
Option 2: No Collections Needed (Super Simple)
Convert each number to a string, then check if any character from the first string exists in the second:
class SharedDigit { public static boolean hasSharedDigit(int number1, int number2) { if (number1 <= 10 || number2 <= 10 || number1 < 0 || number2 < 0) { return false; } String num1Str = String.valueOf(number1); String num2Str = String.valueOf(number2); for (char c : num1Str.toCharArray()) { if (num2Str.contains(String.valueOf(c))) { return true; } } return false; } }
Testing the Fixed Code
After fixing the original list assignment bug and updating validation, your test cases will return the expected results:
29 and 99 result is: true 35 and 58 result is: true 8 and 84 result is: false 57 and 49 result is: false
内容的提问来源于stack exchange,提问作者Cliven

