LintCode :Big Integer Addition
来源:互联网 发布:mac vpn推荐 编辑:程序博客网 时间:2024/06/04 08:41
描述:
Given two non-negative integers num1
and num2
represented as string, return the sum of num1
and num2
.
注意事项
- 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 hereint add_bit = 0, i = 0;char temp, result[6100] = { 0 };const char *n1 = num1.c_str();const char *n2 = num2.c_str();int len1 = strlen(n1);int len2 = strlen(n2);while (len1 != 0 && len2 != 0) {len1--; len2--;result[i] = (add_bit + n2[len2] + n1[len1] - 2 * '0') % 10 + '0';add_bit = (n2[len2] + n1[len1] + add_bit - '0' * 2) / 10;i++;}if (len1 > len2) {while (len1) {len1--;result[i] = (add_bit + n1[len1] - '0') % 10 + '0';add_bit = (n1[len1] + add_bit - '0') / 10;i++;}}else {while (len2) {len2--;result[i] = (add_bit + n2[len2] + add_bit- '0') % 10 + '0';add_bit = (add_bit + n2[len2] - '0') / 10;i++;}}if (add_bit) {result[i] = '1';i++;}for (int j = 0; j < i / 2; j++){temp = result[j];result[j] = result[i - 1 - j];result[i - 1 - j] = temp;}result[i] = '\0';return result;}};
阅读全文
0 0
- LintCode :Big Integer Addition
- lintcode(655)Big Integer Addition
- Google/LintCode:E-Big Integer Addition
- LintCode python 小白4-Big Integer Addition
- Big Integer Addition
- [Amazon] Big Integer Addition
- Big Integer Addition
- Big Integer Addition
- lintcode(656)Big Integer multiplication
- Big Integer Addition存储在字符串内的整数相加
- Sicily 1020 Big Integer
- sicily 1020 Big Integer
- 1020. Big Integer
- Sicily.1020. Big Integer
- Sicily 1020. Big Integer
- sicily 1020. Big Integer
- Sicily 1020. Big Integer
- Sicily 1020 Big Integer
- springboot的错误页面设置
- <泛型>的认识
- 自定义String类
- 初学 linux NFS
- poj3974 Palindrome(manacher)
- LintCode :Big Integer Addition
- 数据结构:链表栈
- POJ3133 Manhattan Wiring(难题 较为复杂)
- 鼠标滚轮事件之支付宝案例
- ICG博弈游戏的必胜策略
- 用Apache Spark进行大数据处理之Spark SQL(2)
- 例说数据结构&STL(四)——queue
- 逻辑回归LogisticRegression
- Python-17 lambda表达式