当前位置:网站首页>【LeetCode】415. String addition
【LeetCode】415. String addition
2022-06-24 08:59:00 【Uaena_ An】
【LeetCode】415. String addition

🧸 Reading questions
Put two strings use int Formal addition , The result is string Print .
This question cannot be used int/long Because the test cases are very long ,longlong I can't save it , So we have to calculate by bits !
🧸 Code
Define two tags ned1/end2 Start at the end of the two arrays and move forward , And use carry Record carry ,ret Calculate the sum of the two numbers , Addition of two numbers >10 be ret -= 10 ,carry Set up 1, otherwise carry Set up 0;
establish s The string appends the result of the calculation , Finally, turn it over ( This is to prevent , The header will always move the data , cause 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;
}
};
🧸 Decoding code
Define two tags , Forward access from the tail of the two arrays .
int end1 = num1.size()-1,end2 = num2.size()-1;
Definition carry Save carry
int carry = 0;
Definition s Save results
string s;
As long as one side of the cycle is not finished , I will continue to visit , because carry It may be worth , It is possible to carry all the time
for example :9999+11 ,11 After walking ,carry = 1, therefore 9999 And continue to visit to know carry Be placed 0
while(end1 >=0 ||end2>=0)
{
x1x2 It is used for unit calculation That is to say x1 = num1[end]
Because every count has to be reset x1,x2
therefore
int x1 = 0;
Judge end1 Have you finished , Keep counting before you finish , It's over x1 = 0
if(end1>= 0 )
{
x1 = num1[end1] - '0';
--end1;
}
Follow x1 Empathy
int x2 = 0;
if(end2>= 0 )
{
x2 = num2[end2] - '0';
--end2;
}
ret Is stored x1+x2 Result
int ret = x1+x2 + carry;
Calculated carry , If you add two numbers >9 be ret-=10, carry = 1
if(ret > 9)
{
ret-=10;
carry = 1;
}
else
{
Must judge else , Otherwise, the carry may always be 1
carry = 0;
}
The result of adding two numbers by tail interpolation
s += ret + '0';
}
It's over , If there is another number in the carry , And you have to put the tail in
if(carry == 1)
{
s += '1';
}
Finally, turn over and get the correct answer
reverse(s.begin(),s.end());
return s;
}
come on. I wish you get what you want offer!
边栏推荐
- What is the future development trend of Business Intelligence BI
- I heard that you are still spending money to buy ppt templates from the Internet?
- 小程序wx.show
- 2138. 将字符串拆分为若干长度为 k 的组
- Spark - LeftOuterJoin 结果条数与左表条数不一致
- uniapp 开发多端项目如何配置环境变量以及区分环境打包
- 4274. 后缀表达式
- 数云发布2022美妆行业全域消费者数字化经营白皮书:全域增长破解营销难题
- 1704. 判断字符串的两半是否相似
- leetcode——错误的集合
猜你喜欢

Telnet port login method with user name for liunx server

【NOI模拟赛】寄(树形DP)

【使用 PicGo+腾讯云对象存储COS 作为图床】

【量化投资】离散傅里叶变换求数组周期

2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。

I heard that you are still spending money to buy ppt templates from the Internet?

MySQL | 存储《康师傅MySQL从入门到高级》笔记

从华为WeAutomate数字机器人论坛,看政企领域的“政务新智理”

Data midrange: detailed explanation of the technical stack of data acquisition and extraction

数据中台:中台架构及概述
随机推荐
Pytorch读入据集(典型数据集及自定义数据集两种模式)
【PyTorch基础教程30】DSSM双塔模型代码解析
【LeetCode】541. 反转字符串 II
用VNC Viewer的方式远程连接无需显示屏的树莓派
Become an IEEE student member
tcpdump抓包实现过程
Pymysql inserts data into MySQL and reports an error for no reason
2138. 将字符串拆分为若干长度为 k 的组
2020中国全国各省市,三级联动数据,数据机构(数据来自国家统计局官网)
数组相向指针系列
K8s deployment of highly available PostgreSQL Cluster -- the road to building a dream
数据中台:中台实践与总结
【LeetCode】387. 字符串中的第一个唯一字符
Using skills of xargs -- the way to build a dream
SLAM14讲中Sophus包的安装问题
GradScaler MaxClipGradScaler
Qingcloud based R & D cloud solution for geographic information enterprises
[quantitative investment] discrete Fourier transform to calculate array period
2138. splitting a string into groups of length k
开源之夏中选名单已公示,基础软件领域成为今年的热门申请