描述:

给定32位整数,反转,如321转成123。

解决:

关键是溢出检测:

int reverse(int x) {
int ret = ;
int temp; while (x) {
temp = ret * + x % ;
if (temp / != ret)
return ;
ret = temp;
x /= ;
}
return ret;
}

看了下其他答案,还有一些思路:

先声明个long,看最后是否溢出,这样只有long是64位时可以,或者用int64_t。

还有先转字符串反转再转数字的。

leetcode 7 reverse integer 反转整数的更多相关文章

  1. [leetcode]7. Reverse Integer反转整数

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

  2. [Leetcode] reverse integer 反转整数

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

  3. 7. Reverse Integer 反转整数

    [抄题]: 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数).   样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 [暴力解法]: ...

  4. [LeetCode] 7. Reverse Integer 翻转整数

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

  5. LeetCode 7. Reverse Integer 一个整数倒叙输出

    潜在问题:(1)随着求和可能精度会溢出int 范围,需要使用long 来辅助判断是否溢出,此时返回 0 Assume we are dealing with an environment which ...

  6. 【LeetCode题解】7_反转整数

    目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...

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

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

  8. leetCode(62)-Reverse Integer

    题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 clic ...

  9. leetcode:Reverse Integer 及Palindrome Number

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

随机推荐

  1. flex布局在垂直居中里,元素超过容器大小后,不能通过滚动条滚动到顶端,这是个flex的bug

    The Problem Flexbox makes centering very easy. By simply applying align-items: center and justify-co ...

  2. Java 工程师求职遇害|多一分警惕,少一份悲剧

    当朋友圈里满是战狼票房屡创新高的刷屏文章时,一则有关 Java 开发工程师李文星面试遇害的报道,却令人唏嘘不已.年仅23岁.正值青春年少.怀揣着通过打拼奋斗实现养家糊口梦想的大学毕业生,在初入职场的第 ...

  3. 命令行视频(ts/m3u8)下载工具 —— youtube-dl(ffmpeg 解码)

    youtube-dl 支持的站点:youtube-dl Supported sites youtube-dl 命令行参数: –version:查看版本: 1. 命令行工具安装 安装视频编解码工具 ff ...

  4. 2017《Java技术》预备作业02

    1.学习使用Git和码云托管代码 参考资料:如何使用Git和码云 安装Git 在码云注册账号,新建项目,名称为Java-CS01(02)XXX, 一班为CS01,二班为CS02,后三位或两位为姓名缩写 ...

  5. Jmeter-线程组执行顺序控制

    线程组按顺序来执行,大概思路, 1.需要控制线程组内的操作在满足某一条件才执行,那么就需要使用if或者while: 2.要使用if或者while都需要一个变量,而这个变量要在两个或多个线程组内使用,那 ...

  6. 《DSP using MATLAB》 示例 Example 9.16

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  7. JavaBean和Map的相互转换

    JavaBean和Map的相互转换 一.JavaBean 1.什么是JavaBean? JavaBean其实就是一种遵循特定写法的类,必须遵循一定的规范: 类必须由public修饰,并且保证有公共的无 ...

  8. Cacti不出图

    http://os.51cto.com/art/201404/434915.htmhttp://os.51cto.com/art/201109/289306.htmhttp://freeloda.bl ...

  9. 如何查看oracle表空间是否自动扩展

    select file_name,autoextensible,increment_by from dba_data_files

  10. api proxy设置 后端服务器代理

    location ^~ /api/{ ssi on; ssi_silent_errors off; proxy_redirect off; proxy_set_header Host $host; p ...