给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。
注意:
    num1 和num2 的长度都小于 5100.
    num1 和num2 都只包含数字 0-9.
    num1 和num2 都不包含任何前导零。
    你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式。
详见:https://leetcode.com/problems/add-strings/description/
C++:

class Solution {
public:
string addStrings(string num1, string num2)
{
string res = "";
int m = num1.size(), n = num2.size(), i = m - 1, j = n - 1, carry = 0;
while (i >= 0 || j >= 0)
{
int a = i >= 0 ? num1[i--] - '0' : 0;
int b = j >= 0 ? num2[j--] - '0' : 0;
int sum = a + b + carry;
res.insert(res.begin(), sum % 10 + '0');
carry = sum / 10;
}
return carry ? "1" + res : res;
}
};

参考:https://www.cnblogs.com/grandyang/p/5944311.html

415 Add Strings 字符串相加的更多相关文章

  1. [LeetCode] 415. Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  2. [leetcode]415. Add Strings字符串相加

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...

  3. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  4. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  5. 36. leetcode 415. Add Strings

    415. Add Strings Given two non-negative integers num1 and num2 represented as string, return the sum ...

  6. 【leetcode】415. Add Strings

    problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...

  7. 【LeetCode】415. Add Strings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...

  8. Leetcode415Add Strings字符串相加

    给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 注意: num1 和num2 的长度都小于 5100. num1 和num2 都只包含数字 0-9. num1 和num2 都不包 ...

  9. LeetCode - 415. Add Strings

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

随机推荐

  1. Native进程之Trace原理(转)——可直接输出某进程的栈帧——debuggerd

    一. 概述 当发生ANR(Application Not Response,对于Java进程可通过kill -3向目标进程发送信号SIGNAL_QUIT, 输出相应的traces信息保存到目录/dat ...

  2. router-link的a样式变成div样式元素属性

    <router-link tag="div" :to="itemChild.path"><span>{{itemChild.name}} ...

  3. PHP 按位与或 (^ 、&)

    今天朋友群里朋友问了下 按位与或的问题.. 按位于主要是对二进制数操作. <?php $a = 1; $b = 2; $c = $a^b; ?> 这里不是单纯的相加关系 十进制 1换算成二 ...

  4. react-grid-layout

    一个好用的拖拽.自适应布局 react 插件 基本使用: // 显示全部 chart 内容区域 import React,{PureComponent} from 'react'; import {R ...

  5. 【iOS系列】-UITableView的使用

    UITableView的使用: 第一:数据展示条件 1,UITableView的所有数据都是由数据源(dataSource)提供,所以想在UITableView展示数据,必须设置UITableview ...

  6. 64位windows上访问64位oracle 12c

    64位windows上访问64位oracle 12c,这会有啥问题? 没啥问题.问题是,我64位操作系统的机器上装了个oracle 10g.而oracle 10g好像是不区分啥32位.64位的,一律3 ...

  7. ubuntu字符界面下显示中文和调整分辨率

    1.sudo apt-get install zhcon 2.vi /etc/zhcon.conf  修改下面两行 x_resolution 1024 y_resolution 768 完成这两步后在 ...

  8. ios UIWebView 播放优酷土豆视频

    将以下的代码嵌套在html里.然后webView载入这个网页.或这段html码,即可了,无须要使用像网上说的html5去兼容 watermark/2/text/aHR0cDovL2Jsb2cuY3Nk ...

  9. vue学习1

    1.<div id="app">{{message}}<input v-model="message"></div>new ...

  10. commons-fileupload、smartUpload和commons-net-ftp

    1.本地上传 在许多Web站点应用中都需要为用户提供通过浏览器上传文档资料的功能,例如,上传个人相片.共享资料等.在DRP中,就有这个一个功能,需要将对应的物料图片上传并显示.对于上传功能,其实在浏览 ...