Leetcode 67 Add Binary
Given two binary strings, return their sum (also a binary string).
The input strings are both non-empty and contains only characters 1
or 0
.
Example 1:
Input: a = "11", b = "1" Output: "100"
Example 2:
Input: a = "1010", b = "1011" Output: "10101"
這個題的意思就是實現一個二進位制的加法,模擬題意即可以實現
1)
class Solution {
public:
string addBinary(string a, string b) {
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());//首先把兩個字串逆轉
int t = 0;
string res = "";//計算後的字串
for(int i = 0 ; i < a.size() || i < b.size() ;i++){
int va = i >= a.size() ? 0 : a[i] - '0';
int vb = i >= b.size() ? 0 : b[i] - '0';
int s = va + vb + t;//標識加之後的數值
t = s / 2;//將位置推進一位
s %= 2;//看此位置上為0還是1
res += to_string(s);//變為字串
}
if(t) res += '1';//如果還有餘數,那麼最後的時候
return string(res.rbegin(),res.rend());//進行逆轉
}
};
2)
class Solution {
public:
string addBinary(string a, string b) {
int counter1 = a.length()-1;
int counter2 = b.length()-1;
int carry = 0;
string res;
while (counter1>=0 || counter2>=0) {
int sum = carry;
if (counter1>=0) {
sum += (a[counter1] - '0');
}
if (counter2>=0) {
sum += (b[counter2] - '0');
}
res.append(to_string(sum % 2));
carry = sum / 2;
counter1--;
counter2--;
}
if (carry != 0) res.append(to_string(carry));
int n = res.length();
// Swap character starting from two
// corners
for (int i = 0; i < n / 2; i++) {
swap(res[i], res[n - i - 1]);
}
return res;
}
};
相關文章
- LeetCode67. Add Binary(二進位制加法)LeetCode
- Add Binary 二進位制求和
- Leetcode Binary Tree PathsLeetCode
- [LeetCode] 258. Add DigitsLeetCodeGit
- LeetCode 2 Add Two NumbersLeetCode
- LeetCode-2 Add Two NumbersLeetCode
- [LeetCode] 282. Expression Add OperatorsLeetCodeExpress
- LeetCode 2. Add Two NumbersLeetCode
- [LeetCode] 226. Invert Binary TreeLeetCode
- [LeetCode] 543. Diameter of Binary TreeLeetCode
- LeetCode 543. Diameter of Binary TreeLeetCode
- Binary Tree Level Order Traversal [LEETCODE]LeetCode
- LeetCode545.Boundary-of-Binary-TreeLeetCode
- Leetcode 226. Invert Binary TreeLeetCode
- [leetcode]binary-tree-inorder-traversalLeetCode
- [leetcode]maximum-depth-of-binary-treeLeetCode
- LeetCode之Univalued Binary Tree(Kotlin)LeetCodeKotlin
- LeetCode之Binary Tree Pruning(Kotlin)LeetCodeKotlin
- LeetCode のminimum-depth-of-binary-treeLeetCode
- Leetcode 298 Binary Tree Longest Consecutive SequenceLeetCode
- LeetCode 104. Maximum Depth of Binary TreeLeetCode
- LeetCode 98. Validate Binary Search TreeLeetCode
- LeetCode | 144. Binary Tree Preorder TraversalLeetCode
- LeetCode | 145. Binary Tree Postorder TraversalLeetCode
- 【LeetCode】617. Merge Two Binary TreesLeetCode
- Leetcode 94. Binary Tree Inorder TraversalLeetCode
- Leetcode 144. Binary Tree Preorder TraversalLeetCode
- Leetcode 145. Binary Tree Postorder TraversalLeetCode
- LeetCode 617. Merge Two Binary TreesLeetCode
- leetcode-124-Binary Tree Maximum Path SumLeetCode
- [LeetCode] 501. Find Mode in Binary Search TreeLeetCode
- [LeetCode] 671. Second Minimum Node In a Binary TreeLeetCode
- LeetCode 124. Binary Tree Maximum Path SumLeetCode
- [leetcode]convert-sorted-array-to-binary-search-treeLeetCode
- LeetCode之Construct String from Binary Tree(Kotlin)LeetCodeStructKotlin
- LeetCode之All Possible Full Binary Trees(Kotlin)LeetCodeKotlin
- LeetCode 501. Find Mode in Binary Search TreeLeetCode
- [LeetCode] 2196. Create Binary Tree From DescriptionsLeetCode