1、题目描述

2、问题分析

直接按照加法运算规则运算即可,注意进位问题。

3、代码

 string addStrings(string num1, string num2) {
if( num1.empty() ) return num2;
if( num2.empty() ) return num1; string::reverse_iterator it1 = num1.rbegin() ;
string::reverse_iterator it2 = num2.rbegin() ;
string s ;
int up = ;
while( it1 != num1.rend() && it2 != num2.rend() ){
int a = (*it1 - '') + (*it2 - '') + up ;
if( a < ){
s = std::to_string( a ) + s;
up = ;
}else{
s = std::to_string( a - ) + s;
up = ;
}
++it1;
++it2;
} while( it1 != num1.rend() ){
if( *it1 - '' + up < ){
s = std::to_string( *it1 - '' + up ) + s;
up = ;
}else{
s = std::to_string( *it1 - '' + up - ) + s;
up = ;
}
++it1;
} while( it2 != num2.rend() ){
if( *it2 - '' + up < ){
s = std::to_string( *it2 - '' + up ) + s;
up = ;
}else{
s = std::to_string( *it2 - '' + up - ) + s;
up = ;
}
++it2 ;
} if( up == ){
s = std::to_string() + s;
} return s; }

LeetCode题解之Add Strings的更多相关文章

  1. 【leetcode】415. Add Strings

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

  2. leetcode题解2. Add Two Numbers

    题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

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

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

  4. LeetCode算法题-Add Strings(Java实现)

    这是悦乐书的第223次更新,第236篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第90题(顺位题号是415).给定两个非负整数num1和num2表示为字符串,返回num ...

  5. 《LeetBook》LeetCode题解(2):Add Two Numbers [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  6. LeetCode题解 #2 Add Two Numbers

    题目大意:使用链表表示的两个整数,计算出其和,以同样的形式返回. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 ...

  7. LeetCode 题解之Add Digits

    1.问题描述 2.问题分析 循环拆分数字,然求和判断. 3.代码 int addDigits(int num) { ) return num; int result = num; do{ vector ...

  8. LeetCode题解之Multiply Strings

    1.题目描述 2.问题分析 按照手算乘法的过程进行计算,首先计算乘法,然后计算加法. 3.代码 string multiply(string num1, string num2) { string s ...

  9. LeetCode 题解之Add Binary

    1.题目描述 2.题目分析 使用string 的逆向指针,做二进制加法,注意进位问题就可以. 3.代码 string addBinary(string a, string b) { string::r ...

随机推荐

  1. Python -- 网络编程 -- Socket发送文件

    客户端如果直接send两次,一次发文件名,一次发文件内容 服务端接受的时候会一起接收,不知怎么分开发送,或者分开接收, 或者全部接收再解析内容 今天发现传送mp3文件的时候没问题,传送文本文件的话,以 ...

  2. R语言运算符

    运算符是一个符号,它告诉编译器执行特定的数学或逻辑操作. R语言丰富的内置运算符,并提供以下类型的运算符. 运算符类型 在R编程中有以下类型的运算符 - 算术运算符 关系运算符 逻辑运算符 赋值运算符 ...

  3. redis实战笔记(3)-第3章 Redis命令

    第3章 Redis命令   本章主要内容 字符串命令. 列表命令和集合命令 散列命令和有序集合命令 发布命令与订阅命令 其他命令   在每个不同的数据类型的章节里, 展示的都是该数据类型所独有的. 最 ...

  4. PL/SOL csv格式导出查询结果时出现某些列的数据被四舍五入了的解决办法

    昨天用pl/sql从oracle数据库捞取数据时,发现导出的csv格式中某些列的数据被进行了四舍五入处理了,当然这些列都是纯数字的,百思不得其解,后来上网才,才得知了原因. 这并不是导出的CSV文件数 ...

  5. SQL 集合例子

    IF OBJECT_ID('tempdb..#Purchase', 'U') IS NOT NULL DROP TABLE #Purchase; CREATE TABLE #Purchase ( Pu ...

  6. Extjs相关知识点梳理

      store是一个为Ext器件提供record对象的存储容器,行为和属性都很象数据表 方法:不列举继承来的方法 Store( Object config ) 构造,config定义为{ autoLo ...

  7. pictureBox控件获得图片路径的三种方法及自适应大小属性

    1.绝对路径: this.pictureBox2.Image=Image.FromFile("D:\\001.jpg"); 2.相对路径: Application.StartupP ...

  8. Uploadify火狐出现302错误

    $(function () { var auth = "@(Request.Cookies[FormsAuthentication.FormsCookieName] == null ? st ...

  9. 基于.NET Core2的图片上传

    其实,.NET Core2的图片上传挺好做的,只是,有些坑要注意.......话不多说,上代码 public async Task<IActionResult> Upload([FromS ...

  10. AngleSharp一些示例

    看到了AngleSharp,感觉这个非常好用,比HtmlAgilityPack感觉好用点 AngleSharp 地址:https://github.com/AngleSharp/AngleSharp ...