[抄题]:

将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数)。

 
样例

给定 x = 123,返回 321

给定 x = -123,返回 -321

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

负数、末尾有0均可用该方法处理

[思维问题]:

[一句话思路]:

分离一位数、一边变长 另一边变短

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 不知道newres怎么变大:依靠res来递推变大

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

  1. 不知道newres怎么变大:依靠res来递推变大

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

public class Solution {
/**
* @param n: the integer to be reversed
* @return: the reversed integer
*/
public int reverse(int x) {
//ini
int res = 0, newRes = 0; while (x != 0) {
int tail = x % 10;
newRes = res * 10 + tail;
if ((newRes - tail) / 10 != res) {
return 0;
}
res = newRes;
x = x / 10;
} //return
return newRes;
}
}

7. Reverse Integer 反转整数的更多相关文章

  1. [Leetcode] reverse integer 反转整数

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

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

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

  3. leetcode 7 reverse integer 反转整数

    描述: 给定32位整数,反转,如321转成123. 解决: 关键是溢出检测: int reverse(int x) { ; int temp; while (x) { temp = ret * + x ...

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

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

  5. [LintCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...

  6. 【LeetCode】7、Reverse Integer(整数反转)

    题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...

  7. 7. Reverse Integer[E]整数反转

    题目 Given a 32-bit signed integer, reverse digits of an integer. Example1: x = 123, return 321 Exampl ...

  8. lintcode :reverse integer 颠倒整数

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

  9. 【LeetCode每天一题】Reverse Integer(反转数字)

    Given a 32-bit signed integer, reverse digits of an integer. Example 1:                              ...

随机推荐

  1. bzoj 2131 免费的馅饼

    Written with StackEdit. Description Input 第一行是用空格隔开的二个正整数,分别给出了舞台的宽度\(W\)(\(1\)到\(10^8\)之间)和馅饼的个数\(n ...

  2. kali视频学习(11-15)

    第四周kali视频(11-15)学习 11.漏洞分析之OpenVAS使用 12.漏洞分析之扫描工具 13.漏洞分析之WEB爬行 14.漏洞分析之WEB漏洞扫描(一) 15.漏洞分析之WEB漏洞扫描(二 ...

  3. sql_自连接,181,182,196,197

    181. Employees Earning More Than Their Managers https://leetcode.com/problems/employees-earning-more ...

  4. Weblogic配置SSl使用Https

    一 .可以开启自带的SSL连接 启动weblogic,进入左侧菜单,点击左侧的安全领域-->点击myrealm-->点击角色和策略-->点击服务器AdminServer 点击保存,w ...

  5. mybatis-generator的坑

    有天发现mybatis-generator不能用,要加什么根项目前缀, 搞到我重新从github下载,然后发现仓库用不了,原来新项目会改我的maven配置(新坑 后来终于明白要在子项目栏使用手脚架,就 ...

  6. js练习题笔记

    javascrip测试题: 一.选择题(每题2分,多选题错选.少选不得分) 1.分析下段代码输出结果是( )    var arr = [2,3,4,5,6];    var sum =0;    f ...

  7. Pycharm安装工具包

    1.在settings->Project Interpreter->configure interpreters->Install中可以找到所有的模块. PyCharm IDE会帮助 ...

  8. thinkphp5 设置.htaccess报input file specified的解决方法

    先去检查服务器设置,这个网上方法很多就不说了,如果服务器没问题还是报这个错误的话可能和php版本有关 php5.4和以下版本的.htaccess <IfModule mod_rewrite.c& ...

  9. cors,跨域资源共享,Java配置

    一.概念 1. 如果两个页面的协议.域名和端口是完全相同的,那么它们就是同源的,不同则为跨域 2. ajax本身实际上是通过XMLHttpRequest对象来进行数据的交互,而浏览器出于安全考虑,不允 ...

  10. 解决eclipse Blocked : the user operation is waiting

    本文转载自:http://blog.csdn.net/shaw1994/article/details/44106679 出现这种情况的时候整个adb都跪了, eclipse一run就会跪, 而且还容 ...