简体   繁体   English

NumberFormatException 对于输入字符串:“9646324351”

[英]NumberFormatException For input String: "9646324351"

Here's a snapshot of a problem on LeetCode :这是LeetCode 上一个问题的快照:

在此处输入图片说明

For this problem, they provide this framework code you have to fill in:对于这个问题,他们提供了你必须填写的框架代码:

public int reverse(int x) {
}

When testing solutions, it seems to provide an unreasonable input and won't let me submit my solution.在测试解决方案时,它似乎提供了不合理的输入,并且不会让我提交我的解决方案。 The error is:错误是:

在此处输入图片说明

Note that the input is 1534236469 .请注意,输入是1534236469 That fits in an int as input , but the reversed version, 9646324351 , naturally doesn't, as the max positive value of an int in Java is 2147483647 .这适合int作为input ,但相反的版本9646324351自然不9646324351 ,因为 Java 中int的最大正值是2147483647

Is this just an error in the LeetCode test?这只是 LeetCode 测试中的错误吗? Or is there some trick I can't imagine that magically lets me return 9646324351 , a clearly out-of-range value, back from reverse as an int ?或者有什么我无法想象的技巧让我神奇地返回9646324351 ,一个明显超出范围的值,从reverse返回一个int

Here's my code, but the code almost doesn't matter, as the return type ( int ) is fixed by the problem (so by "integer" they really mean int , not long ):这是我的代码,但代码几乎无关紧要,因为返回类型 ( int ) 是由问题修复的(因此“整数”实际上是指int ,而不是long ):

public int reverse(int x) {
    String intString;
    StringBuilder sb = new StringBuilder();
    if(x < 0){
        sb.append("-");
        x = x * -1;
    }
    intString = Integer.toString(x);
    for(int i = intString.length() - 1; i >= 0; i--){
        sb.append(intString.charAt(i));
    }
    String resultString = sb.toString();
    int result = Integer.parseInt(resultString);
    return result;
}

This is an error in the LeetCode test.这是 LeetCode 测试中的错误。 There is simply no way to store the return value they're asking for in an int , thus no way to return it from reverse per the framework code provided.根本无法将他们要求的返回值存储在int ,因此无法根据提供的框架代码从reverse返回它。

Perhaps the test cases were randomly generated across the full range of int , not taking into account that reverse can take the value out of range for an int for certain inputs.也许测试用例是在int的整个范围内随机生成的,没有考虑到reverse可能会使某些输入的值超出int的范围。

It turns out that they want you to return 0 if the reversed number would be out of range.事实证明,如果反向数字超出范围,他们希望您返回0 Not that they bother to mention that anywhere in the problem.并不是说他们费心在问题的任何地方提及这一点。

I finished by follow method我按照方法完成了

    int result=0;
    try{
        result = Integer.parseInt(stringBuilder.toString());
        return result;
    }catch(Exception e){
        return 0;
    }

You should use Long.parseLong() .你应该使用Long.parseLong() The number is too big for integer.这个数字对于整数来说太大了。 The maximum value for integer is 2^31-1=2147483647.整数的最大值为 2^31-1=2147483647。

I have successfully submit the code but still I want to optimize in terms of running speed and space utilization.我已经成功提交了代码,但我仍然想在运行速度和空间利用率方面进行优化。 Can you please share your thoughts on this.能否请您分享您对此的看法。

class Solution {
    public int reverse(int x) {
        String reverse;
        if(x==0 && x > Integer.MAX_VALUE && x < Integer.MIN_VALUE){return 0;}
        if(x<0){
            x = Math.abs(x);
            String temp = String.valueOf(x);
            int len = temp.length();
            reverse = "-";
            for(int i=len-1;i>=0;i--){
                reverse = reverse + temp.charAt(i);
            } 
        }else{
            String temp = String.valueOf(x);
            int len = temp.length();
            reverse = "";
            for(int i=len-1;i>=0;i--){
                reverse = reverse + temp.charAt(i);
            } 
        } 
        try{
        int rev = Integer.parseInt(reverse);
        return rev;
        }catch(Exception e){return 0;}
    }
}

Runtime: 10 ms, faster than 8.10% of Java online submissions for Reverse Integer.运行时间:10 毫秒,比 Java 在线提交的反向整数快 8.10%。 Memory Usage: 37 MB, less than 13.88% of Java online submissions for Reverse Integer.内存使用:37 MB,不到反向整数的 Java 在线提交的 13.88%。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM