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?

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.

click to show spoilers.

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.

思路: 注意负数和溢出情况都是 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的更多相关文章

  1. leetcode:Reverse Integer 及Palindrome Number

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

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

  3. LeetCode之“数学”:Reverse Integer && Reverse Bits

    1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2:  ...

  4. Reverse Integer - Palindrome Number - 简单模拟

    第一个题目是将整数进行反转,这个题实现反转并不难,主要关键点在于如何进行溢出判断.溢出判断再上一篇字符串转整数中已有介绍,本题采用其中的第三种方法,将数字转为字符串,使用字符串比较大小的方法进行比较. ...

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

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

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

  7. 9. Palindrome Number

    /* Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers ...

  8. No.009 Palindrome Number

    9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...

  9. LeetCode--No.009 Palindrome Number

    9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...

随机推荐

  1. MVC模型

    MVC:model.view.controller. 浏览器browser发出一个请求,被servlet(控制器controller)接收,由servlet去实例化一个模型层(JavaBean)的对象 ...

  2. PureLayout和Masonry比较

    一年前那时我做iOS开发,为了自动布局适配多种屏幕,我一般使用Masonry,后来偶然地在一个视频教程中发现老师使用了UIView+Autolayout(现在作者改名为PureLayout)自动布局, ...

  3. hive的基本操作

    1.创建表 First, create a table with tab-delimited text file format: (1)CREATE TABLE u_data ( userid INT ...

  4. C++指针比较的问题

    在C++里面,指针的比较是要保障type-safe的,也就是说,这两个指针必须是convertible的:从一个指针能够直接转换到另一个指针(有中间路径不算,不然都往void*转没完没了),顺序不限 ...

  5. HDU 1538

    http://acm.hdu.edu.cn/showproblem.php?pid=1538 经典经济学问题,海盗分金 分析http://www.guokr.com/article/41423/ #i ...

  6. 浅谈C#中的接口和抽象类

    C#中接口为"其他方面互不相干"的类型提供公共的服务和特征:C#中class只支持但继承,使用接口却支持多继承,例如:C#中System.String是从System空间中的4个i ...

  7. C++学习笔记22:设备

    设备类型 设备文件的性质 设备文件不是普通的磁盘文件 读写设备的数据需要与相应的设备驱动器通信 设备文件的类型 字符设备:读写串行数据字节流,如串口.终端等 块设备:随机读写固定尺寸数据块,如磁盘设备 ...

  8. SPARQL1.1 101 Language and Jena support

    1 introduction definition cited from SPARQL 1.1 Overview: SPARQL 1.1 is a set of specifications that ...

  9. [读书笔记]java中的类加载器

    以下内容大多来自周志明的<深入理解Java虚拟机>. 类加载器是java的一项创新,也是java流行的重要原因之一,它最初是为了满足java applet的需求而开发出来. 什么是appl ...

  10. dede织梦后台如何修改?如何增加删除菜单?(

    织梦后台的模板存放在dede/templets下面, 登陆首页模板login.htm, 进入首页模板index_body.htm, 左侧菜单模板index_menu2.htm. 修改左侧菜单dede/ ...