题目链接:https://leetcode.com/problems/reverse-integer/description/

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: [−2^31,  2^31 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

思路:

  • 对待处理的整数X,拷贝一份并命名为 copy,取拷贝值的绝对值进行下述数据处理;
  • 用长整型变量 ans 来存储翻转后的数值, ans变量赋初值为0; 当copy > 0 时对copy进行整除和取余操作:
    • int cur = copy % 10;  // 当前处理的数位的值
    • ans = ans * 10 + cur;
    • copy = copy / 10;
  • 对求得的ans值进行范围检验
  • X值为正数,则返回ans;X值为负数则返回 -ans 。 

编码如下

 class Solution {
public:
int reverse(int x) {
// 若数值不在 [−2^31, 2^31 − 1]范围内,则返回0
//if (x < (1 << 31) || x > (~(1 << 31))) return 0; // 若x属于[-9, 9]这个范围内,则返回x
if (- < x && x < ) return x; // 其他情况
long int ans = ;
int copy = (x >= ? x : -x); while (copy > )
{
int cur = copy % ; // 当前处理的数位值
ans = ans * + cur;
copy = copy / ;
} ans = (x >= ? ans : -ans);
if (ans < ( << ) || ans > (~( << ))) return ; return static_cast<int>(ans);
}
};

007. Reverse Integer的更多相关文章

  1. No.007 Reverse Integer

    7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...

  2. LeetCode--No.007 Reverse Integer

    7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...

  3. 【JAVA、C++】LeetCode 007 Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...

  4. 【LeetCode】007. Reverse Integer

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  5. [Leetcode]007. Reverse Integer

    public class Solution { public int reverse(int x) { long rev=0; while(x!=0){ rev = rev*10+x%10; x=x/ ...

  6. 007 Reverse Integer 旋转整数

    Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123Output:  321Example ...

  7. LeetCode 【2】 Reverse Integer --007

    六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...

  8. 《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 书的地址:https://hk029.gitbooks.io/leetboo ...

  9. Python字符串倒序-7. Reverse Integer

    今天做了下LeetCode上面字符串倒序的题目,突然想Python中字符串倒序都有哪些方法,于是网上查了下,居然有这么多种方法: 个人觉得,第二种方法是最容易想到的,因为List中的reverse方法 ...

随机推荐

  1. Can you answer these queries I SPOJ - GSS1 (线段树维护区间连续最大值/最大连续子段和)

    You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A query is defi ...

  2. 批量删除Zen Cart 无图片商品

    <?php /** * * @ 批量删除Zen Cart 无图片商品 * @ 使用方法: 将本文件上传到网站根目录下运行 http://你的域名/zcdelpro.php * @ $status ...

  3. GitChat·人工智能 | 除了深度学习,机器翻译还需要啥?

    本文开始要写作的时候,翻译圈里出了一个“爆炸性”的事件.6月27日下午,一个同传译员在朋友圈里爆料:某AI公司请这位译员去“扮演”机器同传,制造人工智能取代人工同传的“震撼”效果. 这个事件瞬间在译员 ...

  4. 集合(五) TreeMap

    4.TreeMap SortedMap接口继承Map接口,是排序键值对的接口,实现排序的的方法是Comparator.而NavigableMap接口继承于SortedMap,新增了一些导航方法.而Tr ...

  5. tornado框架自定义中间件过程中的一些基础技术(1)

    为了检查当前请求是否在用户的权限列表中,我们需要获取uri(也就是当前链接),下列代码说明了获取的过程,也证明了python魔术方法的重要性class testHandler(RequestHandl ...

  6. css3 扇形动画

    扇形动画,因为我工作中遇到了只执行一次就ok,所以没细研究,该css暂时只能执行1次扇形动画,无限循环会有问题. css: @keyframes rotateAn{ 0%{transform: rot ...

  7. [Functional Programming] Add, Mult, Pow, isZero

    const log = console.log; // zero :: &fa.a const zero = f => x => x; // zero is F // once : ...

  8. [Python自学] day-16 (JS、作用域、DOM、事件)

    一.JS中的三种函数 1.普通函数 function func(){ console.log("Hello World"); } func() 2.匿名函数 setInterval ...

  9. Trying to get property 'art_id' of non-object

    “Trying to get property 'art_id' of non-object”     正在尝试获取非对象的“art-id”属性. 我之前也是这么写的没出问题<td>{{$ ...

  10. easyui datagrid 去除单击行选中事件

    转:http://www.xue163.com/588880/39049/390490560.html 解决方案: onClickRow: function (rowIndex, rowData) { ...