PalindromeNumber

package leetcode;

import java.util.ArrayList;
import java.util.List;

public class PalindromeNumber {
    public boolean isPalindrome(int x) {
        if (x < 0) return false;
        if (x == 0) return true;
        List<Integer> nunms = this.getSingleNums(x);
        int length = nunms.size();
        for (int i = 0;i < length/2; i++){
            if (nunms.get(i) != nunms.get(length -1 - i)) return false;
        }
        return true;
    }

    private List<Integer> getSingleNums(int x){
        List<Integer> nums = new ArrayList<>();
        while (x > 0){
            nums.add(x%10);
            x = x/10;
        }
        return nums;
    }

    public static void main(String[] args) {
        PalindromeNumber pn = new PalindromeNumber();
        System.out.println(pn.isPalindrome(12122121));
    }
}
阅读更多
文章标签: leetcode
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭