[抄题]:

将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 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. docker 基本学习

    Docker的应用场景: 加速本地开发和构建流程,使其更加高效.更加轻量化.本地开发人员可以构建.运行并分享Docker容器.容器可以在开发环境中构建,然后轻松地提交到测试环境中,并最终进入生产环境. ...

  2. 使用Gson轻松解决复杂结构的Json数据解析

    转载请注明来源: http://blog.csdn.net/kjunchen/article/details/50961803 JSON简介 JSON(JavaScript Object Notati ...

  3. C# 浅拷贝与深拷贝(复制)

    在有些时候,我们需要从数据库读取数据填充对象或从硬盘读取文件填充对象,但是这样做相对耗时.这时候我们就想到了对象的拷贝.本文即以实例形式解析了C#浅拷贝和深拷贝的用法. C#中有两种类型变量,一种 是 ...

  4. 对django整个网站搭建文件的总结

    1.比方说现在是写一个使用HTTP 2.0的HTTP服务器,在不改变现有程序配置的情况下,需要重新编译HTTP服务器,比方说,用的是Nginx服务器,需要隐藏服务器版本,如果想要小绿锁 也就是网站安全 ...

  5. gatsbyjs 使用

    1. 安装 npm install --global gatsby-cli 2. 使用 // 创建项目 gatsby new dalong cd dalong // 启动 gatsby develop ...

  6. openfaas 了解

    1. 官方介绍 OpenFaaS (Functions as a Service) is a framework for building serverless functions with Dock ...

  7. Linux环境下安装jenkins

    废话不多说,直接开始 1.从官网下载Jenkins的war包 2.下载好的War放到Tomcat的网站根目录webapps下,然后启动Tomcat. 3.打开浏览器,输入http://IP:8080/ ...

  8. FastAdmin CMS 插件相关文章收集(2018-08-16)

    FastAdmin CMS 插件相关文章收集(2018-08-16) CMS内容管理系统(含小程序) 介绍 https://www.fastadmin.net/store/cms.html CMS内容 ...

  9. ui-router 1.0以上的 $stateChangeStart

    ui-router transitionhooks 统一控制路由跳转, 前台控制如果没有登录就跳转到登录页面, 当然也可以在后台控制, 如果没有登录就返回对应的错误码, 然后在response中直接跳 ...

  10. maven搭建

    http://blog.csdn.net/zhshulin/article/details/30779873 http://blog.csdn.net/zhshulin/article/details ...