题目意思:int数字反转

考虑:越界问题

 class Solution {
public:
int reverse(int x) {
long ans=;
while(x){
ans=ans*+x%;
x=x/;
}
return (ans > INT_MAX || ans < INT_MIN)? : ans;
}
};

ps:leetcode中long比int要长,可是visual c++中long和int取值范围一样

因而有了下面这种我认为更好的代码

 class Solution {
public:
int reverse(int x) {
int ans=;
while(x>=||x<=-){
ans=ans*+x%;
x=x/;
}
if(ans>INT_MAX/||(ans==INT_MAX/&&x>INT_MAX%))
return ;
if(ans<INT_MIN/||(ans==INT_MIN/&&x<INT_MIN%))
return ;
return ans*+x%;
}
};

   

7 Reverse Integer(数字反转Easy)的更多相关文章

  1. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  2. 【LeetCode】Reverse Integer(整数反转)

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

  3. [LeetCode]7. Reverse Integer整数反转

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

  4. 【LeetCode】7. Reverse Integer 整数反转

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:整数,反转,题解,Leetcode, 力扣,Python, ...

  5. Leetcode7 : Reverse Integer 整数反转问题

    问题描述 Example1: x = 123, return 321 Example2: x = -123, return -321 原题链接: https://leetcode.com/proble ...

  6. LeetCode第[7]题(Java):Reverse Integer 标签:数学

    题目:Reverse Integer 难度:Easy 题目内容: Given a 32-bit signed integer, reverse digits of an integer. Note:A ...

  7. Reverse Integer - 反转一个int,溢出时返回0

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

  8. LeetCode之Easy篇 ——(7)Reverse Integer

    7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: Out ...

  9. P1553 数字反转(升级版)(copy(),reverse(),find(),substr(),erase())

    题目描述 给定一个数,请将该数各个位上数字反转得到一个新数. 这次与 NOIp2011 普及组第一题不同的是:这个数可以是小数,分数,百分数,整数.整数反转是将所有数位对调:小数反转是把整数部分的数反 ...

随机推荐

  1. Delphi 生成excel中的饼图

    生成excel中的饼图 var i,j,m,n,count1:integer; str:string; Jdate:tdatetime; channellist,potBstrtime,potEstr ...

  2. android EditText监听事件及参数详解

    editText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence ...

  3. Lucene简介

    1 lucene简介1.1 什么是lucene    Lucene是一个全文搜索框架,而不是应用产品.因此它并不像www.baidu.com 或者google Desktop那么拿来就能用,它只是提供 ...

  4. 慕课python3.5学习笔记

    本文章中有部分代码为python2 慕课python入门 慕课python进阶 布尔值 布尔值可以用and.or和not运算. and运算是与运算,只有所有都为 True,and运算结果才是 True ...

  5. .net自定义控件

    一.[.net自定义控件]TextBox控件重写 之NumTextBox 参考博客:http://www.yongfa365.com/Item/NumTextBox.html 二.[.net自定义控件 ...

  6. 账户管理命令 useradd、groupadd

    内容提要: 1. 掌握用户的 增/删/改 命令 2. 掌握组的 增/删/改 命令 组管理 1)groupadd groupadd 用于添加组账号.格式如下: groupadd [-g GID] GRO ...

  7. 用Lighttpd做图片服务器

    http://www.lsanotes.cn/install_lighttpd 用Lighttpd做图片服务器 一.安装lighttpd所需的库文件1.安装 pcrewgetftp://ftp.csx ...

  8. Android设置虚线、圆角、渐变

    有图又真相,先上图再说. 点击效果: 设置虚线: <?xml version="1.0" encoding="utf-8"?> <shape  ...

  9. PHP CodeBase: 判断用户是否手机访问(转)

    随着移动设备的普及,网站也会迎来越来越多移动设备的访问.用适应PC的页面,很多时候对手机用户不友好,那么有些时候,我们需要判断用户是否用手机访问,如果是手机的话,就跳转到指定的手机友好页面.这里就介绍 ...

  10. innode 节点

    [root@localhost soft]# ls -i tt1 tt2 xx.c [root@localhost soft]# stat tt1 File: `tt1' Size: 4096 Blo ...