My Java Solution without Bit Manipulation beats 1.61%


  • 0
    S
     public int getSum(int a, int b) {
            if(a ==0 && b == 0) return 0;
            
            if(a > 0 && b == 0) {
              return a;
            }
            if(a == 0 && b > 0) {
              return b;
            }
            int sum = 0;
            while(a !=0) {
                  
                  if(a < 0){
                    a++;
                    sum--;
                  }else {
                    a--;
                    sum++;
                  }
            }
            while(b !=0) {
                  
                  if(b < 0){
                    b++;
                    sum--;
                  }else {
                   b--;
                   sum++;
                  }
            }
            
            return sum;     
        }
    

Log in to reply
 

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