【leetcode】67. Add Binary
@requires_authorization
@author johnsondu
@create_time 2015.7.15 11:00
@url [add binary](https://leetcode.com/problems/add-binary/)
/*******************
* 模擬大數相加
* 時間複雜度: O(n)
* 空間複雜度: O(n)
******************/
class Solution {
public:
string addBinary(string a, string b) {
int lena = a.size();
int lenb = b.size();
string ans = "";
string tmpa = "";
string tmpb = "";
for(int i = lena-1; i >= 0; i --) tmpa += a[i];
for(int i = lenb-1; i >= 0; i --) tmpb += b[i];
int mins = min(lena, lenb);
int carry = 0;
for(int i = 0; i < mins; i ++){
int res = (tmpa[i] - '0') + (tmpb[i] - '0') + carry;
carry = res > 1 ? 1 : 0;
res = res % 2;
ans += (res + '0');
}
for(int i = mins; i < lena; i ++){
int res = (tmpa[i] - '0') + carry;
carry = res > 1 ? 1 : 0;
res = res % 2 ;
ans += (res + '0');
}
for(int i = mins; i < lenb; i ++){
int res = (tmpb[i] - '0') + carry;
carry = res > 1 ? 1 : 0;
res = res % 2;
ans += (res + '0');
}
if(carry){
ans += (carry + '0');
}
int len_ans = ans.size();
for(int i = 0; i < len_ans / 2; i ++){
char tmp = ans[i];
ans[i] = ans[len_ans-i-1];
ans[len_ans-i-1] = tmp;
}
return ans;
}
};
// Simplified Version
class Solution {
public:
string addBinary(string a, string b) {
int lena = a.size() - 1;
int lenb = b.size() - 1;
string ans = "";
int carry = 0;
while(lena >= 0 || lenb >= 0 || carry > 0){
int res = carry;
if(lena >= 0) res += (a[lena] - '0');
if(lenb >= 0) res += (b[lenb] - '0');
carry = res / 2;
ans = string(1, (res & 1) + '0') + ans;
lena --;
lenb --;
}
return ans;
}
};
相關推薦
【leetcode】67. Add Binary
@requires_authorization @author johnsondu @create_time 2015.7.15 11:00 @url [add binary](https://leet
【leetcode】67. Add Binary(Python & C++)
67. Add Binary 題目連結 67.1 題目描述: Given two binary strings, return their sum (also a binary string). For example, a = “11” b
【LeetCode】67. Balanced Binary Tree
題目描述(Easy) Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: a b
【Leetcode】之Add Binary
一.問題描述 Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 二.我的解題思路 這道題也比較容
【leetcode】67.(Easy)Add Binary
提交程式碼: class Solution { public String addBinary(String a, String b) { int numA,numB,carry=0; int p1=a.length()-1,p2=b.length()-1;
【Leetcode】110. Balanced Binary Tree
out method easy fin nod gen als lan generated Question: Given a binary tree, determine if it is height-balanced. For this problem, a he
【LeetCode】2.Add Two Numbers 兩數相加
輸入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 輸出:7 -> 0 -> 8 原因:342 + 465 = 807 class ListNode: def __init__(self, x):
【LeetCode】2. Add Two Numbers 兩數相加
給出兩個 非空 的連結串列用來表示兩個非負的整數。其中,它們各自的位數是按照 逆序 的方式儲存的,並且它們的每個節點只能儲存 一位 數字。 如果,我們將這兩個數相加起來,則會返回一個新的連結串列來表示它們的和。 您可以假設除了數字 0 之外,這兩
【LeetCode】68. Flatten Binary Tree to Linked List
題目描述(Medium) Given a binary tree, flatten it to a linked list in-place. 題目連結 Example 1: For example, given the following tree:
【LeetCode】73. Unique Binary Search Trees II
題目描述(Medium) Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n. 題目連結 Example
【leetcode】2-Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their
【LeetCode】761. Special Binary String 解題報告(Python)
題目描述: Special binary strings are binary strings with the following two properties: The number of 0’s is equal to the number of
【LeetCode】919. Complete Binary Tree Inserter 解題報告(Python)
題目描述: A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all node
【LeetCode】67. 二進位制求和
題目描述 給定兩個二進位制字串,返回他們的和(用二進位制表示)。 輸入為非空字串且只包含數字 1 和 0。 示例 輸入: a = “11”, b = “1” 輸出: “100” 輸入: a = “10
【LeetCode】98. Validate Binary Search Tree(C++)
地址:https://leetcode.com/problems/validate-binary-search-tree/ 題目: Given a binary tree, determine if it is a valid binary search tree (BST).
【LeetCode】95. Unique Binary Search Trees II(C++)
地址:https://leetcode.com/problems/unique-binary-search-trees-ii/ 題目: Given an integer n
【LeetCode】114. Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 / \ 2 5 / \ \ 3 4
【Leetcode】67.二進位制求和
給定兩個二進位制字串,返回他們的和(用二進位制表示)。 輸入為非空字串且只包含數字 1 和 0。 示例 1: 輸入: a = “11”, b = “1” 輸出: “100” 示例 2: 輸入: a = “1010”, b = “1011” 輸出:
【LeetCode】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, you
【LeetCode】965. Univalued Binary Tree 解題報告(Python & C++)
作者: 負雪明燭 id: fuxuemingzhu 個人部落格: http://fuxuemingzhu.cn/ 目錄 題目描述 題目大意 解題方法 BFS DFS 日期