【LeetCode从零单排】No.9 Palindrome Number
題目
? ? ? 這道題是迄今為止最快通過的一道題,改了兩次就過了,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
******************************************/
總結
以上是生活随笔為你收集整理的【LeetCode从零单排】No.9 Palindrome Number的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【LeetCode从零单排】No.8 S
- 下一篇: 【LeetCode从零单排】No14.L