Leetcode 415. Add Strings
題目的意思是求兩個用字串表示的非負整數的和,返回型別同樣要求是字串型別。兩個數字都不包含前導0,要求我們不能直接將字串轉化為整數也不能使用現成的庫函式。
思路很簡單,如何寫簡潔了不容易,參考Leetcode 67. Add Binary的思路。
class Solution {
public:
string addStrings(string num1, string num2) {
int a,b,c=0,i=num1.size()-1, j=num2.size()-1;
string s;
while(i>=0 || j>=0 ||c==1)
{
a=(i>=0?num1[i--]-'0':0);
b=(j>=0?num2[j--]-'0':0);
s=char((a+b+c)%10+'0')+s;
c=(a+b+c)/10;
}
return s;
}
};
相關文章
- Add Strings 字串相加字串
- Leetcode Multiply StringsLeetCode
- Leetcode-Multiply StringsLeetCode
- Multiply Strings leetcode javaLeetCodeJava
- LeetCode-Isomorphic StringsLeetCode
- LeetCode-Group Shifted StringsLeetCode
- leetcode刷題--Isomorphic StringsLeetCode
- Leetcode Add BinaryLeetCode
- Leetcode Add Two NumbersLeetCode
- [LeetCode] 205. Isomorphic StringsLeetCode
- LeetCode-Encode and Decode StringsLeetCode
- LeetCode 2 Add Two NumbersLeetCode
- Leetcode 67 Add BinaryLeetCode
- LeetCode-Add and Search WordLeetCode
- LeetCode-Expression Add OperatorsLeetCodeExpress
- Leetcode-Add BinaryLeetCode
- Leetcode-Add Two NumbersLeetCode
- Add Binary Leetcode javaLeetCodeJava
- Add Two Numbers leetcode javaLeetCodeJava
- [LeetCode] 258. Add DigitsLeetCodeGit
- [LeetCode] 282. Expression Add OperatorsLeetCodeExpress
- LeetCode-2 Add Two NumbersLeetCode
- LeetCode 2. Add Two NumbersLeetCode
- LeetCode-Different Ways to Add ParenthesesLeetCode
- Leetcode 258. Add DigitsLeetCodeGit
- LeetCode OJ : 2 Add Two NumbersLeetCode
- [LeetCode] 893. Groups of Special-Equivalent StringsLeetCodeUI
- LeetCode Greatest Common Divisor of Strings All In OneLeetCode
- [leetCode][016] Add Two NumbersLeetCode
- [Leetcode] 2. Add Two Numbers QuestionEditorial SolutionLeetCode
- 258. Add Digits--LeetCode RecordGitLeetCode
- LeetCode2: Add two numbers(兩數相加)LeetCode
- [LeetCode] Add Two Numbers 兩個數字相加LeetCode
- Fifth. LeetCode 2:Add Two Numbers 兩數之和LeetCode
- LeetCode 解題報告 - 2. Add Two NumbersLeetCode
- LeetCode67. Add Binary(二進位制加法)LeetCode
- 【LeetCode 67_字串_算術運算】Add BinaryLeetCode字串
- LeetCode解題報告 241. Different Ways to Add Parentheses [medium]LeetCode