Sollicitatievraag bij Salesforce

In java and using math, check if a number is a palindrome.

Antwoorden op sollicitatievragen

Anoniem

20 mrt 2016

boolean isPalindromNumber(int n) { if(n == null) return true; return n == reverseNumber(n); } long reverseNumber(int n) { long rev = 0; while(n != 0) rev = rev * 10 + n % 10; n /= 10; } return rev; }

2

Anoniem

13 dec 2018

The above doesn't actually work for all cases. For example try 2020. This will solve for all possibilities: public static boolean isPalindromeNumber(int n) { int rev = 0; int orig = n; while(n != 0) { rev = rev * 10 + n % 10; n /= 10; } if(orig % 10 == 0) { rev *= 10; } return (orig == rev); }

Anoniem

9 feb 2020

How is 2020 a palindrome?

Anoniem

2 mrt 2016

The point is to use math, as explicitly said.

Anoniem

5 jan 2016

Convert to string and then either (a) revers and compare or (b) using two counters compare characters from both ends until they are not equal or they meet.