[LeetCode] Palindrome Number 解题报告
来源:互联网 发布:ubuntu怎么安装wps 编辑:程序博客网 时间:2024/05/13 21:08
[题目]
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.
[中文翻译]
确定整数是否是回文。 不使用额外的空间。
提示:
负整数是回文吗? (如,-1)
如果你正在考虑将整数转换为字符串,请注意不使用额外空间的限制。
您也可以尝试反转整数。 但是,如果已解决了“Reverse Integer”的问题,则知道反向整数可能溢出。 你将如何处理这种情况?
有一个更通用的方法来解决这个问题。
[解题思路]
先反转整数,然后比较反转的整数是否与原整数相同。对于溢出的情况,使用long long即可解决。不是很确定,这种方法是否违反了不适用额外空间的限制。
看Discuss的时候,发现其实整数只需要反转一半即可。
[C++代码]
class Solution {public: bool isPalindrome(int x) { if (x < 0) return false; long long y = 0; int tmp = x; while (x > 0) { y = y * 10 + x % 10; x = x / 10; } return tmp == y; }};
- [LeetCode]Palindrome Number,解题报告
- LeetCode解题报告--Palindrome Number
- [LeetCode] Palindrome Number 解题报告
- [LeetCode] Palindrome Number 解题报告
- 【LeetCode】Palindrome Number 解题报告
- [leetcode] 9. Palindrome Number 解题报告
- [Leetcode] 9. Palindrome Number 解题报告
- # leetcode解题报告9. Palindrome Number
- [LeetCode 解题报告]009.Palindrome Number
- [leetcode]9. Palindrome Number@Java解题报告
- LeetCode Problem9 Palindrome Number解题报告
- 【LeetCode】9.Palindrome Number(Easy)解题报告
- 【Leetcode】9.Palindrome Number 解题
- LeetCode---Palindrome Number解题分析
- LeetCode解题-#9-Palindrome Number
- [LeetCode]Palindrome Partitioning,解题报告
- 【LeetCode】Palindrome Partitioning 解题报告
- LeetCode-Shortest Palindrome-解题报告
- java中this用法和javascript中的this用法
- 寻找WindowsFormsHost的苦难历程
- 4、快速排序
- PythonOCC 教程1: 安装
- 第三章-使用字符串——python基础教程(第二版)笔记
- [LeetCode] Palindrome Number 解题报告
- Android 缓存机制(二)
- 微信小程序,不可不知的一二三四
- 第三方开源库:流式布局:FlowLayout
- 开源PLM软件Aras详解七 在Aras的Method中如何引用外部DLL
- .cpp调用.c的变量或函数【转】
- 如何判断两个可移动磁盘卷是否在同一个USB HUB上?
- 生产者消费者问题
- Java算法基础之快速排序算法