当前位置:网站首页>【LeetCode】415. 字符串相加
【LeetCode】415. 字符串相加
2022-06-24 07:07:00 【Uaena_An】
【LeetCode】415. 字符串相加

🧸读题
将两个字符串 用int形式相加,结果用string打印。
此题不能用int/long因为测试用例很长,longlong也存不下,所以要按位计算!
🧸代码
定义两个标记 ned1/end2从两个数组尾部开始向前走,并且用carry记录进位,ret计算两数之和,两数相加>10 则ret -= 10 ,carry置1,否则carry置0;
创建s字符串将计算结果尾插,最后再翻转过来(这么做是为了防止,头插会一直挪动数据,造成O(N2))
class Solution {
public:
string addStrings(string num1, string num2) {
int end1 = num1.size()-1,end2 = num2.size()-1;
int carry = 0;
string s;
while(end1 >=0 ||end2>=0)
{
int x1 = 0;
if(end1>= 0 )
{
x1 = num1[end1] - '0';
--end1;
}
int x2 = 0;
if(end2>= 0 )
{
x2 = num2[end2] - '0';
--end2;
}
int ret = x1+x2 + carry;
if(ret > 9)
{
ret-=10;
carry = 1;
}
else
{
carry = 0;
}
s += ret + '0';
}
if(carry == 1)
{
s += '1';
}
reverse(s.begin(),s.end());
return s;
}
};
🧸解读代码
定义两个标记,从两个数组尾部向前访问。
int end1 = num1.size()-1,end2 = num2.size()-1;
定义carry保存进位
int carry = 0;
定义s保存结果
string s;
循环只要有一方没走完,就继续访问,因为carry可能有值,有可能会一直进位
例如:9999+11 ,11走完后,carry = 1,所以9999还要继续访问知道carry被置0
while(end1 >=0 ||end2>=0)
{
x1x2是用来进行单位计算的 也就是x1 = num1[end]
因为每一次计数都要重置x1,x2
所以
int x1 = 0;
判断end1有没有走完,没走完就继续算,走完了x1 = 0
if(end1>= 0 )
{
x1 = num1[end1] - '0';
--end1;
}
跟x1同理
int x2 = 0;
if(end2>= 0 )
{
x2 = num2[end2] - '0';
--end2;
}
ret是存储 x1+x2的结果
int ret = x1+x2 + carry;
计算进位,如果两数相加>9 则ret-=10,进位 = 1
if(ret > 9)
{
ret-=10;
carry = 1;
}
else
{
必须判断else ,否则进位可能一直是1
carry = 0;
}
尾插两数相加的结果
s += ret + '0';
}
里面判断完,如果进位里还有一个数,还要尾插进去
if(carry == 1)
{
s += '1';
}
最后翻转则得到正确答案
reverse(s.begin(),s.end());
return s;
}
加油 祝你拿到心仪的offer!
边栏推荐
- RuntimeError: Missing dependencies:XXX
- Smart power plant: how to make use of easycvr to build a safe, stable, green and environment-friendly intelligent inspection platform
- JS to find and update the specified value in the object through the key
- 数据中台:中台架构及概述
- 不能改变虚拟机电源状态报错解决方案
- xargs使用技巧 —— 筑梦之路
- Rsync for file backup
- Increase insert speed
- GradScaler MaxClipGradScaler
- 什么是图神经网络?图神经网络有什么用?
猜你喜欢

Matlab camera calibrator camera calibration

Base64编码详解及其变种(解决加号在URL变空格问题)

数据中台:数据中台技术架构详解

China chip Unicorn Corporation

MySQL | 视图《康师傅MySQL从入门到高级》笔记

WebRTC系列-网络传输之5选择最优connection切换

【PyTorch基础教程30】DSSM双塔模型代码解析

Why can ping fail while traceroute can

K8S部署高可用postgresql集群 —— 筑梦之路
![[team management] 25 tips for testing team performance management](/img/bd/0ef55630de43efcf5aa663f3099fce.jpg)
[team management] 25 tips for testing team performance management
随机推荐
不能改变虚拟机电源状态报错解决方案
Xtrabackup for data backup
更改SSH端口号
数据中台:数据采集和抽取的技术栈详解
[10 day SQL introduction] Day2
Scheduled database backup script
2138. 将字符串拆分为若干长度为 k 的组
Win11 blank when using VIM to view content in cmder
Solving linear equations with MATLAB ax=b
Telnet port login method with user name for liunx server
every()、map()、forEarch()方法。数组里面有对象的情况
Base64编码详解及其变种(解决加号在URL变空格问题)
À propos de ETL il suffit de lire cet article, trois minutes pour vous faire comprendre ce qu'est ETL
api平台通用签名机制
工具类
数据中台:中台实践与总结
There was an error checking the latest version of pip
China chip Unicorn Corporation
Liunx change the port number of vsftpd
教程篇(5.0) 08. Fortinet安全架构集成与FortiXDR * FortiEDR * Fortinet 网络安全专家 NSE 5