Tuesday, 5 September 2017

java - number is palindrome ?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class _Math {
    
    /**
     * check if number is palindrome
     * 
     * @param num
     * @return
     */
    static boolean is_palindrome(long num){
        if(num < 10 && num > -10)
            return true;

        int[] nums = digits_in_number(num);

        int start = 0;
        int end = nums.length - 1;

        while(start <= end){
            if(nums[start++] != nums[end--])
                return false;
        }
        return true;
    } 
}

styled using hilite.me

No comments:

Post a Comment