2014年5月13日星期二

Loop Solution - Palindrome Number

public class Solution {
    public boolean isPalindrome(int x) {
     
        if(x < 0){
            return false;
        }
     
        // Stack<Integer> processStack = new Stack<Integer>();
        // Queue<Integer> processQueue = new LinkedList<Integer>();
        // //int divide = x / 10;
        // //int remain = x % 10;
        // while(x / 10 != 0 || x % 10 != 0){
        //     processStack.push(x % 10);
        //     processQueue.offer(x % 10);
        //     x = x / 10;
        // }
     
        // while(!processStack.isEmpty() && !processQueue.isEmpty()){
        //     if(processStack.pop() != processQueue.poll()){
        //         return false;
        //     }
        // }
     
        // if(!processStack.isEmpty() || !processQueue.isEmpty()){
        //     return false;
        // }
     
        // return true;
     
        int size = 0;
        int base = x;
        while(base / 10 != 0 || base % 10 != 0){
            ++size;
            base = base / 10;
        }
        int[] process = new int[size];
        while((x / 10 != 0 || x % 10 != 0) && size >= 0){
            process[--size] = x % 10;
            x = x / 10;
        }
        int lpS = 0;
        int lpE = process.length - 1;
        while(lpS < lpE){
            if(process[lpS] != process[lpE]){
                return false;
            }
            lpS++;
            lpE--;
        }
     
        return true;
    }
}

没有评论: