Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

Note:
Assume we are dealing with an environment which could only store
integers within the 32-bit signed integer range: [−231, 231 − 1]. For
the purpose of this problem, assume that your function returns 0 when
the reversed integer overflows.

题目要求我们把一个整数翻转过来成为另一个整数,本身很简单,只要利用while循环,不停地对x对10取余和相除,就可以不断地得出每一位的数字。

class Solution {
public int reverse(int x) {
int res = 0,tag=0;
while (x != 0) {
tag = res * 10 + x % 10;
res = tag;
x = x / 10;
}
return res;
}
}

但是这题有个很有意思的地方,题目设置了比较大的输入空间,
X在整形int能显示4个字节[−2^31, 2^31 −
1]这个范围上,已知2^31-1=2147483647,-2^31=-2147483648,假设X=2333333333,反过来就是3333333332,显然大过了int能表示的范围,这里的问题就是怎么判断出反过来的时候溢出了。
这里有一个小方法,在上面的代码中,tag=res*10+x%10,反过来,一定有res=tag/10,
如果t溢出的话,tag/10肯定不可能还是res,我们可以用这个方法来判断溢出。

class Solution {
public int reverse(int x) {
int res = 0,tag=0;
while (x != 0) {
tag = res * 10 + x % 10;
if(tag/10!=res) return 0;
res = tag;
x = x / 10;
}
return res;
}
}

这里的复杂度取决于输入x的长度,所以时间复杂度为log10(x)

[LeetCode]7. Reverse Integer整数反转的更多相关文章

  1. 【LeetCode】Reverse Integer(整数反转)

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

  2. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  3. 【LeetCode】7. Reverse Integer 整数反转

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:整数,反转,题解,Leetcode, 力扣,Python, ...

  4. Leetcode7 : Reverse Integer 整数反转问题

    问题描述 Example1: x = 123, return 321 Example2: x = -123, return -321 原题链接: https://leetcode.com/proble ...

  5. leetcode:Reverse Integer(一个整数反序输出)

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

  6. leetcode:Reverse Integer 及Palindrome Number

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

  7. Leetcode#344. Reverse String(反转字符串)

    题目描述 编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man ...

  8. LeetCode 7 Reverse Integer & int

    Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...

  9. Leetcode 7. Reverse Integer(水)

    7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...

随机推荐

  1. AlteraFPGA使用通用SPIFlash - 张亚群的技术专栏 - 博客频道 - CSDN.NET

    AlteraFPGA使用通用SPIFlash - 张亚群的技术专栏 - 博客频道 - CSDN.NET Altera器件有EPCS系列配置器件,其实,这些配置器件就是我们平时通用的SPIFlash,据 ...

  2. Sequence Models 笔记(二)

    2 Natural Language Processing & Word Embeddings 2.1 Word Representation(单词表达) vocabulary,每个单词可以使 ...

  3. Shape和 layer-list

    shape 基本使用 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android= ...

  4. leetcode-002

    给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...

  5. 面试题: redis面试题 有用 redis详细

    redis面试题目总结 2017-08-25 09:31:02      0个评论    来源:Java仗剑走天涯   收藏   我要投稿   (1)什么是redis? Redis 是一个基于内存的高 ...

  6. ARC097D Equals

    传送门 题目 We have a permutation of the integers from 1 through N, p1, p2, .., pN. We also have M pairs ...

  7. 掌握HDFS的Java API接口访问

    HDFS设计的主要目的是对海量数据进行存储,也就是说在其上能够存储很大量文件(可以存储TB级的文件).HDFS将这些文件分割之后,存储在不同的DataNode上, HDFS 提供了两种访问接口:She ...

  8. 安全测试 + 渗透测试 Xmind 要点梳理

    从事测试工作多年,一直对安全测试充满神秘感.买了本书,闲来无事时翻看了解.发现书的开头提供的Xmind脑图挺有参考价值,所以做了次“搬运工”,提供给想接触了解安全测试/渗透测试的小伙伴. 安全测试要点 ...

  9. 2017乌鲁木齐区域赛I(带权并查集)

    #include<bits/stdc++.h>using namespace std;int f[200010];//代表元long long rl[200010];//记rl[i]为结点 ...

  10. sg函数和nim游戏的关系

    sg函数和nim游戏的关系 本人萌新,文章如有错漏请多多指教-- 我在前面发了关于nim游戏的内容,也就是说给n堆个数不同的石子,每次在某个堆中取任意个数石子,不能取了就输了.问你先手是否必胜.然后只 ...