leetcode 9 Palindrome Number
来源:互联网 发布:剑网三喵姐捏脸数据 编辑:程序博客网 时间:2024/06/05 02:40
1. 题目分析
题目描述:Determine whether an integer is a palindrome. Do this without extra space.
该题含义:判断所给定的函数是不是回树,且不能占用额外的空间计算。
题目分析:如果将该整数进行倒转,存为另一个整数,然后对两个整数进行对比,会占用额外的计算空间,所以该种方法不适用。所以,取整数第一位、最后一位进行对比,而后去掉该两位后继续使用该方法进行手尾对比能够解决这个问题。
2. 题目解答–cpp
class Solution {public: bool isPalindrome(int x) { //当给定函数为负数时,返回false。 if (x < 0) return false; int count = 1; //计算所给定的整数的最高位是哪一位,Eg:x=323,count=100 while(x/count >=10) { count *= 10; } //取首尾进行对比后去掉首尾 while (x > 0) { int left = x/count; int right = x%10; if (left == right) { x = x%count/10; count /= 100; } else { return false; } } return true; }};
3. 心得体会
int cp = x;int count = 0;while(cp / 10 > 0) { cp = cp / 10; count++;}
该种方法的缺点是:在进行判断的时候需要使用pow()方法来计算10的count次幂,而pow()方法的参数要求至少有一个为double型,且该方法的返回值也为double型。所以在计算中需要不停的进行int和double类型间的强制转换。十分麻烦且容易出错。
阅读全文
0 0
- LeetCode 9 - Palindrome Number
- leetcode 9 Palindrome Number
- Leetcode【9】:Palindrome Number
- [leetcode 9] Palindrome Number
- [Leetcode] 9 - Palindrome Number
- LeetCode(9) Palindrome Number
- [leetcode 9] Palindrome Number
- LeetCode 9:《Palindrome Number》
- leetcode.9---------------Palindrome Number
- leetCode #9 Palindrome Number
- leetcode 9 Palindrome Number
- LeetCode 9: Palindrome Number
- leetcode-9 Palindrome Number
- LeetCode 9 Palindrome Number
- LeetCode 9 Palindrome Number
- LeetCode 9 - Palindrome Number
- leetcode #9 Palindrome Number
- leetcode 9 -- Palindrome Number
- Struts2 国际化
- linux Centos7.2 lnmp 环境搭建
- 工作中遇到一些知识点2017.7.13
- eclipse 4.5搭建maven web项目
- Nginx 配置代码
- leetcode 9 Palindrome Number
- Windows + Faster R-CNN配置若干问题与解决
- Cocos2d 3.X将JS转成JSC文件
- 抽象类和接口以及二者的区别
- python IP处理模块 IPy
- TokuDB配置目录参数——脚本实现
- ubuntu安装软件出现Unable to locate package错误解决
- jsp时间插件
- VS常用快捷键