7. Reverse Integer 反转整数
[抄题]:
将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数)。
给定 x = 123
,返回 321
给定 x = -123
,返回 -321
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
负数、末尾有0均可用该方法处理
[思维问题]:
[一句话思路]:
分离一位数、一边变长 另一边变短
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 不知道newres怎么变大:依靠res来递推变大
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
- 不知道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 反转整数的更多相关文章
- [Leetcode] reverse integer 反转整数
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...
- [leetcode]7. Reverse Integer反转整数
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- leetcode 7 reverse integer 反转整数
描述: 给定32位整数,反转,如321转成123. 解决: 关键是溢出检测: int reverse(int x) { ; int temp; while (x) { temp = ret * + x ...
- Reverse Integer - 反转一个int,溢出时返回0
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- [LintCode] Reverse Integer 翻转整数
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...
- 【LeetCode】7、Reverse Integer(整数反转)
题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...
- 7. Reverse Integer[E]整数反转
题目 Given a 32-bit signed integer, reverse digits of an integer. Example1: x = 123, return 321 Exampl ...
- lintcode :reverse integer 颠倒整数
题目: 颠倒整数 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 解题: 直接 ...
- 【LeetCode每天一题】Reverse Integer(反转数字)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: ...
随机推荐
- 浅谈如何在SQL Server中生成脚本
在生成脚本过程中,有很多参数可以选择,合理的配置这些参数,可以让我们很方便的按照我们的期望生成脚本. 生成脚本的一些选项,如下图: 我这里是SQL 2005 的选项, SQL 2008 的选项跟这个稍 ...
- 《DSP using MATLAB》示例Example7.19
代码: M = 33; alpha = (M-1)/2; Dw = 2*pi/M; l = 0:M-1; wl = Dw*l; %Hdr = [0, 0, 1, 1]; wdl = [0, 0.6, ...
- C# 自定义exe引用的dll路径
MSDN原文:https://msdn.microsoft.com/library/twy1dw1e(v=vs.100).aspx <runtime> 的 <assemblyBind ...
- 转:使用django-admin.py创建django工程
原文:http://blog.csdn.net/a921800467b/article/details/8257352 安装Django首先需要安装数据库,可选的数据库有好几种,我选择的是MySQL数 ...
- Python reload() 函数
reload(module) 作用: 用于重新载入之前载入的模块. module -- 模块对象.返回模块对象. 实例: 重新载入 sys 模块,并设置默认编码为 utf8 >>> ...
- dede数据库类使用方法 $dsql(转)
dede数据库类使用方法 $dsql dedecms的数据库操作类,非常实用,在二次开发中尤其重要,这个数据库操作类说明算是奉献给大家的小礼物了. 引入common.inc.php文件 ? 1 r ...
- linux系统无法挂载U盘
插上U盘 [ 2407.650440] usb 1-3.3: new high speed USB device number 7 using s5p-ehci [ 2407.887332] usb ...
- CentOS 7 Update GCC G++
curl https://ftp.gnu.org/gnu/gcc/gcc-5.4.0/gcc-5.4.0.tar.bz2 -O echo "extracting files..." ...
- lnmp php7 安装mysqli扩展 undefined function mysqli_connect()
在用ci框架的时候, https://blog.csdn.net/zqtsx/article/details/8746497 https://blog.csdn.net/move_now/articl ...
- module_param 用于动态开启/关闭 驱动打印信息
1.定义模块参数的方法: module_param(name, type, perm); 其中,name:表示参数的名字; type:表示参数的类型; perm:表示参数的访问权限 ...