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"

解法一:

 class Solution {
public:
/**
* @param num1 a non-negative integers
* @param num2 a non-negative integers
* @return return sum of num1 and num2
*/
string addStrings(string& num1, string& num2) {
// Write your code here
int add_bit = , i = ;
char temp, result[] = { };
const char *n1 = num1.c_str();
const char *n2 = num2.c_str();
int len1 = strlen(n1);
int len2 = strlen(n2);
while (len1 != && len2 != ) {
len1--; len2--;
result[i] = (add_bit + n2[len2] + n1[len1] - * '') % + '';
add_bit = (n2[len2] + n1[len1] + add_bit - '' * ) / ;
i++;
}
if (len1 > len2) {
while (len1) {
len1--;
result[i] = (add_bit + n1[len1] - '') % + '';
add_bit = (n1[len1] + add_bit - '') / ;
i++;
}
}
else {
while (len2) {
len2--;
result[i] = (add_bit + n2[len2] + add_bit- '') % + '';
add_bit = (add_bit + n2[len2] - '') / ;
i++;
}
}
if (add_bit) {
result[i] = '';
i++;
}
for (int j = ; j < i / ; j++)
{
temp = result[j];
result[j] = result[i - - j];
result[i - - j] = temp;
}
result[i] = '\0';
return result;
}
};

写法太复杂

解法二:

 class Solution {
public:
/**
* @param num1 a non-negative integers
* @param num2 a non-negative integers
* @return return sum of num1 and num2
*/
string addStrings(string& num1, string& num2) {
int m = num1.size();
int n = num2.size();
string result;
int i = m - , j = n - ;
int carry = ;
while (i >= || j >= ) {
int sum = carry;
sum += (i >= ) ? num1[i--] - '' : ;
sum += (j >= ) ? num2[j--] - '' : ;
carry = sum / ;
sum %= ;
result += '' + sum;
} if (carry) {
result += '';
} reverse(result.begin(), result.end());
return result;
}
};

参考@jiadaizhao 的代码

解法三:

 public class Solution {
/*
* @param num1: a non-negative integers
* @param num2: a non-negative integers
* @return: return sum of num1 and num2
*/
public String addStrings(String num1, String num2) {
//start from adding the last digits of num1, num2:
//if the current sum > 10, save 1 in `carry`,
//add to the front of StringBuilder sb
//... doing this till both indice less than 0 int i = num1.length()-1, j = num2.length()-1, carry = 0, curSum = 0;
StringBuilder sb = new StringBuilder();
while (i >= 0 || j >= 0 || carry == 1) {
//Integer.valueOf(String.valueOf(char)) is to remind me that the value of char is mapped to the decimal value in ascii
int curNum1 = i >= 0 ? Integer.valueOf(String.valueOf(num1.charAt(i))) : 0;
int curNum2 = j >= 0 ? Integer.valueOf(String.valueOf(num2.charAt(j))) : 0;
int sum = carry + curNum1 + curNum2;
curSum = sum % 10; carry = sum/10;
sb.insert(0, curSum);
i--; j--;
}
return sb.toString();
}
}

参考@linspiration 的代码

https://segmentfault.com/a/1190000012466338

解法四:

 public class Solution {
/**
* @param num1 a non-negative integers
* @param num2 a non-negative integers
* @return return sum of num1 and num2
*/
public String addStrings(String num1, String num2) {
if (num1 == null || num1.length() == 0) {
return num2;
}
if (num2 == null || num2.length() == 0) {
return num1;
}
int index1 = num1.length() - 1;
int index2 = num2.length() - 1;
int carry = 0;
StringBuilder sb = new StringBuilder();
while (index1 >= 0 || index2 >= 0) {
if (index1 >= 0) {
carry = carry + (num1.charAt(index1) - '0');
}
if (index2 >= 0) {
carry = carry + (num2.charAt(index2) - '0');
}
sb.insert(0, carry % 10);
carry = carry / 10;
--index1;
--index2;
}
if (carry > 0) {
sb.insert(0, carry);
}
return sb.toString();
}
}

参考@Microthinking 的代码

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

655. Big Integer Addition【easy】的更多相关文章

  1. 661. Image Smoother【easy】

    661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...

  2. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

  3. 605. Can Place Flowers【easy】

    605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...

  4. 485. Max Consecutive Ones【easy】

    485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...

  5. 448. Find All Numbers Disappeared in an Array【easy】

    448. Find All Numbers Disappeared in an Array[easy] Given an array of integers where 1 ≤ a[i] ≤ n (n ...

  6. 561. Array Partition I【easy】

    561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...

  7. 219. Contains Duplicate II【easy】

    219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...

  8. 217. Contains Duplicate【easy】

    217. Contains Duplicate[easy] Given an array of integers, find if the array contains any duplicates. ...

  9. 2. Trailing Zeros【easy】

    2. Trailing Zeros[easy] Write an algorithm which computes the number of trailing zeros in n factoria ...

随机推荐

  1. 【转载】【Todo】银弹与我们的职业

    看到一段文字,不得不单独拎出来. 然后再借用一下g9老大的<银弹和我们的职业>中的话: 银弹和我们的职业发展有什么相干?很简单:我们得把时间用于学习解决本质困难.新技术给高手带来方便.菜鸟 ...

  2. KISS

    哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈 ...

  3. 【Other】ASCII Text Art

    http://fsymbols.com/cn/wenzi-yishu/ http://fsymbols.com/text-art/#all_cats http://chris.com/ascii/in ...

  4. 【转】dependency injection 的例子

    Dependency Injection in PHP. Create your own DI container. / blog / PHP By my opinion one of the big ...

  5. ASP.NET MVC扩展之HtmlHelper辅助方法

    什么是HtmlHelper辅助方法? 其实就是HtmlHelper类的扩展方法,如下所示: namespace System.Web.Mvc.Html { public static class Fo ...

  6. VS2013编译boost1.55库

    1. 官网下载最新的Boost库,我的是1.55 2. 在使用vs2013编译boost-1.55.0之前,先要给boost做下修改: boost_1_55_0\boost\intrusive\det ...

  7. onethink 密码加密方式详解

    /** * 系统非常规MD5加密方法 * @param string $str 要加密的字符串 * @return string */ function think_ucenter_md5($str, ...

  8. Android Training - Volley(Lesson 0 - 序言)

    写在http://hukai.me/blog/android-training-volley-index/

  9. Machine Learning:PageRank算法

    1. PageRank算法概述 PageRank,即网页排名,又称网页级别.Google左側排名或佩奇排名.         在谷歌主导互联网搜索之前, 多数搜索引擎採用的排序方法, 是以被搜索词语在 ...

  10. Sphinx-安装和配置

    本例是在Linux下, 环境 CentOS6.5 + PHP5.6.8 + MySQL5.6.13 + Sphinx2.3.1-beta 到官网下载对应环境的安装包, 按照官方文档指定步骤进行安装 第 ...