# 415.Add Strings

**415.Add Strings**

难度:Easy

> 给定两个字符串形式的非负整数 num1 和num2 ，计算它们的和。

注意：

```
num1 和num2 的长度都小于 5100.
num1 和num2 都只包含数字 0-9.
num1 和num2 都不包含任何前导零。
你不能使用任何內建 BigInteger 库， 也不能直接将输入的字符串转换为整数形式。
```

字符串相加，从最低位开始相加，留一个flag表示进位。

```
class Solution {
public:
    string addStrings(string num1, string num2) {
        num1= '0'+num1;
        num2='0'+num2;
        while(num1.length()>num2.length())
            num2 = '0'+num2;
        while(num2.length()>num1.length())
            num1 = '0'+num1;
        int flag=0;
        string res;
        for(int i=num1.size()-1; i>0;i--)    
        {
            int tmp=(num1[i]-'0' ) +(num2[i] - '0')+flag;
            if(tmp>=10)
            {
                flag=1;
                tmp-=10;
                
            }
            else
                flag=0;
            res= (char)('0'+tmp) +res;
        }
        if(flag)
            res='1'+res;
        return res;
        
    }
};
```

> 执行用时 :24 ms, 在所有 C++ 提交中击败了24.74%的用户\
> 内存消耗 :74.6 MB, 在所有 C++ 提交中击败了6.03%的用户


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/415.add_strings.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
