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 ...
随机推荐
- c# 配置文件之configSections配置(三)
使用IConfigurationSectionHandler配置configSections ·步骤1:定义一个实体类 using System; using System.Collections.G ...
- debug实战:Unmanaged High Memory非托管高内存
最近又监控到一个高内存的问题,周五下班把系统打开,周末2天没关,周一来看已经涨到5.2G,这次与以往不同,不是.net的内存泄漏,而是非托管引起的. 1. 抓dump,确定高内存的类型 //dump有 ...
- Enable SSHD on Ubuntu
https://help.ubuntu.com/community/SSH/OpenSSH/Configuring
- css3 弹框提示样式
.common-dialog-box{ opacity: 0; filter: alpha(opacity=0); position: fixed; top: 0%; left: 50%; z-ind ...
- (转)javascript匿名函数的写法、传参和递归
(原)http://www.veryhuo.com/a/view/37529.html (转)javascript匿名函数的写法.传参和递归 http://www.veryhuo.com 2011-0 ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- iOS 利用 Framework 进行动态更新
http://nixwang.com/2015/11/09/ios-dynamic-update/ 前言 目前 iOS 上的动态更新方案主要有以下 4 种: HTML 5 lua(wax)hotpat ...
- uva 10375
/* 选择与除法_________________________________________________________________________________ #include & ...
- 关于 Graph Convolutional Networks 资料收集
关于 Graph Convolutional Networks 资料收集 1. GRAPH CONVOLUTIONAL NETWORKS ------ THOMAS KIPF, 30 SEPTE ...
- Mac 下 Intellij IDEA 2016.1.2+maven+jetty+ JRebel 6.4.3 破解+spring mvc
准备阶段: Intellij IDEA 2016.1.2 (官方下载,作者下载的是社区版): JRebel for IntelliJ插件安装,可选择在线安装,在线安装的是最新版,我选择本地安装, 下 ...