【LeetCode从零单排】No.7 Reverse Integer

前话

今天开始励志刷一下leetcode上面的题目(还好这个网站没被TG和谐)。从easy的开始,数一下差不多有40道,争取两个月搞定。

题目

没想到做的第一道题目,虽然看似简单,却费了很大周折。

题目如下:

Reverse digits of an integer.

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

刚看到这道题,首先蹦出的想法是把整数转换为字符串,然后前后位置换下再转回int型,实事证明这样是不可取的,因为当输入的int型数字很大的时候,比如412851289525983,就会产生溢出,这样在使用Integer.parseInt这种函数的时候会报错,正确的做法是对数字进行int操作,利用取10的余数获得每位数字。下面展示下,错误答案和正确答案。

代码

1.正确解

public class Solution {
    public int reverse(int x) {
        int sum = 0;
        while (Math.abs(x) != 0)
        {
            if(Math.abs(sum) > Integer.MAX_VALUE / 10)
            {
                return 0;
            }
            sum = sum * 10 + x % 10;
            x = x / 10;
        }

        return sum;
    }
}

2.错误解

public class Solution {
    public int reverse(int x) {
     	 if(Math.abs(x)>100){
			 return 0;
		 }
		 else{
	     String x_str=Integer.toString(x);
	     char[] x_char = x_str.toCharArray();
	     String x_reverse_str="";
	     //System.out.print(Character.isDigit(x_char[0]));
	    if(Character.isDigit(x_char[0])){
	    	 for(int i=(x_str.length()-1);i>=0;i--){
	    		 x_reverse_str+=x_char[i];
	      	 }
	    	 }
	    else{
	    	  x_reverse_str+=x_char[0];
	    	  for(int i=(x_str.length()-1);i>=1;i--){
		    		 x_reverse_str+=x_char[i];
		    	 }
	    }
	     return Integer.parseInt(x_reverse_str);}

    }
}

代码下载:https://github.com/jimenbian/GarvinLeetCode

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/

时间: 2024-08-13 14:24:35

【LeetCode从零单排】No.7 Reverse Integer的相关文章

【LeetCode从零单排】No118 Pascal's Triangle

题目 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 代码 public class Solution { public List<List<Integer>> generate(int numRows) { List<List

LeetCode第[7]题(Java):Reverse Integer 标签:数学

题目:Reverse Integer 难度:Easy 题目内容: Given a 32-bit signed integer, reverse digits of an integer. Note:Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assum

【LeetCode从零单排】No.9 Palindrome Number

题目 这道题是迄今为止最快通过的一道题,改了两次就过了,runtime一般(中等偏下,这点不太满意).Palindrome就是判断一个整数是否对称. Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thin

【LeetCode从零单排】No.8 String to Integer (丧心病狂的一道题)

题目 题目很简单,就是写一个函数把string转换成int,但是通过率只有可怜的11%,难点是要考虑所有情况,特别是int溢出边界,反正我是写了2个小时还没解决,先放到这,有空接着搞,现在应该还有最后一个bug. Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see

【LeetCode】【Python题解】Reverse Integer

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 throu

【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters

题目 Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest su

【LeetCode从零单排】No70.ClimbingStairs

题目 爬楼梯问题,这是一道很有趣的问题.首先看题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 这道题一共有三个方法实现(我想到的): 1.递归(对应代码climbStairs) 如果楼梯有n层,我们回退到n-

【LeetCode从零单排】No67.AddBinary

题目 Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 代码 public class Solution { public String addBinary(String a, String b) { String lon= a.length()-b.length()>=0 ?

【LeetCode从零单排】No38.CountAndSay

题目 The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211. Given