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.
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】的更多相关文章
- 661. Image Smoother【easy】
661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 605. Can Place Flowers【easy】
605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
- 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 ...
- 561. Array Partition I【easy】
561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...
- 219. Contains Duplicate II【easy】
219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...
- 217. Contains Duplicate【easy】
217. Contains Duplicate[easy] Given an array of integers, find if the array contains any duplicates. ...
- 2. Trailing Zeros【easy】
2. Trailing Zeros[easy] Write an algorithm which computes the number of trailing zeros in n factoria ...
随机推荐
- Java Excel 插入图片
在POI中有HSSFPatriarch对象,该对象为画图的顶级管理器,它的createPicture(anchor, pictureIndex)方法就能够在Excel插入一张图片.所以要在Excel中 ...
- socket连接和http连接的区别
socket连接和http连接的区别 HTTP协议:简单对象访问协议,对应于应用层 ,HTTP协议是基于TCP连接的 tcp协议: 对应于传输层 ip协议: 对应于网络层 TCP/IP ...
- MySQL建表时,日期时间类型选择
MySQL(5.5)所支持的日期时间类型有:DATETIME. TIMESTAMP.DATE.TIME.YEAR. 几种类型比较如下: 日期时间类型 占用空间 日期格式 最小值 最大值 零值表示 D ...
- 深入理解Java中为什么内部类可以访问外部类的成员
内部类简介 虽然Java是一门相对比较简单的编程语言,但是对于初学者, 还是有很多东西感觉云里雾里, 理解的不是很清晰.内部类就是一个经常让初学者感到迷惑的特性. 即使现在我自认为Java学的不错了, ...
- AI 语音对话技术
机器学习以及自然语言处理技术的进步,开启了人与人工智能进行语音交互的可能,人们透过对话的方式获取信息.与机器进行交互,将不再只是存在科幻情结当中.语音交互是未来的方向,而智能音箱则是语音交互落地的第一 ...
- RenderMonkey 练习 第三天 【OpenGL renderToTexture】
渲染到纹理: 1. 新建一个OpenGL 空effect; 2. 添加渲染目标纹理, Add Texture-> Add Render Texture 3. 添加一个渲染pass 4. 将pas ...
- IP地址转换、主机大小端、htonl、ntohl实现
copy #include <IOSTREAM> //#include <WINSOCK.H> using std; typedef uint16; unsigned ...
- NDK下IPC问题
由于AllJoyn的join session timeout问题一直无法解决,我们怀疑AllJoyn有些内部变量没有清理干净,因此考虑将AllJoyn相关功能放到一个单独的进程中,一旦join ses ...
- 2017.12.25 Mybatis物理分页插件PageHelper的使用(二)
参考来自: 官方文档的说明:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md 上篇博客 ...
- 将本地jar包添加到maven中
将需要引入的jar包拷贝到maven项目的WEB-INF/lib中 在pom.xml中配置如下: <dependency> <groupId>com.xxxxx.union&l ...