描述:

给定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. ArrayAdapter的用法

    list = new ArrayList<String>(); //创建一个list list.add("ID列表"); //列表里面的条目 /*ArrayAdapte ...

  2. 《DSP using MATLAB》Problem 2.4

    生成并用stem函数画出这几个序列. 1.代码: %% ------------------------------------------------------------------------ ...

  3. 51nod 算法马拉松4 B递归(YY)

    递归   基准时间限制:1 秒 空间限制:131072 KB 分值: 80 函数f(n,m) { 若n=1或m=1返回a[n][m]; 返回f(n-1,m)异或f(n,m-1); } 读入2<= ...

  4. CH1812 生日礼物

    题意 描述 ftiasch 18岁生日的时候,lqp18_31给她看了一个神奇的序列 A1, A2, ..., AN. 她被允许选择不超过 M 个连续的部分作为自己的生日礼物.ftiasch想要知道选 ...

  5. phpmyadmin不允许一个表创建多个主键的解决办法

    在phpmyadmin中执行建表语句 CREATE TABLE `user3` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(2 ...

  6. php 文件上传$_FILES中error返回值详解

    用PHP上传文件时,我们会用程序去监听浏览器发送过来的文件信息,首先会通 过$_FILES[fieldName]['error']的不同数值来判断此欲上传的文件状态是否正常.$_FILES[field ...

  7. java编码-多重(正常)

    String ISO = "ISO-8859-1"; String UTF = "UTF-8"; String GBK = "GBK"; S ...

  8. ORA-12560:TNS:协议器错误的解决方法

    使用SQL Plus登录数据库时,系统报ORA-12560:TNS:协议器错误.之前建了三个数据库实例,删除了一个实例.现在登录其中一个数据库报此错误.   工具/原料   Oracle11g 方法/ ...

  9. 02 - Unit03:注册功能实现

    注册功能实现 发送Ajax请求 服务器处理 Ajax回调处理 发送Ajax请求 绑定事件: "注册"按钮的单击事件 获取参数: 用户名/密码/昵称 请求地址: /user/regi ...

  10. vim配置之安装脚本

    vimConfig/install/install.sh git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle cp ...