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] 205. Isomorphic StringsLeetCode
- Leetcode 67 Add BinaryLeetCode
- [LeetCode] 258. Add DigitsLeetCodeGit
- LeetCode 2 Add Two NumbersLeetCode
- LeetCode Greatest Common Divisor of Strings All In OneLeetCode
- LeetCode-2 Add Two NumbersLeetCode
- [LeetCode] 282. Expression Add OperatorsLeetCodeExpress
- LeetCode 2. Add Two NumbersLeetCode
- [LeetCode] 893. Groups of Special-Equivalent StringsLeetCodeUI
- LeetCode2: Add two numbers(兩數相加)LeetCode
- Fifth. LeetCode 2:Add Two Numbers 兩數之和LeetCode
- LeetCode67. Add Binary(二進位制加法)LeetCode
- leetcode 兩數相加(add two numbers) Python程式設計實現LeetCodePython程式設計
- no-strings-attached
- Encode and Decode Strings
- Go Standard library - stringsGo
- strings 和 strconv 包
- git add all和git add .區別Git
- 4.5.1 add
- DataTransferItemList.add()
- 2.3 ADD CREDENTIALSTORE
- 2.2 ADD CHECKPOINTTABLE
- git add errorGitError
- JavaScript select add()JavaScript
- [ABC343G] Compress Strings
- CF482C Game with StringsGAM
- Golang 常用的 strings 函式Golang函式
- golang中的strings.EqualFoldGolang
- 聊聊Java 9的Compact StringsJava
- ArrayList宣告,Add(), Insert();
- 7.11 ADD_MONTHS
- IDBObjectStore.add() 方法Object
- Add Cmder Terminal to PHPStormPHPORM
- 4.5.1.2 srvctl add databaseDatabase
- 4.5.1.3 srvctl add listener
- 4.5.1.4 srvctl add ons
- 4.5.1.1 srvctl add asmASM
- 撤銷git addGit