7. Reverse Integer

题目描述:

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123

Output: 321

Example 2:

Input: -123

Output: -321

Example 3:

Input: 120

Output: 21

Note:

Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

问题解法 一:

class Solution {
public int reverse(int x) {
int reversed = 0;
int pop = 0;
while(x!=0)
{
pop = x%10;
x = x/10; if(reversed>Integer.MAX_VALUE/10 || (reversed==Integer.MAX_VALUE/10 && pop>7)){
return 0;
} if(reversed<Integer.MIN_VALUE/10 || (reversed==Integer.MIN_VALUE/10 && pop<-8)){
return 0;
} reversed = reversed*10+pop; } return reversed;
}
}

在本题中,难点主要是有限整数的翻转和防止值溢出。

  • 对于有限整数的翻转,本题采用的方法是用循环,取余,再除以10的方法
  • 而对于栈溢出

根据如下公式:

采用:

if (reversed>Integer.MAX_VALUE || (reversed==Integer.MAX_VALUE && pop>7)

if(reversed<Integer.MIN_VALUE || (reversed==Integer.MIN_VALUE && pop<-8))

问题解法 二:

当然还有一种解法就是使用long型号数组,再转化成(int), 这里省去了复杂的公式判断;因为在int型中,如果不按照公式进行判断的话,就会溢出,缺点是由于测试数据并未超过long型号的长度,所以也能通过。

 public int reverse(int x) {
long res = 0;
while (x != 0) {
res = res * 10 + x % 10;
x = x / 10;
} if (res < Integer.MIN_VALUE || res > Integer.MAX_VALUE) {
return 0;
} else {
return (int)res;
}
}

Leetcode练习题 7. Reverse Integer的更多相关文章

  1. 《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 书的地址:https://hk029.gitbooks.io/leetboo ...

  2. C# 写 LeetCode easy #7 Reverse Integer

    7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 ...

  3. 【一天一道LeetCode】#7. Reverse Integer

    一天一道LeetCode系列 (一)题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, ...

  4. 【算法】LeetCode算法题-Reverse Integer

    这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...

  5. leetcode:7. Reverse Integer

    这题简单,也花了我好长时间,我自己写的code比较麻烦,也没啥技巧:按正负性分类执行,先转化成字符串,用stringbuilder进行旋转,如果超出范围了就用try catch public int ...

  6. 【LeetCode】7. Reverse Integer 整数反转

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:整数,反转,题解,Leetcode, 力扣,Python, ...

  7. 【LeetCode】#7 Reverse Integer

    [Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...

  8. 【LeetCode】7. Reverse Integer 整型数反转

    题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 思路:不 ...

  9. LeetCode(7) - Reverse Integer

    题目的要求就是要反转一个Integer,例如输入123,则输出321,这一题比较tricky的地方就是它有可能越界,就是说1234567899,反过来是9987654321是一个越界的Integer, ...

随机推荐

  1. 编译安装最新版nettle和gnutls

    编译安装最新版gnutls的时候,总是会出libnettle 3.4.1 was not found的报错信息. 即使编译安装了nettle的最新版3.5之后,依然会报该错. 原因是gnutls编译的 ...

  2. Spring Cloud中Hystrix 线程隔离导致ThreadLocal数据丢失问题分析

    最近spring boot项目中由于使用了spring cloud 的hystrix 导致了threadLocal中数据丢失,其实具体也没有使用hystrix,但是显示的把他打开了,导致了此问题. 导 ...

  3. efcore dotnet cli add-migrations update-database

    add-migrations update-database 如何通过dotnet cli调用 dotnet tool install --global dotnet-ef dotnet ef mig ...

  4. OpenGL光照2:材质和光照贴图

    本文是个人学习记录,学习建议看教程 https://learnopengl-cn.github.io/ 非常感谢原作者JoeyDeVries和多为中文翻译者提供的优质教程 的内容为插入注释,可以先跳过 ...

  5. MySQL EXPLAIN 语句

    对于 MySQL 在执行时来说,EXPLAIN 功能上与 DESCRIBE 一样.实际运用中,后者多用来获取表的信息,而前者多用于展示 MySQL 会如何执行 SQL 语句(Obtaining Exe ...

  6. centos 安装gitlab

    1.开始安装依赖软件:yum -y install policycoreutils openssh-server openssh-clients postfix 2.设置postfix开机自启动,po ...

  7. E203数据冲突处理OITF

    流水线的数据冲突分为三类:WAR,RAW,WAW https://wenku.baidu.com/view/e066926d48d7c1c708a14508.html WAR: write after ...

  8. ABP进阶教程1 - 条件查询

    点这里进入ABP进阶教程目录 添加实体 打开领域层(即JD.CRS.Core)的Entitys目录 //用以存放实体对象添加一个枚举StatusCode.cs //状态信息 using System; ...

  9. [转]RHEL7上配置NFS服务

    原文地址:http://380531251.blog.51cto.com/7297595/1659865 1.课程目标 了解什么是NFS及其功能: 掌握NFS的配置: 掌握NFS的验证: 能够单独熟练 ...

  10. redis分享

    Redis介绍 ´Redis是一种基于键值对的NoSQL数据库. ´Redis基于内存来存放数据. ´速度快,官方给出读写性能可达到10万/秒(数据存内存,C语言实现,单线程架构). ´丰富的数据结构 ...