415. 字符串相加

在这里插入图片描述
就是大整数相加,从后往前加,只要有一个字符串没遍历完就要继续,如果还有carry也要继续
最后将res反转即可

class Solution {
public:
    string addStrings(string num1, string num2) {
        //就是大整数相加,从后往前加,只要有一个字符串没遍历完就要继续,如果还有carry也要继续
        //最后将res反转即可
        string res = "";
        int len1 = num1.length()-1, len2 = num2.length()-1;
        int sum = 0, carry = 0;
        int a = 0, b = 0;
        while(len1 >= 0 || len2>=0 ||carry != 0){
            a = (len1 < 0 ? 0 : num1[len1--]-'0');
            b = (len2 < 0 ? 0 : num2[len2--]-'0');
            sum = (a+b+carry)%10;
            carry = (a+b+carry)/10;
            res += (sum+'0');
        }
        reverse(res.begin(),res.end());
        return res;
    }
};
class Solution {
public:
    string addStrings(string num1, string num2) {
        //也是类似大整数相加,num1或者num2没遍历完,或者carry==1的情况下都不要退出
        reverse(num1.begin(),num1.end());
        reverse(num2.begin(),num2.end());
        int index1 = 0, index2 = 0;
        int carry = 0;
        int sum = 0;
        string res = "";
        int tmp1 = 0, tmp2 = 0;
        while(index1 < num1.length() || index2 < num2.length() || carry != 0){
            tmp1 = (index1 == num1.length() ? 0 : num1[index1++]-'0');
            tmp2 = (index2 == num2.length() ? 0 : num2[index2++]-'0');
            sum = (tmp1 + tmp2 + carry)%10;
            carry = (tmp1 + tmp2 +carry)/10;
            res += (sum+'0');
        }
        reverse(res.begin(),res.end());
        return res;
    }
};
已标记关键词 清除标记
相关推荐
<p> <b><span style="background-color:#FFE500;">【超实用课程内容】</span></b> </p> <p> <br /> </p> <p> <br /> </p> <p> 本课程内容包含讲解<span>解读Nginx的基础知识,</span><span>解读Nginx的核心知识、带领学员进行</span>高并发环境下的Nginx性能优化实战,让学生能够快速将所学融合到企业应用中。 </p> <p> <br /> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <b><br /> </b> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <b><span style="background-color:#FFE500;">【课程如何观看?】</span></b> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> PC端:<a href="https://edu.csdn.net/course/detail/26277"><span id="__kindeditor_bookmark_start_21__"></span></a><a href="https://edu.csdn.net/course/detail/27216">https://edu.csdn.net/course/detail/27216</a> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 移动端:CSDN 学院APP(注意不是CSDN APP哦) </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 本课程为录播课,课程永久有效观看时长,大家可以抓紧时间学习后一起讨论哦~ </p> <p style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <br /> </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <strong><span style="background-color:#FFE500;">【学员专享增值服务】</span></strong> </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <b>源码开放</b> </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 课件、课程案例代码完全开放给你,你可以根据所学知识,自行修改、优化 </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 下载方式:电脑登录<a href="https://edu.csdn.net/course/detail/26277"></a><a href="https://edu.csdn.net/course/detail/27216">https://edu.csdn.net/course/detail/27216</a>,播放页面右侧点击课件进行资料打包下载 </p> <p> <br /> </p> <p> <br /> </p> <p> <br /> </p>
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:C马雯娟 返回首页