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. 2017-2018-2 20155314《网络对抗技术》Exp2 后门原理与实践

    2017-2018-2 20155314<网络对抗技术>Exp2 后门原理与实践 目录 实验要求 实验内容 实验环境 预备知识 1.后门概念 2.常用后门工具 实验步骤 1 用nc或net ...

  2. JavaWeb界面在线配置代码生成器

    关于直接main方法运行生成代码可参考我的这篇文章:MP实战系列(六)之代码生成器讲解 在线配置主要参考jeesite和jeecg,gun等开源项目,但是与它们相比又有很多不同? 与jeesite相比 ...

  3. Fedora安装Snapd和Snap软件包

    导读 Snappy包管理器是一个跨发行版的包管理器.它最初是为Ubuntu系统构建的,但现在其他主要的Linux发行版( Fedora, Linux Mint, RHEL, OpenSUSE,Arch ...

  4. protobuf可变长编码的实现原理

    protobuf中的整数,如int32.int64.uint32.uint64.sint32.sint64.bool和enum,采用可变长编码,即varints. 这样做的好处是,可以节省空间.根据整 ...

  5. GZIP压缩提高网络传输效率

    [spring]通过GZIP压缩提高网络传输效率(可以实现任何资源的gzip压缩.包括AJAX) gzip是http协议中使用的一种加密算法,客户端向web服务器端发出了请求后,通常情况下服务器端会将 ...

  6. php操作mysql使用的socket

    在本地安装了php,安装了mysql,mysql-server.mysql服务也启动了. php扩展中也有mysql > php -m | grep mysql 然而执行mysql_connec ...

  7. c# WPF 设置窗口一直在其中窗口后面/底层窗口

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  8. pandas:字段值插入数据表第一行的解决办法

    1. 问题描述 在对课程表进行数据抽取时,由于课表结构的原因,需要在原始表字段名作为第一行数据,并对原始字段名进行替换. 原始数据如下所示: 2. 解决办法 经思考,此问题可抽象为:在不影响原始数据的 ...

  9. R实战 第十篇:列联表和频数表

    列联表是观测数据按两个或更多属性(定性变量)分类时所列出的频数分布表,它是由两个以上的变量进行交叉分类的频数分布表.交互分类的目的是将两变量分组,然后比较各组的分布状况,以寻找变量间的关系. 按两个变 ...

  10. SqlServer 案例:已有汽车每日行驶里程数据,计算其每日增量

    需求说明 某公司某项业务,需要获得用户每日行车里程数.已知能获得该车每日提交的总里程数,如何通过 T-SQL 来获得其每日增量里程? 解决方案 首选需要对数据进行编号,利用开窗函数 OVER() 实现 ...