Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

解题:设定一个变量sum存放反转后的答案,每次取输入x的最后一位n,并用sum = sum*10+n更新sum。

例如题设的数字123,初始sum为0,过程如下:

取末尾的3,sum=3,x=12

取末尾的2,sum=32,x=1

取末尾的1,sum=321,x=0

特别注意x一开始就是0的处理,直接返回0就可以了。

代码:

 class Solution {
public:
int reverse(int x) {
int sum = ;
if(x == )
return ;
while(x != ){
int temp = x%;
sum = sum*+temp;
x /= ;
}
return sum;
}
};

题目还给出了一些思考:

1.If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

上述的方法避免了这个问题,10和100的输出都是1

2.Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).

【leetcode刷题笔记】Reverse Integer的更多相关文章

  1. leetcode算法题笔记|Reverse Integer

    /** * @param {number} x * @return {number} */ var reverse = function(x) { var s; if(x<0){ s=-x; } ...

  2. 【leetcode刷题笔记】Integer to Roman

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  3. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  4. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  5. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  6. leetCode练题——7. Reverse Integer

    1.题目:   7. Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: I ...

  7. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  8. 【leetcode刷题笔记】Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  9. 【leetcode刷题笔记】Reverse Nodes in k-Group

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

随机推荐

  1. 初学css个人笔记

    1.css类选择器中的类名的第一个字符不能是数字,无法再Mozilla或Firefox中起作用. 2.css中id选择器中的属性只能在每个html文档中出现一次. 3.css样式表中不需要在属性值与单 ...

  2. 关于.cap文件分析

    CAP文件是比较通用的一种文件格式,基本上大多数抓包软件都支持以此格式将捕获的网络数据包存储下来.    需要说明的是,CAP文件存储下来的,一般都是网络数据帧.不同网络传输协议下的帧格式是有差异的, ...

  3. CGI是什么 搜索了这么多,大致看明白了保留下来。

    转自:http://blog.chinaunix.net/uid-13408389-id-2894933.html 分类: CGI是什么 CGI是CommonGatewayInterface 的简称. ...

  4. .net之GridView、DataList、DetailsView(一)

    GridView:两种数据绑定方法 方法一:得到数据后,赋给DataSource属性,然后执行控件的DataBind()方法. BLL.Article bll = new BLL.Article(); ...

  5. iOS lipo 移除i386 x86_64两个平台

    由于 iOS 编译的特殊性,为了方便开发者使用,我们将 i386 x86_64 armv7 arm64 几个平台都合并到了一起, 所以使用动态库上传appstore时需要将i386 x86_64两个平 ...

  6. sublime使用技巧(2)-- 实用插件推荐【持续更新】

    1.Auto semicolon 在括号内输入分号,会自动把光标移到行尾然后再输入分号. 2.DocBlockr 补全注析格式,例如在函数上面输入/** + Enter,就会自动补全函数的注析说明. ...

  7. centos 6.9使用Rsync+Inotify-tools实现数据实时同步

    centos 6.9使用Rsync+Inotify-tools实现数据实时同步 说明: 操作系统:CentOS 6.9 源服务器:192.168.1.222 备份服务器:192.168.1.1.233 ...

  8. Frogger - poj 2253 (Dijkstra)

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28802   Accepted: 9353 Description Fr ...

  9. x264源代码学习1:概述与架构分析

    函数背景色 函数在图中以方框的形式表现出来.不同的背景色标志了该函数不同的作用: 白色背景的函数:不加区分的普通内部函数. 浅红背景的函数:libx264类库的接口函数(API). 粉红色背景函数:滤 ...

  10. <button>标签也能提交表单问题

    如何避免<button>标签也能提交表单的问题: 只需加上一个属性:type='button'即可:如<button type="button"> < ...