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 ...
随机推荐
- 【Sets】使用Google Guava工程中Sets工具包,实现集合的并集/交集/补集/差集
获取两个txt文档的内容~存储进集合中求集合的并集/交集/补集/差集 package com.sxd.readLines.aboutDB; import java.io.BufferedReader; ...
- 绕过WAF继续SQL注入
Web Hacker总是生存在与WAF的不断抗争之中的,厂商不断过滤,Hacker不断绕过.WAF bypass是一个永恒的话题,不少基友也总结了很多奇技怪招.那今天我在这里做个小小的扫盲吧.先来说说 ...
- Android LayoutInflater布局填充器
Android LayoutInflater布局填充器 把一份xml布局文件转为View对象,这就是layoutinflater的作用. 对于一个没有被载入或者想要动态载入的界面,都需要使用Layou ...
- 自制DEV皮肤
1.打开DEV自带的皮肤制作工具 2.创建一个新的皮肤工程[File][New] 3.改变指定控件的颜色,比如背景色 4.改变控件边框的颜色.粗细
- 十招让Ubuntu 16.04用起来更得心应手
Ubuntu 16.04是一种长期支持版本(LTS),是Canonical承诺发布五年的更新版.也就是说,你可以让这个版本在电脑上运行五年!这样一来,一开始就设置好显得特别重要.你应该确保你的软件是最 ...
- 如何在 Ubuntu 上搭建网桥
导读作为一个 Ubuntu 16.04 LTS 的初学者.如何在 Ubuntu 14.04 和 16.04 的主机上搭建网桥呢?顾名思义,网桥的作用是通过物理接口连接内部和外部网络.对于虚拟端口或者 ...
- JAVA NIO 之ByteBuffer的mark、position、limit、flip、reset,get方法介绍
参考博客:http://blog.csdn.net/sunzhenhua0608/article/details/31778519 先来一个demo: import java.nio.ByteBuff ...
- ispy 编译笔记
xcopy "$(ProjectDir)dlls\$(PlatformName)\*.*" "$(ProjectDir)$(OutDir)" /Y if NOT ...
- WIN7如何查找网络打印机
1 在开始菜单中输入"打印机"并点击"添加打印机" 2 点击下面一个,并搜索家庭组的打印机,一般可以搜到(注意这台电脑不能关机或睡眠). 3 查找并添加会需要安 ...
- webDriver API——第6部分Locate elements By
These are the attributes which can be used to locate elements. See the Locating Elements chapter for ...