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: ...
随机推荐
- RedHat Server Enterprise 6安装G++
RedHat 6默认是安装有GCC,而没有安装G++编译 要安装G++前最好先查看下GCC的版本号,通常GCC的版本和G++的版本是相同的,知道GCC的版本再去找G++的安装文件就容易些,版本号有在安 ...
- (十)java条件结构
条件结构 if(条件表达式) {}: if(条件表达式){} else {}; if(条件表达式){} else if(条件表达式) {} else if(条件表达式){} ...... else{} ...
- HDU - 6395:Sequence (分块+矩阵)
题面太丑了,就不复制了. 题意:F1=A: F2=B: Fn=D*Fn-1+C*Fn-2+P/i:求Fn. 思路:根据P/i的值划分区间,每个区间矩阵求. 带常数的矩阵: #include<bi ...
- 重温CLR(三)类型基础
所有类型都从System.Object派生 “运行时”要求每个类型最终都要从System.Object类型派生.也就是说,一下两个类型的定义完全一致. //隐式派生自Object class Empl ...
- UIImage+PYJColorBecomeImage
UIImage+PYJColorBecomeImage.h: // // UIImage+PYJColorBecomeImage.h // 颜色转成图片 // // Created by PengYu ...
- serf 简单使用
1. 介绍 // 以下为官方介绍,说白了就是进行系统的集群节点管理 Serf uses an efficient gossip protocol to solve three major proble ...
- 关于 SMT 一个重要提示
关于 SMT 一个重要提示 温度曲线是对应的具体的PCB的最差焊盘位置处的所用焊锡膏对应的熔化曲线.从这种角度来观察,我想你会明白我的意思的. https://www.amobbs.com/forum ...
- 第21篇 ubuntu安装ftp服务器(转载)
ubuntu安装ftp服务器 1: 安装vsftpd ~$ sudo apt-get install vsftpd ubuntu10.10自己装了,这步省略. 2: 配置vsftpd 2.1 修改vs ...
- c++ 浅谈 new 的使用
实例用法: 创建对象: class U_Ptr smart; U_Ptr* ptr = new U_Ptr(); class U_Ptr smart(new U_Ptr(p)); int *p = n ...
- PhysicalDrive
由于"\"是C/C+中转义符, "\\\\.\\"就相当于\\.\ 在Windows中 \\.\ 前缀用于标识设备,其中的"."表示本地计算 ...