leetcode:Reverse Integer 及Palindrome Number

时间:2022-08-26 23:31:46

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&period; Reverse Integer &amp&semi;&amp&semi; Palindrome Number

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

  2. Leetcode 题目整理-3 Palindrome Number &amp&semi; 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&period; Palindrome Number

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

  7. LeetCode&colon; Reverse Integer 解题报告

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

  8. 【LeetCode算法题库】Day3:Reverse Integer &amp&semi; String to Integer &lpar;atoi&rpar; &amp&semi; Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  9. leetcode reverse integer&amp&semi;&amp&semi;Palindrome Number

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

随机推荐

  1. Zabbix性能优化

    前言 如果不做表分区和删除历史数据规则设置的话,随着时间的推移zabbix的查询性能会变得很低 查看zabbix的性能 通过zabbix的NVPS(每秒处理数值数)来衡量其性能,在zabbix的das ...

  2. python 安装模块步骤

    1.下载 pyocr-0.4.1.tar.gz   tar.gz文件  解压  放到 c:/python27 文件夹下面 C:\Python27\pyocr-0.4.1  直接 cmd 命令 进入   ...

  3. Linux&lpar;Centos&rpar;全自动异地备份数据(WEB&plus;Mysql)

    文章开始之前,先问下各位站长一个问题:什么东西对于站长是十分重要的?其实对于站长而言,很多东西都是很重要的.但我们现在排除外在因素,把范围缩小到网站系统本身,哪些是非常重要的呢?网站数据就是其中之一了 ...

  4. ie浏览器下HTML上传控件input&equals;file的美化

    近期写东西用到了input=file这个按钮,给其添加背景,在其它浏览器上都可以正常的显示,可一到ie上便不听话了,完全没有添加上,显的很难看.今天在网上找到一方法,试过后感觉很好,终于把这个问题给解 ...

  5. DevExpress XtraReports 入门一 创建 Hello World 报表

    原文:DevExpress XtraReports 入门一 创建 Hello World 报表 本文只是为了帮助初次接触或是需要DevExpress XtraReports报表的人群使用的,为了帮助更 ...

  6. lua c函数注册器

    lua与c的交互 关于lua和c的交互,主要有两个方面,一是lua调用c的函数,而另一个则是c调用lua函数.而这些都是通过lua stack来进行的. c调用lua 在c里面使用lua,主要是通过l ...

  7. XVIII Open Cup named after E&period;V&period; Pankratiev&period; Grand Prix of Saratov

    A. Three Arrays 枚举每个$a_i$,双指针出$b$和$c$的范围,对于$b$中每个预先双指针出$c$的范围,那么对于每个$b$,在对应$c$的区间加$1$,在$a$处区间求和即可. 树 ...

  8. 用于主题检测的临时日志&lpar;c5ac07a5-5dab-45d9-8dc2-a3b27be6e507 - 3bfe001a-32de-4114-a6b4-4005b770f6d7&rpar;

    这是一个未删除的临时日志.请手动删除它.(5051e554-d10d-4e48-b2ca-37c38a30153a - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

  9. weixinShare&period;js &sol; 极简微信分享插件

    weixinShare.js / 极简微信分享插件 / 版本:0.1 这是一个很简单.很实用的微信分享插件,无需jQuery,只需要在网页里加入一行JS代码,即可自动识别微信浏览器并启动微信分享的提示 ...

  10. Linux基础命令---显示域名ypdomainname

    ypdomainname   ypdomainname指令显示由函数“getdomainname”返回的主机域名,使用这个指令也可以设置一个主机NIS/YP域名. 此命令的适用范围:RedHat.RH ...