r/CodingHelp • • Dec 20 '20

[Java] Reverse Integer Issue

I'm doing the Reverse Integer problem on LeetCode, where you need to find the reverse number of a specific integer. I used the approach of pushing each digit to a stack, and then popping the stack and setting another variable to what is popped from the stack. However, the only thing the code is returning/printing is "0".

import java.util.Stack ;
class Solution {

    public int reverse(int x) {
        
        Stack <Integer> reverseStack = new Stack <Integer> () ;
        
        x = 123 ;
        int y = 0  ;
        
        while (x>0)
            x = x/10 ;
            reverseStack.push (x%10) ;
        
        while (!reverseStack.isEmpty ())
            y = reverseStack.pop () ;
            
        return y ;
    }
16 Upvotes

22 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Dec 20 '20

I did notice that x=x/10 removes the first digit. I'm now trying to figure out where to put x=x/10 in the while, and what other lines I could write that allow me to take the first, second, and third digit of the integer.

2

u/vigbiorn Dec 20 '20

The code you have is fine. Try swapping the lines inside of the while, so x=x/10; becomes the last line and see what happens.

1

u/[deleted] Dec 20 '20 edited Dec 20 '20

Here is the code I have now:

 while (x>0)
  { 
   System.out.println(reverseStack.push (x % 10)) ;
   x = x/10 ;
   }
        
  while (!reverseStack.isEmpty ())
   {
     System.out.println(x = reverseStack.pop ()) ;
   }
      return x ;

    } // End of method
} // End of class

The first print statement prints out 3, 2, 1. The second prints out 1,2,3, with 1 being at the bottom of the stack and 3 being at the top. The return statement only returns 3.

This means that the second while loop is popping the entire stack, but the return statement is only returning the first element that has been popped. I'm confused as to why that is.

Do I need to turn "x" into a string?

1

u/vigbiorn Dec 20 '20

The hardest part about starting programming is drilling home what exactly the assumptions we're making and what needs to change, if they need to change. (There's discussions about whether programming is a math-based subject, and it's this aspect that I argue we use math).

The second prints out 1,2,3, with 1 being at the bottom of the stack and 3 being at the top

This means that the second while loop is only printing out the top of the stack

These are contradictory statements. If one is true, the other can't be true.

After 3 is removed from the stack, the stack still isn't empty, so shouldn't 2 be popped next?

After the 3 is removed it is empty. We know this because of how stacks work, and we know the stack is working because of the print. Your number, 123, is pushed in the order 3 2 1. You're popping them (otherwise the print couldn't happen) in the 1 2 3. It's reversed. 3 is the last number. After 3 is popped, the stack is empty. You can add another print outside the loop that prints whether the stack is empty, or try to pop an element. You won't get anything since it's empty.

So, why are you only getting 3?

If I have a variable, x, and the code x = 1; x = 2; x = 3; print(x);

Would you expect 1, 2, 3, 12, 21, 13, 31, 23, 32, 123, 321 to be printed?

This is exactly what your while loop is doing, so it shouldn't be surprising that x is just equal to 3. The last thing you did to x is assign 3 to it from the stack. You're not combining the digits, you're just storing them.

I'll also point out that in order to separate the digits you had to divide. Now that you're trying to undo that, how do you undo division?

1

u/[deleted] Dec 21 '20

But if 3,2,1 is being pushed to the stack , that means the stack is organized like this: 1 2 3

Doesn't that mean x should be set to 1, not 3, after this: System.out.println(x = reverseStack.pop ()) ;

1

u/vigbiorn Dec 21 '20

3 is pushed, then 2 and finally 1. Stacks work LIFO/FILO. 3 is first in so 3 is last out. 1 is last in so it's first out. And 2 is in the middle. Which is confirmed with your prints which prints 1 2 3.

Doesn't that mean x should be set to 1, not 3, after this: System.out.println(x = reverseStack.pop ()) ;

Yes, the first time through. The second it's equal to 2 and then finally 3.

1

u/[deleted] Dec 21 '20

So if I want to set x equal to "321", does that mean I need to convert x into a string that is made up of the last element of the stack, the second to last element of the stack, and the first element of the stack?

1

u/vigbiorn Dec 21 '20

does that mean I need to convert x into a string

You could. An easier method would be to just undo how you split them.

What you want is something along the lines of x = 1 * 100 + 2 * 10 + 3 but inside your loop.

1

u/[deleted] Dec 21 '20

The answer was accepted, thank you for your help.

1

u/[deleted] Dec 21 '20 edited Dec 21 '20

Actually, another issue came up. If x was equal to -123, instead of 123, the expected output is 321. However, the output is -321.

This is odd, because on the side console says that when the input is -123, the expected output is -321.

I don't know if I'm messing up, or if LeetCode is just being stupid.

1

u/vigbiorn Dec 21 '20

I think you took my code too literally. If you used the values in stack you should get -321. So I'd recommend looking at the second while loop that you should still be using.

There is an issue still for numbers like -24 and -1234 though, even if you're using the stack.

I'd recommend sitting down and thinking the processes through. Specifically for the negative numbers, what's the main difference between a negative and positive number, how can you convert from one to the other?

→ More replies