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 ...
随机推荐
- poj 2536 Gopher II (二分匹配)
Gopher II Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6345 Accepted: 2599 Descrip ...
- js 验证码 倒计时60秒
js 验证码 倒计时60秒 <input type="button" id="btn" value="免费获取验证码" /> & ...
- php 通过变量 来调用函数
<?php function fun() { echo 'fun'; } $a = 'fun'; $a(); ?> 复制代码 上面的$a变量就是fun()函数,调用$a()和调用fun() ...
- html、css、js文件加载顺序及执行情况
HTML页面加载和解析流程 1. 用户输入网址(假设是个html页面,并且是第一次访问),浏览器向服务器发出请求,服务器返回html文件. 2. 浏览器开始载入html代码,发现<head& ...
- 利用Lambda获取属性名称
感谢下面这篇博文给我的思路: http://www.cnblogs.com/daimage/archive/2012/04/10/2440186.html 上面文章的博主给出的代码是可用的,但是调用方 ...
- Azure china服务状态报告查看网址
https://www.azure.cn/support/service-dashboard/
- oracle 未找到提供程序。该程序可能未正确安装
使用ADO连接oracle数据库时,连接串使用Provider=OraOLEDB.Oracle时提示"未找到提供程序.该程序可能未正确安装". 原因:由于我之间安装oracle_o ...
- 6本Android开发必备图书
学习一样新事物或许有多种方式,报培训班,看视频,向高手请教等等,但一本好书往往可以让你少走很多弯路,事半功倍.今天与大家分享一些Android开发值得一读的书籍,希望对新手安卓开发者们有所帮助. 1. ...
- 关于把A表中的数据复制到B表中(整理)
如果A,B两个表中没有重复数据且表结构一样可以直接 insert into B select * from A 如果结构不一样可以 insert into B(字段列表),select 字段列表 fr ...
- Activity的生命周期及各生命周期方法的作用
一.Activity的生命周期中各个方法的作用 onCreate(): 做Activity上所需要数据的初始化工作. onStart(): 显示Activity界面,此时用户对界面可见但不可交互. o ...