7.Reverse Integer

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.

没有想出来, 仿照(或者照搬?)http://www.cnblogs.com/grandyang/p/4125588.html来写

注意点:

反转溢出问题, 解决方法: 使用比该数字范围大的类型定义返回值

版本一: 拆分每个位的思想是我的, 其他细节参考上述博客

class Solution {
public:
int reverse(int x) {
long long res = 0;
vector<int> bit; while (0 != x) {
bit.push_back(x % 10);
x /= 10;
} for (int i = 0; i < bit.size(); i++) {
res =res * 10 + bit[i];
} return (res < INT_MIN || res > INT_MAX) ? 0 : res;
}
};

版本二: 半个自己写的, 考虑符号, 虽然多余了

class Solution {
public:
int reverse(int x) {
long long ret = 0;
// int positive = 1;
long long positive = 1; if (x < 0) {
positive = -1;
x *= positive;
} while (x != 0) {
ret = ret * 10 + x % 10;
x /= 10;
} ret *= positive; return (ret < INT_MIN || ret > INT_MAX) ? 0 : ret;
}
};
class Solution {
public:
int reverse(int x) {
long long int ret = 0; while(0 != x) {
ret = ret*10 + x%10;
x /= 10;
} //return (ret > INT_MIN || ret < INT_MAX)? ret : 0;
return (ret < INT_MIN || ret > INT_MAX) ? 0 : ret;
}
};

7_Reverse Integer的更多相关文章

  1. 9_Palindrome Number

    9.Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it ...

  2. LeetCode 7. Reverse Integer

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

  3. Integer.parseInt 引发的血案

    Integer.parseInt 处理一个空字符串, 结果出错了, 程序没有注意到,搞了很久, 引发了血案啊!! 最后,终于 观察到了, 最后的部分: Caused by: java.lang.NoC ...

  4. 由一个多线程共享Integer类变量问题引起的。。。

    最近看到一个多线程面试题,有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC- 看到这个题目,首先想到的是解决方法是定义一个Integer类对象,初始化为0,由3个线 ...

  5. [LeetCode] Integer Replacement 整数替换

    Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...

  6. [LeetCode] Integer Break 整数拆分

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  7. [LeetCode] Integer to English Words 整数转为英文单词

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

  8. [LeetCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  9. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

随机推荐

  1. ✅Vue选择图像

    下载 Vue选择图像Vue选择图像 Vue 2.用于从列表中选择图像的组件 演示 https://mazipan.github.io/vue-select-image/ 安装 #纱 纱添加vue-se ...

  2. 初始python模块

    Python语言中,模块分为三类. 第一类:内置模块,也叫做标准库.此类模块就是python解释器给你提供的,比如我们之前见过的 time模块,os模块.标准库的模块非常多(200多个,每个模块又有很 ...

  3. Docker(Docker Toolbox)配置镜像加速更换国内源

    自己当时装的是Win10专业工作室版本,不知道为什么不支持window for docker, 所以选择了Docker Toolbox 的方式,主要是为了学习,虽然这种方式是不建议安装的,但是基础的学 ...

  4. Informatic 内些坑

    1. 工作流调用工作流(可实现无规则时间点自由调度) pmcmd startworkflow -sv 集成服务名称 -d 配置域名称 -u  Administrator -p Administrato ...

  5. markdown的基本使用

    1.什么是markdown? markdown是一种轻量级的标记语言 可以转换为html/xhtml和其它格式 可读.直观.学习成本低 当你学会使用markdown编写文档时,你会感觉自己发现了一个新 ...

  6. 多测师讲解性能测试_面试题_001高级讲师肖sir

    什么叫做性能测试?1. 软件的性能是软件的一种非功能特性,它关注的不是软件是否能够完成特定的功能,所以一般来说性能测试介入的时机是在功能测试完成之后.另外,由定义中的及时性可知性能也是一种指标,可以 ...

  7. MeteoInfoLab脚本示例:Streamline流线图

    绘制Stramline流线图的函数是streamline,需要两个变量(U/V分量或者风向/风速).脚本程序: f = addfile('D:/Temp/GrADS/model.ctl') u = f ...

  8. spring boot: 用thymeleaf嵌套循环展示多层数据(spring boot 2.3.2)

    一,什么情况下会用到嵌套循环? 当我们展示多个分类时,每个分类下又展示出推荐的前几个商品,   这时我们需要用到嵌套循环 看一个例子: 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https:/ ...

  9. 【Azure Redis 缓存 Azure Cache For Redis】如何设置让Azure Redis中的RDB文件暂留更久(如7天)

    问题描述 Azure Redis和所有的Redis服务一样,可以让你保留存储在Redis中的数据.以防万一在Redis服务器出现故障的时候能尽可能小的减少数据的损失.在Azure Redis服务中,默 ...

  10. COMET —— 常识Transformer用于自动知识图构建

    <COMET:Commonsense Transformers for Automatic Knowledge Graph Construction> 论文地址 论文源码 任务 目的层面 ...