欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Palindrome Number

程序员文章站 2024-03-22 14:38:52
...

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true

Example 2:

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Follow up:

Coud you solve it without converting the integer to a string?

判断一个数字是不是回文

ac code:

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

class Solution {
    public boolean isPalindrome(int x) {
    	if(x<0) {
    		return false;
    	}
    	List<Integer> list=new ArrayList<Integer>() ;
    	while(x>0) {
    		list.add(x%10);
    		x/=10;
    	}
    	int length=list.size();
    	int[] ints=new int[length];
    	int k=0;
    	for (Integer integer : list) {
			ints[k++]=integer;
		}
    	for(int i=0;i<length;i++) {
    		if(ints[i]!=ints[length-1-i])
    			return false;
    	}
        return true;
    }
}