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. 解决Use 'LimitInternalRecursion' to increase the limit if necessary的问题 CodeIgniter .htaccess

    配置.htaccess如下: RewriteEngine on RewriteBase / RewriteCond $1 !^(index\.php|images|robots\.txt|css|js ...

  2. 03 ArcPython实战篇一

    1.自增计算 (字段计算器) total = 0 def accumulate(increment):        global total        if total:            ...

  3. Vue:Vue-Cli 实现的交互式的项目脚手架

    一.这份文档是对应 @vue/cli.老版本的 vue-cli 文档请移步https://github.com/vuejs/vue-cli/tree/v2#vue-cli-- Vue CLI 是一个基 ...

  4. ubuntu20 使用命令安装 mysql

    命令安装 mysql sudo apt-get update sudo apt-get install -y mysql-server mysql-client 查看 mysql 安装情况 servi ...

  5. 关于Elasticsearch版本升级,Kibana报index迁移与需要x-pack插件问题

    关于Elasticsearch版本升级,Kibana报index迁移与需要x-pack插件问题 这个问题是由于elasticsearch旧版残留文件导致,使用下述指令删除即可 查看所有elastics ...

  6. 1.Linux内核模块编程

    1.模块加载程序结构 - 模块加载函数: static int _init init_function(void); module_init(init_function); - 模块卸载函数: sta ...

  7. MeteoInfoLab脚本示例:站点数据散点图

    这里演示从micaps第一类数据(地面全要素观测)中读取一个变量(用DimDataFile类的stationdata方法),然后maskout掉中国区域之外的数据,利用scatterm函数绘制散点图. ...

  8. C++ concurrent_queue

    ConcurrentQueue 用C++11提供的多线程类实现一个线程安全的队列: #include <queue> #include <mutex> #include < ...

  9. BASH提示符颜色、显示返回值,终端标题显示当前目录与正在执行的命令

    BASH的PS1变量控制提示符相关的东西,善用它可以让BASH用起来舒服很多 提示符颜色 提示符显示上一个命令的返回值(exit code),并根据是否0调整颜色 提示符生成的时间(这样就知道上一条命 ...

  10. centos平台scp通过密钥远程复制文件(免密登录)

    一,说明:两台机器的平台和ip 1,a服务器: centos8:ip:121.122.123.47 版本 [root@yjweb ~]# cat /etc/redhat-release CentOS ...