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. 【Tomcat MyEcplise】MyEcplise添加一个新的Server,Tomcat8.5报错

    添加新的Tomcat添加不进去,报错: The Apache Tomcat installation at this directory is version 8.5.11.  A Tomcat 8. ...

  2. Struct2_使用Ajax调用Action方法并返回值

    一.Login.jsp 1.<head>引入jquery: <script type="text/javascript" src="http://aja ...

  3. Oracle 11gR2 RAC连接时ORA-12545错误

    刚装成的Oracle 11gR2的RAC集群 三个节点 创建好数据库,一切都在正常的进行中 然后去安装了一个oracle client,进行链接时出现了ORA-12545 然后简单的baidu了一下, ...

  4. PHP版本切换

    前言 php是为了快速构建一个web页面而迅速被大家广为接受的开源语言,通过不断发展已经有了很多的php开源系统,满足了目前大部分用户的站点需求.1995年初php诞生到现在已经存在多个版本,并且每个 ...

  5. 简单总结es6箭头符号

    1.es6箭头符号的几种写法 (1)没有参数 ()=>1*1 (2)一个参数 x=>x*x (3)两个参数以及多个参数 (x,y,z)=>x*y*z 2.箭头符号不会绑定this.a ...

  6. 关于Java设计模式的一些概况

    设计模式(Design pattern)在软件行业一直都扮演着很重要的角色.最近感觉自己对设计模式的知识有些遗忘了,虽然以前也看了很多,但是坦白说,其实并没有怎么理解.基本还是为了应付面试.然后,在工 ...

  7. [转]SQL Server 2008支持将数据导出为脚本

    本文转自:http://blog.csdn.net/studyzy/article/details/4303759 以前我们要将一个表中的数据导出为脚本,那么只有在网上找一个导出数据的Script,然 ...

  8. Unity3D新手教学,让你十二小时,从入门到掌握!(二) [转]

    版权声明:本文为Aries原创文章,转载请标明出处.如有不足之处欢迎提出意见或建议,联系QQ531193915 继续上一讲的内容,首先呢, 为了接下来要做的小游戏,在这里我要小小的修改一下移动的代码. ...

  9. 11g R2单实例手工建库

    官档地址:Administrator's Guide --->>>Creating and Configuring an Oracle Database--->>> ...

  10. mysql结构相同的三张表查询一条记录\将一张表中的数据插入另外一张表

    将一张表中的数据插入另外一张表 1.两张表结构相同 insert into 表1名称 select * from 表2名称 2.两张结构不相同的表 insert into 表1名称(列名1,列名2,列 ...