Need Help with Code - JAVA


  • 0
    V
      public class Solution {
       public int reverse(int x) {
        
        // Storing the number in StringBuffer
        StringBuffer output = new StringBuffer();
        
        // Check for Negative Number so that I can add negative sign later
        boolean flag = false;
        
        // If less than 0, it is negative. Ha!
        if (x < 0) flag = true;
        
        // From int to sb
        output.append(Math.abs(x));
        
        // Left Index
        int i = 0;
        
        // Right Index
        int j = output.length()-1;
        
        // Half way
        while (i <= j) {
            // Replace them every time until i crosses j
            output = replace(output,i,j);
            i++;
            j--;
        }
        
        // Adding the negative sign and converting back to Integer
        if (flag)
            return -1 * (int)Long.parseLong(output.toString());
        else return (int)Long.parseLong(output.toString());
    }
    
    /**
     * Just some replace method
     **/
    public StringBuffer replace(StringBuffer input, int i, int j) {
        
        char temp = input.charAt(i);
        input.setCharAt(i, input.charAt(j));
        input.setCharAt(j, temp);
        
        return input;
    }
    }
    

    Output timesout but I am making half the number of switches compared to other methods. Please help/


  • 1
    M

    When I tried running your code, I got "java.lang.NumberFormatException: For input string: "8463847412-"".
    I guess it's basically because you are not multiplying the negative numbers with -1. If the flag is set, multiply the number with -1.

    if(x<0){
    flag=true;
    //This will remove the negative sign from the front. Or start with i=1 if flag is set to true. 
    x=x*-1;
    
    }
    

    Also, you can use Integer.parseInt instead of doing Long.parseLong and casting it back to an int. I am not sure if you are doing it for any reason.


  • 0
    V

    The flag is the indication whether if a number is negative or not. Only if flag is true shall I multiply the number by -1. Does that sound correct? I thought probably reversing the number will throw me out of the Integer range hence long. That is why long.


  • 0
    M

    That sounds about right. If the number is less than 0, set the flag = true and then multiply the number with -1 and carry out your swaps. Then before you return multiply the result with -1(you have already done this).


  • 0
    V

    @Mino-De I have already gotten the absolute of the number irrespective of it being positive or negative in the statement:

    output.append(Math.abs(x));

    Does it make sense to you? Is it incorrect?

    Thanks for your help!


  • 1
    M

    Sorry. I guess I didn't check it properly. However, your code doesn't work for the value -2147483648.
    Because the maximum integer value is 2147483647. So, math.abs doesn't work in this case. I would suggest that you append the integer and then start with i=1 if it is a negative number and i=0 if it's a positive integer.
    I am not sure why it runs overtime. However, I modified your solution to handle spl cases and it got accepted for me.
    Here is the solution

    public class Solution {
       public int reverse(int x) {
        
        // Storing the number in StringBuffer
        StringBuffer output = new StringBuffer();
        
        // Check for Negative Number so that I can add negative sign later
        boolean flag = false;
        
        // If less than 0, it is negative. Ha!
        if (x < 0) flag = true;
        
        // From int to sb
        output.append(x);
        //System.out.println(output.toString() + " " + Math.abs(x));
        // Left Index
       // Notice that I am starting from index 1 if it a negative number and leaving the - sign as it is in the string
        int i = flag?1:0;
        
        // Right Index
        int j = output.length()-1;
        
        // Half way
        while (i <= j) {
            // Replace them every time until i crosses j
            output = replace(output,i,j);
            i++;
            j--;
        }
        long temp=Long.parseLong(output.toString());
        if(temp> Integer.MAX_VALUE || temp<Integer.MIN_VALUE){
            return 0;
        }
        // Adding the negative sign and converting back to Integer
        //Returning the value without multiplying with -1 because i didn't remove the -ve sign from the string. 
        return Integer.parseInt(output.toString());
    }
    
    /**
     * Just some replace method
     **/
    public StringBuffer replace(StringBuffer input, int i, int j) {
        
        char temp = input.charAt(i);
        input.setCharAt(i, input.charAt(j));
        input.setCharAt(j, temp);
        
        return input;
    }
    }
    

    Hope this helps


  • 0
    V

    Much clearer now! Thank you!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.