LeetCode题解之Add Strings
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的更多相关文章
- 【leetcode】415. Add Strings
problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...
- leetcode题解2. Add Two Numbers
题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...
- 【LeetCode】415. Add Strings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...
- LeetCode算法题-Add Strings(Java实现)
这是悦乐书的第223次更新,第236篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第90题(顺位题号是415).给定两个非负整数num1和num2表示为字符串,返回num ...
- 《LeetBook》LeetCode题解(2):Add Two Numbers [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode题解 #2 Add Two Numbers
题目大意:使用链表表示的两个整数,计算出其和,以同样的形式返回. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 ...
- LeetCode 题解之Add Digits
1.问题描述 2.问题分析 循环拆分数字,然求和判断. 3.代码 int addDigits(int num) { ) return num; int result = num; do{ vector ...
- LeetCode题解之Multiply Strings
1.题目描述 2.问题分析 按照手算乘法的过程进行计算,首先计算乘法,然后计算加法. 3.代码 string multiply(string num1, string num2) { string s ...
- LeetCode 题解之Add Binary
1.题目描述 2.题目分析 使用string 的逆向指针,做二进制加法,注意进位问题就可以. 3.代码 string addBinary(string a, string b) { string::r ...
随机推荐
- CGI PL PERL脚本 提权
windows 2003 下,安装ActivePerl-5.16.2.1602-MSWin32-x86-296513 IIS 添加CGI支持.并在对应网站配置下面,添加CGI后缀或PL后缀 与 对应的 ...
- Android 开发工具类 14_ JsonTools
天气 JSON 数据解析 package com.example.weather_json.tools; import java.util.ArrayList; import java.util.Li ...
- fine ui使用笔记
1.top.X.alert("保存成功"); 2.Alert.GetShowInTopReference("这是在服务器端生成的客户端事件"); 注明:1与2等 ...
- MVC及MVC Core在filter中如何获取控制器名称和Action名称
很多时候我们需要使用过滤器来实现一些拦截.验证等行为,此时我们能获取到的Context是ActionExecutingContext ,我们如何通过这个Context来获得Action.Control ...
- 详解REST架构风格
编辑推荐: 本文来自于segmentfault.com,一起了解REST的内在,认识REST的优势,而不再将它当作是“理所当然” 引言 作为Web开发者,你可能或多或少了解一些REST的知识,甚至已经 ...
- 云存储(Swift+Keystone)部署策略
Swift是OpenStack的对象存储模块,Keystone是OpenStack的权限验证模块.可以于这两个模块搭建一个较为完善的云存储系统. 1.官方方案 云存储的服务器分三种类型: 验证节点 A ...
- CSS Sprite 精灵图
.bg_sprite{background-image:url(/整图地址); background-repeat:no-repeat} 引用该类 .. 然后在元素中逐一定义背景坐标 .. 以下为关键 ...
- 在SQLSERVER中如何检测一个字符串中是否包含另一个字符串
--当charindex返回值大于0时则包含 为0不包含 select CHARINDEX('456','123456') SQL语句使用CHARINDEX函数,来测试一个字符串中是否包含另一个字 ...
- Winfrom 基于TCP的Socket服务端 多线程(进阶版)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- [javaSE] 集合框架(ArrayList,LinkedList,Vector)
ArrayList特点:底层使用数组数据结构,查询速度快(使用脚标查),插入删除慢(索引要改变) LinkedList特点:底层使用链表数据结构,查询慢(需要一个一个去问),插入删除快 Vector特 ...