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: ...
随机推荐
- 《DSP using MATLAB》示例Example7.23
代码: wp = 0.2*pi; ws = 0.3*pi; Rp = 0.25; As = 50; [delta1, delta2] = db2delta(Rp, As); [N, f, m, wei ...
- LeetCode Path Sum IV
原题链接在这里:https://leetcode.com/problems/path-sum-iv/description/ 题目: If the depth of a tree is smaller ...
- HttpModule和HttpHandler -- 系列文章
ASP.NET 生命周期 在ASP.Net2.0中使用UrlRewritingNet实现链接重写 IHttpModule实现URL重写 使用IHttpHandler防盗链 HttpModule,Htt ...
- Cocoapods的操作
1.卸载Cocoapods命令: sudo gem uninstall cocoapods 2.安装Cocoapods的命令: >安装最新版本 sudo gem install cocoapod ...
- 搭建Dynamic Web Project(动态web项目)的springmvc工程1
本文转载自:http://blog.csdn.net/typa01_kk/article/details/45902955 此篇创建Dynamic Web Projec工程(动态web项目),下一篇, ...
- selenium新的定位方法,更简洁很方便
亲测是可以的 self.driver.find_element('id','kw').send_keys(u"凯宾斯基")
- 开启vmotion,实现虚拟机可以在线迁移的选项
先决条件: 1.vcenter5.5 2.vmotion服务开启 3.分布式交换机已经部署完毕 4.虚拟机在线迁移必须在web管理下,在vclient不可以
- phpmailer绑定邮箱
1.配置 <?php return array ( 'email_host' => 'smtp.aliyun.com', 'email_port' => '25', 'email_u ...
- PHP 统计数据功能 有感
统计,就是把基本的数据,整合起来. 用到sql的,有group by 功能,count功能,order by功能等等. sql将收集的数据,进行统计分析. 一般情况下,sql处理后得到的数据,还要通过 ...
- mybatis如何防止sql注入(2)
Mybatis框架下SQL注入漏洞修复建议1. 模糊查询like SQL注入修复建议按照新闻标题对新闻进行模糊查询,可将SQL查询语句设计如下:select * from news where ti ...