65. Reverse Integer && Palindrome Number
Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321 Example2: x = -123, return -321
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).
思路: 注意符号,溢出。
class Solution {
public:
int reverse(int x) {
int tag = 1;
if(x < 0){
tag = -1;
x *= -1;
}
int k = 0, y = 0;
while(x > 0){
y = y * 10 + (x % 10);
x /= 10;
}
if(y < 0) printf("Overflow!\n");
return (y * tag);
}
};
Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.
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.
思路: 注意负数和溢出情况都是 false. 其余情况,就是反转再判断,参考上题.
class Solution {
public:
bool isPalindrome(int x) {
if(x < 0) return false;
int v = x, y = 0;
while(v > 0){
y = y * 10 + (v % 10);
v /= 10;
}
if(y == x) return true;
return false; // 注意:溢出时,也肯定是 false!
}
};
65. Reverse Integer && Palindrome Number的更多相关文章
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
- LeetCode之“数学”:Reverse Integer && Reverse Bits
1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2: ...
- Reverse Integer - Palindrome Number - 简单模拟
第一个题目是将整数进行反转,这个题实现反转并不难,主要关键点在于如何进行溢出判断.溢出判断再上一篇字符串转整数中已有介绍,本题采用其中的第三种方法,将数字转为字符串,使用字符串比较大小的方法进行比较. ...
- 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List
9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...
- Leetcode 题目整理-3 Palindrome Number & Roman to Integer
9. Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. clic ...
- 9. Palindrome Number
/* Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers ...
- No.009 Palindrome Number
9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...
- LeetCode--No.009 Palindrome Number
9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...
随机推荐
- HTML5 的data-* 自定义属性
HTML5增加了一项新功能是自定义数据属性,也就是 data-*自定义属性. 在HTML5中我们可以使用以data-为前缀来设置我们需要的自定义属性,来进行一些数据的存放. 当然高级浏览器下可通过脚本 ...
- servlet jsp 客户端服务端跳转
jsp 客户端:href jsp 服务端:forward servlet 客户端:response.sendredirect(); servlet 服务器:request.getRequestDisp ...
- Hbase中的BloomFilter(布隆过滤器)
(1) Bloomfilter在hbase中的作用 Hbase利用bloomfilter来提高随机读(get)的性能,对于顺序读(scan)而言,设置Bloomfilter是没有作用的(0.9 ...
- qgroundcontrol开发环境搭建源码编译
qgroundcontrol是一款无人机地面站开源软件,C++/QT开发 在https://github.com/mavlink/qgroundcontrol上就能找到,选择稳定版下载最新的是2.6 ...
- checkbox 全选,反选 ,全不选
在表格或者列表中经常会遇到要全选或者反选等交互,今天总结了一下代码,保留着以后直接拿来用 原理: 1. 全选:当全选checkbox被点击(不管点击之前是什么状态)后,获取其checked状态.然后对 ...
- STL 库中的陷阱----一个难以察觉的 bug
请找出下面程序的 bug? int maxProfit2(vector<int> &prices) { int local[3] = {0}; int global[3] = {0 ...
- addslashes() 函数和stripslashes()函数
addslashes() 函数 定义和用法 addslashes() 函数在指定的预定义字符前添加反斜杠. 这些预定义字符是: 单引号 (') 双引号 (") 反斜杠 (\) NULL 语法 ...
- Win10/UWP新特性系列—使用打印机
微软在Win10时代终于完成的设备系统的大统一,"56个民族,56支花……"(⊙o⊙)…,既然统一了,那么也就意味着API也统一了,所以在UWP中,我们就可以使用统一的打印API来 ...
- Smart210学习记录------linux串口驱动
转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=27025492&id=327609 一.核心数据结构 串口驱动有 ...
- 利用scale9sprite制作动态聊天背景
先上效果图 首先创建Scale9Sprite然后设置设置一个比较重要的属性 auto pScale9bg = ui::Scale9Sprite::create(); pScale9bg->set ...