LeetCode 9. Palindrome Number (回文数字)

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

 


题目标签:Math

  题目给了我们一个int x, 让我们判断它是不是回文数字。

  首先,负数就不是回文数字,因为有个负号。

  剩下的都是正数,只要把数字 reverse 一下,和原来的比较,一样就是回文。

  当有overflow 的时候,返回一个负数就可以了(因为负数肯定不会和正数相等)。

  

 

Java Solution:

Runtime beats 61.47% 

完成日期:06/12/2017

关键词:Palindrome

关键点:reverse number

 1 class Solution 
 2 {
 3     public boolean isPalindrome(int x) 
 4     {
 5         if(x < 0)
 6             return false;
 7         
 8         int pd = reverse(x);
 9         
10         return x == pd ? true : false;
11     }
12     
13     public int reverse(int num)
14     {
15         int res = 0;
16         
17         while(num != 0)
18         {
19             int tail = num % 10;
20             int newRes = res * 10 + tail;
21             
22             if((newRes - tail) / 10 != res) // check overflow
23                 return -1; // -1 will not equal to positive number
24             
25             res = newRes;
26             num = num / 10;
27         }
28         
29         return res;
30     }
31 }

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

转载于:https://www.cnblogs.com/jimmycheng/p/8026151.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值