Very Easy Java Solution. StringBuffer reverse. 32ms runtime


  • 0
    P
    
    	            ArrayList<Character> int1 = new ArrayList<Character> ();
    	            ArrayList<Character> int2 = new ArrayList<Character> ();
    	            ArrayList<Integer> int1Final = new ArrayList<Integer> ();
    	            ArrayList<Integer> int2Final = new ArrayList<Integer> ();
    	            StringBuilder sumStr = new StringBuilder();
    	            for(int i=0;i<num1.length();i++){
    	                int1.add(num1.charAt(i));
    	            }
    	            for(int i=0;i<num2.length();i++){
    	                int2.add(num2.charAt(i));
    	            }
    	            int1.get(0);
    	            for(int i=int1.size();i>0;i--){
    	            	int a = (int) int1.get(i-1);
    	                int1Final.add(a-48);
    	            }
    	            for(int i=int2.size();i>0;i--){
    	                int a = (int) int2.get(i-1);
    	                int2Final.add(a-48);
    	            }
    	            boolean oneBigger = false;
    	            int carry = 0;
    	            if(int1Final.size() > int2Final.size()){
    	                oneBigger = true;
    	            }
    	            if(oneBigger){
    	            for(int j=0;j<=int1Final.size()-1;j++){
    	                if(j>int2Final.size()-1){
    	                 int2Final.add(j,0);   
    	                }
    	              int sum = int1Final.get(j)+int2Final.get(j)+carry;
    	              sumStr.append(sum%10);
    	              if(sum>9){
    	                carry = 1;
    	              }else{
    	                  carry = 0;
    	              }
    	            }
    	            if(carry>=1){
    	            	sumStr.append(carry);	
    	            }
    	            }else{
    	            for(int j=0;j<=int2Final.size()-1;j++){
    	                if(j>int1Final.size()-1){
    	                 int1Final.add(j,0);   
    	                }
    	              int sum = int2Final.get(j)+int1Final.get(j)+carry;
    	              sumStr.append(sum%10);
    	              if(sum>9){
    	                carry = 1;
    	              }else{
    	                  carry = 0;
    	              }
    	            }
    	            if(carry>=1){
    	            	sumStr.append(carry);	
    	            }
    	            }
    	            return sumStr.reverse().toString();
    	        
    

Log in to reply
 

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