Determine whether an integer is a palindrome. Do this without extra space.
2017-11-22 17:40
441 查看
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.
要求还是比较多的。终于自己写出来一个,改了几次终于Accepted了。
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 boolean isPalindrome(int x) { if (x < 0) { return false; } else if (x / 10 == 0) { //个位数默认为true return true; } else { int a = 0; int b = x; while (x != 0) { a = a * 10 + x % 10; //不断取最后一位 x = x / 10; //取到去除最后一位的新值 } if (a == b) { return true; } else { return false; } } }
要求还是比较多的。终于自己写出来一个,改了几次终于Accepted了。
相关文章推荐
- Determine whether an integer is a palindrome. Do this without extra space.
- Determine whether an integer is a palindrome. Do this without extra space.
- 【LeetCode】 Determine whether an integer is a palindrome. Do this without extra space
- Determine whether an integer is a palindrome. Do this without extra space.
- 3、Determine whether an integer is a palindrome
- P31 (**) Determine whether a given integer number is prime.
- THIS IS AN AUTO-GENERATED MESSAGE - PLEASE DO NOT REPLY TO THIS MESSAGE
- P31 (**) Determine whether a given integer number is prime.
- Given an integer, write a function to determine if it is a power of two
- This is a prerequisite condition to test whether sufficient total swap space is available on system
- Determine whether the concatenation of a and b in this order is a square number.
- leetCode Given an integer (signed 32 bits), write a function to check whether it is a power of 4
- Cannot proceed with delivery: an existing transporter instance is currently uploading this package
- ERR hash value is not an integer
- 解决小米手机Android Studio安装app 报错的问题It is possible that this issue is resolved by uninstalling an existi
- Cannot proceed with delivery: an existing transporter instance is currently uploading this package
- 给.Net程序员的PInvoke Tips [1]: String is Sometimes an Integer
- [Resolution]Your search cannot be completed because this site is not assigned to an indexer
- Entity Framework "There is already an open DataReader associated with this 的解决办法
- VS错误error C3872: '0x3000': this character is not allowed in an identifier