当前位置:网站首页>【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!
边栏推荐
- Using skills of xargs -- the way to build a dream
- SLAM14讲中Sophus包的安装问题
- How does the tunnel mobile inspection track robot monitor 24 hours to ensure the safety of tunnel construction?
- From the Huawei weautomate digital robot forum, we can see the "new wisdom of government affairs" in the field of government and enterprises
- 数据中台:数据中台技术架构详解
- 【E325: ATTENTION】vim编辑时报错
- Essay - Reflection
- 数据中台:中台架构及概述
- What is the future development trend of Business Intelligence BI
- OpenCV每日函数 结构分析和形状描述符(7) 寻找多边形(轮廓)/旋转矩形交集
猜你喜欢

关于 GIN 的路由树
![[noi Simulation Competition] geiguo and time chicken (structure)](/img/4c/ed1b5bc2bed653c49b8b7922ce1674.png)
[noi Simulation Competition] geiguo and time chicken (structure)

JS to find and update the specified value in the object through the key

Data middle office: middle office architecture and overview

MySQL | store notes of Master Kong MySQL from introduction to advanced

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

什么是图神经网络?图神经网络有什么用?

听说你还在花钱从网上买 PPT 模板?

2022.06.23(LC_144,94,145_二叉树的前序、中序、后序遍历)

opencv最大值滤波(不局限于图像)
随机推荐
A tip to read on Medium for free
GradScaler MaxClipGradScaler
数据中台:国内大厂中台建设架构集锦
开源之夏中选名单已公示,基础软件领域成为今年的热门申请
【量化投资】离散傅里叶变换求数组周期
MBA-day25 最值问题-应用题
1528. 重新排列字符串
剑指 Offer 55 - I. 二叉树的深度-dfs法
4275. Dijkstra序列
基于单片机开发的酒精浓度测试仪方案
1844. replace all numbers with characters
The form image uploaded in chorme cannot view the binary image information of the request body
WebRTC系列-网络传输之5选择最优connection切换
数组相向指针系列
Opencv maximum filtering (not limited to images)
华为路由器:ipsec技术
Data middle office: middle office architecture and overview
Double pointer analog
数据中台:数据中台全栈技术架构解析,附带行业解决方案
From the Huawei weautomate digital robot forum, we can see the "new wisdom of government affairs" in the field of government and enterprises