[抄题]:

将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 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. Ubuntu server 安装samba

    安装Samba时,出现了一下问题: linux-image-generic***依赖出现问题,无法安装Samba. 使用apt-get update 更新,吓尿了,全部是忽略,源是系统自带的.改成其他 ...

  2. Elasticsearch.net项目

    Elasticsearch.net项目实战 https://www.cnblogs.com/lucky_hu/p/9746736.html elasticsearch.net项目实战 @智客幸达 目录 ...

  3. PHP 操作XML文档

    <<<操作符需PHP5.3以上版本才能支持,下面程序在wamp环境下测试完成. <?php // Set the content type to be XML, so that ...

  4. 支付宝RSA签名

    1.参考网上相关文章,开放php中的openssl,但使用网上例子调用openssl_pkey_new,一直报100013错误.后改用用支付宝提供的SDKdemo程序 发现使用提供的privkye,可 ...

  5. zabbix 触发器 | count 函数

    摘要:确认多次zabbix监控中小编用的最多的是count的这函数,确认多次以减少了很多误告警,提高了运维效率.可以设置连续几次都异常才发出告警,这样一来,只要发出告警基本上就已经确定发生故障了.co ...

  6. Windows2008 R2上完全卸载Oracle操作步骤(转)

    最近现场项目,碰到了好几次oracle数据库被损坏,而且无法恢复的问题,没办法,只好卸载重装了.oracle卸载确实麻烦,都是从网上查的方法, 为了方便以后查询,在此就做一下记录. Windows20 ...

  7. Scanner 的练习 。。。。依然不懂用法。。。苦恼

    package com.b; import java.util.Random; import java.util.Scanner; public class Core { public static ...

  8. python学习(十八) 程序打包

    18.1  Distutils基础 18.2 打包 18.2.1 建立存档文件 18.2.2 创建Windows安装程序或RPM包 18.3 编译扩展 18.4 使用py2exe创建可执行程序

  9. python学习(一) 基础知识

    开始学习<Python基础教程> 1.2 交互式解释器 按照书上的例子敲了个最简单的print函数,居然报错: >>> print "fsdfs"Sy ...

  10. 获取Request.Form所有内容

    string wwww = "";        for (int i = 0; i < Request.Form.Count; i++)        {          ...