My Java Solution without using long


  • -1
    F
    public class Solution {
        public int reverse(int x) {
            int res = 0;
    
            while (x != 0) {
                if (res*10/10 != res) return 0;  // if res*10 overflows, return 0
                res = 10*res + x%10;
                x /= 10;
            }
           return res;
       }
    }

  • 0
    R

    definitely wrong code, as it can overflow once you add x%10, and the check x*10/10 definitely does not work


  • 0
    F

    think before saying something this offensive. if adding x%10 overflows, it would've been handled by checking if x*10 overflows


  • 0
    G

    I don`t think you need to check the negative number part. Can you explain why that is neccesary?


  • 0
    F

    You're right! I probably mixed up with another problem in my thought process.


Log in to reply
 

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