【LeetCode从零单排】No.9 Palindrome Number
来源:互联网 发布:php网站源码 编辑:程序博客网 时间:2024/05/17 08:41
题目
这道题是迄今为止最快通过的一道题,改了两次就过了,runtime一般(中等偏下,这点不太满意)。Palindrome就是判断一个整数是否对称。
Determine whether an integer is a palindrome. Do this without extra space.
click to show spoilers.
Some hints:
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
代码
public class Solution { public boolean isPalindrome(int x) { if(x<0){ return false; } else{ if(x==0){ return true; } else{ int temp=0; int temp_x=0; temp=x; while(temp/10!=0 || (temp<=9 && temp>0)){ temp_x=temp_x*10; temp_x+=temp%10; temp=temp/10; } // System.out.print(""+temp_x); if(x==temp_x){ return true; } else{ return false; } } } } }
代码下载:https://github.com/jimenbian/GarvinLeetCode
/********************************
* 本文来自博客 “李博Garvin“
* 转载请标明出处:http://blog.csdn.net/buptgshengod
******************************************/
0 1
- 【LeetCode从零单排】No.9 Palindrome Number
- 【LeetCode从零单排】No 191.Number of 1 Bits(考察位运算)
- 【LeetCode从零单排】No.7 Reverse Integer
- 【LeetCode从零单排】No.169 Majority Element(hashmap用法)
- 【LeetCode从零单排】No.135Candy(双向动态规划)
- Leetcode NO.9 Palindrome Number
- LeetCode--No.9--Palindrome Number
- LeetCode No.9 Palindrome Number
- LeetCode No.9 Palindrome Number
- 从零单排9
- 【LeetCode从零单排】No14.LongestCommonPrefix
- 【LeetCode从零单排】No20.ValidParentheses
- 【LeetCode从零单排】No19.RemoveNthNodeFromEndofList
- 【LeetCode从零单排】No21.MergeTwoSortedLists
- 【LeetCode从零单排】No27.Remove Element
- 【LeetCode从零单排】No28Implement strStr()
- 【LeetCode从零单排】No36Valid Sudoku
- 【LeetCode从零单排】No38.CountAndSay
- android vpn 分析
- Java加密技术(四)——非对称加密算法RSA
- Java加密技术(五)——非对称加密算法的由来DH
- Java加密技术(六)——数字签名算法DSA
- [ iOS8之Swift~UI详解]
- 【LeetCode从零单排】No.9 Palindrome Number
- Java加密技术(七)——非对称加密算法最高级ECC
- 机房重构之充值
- 梅州邮储公积金核算系统 导出导入库的脚本
- poj 2576 dp背包/随机交换(体重均分)
- 【iOS开发】解决“点击MPMoviePlayerController播放视频时,出现一个黑屏闪屏问题”
- actionBar适配魅族smartBar
- 高等数学(总结8--多元微分1)
- Java加密技术(八)——数字证书