LeetCode-Palindrome Numbers

来源:互联网 发布:楼凤阁 源码 编辑:程序博客网 时间:2024/05/18 01:36

题目:

Determine whether an integer is a palindrome. Do this without extra space.
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.

注意:负数不是回文;

            0 是回文;

解题思路:现将所给的数倒转过来,比如123倒转后为321,通过用所给数除以10(% 10)取余数、然后再将所给数除以10取商,再将余数乘以10后加上余数,循环来实现;

代码如下:


从网上看到另外一种思路:不断地取第一位和最后一位(10进制下)进行比较,相等则取第二位和倒数第二位,直到完成比较或者中途找到了不一致的位。

代码如下:


0 0
原创粉丝点击