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. [Ubuntu Setup] Ubuntu 13.04 安装 ia32-libs

    http://stackoverflow.com/questions/23182765/how-to-install-ia32-libs-in-ubuntu-14-04-lts sudo -i cd ...

  2. HTML5中表单验证的8种方法

    HTML5中表单验证的8种方法 2012-4-21 11:00| 发布者: benben| 查看: 2765| 评论: 0 摘要: 前一篇,我们介绍了HTML5中新的表单特性和函数, 今天就继续来谈谈 ...

  3. 二十四种设计模式:解释器模式(Interpreter Pattern)

    解释器模式(Interpreter Pattern) 介绍给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子. 示例有一个Message实体类,某个类对它的 ...

  4. 【FireMonkey】StyleBook使用方法

    近期在开发一个团队文档管理工具,使用Embarcadero的XE2-C++builder进行界面开发,使用Firemonkey框架. 而这个框架十分有趣!可能吸引界面开发者的就是这个StyleBook ...

  5. mac office 设置默认视图显示比例

    1.打开word 2.fn+option+F11,选中Normal,右键插入模块,复制以下脚本到编辑器中 Sub AutoOpen() ActiveWindow.ActivePane.View.Zoo ...

  6. 隐藏系统EFI分区Z盘

    找到C:\Windows\System32\cmd.exe程序, 右键单击cmd 选择以管理员身份运行, 打开命令提示符,输入以下命令(不区分大小写)DiskPart回车List空格volume回车s ...

  7. Java笔记18:JUnit单元测试

    1 从http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22junit%22%20AND%20a%3A%22junit%22 上下载最新的junit包. ...

  8. 2018.1.9 博客迁移至csdn

    http://blog.csdn.net/liyuhui195134?ref=toolbar

  9. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何使用随机数DRAND模块

    DRAND函数可以产生0-1的随机浮点数   DRAND的输入Seed有什么意义?     更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/aceta ...

  10. 对PHP中类、继承、抽象的理解(个人总结)

    1,PHP中的类可以被继承:A extends B. 2,类被声明为abstract时:a.该类就仅仅包含模版,我们就不能创建该类的实例对象,必须通过继承来使用new创建对象. b.继承一个抽象类的时 ...