Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

click to show spoilers.

Have you thought about this?

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?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

我比较认可的做法:

比较简洁,没有涉及到将整型转化为字符串,所以也不用特别处理正负的问题。
class Solution {
public:
int reverse(int x) {
int result=0;
do{
if((result>214748364)||(result<-214748364))
return 0;
result*=10;
result+=(x%10);
}while(x=x/10);
return result;
}
};

或:

class Solution {
public:
int reverse(int x) {
double res = 0;
while (x){
res = res * 10 + x % 10;
x = x / 10;
}
return (res > INT_MAX || res < INT_MIN) ? 0 : res;
}
};

看看几种其他的做法,拓展:

1、

class Solution {
public:
int reverse(int x) {
int flag = 0;
if(x < 0)
{
flag = 1;
x = abs(x);
}
string num;
while(x)
{
num += x%10 + '0'; //'0'是空,字符串的结尾处用
x /= 10;
}
int revx = 0;
revx = std::atoi(num.c_str());
if(revx%10 != (num[num.size() -1]-'0'))  //判断溢出
return 0;
if(flag)
revx = 0-revx;
return revx;
}
};

注:atoi()函数原型为: int atoi(char *str),用途是将字符串转换成一个整数值,str是待转化成整数值的字符串.成功则返回转化后的整数值,失败返回0.

c_str()函数原型为:const char *c_str(),如果要将string对象,转化为char*对象,c_str()提供了这样一种方法,它返回一个客户程序可读不可改的指向字符数组的指针。

2、

class Solution {
public:
int reverse(int x) { std::string s = std::to_string(x);//to_string函数--int to string
stringstream m;
int s1;
int d;
int begin=;
if (s[]=='-'){
begin++;
}
int k=s.length()+begin;
for(int i=begin;i<k/;i++)  //左右两边对换
{
s1=s[i];
s[i]=s[k--i];
s[k--i]=s1;
}
m<<s;//向流m中传值
m>>d;//向d中写入值 if (d<=- || d>=){
return ;
}
return d;
}
};

注:http://blog.163.com/chen_dawn/blog/static/11250632010111215937586/   中可详见c++stringstream的用法。

Palindrome Number

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.

回文数:      将这个数的数字按相反的顺序重新排列后,所得到的数和原来的数一样。

分析:首先想到,可以利用上一题,将整数反转,然后与原来的整数比较,是否相等,相等则为 Palindrome 的。可是 reverse() 会溢出。

正确的解法是,不断地取第一位和最后一位(10 进制下)进行比较,相等则取第二位和倒数第 二位,直到完成比较或者中途找到了不一致的位。

class Solution {
public:
bool isPalindrome(int x) {
if (x < 0) return false;//负数显然不是回文数
int p1 = 0, p2 = x;
while (p2 > 0) {
p1 = p1*10 + p2%10;
p2 /= 10;
}
return p1 == x;
}
};

或:

class Solution {
public:
bool isPalindrome(int x) {
if(x<0)
return false;
int z=x;
long y=0;
while(z!=0)
{
y=y*10+z%10;
z=z/10;
}
if(y>INT_MAX || y<INT_MIN || x!=y)
return false;
else
return true;
}
};

leetcode:Reverse Integer 及Palindrome Number的更多相关文章

  1. 65. Reverse Integer && Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, re ...

  2. Leetcode 题目整理-3 Palindrome Number & Roman to Integer

    9. Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. clic ...

  3. 【LeetCode算法-9】Palindrome Number

    LeetCode第9题 Determine whether an integer is a palindrome. An integer is a palindrome when it reads t ...

  4. 【LeetCode】9、Palindrome Number(回文数)

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

  5. LeetCode(9)Palindrome Number

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  6. leetCode练题——9. Palindrome Number

    1.题目 9. Palindrome Number   Determine whether an integer is a palindrome. An integer is a palindrome ...

  7. LeetCode: Reverse Integer 解题报告

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...

  8. 【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 ...

  9. leetcode reverse integer&&Palindrome Number

    public class Solution { public int reverse(int x) { int ret=0; while(x!=0) { int t=x%10; ret=ret*10+ ...

随机推荐

  1. Arduino 433 自定义发射

    /* This is a minimal sketch without using the library at all but only works for the 10 pole dip swit ...

  2. spring mvc 中web.xml配置信息解释

    在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...

  3. python Polygon模块安装

    pip install Polygon这样会安装不了 只能使用pip install Polygon2 或者 pip install Polygon3,也就是必须带版本号

  4. 分布式计算(五)Azkaban使用

    在安装好Azkaban后,熟悉Azkaban的用法花了较长时间,也踩了一些坑,接下来将详细描述Azkaban的使用过程. 目录 一.界面介绍 二.Projects 1. 创建Command类型单一Jo ...

  5. (推荐)Skyline调用WMTS服务接口

    文章地址 http://blog.csdn.net/chaiqi/article/details/9302373 供大家学习参考.

  6. Redis详解(七)------ AOF 持久化

    上一篇文章我们介绍了Redis的RDB持久化,RDB 持久化存在一个缺点是一定时间内做一次备份,如果redis意外down掉的话,就会丢失最后一次快照后的所有修改(数据有丢失).对于数据完整性要求很严 ...

  7. Canvas绘图优化之使用位图--基于createjs库

    在地图上实时绘制大量(万级别)图形,实时绘制的原因是因为各个图形形状不同,图形要按照后端传送的参数来绘制. 用canvas绘制图形比较方便,javascript的api接口也比较简单.现在也有很多的j ...

  8. Tensorflow实例:利用LSTM预测股票每日最高价(一)

    RNN与LSTM 这一部分主要涉及循环神经网络的理论,讲的可能会比较简略. 什么是RNN RNN全称循环神经网络(Recurrent Neural Networks),是用来处理序列数据的.在传统的神 ...

  9. 4358: permu

    4358: permu 链接 分析: 不删除的莫队+可撤销的并查集. 每次询问先固定左端点到一个块内,然后将这些右端点从小到大排序,然后询问的过程中,右端点不断往右走,左端点可能会撤销,但是移动区间不 ...

  10. QT 小总结

    遇到的问题: 1:在debug模式下可以顺利执行,但是换到release模式下没法执行了.会显示 exited with code 1 . 解决办法:把产生的release文件放到QT的bin库下,看 ...