当前位置:网站首页>【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!
边栏推荐
猜你喜欢

原生小程序用画布制作海报,等比例缩放,和uniapp差不多就是写法有点不同

Prompt code when MySQL inserts Chinese data due to character set problems: 1366
![[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle](/img/db/e581087e550a2e460f12047685c48f.png)
[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle

YOLOX backbone——CSPDarknet的实现

开源之夏中选名单已公示,基础软件领域成为今年的热门申请

基于QingCloud的 “房地一体” 云解决方案

4274. suffix expression

What is the future development trend of Business Intelligence BI

"Unusual proxy initial value setting is not supported", causes and Solutions

input的聚焦后的边框问题
随机推荐
Telnet port login method with user name for liunx server
SLAM14讲中Sophus包的安装问题
Using skills of xargs -- the way to build a dream
threejs辉光通道01(UnrealBloomPass && layers)
Data middle office: middle office architecture and overview
Data midrange: analysis of full stack technical architecture of data midrange, with industry solutions
数据中台:数据中台技术架构详解
解决:模型训练时loss出现nan
基于QingCloud的地理信息企业研发云解决方案
520. 检测大写字母
IDEA另起一行快捷键
JS to find and update the specified value in the object through the key
4274. 后缀表达式
数据中台:国内大厂中台建设架构集锦
Data midrange: detailed explanation of the technical stack of data acquisition and extraction
2138. 将字符串拆分为若干长度为 k 的组
4274. suffix expression
Camera projection matrix calculation
K8s deployment of highly available PostgreSQL Cluster -- the road to building a dream
pm2 部署 nuxt3.js 项目