Java Accepted Solution (straightforward, but looks well organized)


  • 0
    D

    addOnce(char1,char2,carry) -> addOnce(char1, '0', carry) for the longer string. Use a class calResult to hold the value and the next carry returned from the addOnce function.

    public class Solution {
            public class calResult
            {
                public char toInsert;
                public int carry;
                public calResult(char toInsert,int carry)
                {
                    this.toInsert=toInsert;
                    this.carry=carry;
                }
            }
            public calResult addOnce(char c1,char c2,int carry)
            {
                int c1Value=c1-'0';
                int c2Value=c2-'0';
                int curResult=c1Value+c2Value+carry;
                char toInsert='\0';
                switch(curResult)
                {
                      case 0: 
                          toInsert='0';
                          carry=0;
                          break;
                      case 1:
                          toInsert='1';
                          carry=0;
                          break;
                      case 2:
                          toInsert='0';
                          carry=1;
                          break;
                      case 3:
                          toInsert='1';
                          carry=1;
                          break;
                      default:
                          break;
                  }
                  calResult cal=new calResult(toInsert,carry);
                  return cal;
            }
            public String plusCarry(String s,int carry)
            {
                if(carry==0)
                    return s;
                else
                {
                    int i=s.length()-1;
                    StringBuilder builder=new StringBuilder();
                    while(i>=0)
                    {
                        calResult cal=addOnce(s.charAt(i),'0',carry);
                        carry=cal.carry;
                        builder.insert(0,cal.toInsert);
                        i--;
                    }
                    if(carry==1)
                        builder.insert(0,'1');
                    return builder.toString();
                }
            }
            public String addBinary(String a, String b) {
              if(a.length()==0)
                return b;
              if(b.length()==0)
                return a;
              int i=a.length()-1;
              int j=b.length()-1;
              StringBuilder builder=new StringBuilder();
              int carry=0;
              while(i>=0&&j>=0)
              {
                  calResult cal=addOnce(a.charAt(i),b.charAt(j),carry);
                  carry=cal.carry;
                  builder.insert(0,cal.toInsert);
                  i--;
                  j--;
              }
              if(i>=0)
                 builder.insert(0,plusCarry(a.substring(0,i+1),carry));
              else if (j>=0)
                 builder.insert(0,plusCarry(b.substring(0,j+1),carry));
              else if(carry==1)
                 builder.insert(0,'1');
              return builder.toString();
            }
        }

Log in to reply
 

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