Palindrome Number_Leetcode_#9

来源:互联网 发布:快手用户数据 编辑:程序博客网 时间:2024/06/17 04:37

1.题目
Determine whether an integer is a palindrome. Do this without extra space.

2.解法
思路:类似于翻转一个数,定义一个y,从个位开始,取该位的数字,y = y*10 + x1%10。最后比较y与x是否相等。
时间复杂度:O(N)

public class Solution {
public boolean isPalindrome(int x) {
if(x < 0){
return false;
}
int x1 = x;
int y = 0;
while(x1 != 0){
y = y * 10 + x1 % 10;
x1 /= 10;
}
return x == y;
}
}

0 0
原创粉丝点击