Java Solution without Try/Catch Block


  • 0
    L
    import java.util.*;
        
    public class Solution {
            public int reverse(int x) {
                List<Integer> buffer = new ArrayList<Integer>();
                int n = 1;
                int y = x;
                int result = 0;
                if(y == Integer.MIN_VALUE || y == Integer.MAX_VALUE) return 0;
                if(x == 0) return 0;
                if(y < 0) y = -y;
                int e;
                while(y != 0)
                {
                    e = y%10;
                    buffer.add(e);
                    y = y/10;
                    if(y > 0)
                        n++;
                }
                if(n < 10)
                    for(int i=0; i < n; i++)
                        result = result + buffer.get(i)*(int)(Math.pow((double)10,(double)(n-1-i)));
                else if(n == 10)
                {
                    if(buffer.get(0) > 2) return 0;
                    
                    for(int i=1; i < n; i++)
                        result = result + buffer.get(i)*(int)(Math.pow((double)10,(double)(n-i-1)));
                    if(result <= Integer.MAX_VALUE%Math.pow(10, 9))
                        result = result + buffer.get(0)*(int)Math.pow((double)10, (double)9);
                    else
                        return 0;
                }
                if(x < 0) result = -result;
                return result;
            }
        }

  • 0
    S

    lol how many variables out there ? :D


Log in to reply
 

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