当前位置:网站首页>【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!
边栏推荐
- On the routing tree of gin
- 阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍
- Mysql数据(Liunx环境)定时备份
- 【LeetCode】541. 反转字符串 II
- Using skills of xargs -- the way to build a dream
- 【Pytorch基础教程31】YoutubeDNN模型解析
- 等保备案是什么意思?应该去哪里办理备案?
- Array opposite pointer series
- OpenCV每日函数 结构分析和形状描述符(7) 寻找多边形(轮廓)/旋转矩形交集
- 216. combined summation III enumeration method
猜你喜欢
随机推荐
520. 检测大写字母
【MySQL从入门到精通】【高级篇】(一)字符集的修改与底层原理
快慢指针系列
1844. replace all numbers with characters
every()、map()、forEarch()方法。数组里面有对象的情况
2138. splitting a string into groups of length k
微博撰写-流程图-序列图-甘特图-mermaid流程图-效果不错
用VNC Viewer的方式远程连接无需显示屏的树莓派
【Pytorch基础教程31】YoutubeDNN模型解析
Pymysql inserts data into MySQL and reports an error for no reason
数据中台:民生银行的数据中台实践方案
MBA-day25 最值问题-应用题
工具类
【LeetCode】415. 字符串相加
Liunx Mysql安装
What is graph neural network? Figure what is the use of neural networks?
原生小程序用画布制作海报,等比例缩放,和uniapp差不多就是写法有点不同
听说你还在花钱从网上买 PPT 模板?
WebRTC系列-网络传输之5选择最优connection切换
Transplantation of xuantie e906 -- fanwai 0: Construction of xuantie c906 simulation environment







