2023每日刷题(六十八)
Leetcode—415.字符串相加
实现代码
class Solution {
public:string addStrings(string num1, string num2) {string ans;int len1 = num1.size();int len2 = num2.size();int i = len1 - 1, j = len2 - 1;int sum = 0, c = 0;while(i >= 0 && j >= 0) {sum = num1[i] - '0' + num2[j] - '0' + c;string s = to_string(sum % 10);ans += s;c = sum / 10;i--;j--;}while(i >= 0) {sum = num1[i] - '0' + c;c = sum / 10;string s = to_string(sum % 10);ans += s;i--;}while(j >= 0) {sum = num2[j] - '0' + c;c = sum / 10;string s = to_string(sum % 10);ans += s;j--;}if(c) {string s = to_string(c);ans += s;}reverse(ans.begin(), ans.end());return ans;}
};
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!