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. [linux]vmstat命令详解-显示虚拟内存状态

    本文转载于http://man.linuxde.net/vmstat 前言:Linux系统的内存分为物理内存和虚拟内存两种.物理内存是真实的,也就是物理内存条上的内存.而虚拟内存则是采用硬盘空间补充物 ...

  2. kafka 0.8.1 新producer 源码简单分析

    1 背景 最近由于项目需要,需要使用kafka的producer.但是对于c++,kafka官方并没有很好的支持. 在kafka官网上可以找到0.8.x的客户端.可以使用的客户端有C版本客户端,此客户 ...

  3. The Application does not have a valid signature

    真机运行程序,报错(The application does not have a valid signature,如图 环境:Xcode7.3,使用cocoapods管理第三方库 如果确认证书没有问 ...

  4. mysql 语句要求

    mysql 语句不可以有单引号,要把单引号替换为双引号!

  5. nginx uri和request_uri区别

    $request_uri This variable is equal to the *original* request URI as received from the client includ ...

  6. sprint3 【每日scrum】 TD助手站立会议第九天

    站立会议 组员 昨天 今天 困难 签到 刘铸辉 (组长) 整合原来做过的功能,并做相应的改进,整合其他的功能 团队进入最终的功能测试阶段,准备发布Beta版 在测试阶段BUG太多,不知道如何解决 Y ...

  7. nginx內建模块使用

    目录 nginx內建模块使用 1. 內建模块的引入 1.1 查看安装信息 1.2 重新指定配置信息 2. 內建模块的使用 2.1 http_stub_status_module 2.2 http_ra ...

  8. $on、$emit和$broadcast的使用

    $emit只能向parent controller传递event与data( $emit(name, args) ) $broadcast只能向child controller传递event与data ...

  9. tomcat安装配置规范

    tomcat用户设置 1 2 [root@host-1 ~]# useradd -u 501 tomcat [root@host-1 ~]# passwd tomcat   安装JDK 1 2 3 4 ...

  10. CSS div固定顶端

    position: fixed;原来只需要这么一个设置就可以!