Java Iterative Code - Doesn't uses bitwise


  • 0
    R

    It runs slower than other solutions.

    class Solution {
        public int getSum(int a, int b) {
            long aa = a, bb = b;
            if(aa>=0 && bb>=0){
                while(aa-->0) bb++;
                 return (int)bb;
            }
            
           if(aa<=0 && bb<=0){
                aa=Math.abs(aa);
                bb=Math.abs(bb);
                while(aa-->0) bb++;
                return -1*(int)bb;
            }
            
            if(aa>=0 && bb<=0){
                bb=Math.abs(bb);
                while(bb-->0) aa--;
                return (int)aa;
            }
            
            if(aa<=0 && bb>=0){
                aa=Math.abs(aa);
                while(aa-->0) bb--;
            }
            return (int)bb;
        }
    }
    

Log in to reply
 

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