文档章节

Reverse Integer -- LeetCode

勤奋的桑尼
 勤奋的桑尼
发布于 2015/07/28 10:26
字数 309
阅读 6
收藏 0

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

click to show spoilers.

Have you thought about this?

Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Update (2014-11-10):
Test cases had been added to test the overflow behavior.

[解题思路]

一般来说整数的处理问题要注意的有两点,一点是符号,另一点是整数越界问题

Init Code : RunTime 308ms

public int reverse(int x) {
        boolean negative = false;
        String str = String.valueOf(x);
         if(x<0){
            negative = true;
            str = str.substring(1);
        }
        char[] strArray = str.toCharArray();
        int length = strArray.length;
        
        for(int i=0;i<length/2;i++){
        	char temp = strArray[i];
        	strArray[i] = strArray[length-1-i];
        	strArray[length-1-i] = temp;
        }
        str = String.valueOf(strArray);
        int reverseX = 0;
        try{
             reverseX = Integer.parseInt(str);
        }catch(Exception e){
            return reverseX;
        }
        if(negative){
            return -reverseX;
        }
        return reverseX;
    }

Improved Code : RunTime :296ms

时间上虽然提升不明显,但是处理上并无需借助String,直接做数字操作即可

public int reverse(int x) {
	   if(x==Integer.MIN_VALUE)
	       return 0;
	   int num = Math.abs(x);
	   int res = 0;
	   while(num!=0){
	       if(res > (Integer.MAX_VALUE-num%10)/10){
	           return 0;
	       }
	       res = res*10+num%10;
	       num /= 10;
	   }
	   return x>0?res:-res;
    }



© 著作权归作者所有

勤奋的桑尼
粉丝 1
博文 32
码字总数 32516
作品 0
徐汇
程序员
私信 提问
[leetcode] Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the int......

jdflyfly
2014/06/24
0
0
leetcode -- Reverse Integer

Reverse Integer Description Given a 32-bit signed integer, reverse digits of an integer. Example 1: Example 2: Example 3: Note: Assume we are dealing with an environment which c......

leiline
2017/11/07
0
0
[leetcode] Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus......

jdflyfly
2014/06/24
0
1
LeetCode:Reverse Integer - 翻转数字

1、题目名称 Reverse Integer(翻转数字) 2、题目地址 https://leetcode.com/problems/reverse-integer/ 3、题目内容 英文:Reverse digits of an integer. 中文:翻转一个正整数的各位,形...

北风其凉
2015/09/20
408
0
LeetCode:Palindrome Number - 回文数

1、题目名称 Palindrome Number(回文数) 2、题目地址 https://leetcode.com/problems/palindrome-number 3、题目内容 英文:Determine whether an integer is a palindrome. Do this witho......

北风其凉
2015/09/24
109
0

没有更多内容

加载失败,请刷新页面

加载更多

idea下springboot 项目在static目录下添加文件不生效

idea下springboot 项目在static目录下添加文件不生效 问题描述 是这样子的,我的项目目录结构如下: 我在static目录下,创建了index.html和aaaa.jpg这两个文件。然后,启动服务访问 http://l...

wotrd
昨天
5
0
k8s1.14 一、环境

1. 4台虚拟机 (CentOS Linux release 7.2.1511 (Core) ) 192.168.130.211 master 192.168.130.212 node1 192.168.130.213 node2 192.168.130.214 node3 2. 设置服务器hostname 2.1 设置本机......

ThomasCheng
昨天
4
0
盖茨:如果我现在开创一家公司 将会专注于AI

新浪科技讯,北京时间 6 月 26 日凌晨消息,微软联合创始人比尔·盖茨(Bill Gates)在周一接受采访时表示,如果他今天从哈佛大学辍学并开创一家新公司,那么这家公司将会专注于人工智能(A...

linuxCool
昨天
1
0
聊聊feign的Retryer

序 本文主要研究一下feign的Retryer Retryer feign-core-10.2.3-sources.jar!/feign/Retryer.java public interface Retryer extends Cloneable { /** * if retry is permitted, retur......

go4it
昨天
14
0
HyperLogLog简介

  (1)HyperLogLog简介      在Redis 在 2.8.9 版本才添加了 HyperLogLog,HyperLogLog算法是用于基数统计的算法,每个 HyperLogLog 键只需要花费 12 KB 内存,就可以计算接近 2^64 个...

SEOwhywhy
昨天
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部