abc_begin

导航

655. Big Integer Addition【easy】

Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.

Notice
  • The length of both num1 and num2 is < 5100.
  • Both num1 and num2 contains only digits 0-9.
  • Both num1 and num2 does not contain any leading zero.
  • You must not use any built-in BigInteger library or convert the inputs to integer directly.
 
Example

Given num1 = "123", num2 = "45"
return "168"

 

解法一:

 1 class Solution {  
 2 public:  
 3     /** 
 4     * @param num1 a non-negative integers 
 5     * @param num2 a non-negative integers 
 6     * @return return sum of num1 and num2 
 7     */  
 8     string addStrings(string& num1, string& num2) {  
 9         // Write your code here  
10         int add_bit = 0, i = 0;  
11         char temp, result[6100] = { 0 };  
12         const char *n1 = num1.c_str();  
13         const char *n2 = num2.c_str();  
14         int len1 = strlen(n1);  
15         int len2 = strlen(n2);  
16         while (len1 != 0 && len2 != 0) {  
17             len1--; len2--;  
18             result[i] = (add_bit + n2[len2] + n1[len1] - 2 * '0') % 10 + '0';  
19             add_bit = (n2[len2] + n1[len1] + add_bit - '0' * 2) / 10;  
20             i++;  
21         }  
22         if (len1 > len2) {  
23             while (len1) {  
24                 len1--;  
25                 result[i] = (add_bit + n1[len1] - '0') % 10 + '0';  
26                 add_bit = (n1[len1] + add_bit - '0') / 10;  
27                 i++;  
28             }  
29         }  
30         else {  
31             while (len2) {  
32                 len2--;  
33                 result[i] = (add_bit + n2[len2] + add_bit- '0') % 10 + '0';  
34                 add_bit = (add_bit + n2[len2] - '0') / 10;  
35                 i++;  
36             }  
37         }  
38         if (add_bit) {  
39             result[i] = '1';  
40             i++;  
41         }  
42         for (int j = 0; j < i / 2; j++)  
43         {  
44             temp = result[j];  
45             result[j] = result[i - 1 - j];  
46             result[i - 1 - j] = temp;  
47         }  
48         result[i] = '\0';  
49         return result;  
50     }  
51 }; 

写法太复杂

 

解法二:

 1 class Solution {
 2 public:
 3     /**
 4      * @param num1 a non-negative integers
 5      * @param num2 a non-negative integers
 6      * @return return sum of num1 and num2
 7      */
 8     string addStrings(string& num1, string& num2) {
 9         int m = num1.size();
10         int n = num2.size();
11         string result;
12         int i = m - 1, j = n - 1;
13         int carry = 0;
14         while (i >= 0 || j >= 0) {
15             int sum = carry;
16             sum += (i >= 0) ? num1[i--] - '0' : 0;
17             sum += (j >= 0) ? num2[j--] - '0' : 0;
18             carry = sum / 10;
19             sum %= 10;
20             result += '0' + sum;
21         }
22         
23         if (carry) {
24             result += '1';
25         }
26         
27         reverse(result.begin(), result.end());
28         return result;
29     }
30 };

参考@jiadaizhao 的代码

 

解法三:

 1 public class Solution {
 2     /*
 3      * @param num1: a non-negative integers
 4      * @param num2: a non-negative integers
 5      * @return: return sum of num1 and num2
 6      */
 7     public String addStrings(String num1, String num2) {
 8         //start from adding the last digits of num1, num2:
 9         //if the current sum > 10, save 1 in `carry`,
10         //add to the front of StringBuilder sb
11         //... doing this till both indice less than 0
12         
13         int i = num1.length()-1, j = num2.length()-1, carry = 0, curSum = 0;
14         StringBuilder sb = new StringBuilder();
15         while (i >= 0 || j >= 0 || carry == 1) {
16             //Integer.valueOf(String.valueOf(char)) is to remind me that the value of char is mapped to the decimal value in ascii 
17             int curNum1 = i >= 0 ? Integer.valueOf(String.valueOf(num1.charAt(i))) : 0;
18             int curNum2 = j >= 0 ? Integer.valueOf(String.valueOf(num2.charAt(j))) : 0;
19             int sum = carry + curNum1 + curNum2;
20             curSum = sum % 10; carry = sum/10;
21             sb.insert(0, curSum);
22             i--; j--;
23         }
24         return sb.toString();
25     }
26 }

参考@linspiration 的代码

https://segmentfault.com/a/1190000012466338

 

解法四:

 1 public class Solution {
 2     /**
 3      * @param num1 a non-negative integers
 4      * @param num2 a non-negative integers
 5      * @return return sum of num1 and num2
 6      */
 7     public String addStrings(String num1, String num2) {
 8         if (num1 == null || num1.length() == 0) {
 9             return num2;
10         } 
11         if (num2 == null || num2.length() == 0) {
12             return num1;
13         }
14         int index1 = num1.length() - 1;
15         int index2 = num2.length() - 1;
16         int carry = 0;
17         StringBuilder sb = new StringBuilder();
18         while (index1 >= 0 || index2 >= 0) {
19             if (index1 >= 0) {
20                 carry = carry + (num1.charAt(index1) - '0');
21             }
22             if (index2 >= 0) {
23                 carry = carry + (num2.charAt(index2) - '0');
24             }
25             sb.insert(0, carry % 10);
26             carry = carry / 10;
27             --index1;
28             --index2;
29         }
30         if (carry > 0) {
31             sb.insert(0, carry);
32         }
33         return sb.toString();
34     }
35 }

参考@Microthinking 的代码

https://blog.happynavy.tk/lintcodes/big-integer-addition/

 

 

posted on 2017-12-30 21:44  LastBattle  阅读(119)  评论(0编辑  收藏  举报