Leetcode 7 Reverse Integer 数论
题意:将整数倒置,该题简单但是需要注意数据的范围,难得的好题。
如果出现1000000003或者-2000000003,倒置后的数超过int的范围,因此返回0,出现这种情况可以使用long long,也可以在在乘以10时进行判断。
注意:Leetcode是用linux环境的,所以他用的是g++4.78编译器,不是vc++编译器,为此在vc++编译器上我们用__int64,而g++编译器就是long long,这些是64位的int,很有用的东西,不做过相关的竞赛或者项目的人,大部分人都不知道C++有64位的int。
当然现在的编译器都支持C++11标准,就不要用__int64或long long,而是用int64,当然还有int128,Leetcode的编译器仅仅支持C++03标准,跟不上时代了,是不是更新下?当然这是题外话。。
然后给出两种解法:
long long的解法,很黄很暴力。。。
class Solution {
public:
    int reverse(int x) {
        long long ans = ;
        int sign = ;
        if(x < ){
            sign = -;
            x = -x;
        }
        for(; x; x/= ){
            ans = ans *  + (x % );
        }
        ans *= sign;
        if(ans > (long long)) return ;
        if(ans < (long long)-) return ;
        return ans;
    }
};
int + 乘之前判断的解法,设计相对精妙
class Solution {
public:
    int reverse(int x) {
        unsigned int MAX = <<;
        int ans = ;
        int sign = ;
        if(x < ){
            sign = -;
            x = -x;
        }
        for(; x; x/= ){
            if(ans > MAX / ) return ;//乘之前的判断,防止溢出
            ans = ans *  + (x % );
        }
        return ans*sign;
    }
};
Leetcode 7 Reverse Integer 数论的更多相关文章
- LeetCode 7 Reverse Integer(反转数字)
		题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ... 
- leetcode:Reverse Integer 及Palindrome Number
		Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ... 
- LeetCode 7 Reverse Integer & int
		Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ... 
- Leetcode  7. Reverse Integer(水)
		7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ... 
- leetcode:Reverse Integer(一个整数反序输出)
		Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ... 
- [LeetCode][Python]Reverse Integer
		# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ... 
- 【LeetCode】Reverse Integer(整数反转)
		这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 ... 
- LeetCode 7. Reverse Integer (倒转数字)
		Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ... 
- [LeetCode] 7. Reverse Integer 翻转整数
		Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ... 
随机推荐
- String与StringBuffer的区别
			首先,String和StringBuffer主要有2个区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringB ... 
- 让spark运行在mesos上 -- 分布式计算系统spark学习(五)
			mesos集群部署参见上篇. 运行在mesos上面和 spark standalone模式的区别是: 1)stand alone 需要自己启动spark master 需要自己启动spark slav ... 
- apache端口的修改
			apache 这个web服务默认在80端口监听...如果你访问一个网站 http://www.baidu.com 则默认一个端口是80 1. 一台机器可以有 1-65535 号端口 2. ... 
- [转]Caffe 深度学习框架上手教程
			Caffe 深度学习框架上手教程 机器学习Caffe caffe 原文地址:http://suanfazu.com/t/caffe/281 blink 15年1月 6 Caffe448是一个清 ... 
- BestCoder Round #87 1001
			GCD is Funny Accepts: 524 Submissions: 1147 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 655 ... 
- DevExpress组件之——PopupMenu组件(转)
			出处:http://www.cnblogs.com/xlx0210/archive/2010/07/14/1777366.html 目录在项目中使用了第三方控件DevExpress,得开始研究其他控件 ... 
- Win7启动修复(Ubuntu删除后进入grub rescue的情况)
			起因:装了win7,然后在另一个分区里装了Ubuntu.后来格掉了Ubuntu所在的分区.系统启动后出现命令窗口:grub rescue:_ 正确的解决方式: 1.光驱插入win7安装盘或者用USB启 ... 
- radio button(单选按钮)
			单选按钮只是input输入框的一种类型. 每一个单选按钮都应该嵌套在它自己的label(标签)元素中. 注意:所有关联的单选按钮应该使用相同的name属性. 下面是一个单选按钮的例子: <lab ... 
- html 问题
			1.footer处理 目标: 页面较短,footer位于页面底部,出现滚动条时,依然在原来的位置,不随滚动条移动 页面较长,位于元素底部 .container{ position: relative ... 
- Spring <context:annotation-config/> 解说
			在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册 AutowiredA ... 
