id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "\n#include <string>\n#include <optional>\n#include <stdexcept>\n#include <vector>\n#include <set>\n\nenum class Token {\n SIGN,\n DECIMAL,\n EXPONENT,\n NUMBERS,\n};\n\nclass TokenIter {\n\n std::string s;\n std::string::const_iterator cur;\n std::optional<Token> last;\n\n static au...
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "class Solution {\npublic:\n bool isNumber(string s) {\n set<char> allowed_chars({'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '+', '-'});\n bool contains_digit = false, contains_digit_post_e = true;\n\n for(int i = 0; i < s.size(); i++)\n {\n if(!allowed_...
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "\n#include <string>\n#include <optional>\n#include <stdexcept>\n#include <vector>\n#include <set>\n\nenum class Token {\n SIGN,\n DECIMAL,\n EXPONENT,\n NUMBERS,\n};\n\nclass TokenIter {\n\n std::string s;\n std::string::const_iterator cur;\n std::optional<Token> last;\n\n static au...
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "\n#include <string>\n#include <optional>\n#include <stdexcept>\n#include <vector>\n#include <set>\n\nenum class Token {\n SIGN,\n DECIMAL,\n EXPONENT,\n NUMBERS,\n};\n\nclass TokenIter {\n\n std::string s;\n std::string::const_iterator cur;\n std::optional<Token> last;\n\n static au...
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "\n\nclass Solution {\npublic:\n bool isNumber(string s, int i = 0, bool number_found = false, bool negative_found = false, bool positive_found = false, bool dot_f = false, bool f_e = false) {\n if (s.size() <= i) return number_found;\n else if (s[i] == ' ') return isNumber(s, i + 1, number...
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "class Solution {\npublic:\n bool isNumber(string s, int i = 0, bool number_found = false, bool negative_found = false, bool positive_found = false, bool dot_f = false, bool f_e = false) {\n if (s.size() <= i) return number_found;\n else if (s[i] == ' ') return isNumber(s, i + 1, number_fou...
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "class Solution {\npublic:\n bool isNumber(string s, int i = 0, bool number_found = false, bool negative_found = false, bool positive_found = false, bool dot_f = false, bool f_e = false) {\n if (s.size() <= i) return number_found;\n else if (s[i] == ' ') return isNumber(s, i + 1, number_fou...
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "class Solution {\npublic:\n bool isNumber(string s, int i = 0, bool number_found = false, bool negative_found = false, bool positive_found = false, bool dot_f = false, bool f_e = false) {\n if (s.size() <= i) return number_found;\n else if (s[i] == ' ') return isNumber(s, i + 1, number_fou...
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "class Solution {\npublic:\n bool isNumber(string s, int i = 0, bool number_found = false, bool negative_found = false, bool positive_found = false, bool dot_f = false, bool f_e = false) {\n if (s.size() <= i) return number_found;\n else if (s[i] == ' ') return isNumber(s, i + 1, number_fou...
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "class Solution {\npublic:\n bool isNumber(string s) {\n if(s.empty()) return false;\n int n = s.size();\n\n int state = 0;\n vector<bool> finals({0, 0, 1, 0, 1, 0, 0, 1, 1}); // 合法的终止状态\n vector<vector<int> > transfer({\n { 0, 1, 2, 3, -1, -1},\n ...
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "class Solution {\npublic:\n bool isNumber(string s) {\n map<char,bool> mp;\n char temp = '0';\n for(int i = 0; i <= 9; i++){\n mp[temp + i] = true;\n }\n mp['e'] = true;\n mp['E'] = true;\n mp['.'] = true;\n mp['-'] = true;\n mp['...
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "class Solution {\npublic:\n bool isNumber(string s) {\n unordered_set<char> digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};\n \n unordered_set<char> signs = {'+', '-'};\n \n unordered_set<char> exponent = {'e', 'E'};\n \n unordered_set<char> ...
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "class Solution {\npublic:\n bool isValid3(string& s,int l,int r){\n if(l > r)return false;\n if(l == r && (s[l] == '+' || s[l] == '-'))return false;\n return true;\n }\n\n bool isValid1(string& s,int l,int r){\n if(l > r)return false;\n map<char,int> M;\n ...
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "class Solution {\npublic:\n bool isValid3(string& s, int l, int r) {\n return !(l > r || (l == r && (s[l] == '+' || s[l] == '-')));\n }\n\n bool isValid1(string& s, int l, int r) {\n map<char,int> M;\n int dotIndex = -1;\n for(int i = l; i <= r; i++) {\n M[s[...
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "class Solution {\npublic:\n bool isNumber(string s) {\n \n set<char> arr = {'1','2','3','4','5','6','7','8','9','+','-','e','.','0'};\n map<char,int> m;\n int ct = 0;\n int ct2 = 0;\n int ct3 = 0;\n int temp = 0;\n for(auto &it : s){\n i...
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "class Solution {\npublic:\n bool isNumber(string s) {\n if (s.empty())\n return false;\n \n auto tokens = split(s, {'e', 'E'});\n\n switch(tokens.size()) {\n case 2:\n return isValueOk(tokens[0]) && isInt(tokens[1]);\n case 1:\n...
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "class Solution {\npublic:\n bool isNumber(string s) {\n map<pair<int,char>,int> m;\n m[{1,'s'}] = 2;\n m[{1,'d'}] = 3;\n m[{1,'.'}] = 4;\n m[{2,'d'}] = 3;\n m[{2,'.'}] = 4;\n m[{3,'d'}] = 3;\n m[{3,'.'}] = 8;\n m[{3,'e'}] = 6;\n m[{4,...
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "class Solution {\npublic:\n bool isNumber(string s) {\n // This is the DFA we have designed above\n map<string, int> dfa[8] = {\n {{\"digit\", 1}, {\"sign\", 2}, {\"dot\", 3}},\n {{\"digit\", 1}, {\"dot\", 4}, {\"exponent\", 5}},\n {{\"digit\", 1}, {\"dot\"...
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "class Solution {\npublic:\n bool isNumber(string s) {\n map<string, int> dfa[8] = {{{\"digit\", 1}, {\"sign\", 2}, {\"dot\", 3}},\n {{\"digit\", 1}, {\"dot\", 4}, {\"exponent\", 5}},\n {{\"digit\", 1}, {\"dot\", 3}},\n ...
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "const regex pattern(\"^[+-]?([0-9]+\\\\.?[0-9]*|[0-9]*\\\\.?[0-9]+)([eE][+-]?[0-9]+)?$\");\n\nclass Solution {\npublic:\n bool isNumber(const string& __restrict s) {\n return regex_match(s, pattern);\n }\n};", "memory": "14000" }
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "#include <regex>\n\nconstexpr auto regexStr = R\"(^[+-]?(\\d+(\\.\\d*)?|\\.\\d+)([eE][+-]?\\d+)?$)\";\nstd::regex numberRegex(regexStr, std::regex::optimize);\n\nclass Solution {\npublic:\n bool isNumber(string s) {\n return std::regex_match(s, numberRegex);\n }\n};", "memory": "14100" }
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "class Solution {\npublic:\n bool isNumber(string s) {\n static const std::regex number_regex(R\"(^[+-]?(\\d+(\\.\\d*)?|\\.\\d+)([eE][+-]?\\d+)?$)\");\n return std::regex_match(s, number_regex);\n }\n};", "memory": "14200" }
65
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
3
{ "code": "class Solution {\npublic:\n bool isNumber(string s) {\n static const std::regex number_regex(\n R\"(^[+-]?(\\d+(\\.\\d*)?|\\.\\d+)([eE][+-]?\\d+)?$)\");\n return std::regex_match(s, number_regex);\n }\n};", "memory": "14200" }
66
<p>You are given a <strong>large integer</strong> represented as an integer array <code>digits</code>, where each <code>digits[i]</code> is the <code>i<sup>th</sup></code> digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain a...
0
{ "code": "class Solution final {\nprivate:\n static constexpr auto radix{10};\n\npublic:\n constexpr vector<int> plusOne(vector<int> &digits) const noexcept {\n auto carry{true};\n for (auto i{ssize(digits) - 1}; i >= 0 && carry; --i)\n if (++digits[i] < radix)\n carry =...
66
<p>You are given a <strong>large integer</strong> represented as an integer array <code>digits</code>, where each <code>digits[i]</code> is the <code>i<sup>th</sup></code> digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain a...
0
{ "code": "class Solution final {\nprivate:\n static constexpr auto radix{10};\n\npublic:\n constexpr vector<int> plusOne(vector<int> &digits) const noexcept {\n auto carry{true};\n for (auto i{ssize(digits) - 1}; i >= 0 && carry; --i)\n if (++digits[i] < radix)\n carry =...
66
<p>You are given a <strong>large integer</strong> represented as an integer array <code>digits</code>, where each <code>digits[i]</code> is the <code>i<sup>th</sup></code> digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain a...
0
{ "code": "class Solution {\npublic:\n vector<int> plusOne(vector<int>& digits) {\n\n for (int i=digits.size()-1; i >= 0; i--) {\n if (digits[i] != 9) {\n digits[i] += 1;\n return digits;\n }\n else\n digits[i] = 0;\n }\n\n...
66
<p>You are given a <strong>large integer</strong> represented as an integer array <code>digits</code>, where each <code>digits[i]</code> is the <code>i<sup>th</sup></code> digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain a...
0
{ "code": "class Solution {\npublic:\n vector<int> plusOne(vector<int>& digits) {\n \n if(digits.empty())\n return {};\n \n int size = digits.size();\n \n int carry = 0;\n\n digits[size - 1]++;\n \n for(int i=size - 1; i>= 0; i--)\n {...
66
<p>You are given a <strong>large integer</strong> represented as an integer array <code>digits</code>, where each <code>digits[i]</code> is the <code>i<sup>th</sup></code> digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain a...
0
{ "code": "class Solution {\npublic:\n vector<int> plusOne(vector<int>& digits) {\n vector<int> ret = digits;\n\n if (ret.back() != 9) {\n ret.back() = ret.back() + 1;\n } else {\n int currIndex = ret.size() - 1;\n while (currIndex >= 0 && ret[currIndex] == 9) ...
66
<p>You are given a <strong>large integer</strong> represented as an integer array <code>digits</code>, where each <code>digits[i]</code> is the <code>i<sup>th</sup></code> digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain a...
0
{ "code": "class Solution {\npublic:\n vector<int> plusOne(vector<int>& v) {\n int n = v.size();\n for(int i = n-1; i >= 0; i--){\n if(i == n-1)\n v[i]++;\n if(v[i] == 10){\n v[i] = 0;\n if(i != 0){\n v[i-1]++;\n ...
66
<p>You are given a <strong>large integer</strong> represented as an integer array <code>digits</code>, where each <code>digits[i]</code> is the <code>i<sup>th</sup></code> digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain a...
0
{ "code": "class Solution {\npublic:\n vector<int> plusOne(vector<int>& digits) {\n int n=0,temp=0,i,num=0;\n for(i=size(digits)-1;i>=0;i--){\n if(digits[i]<9){\n digits[i]=digits[i]+1;\n return digits;\n }\n digits[i]=0;\n }\n\n ...
66
<p>You are given a <strong>large integer</strong> represented as an integer array <code>digits</code>, where each <code>digits[i]</code> is the <code>i<sup>th</sup></code> digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain a...
0
{ "code": "class Solution {\npublic:\n vector<int> plusOne(vector<int>& digits) {\n int n = digits.size();\n vector<int> ans(n+1,0);\n vector<int> Final(n);\n if(digits[n-1] == 9){\n if(n != 1){\n ans[n] = 0;\n digits[n-2]+=1;\n ...
66
<p>You are given a <strong>large integer</strong> represented as an integer array <code>digits</code>, where each <code>digits[i]</code> is the <code>i<sup>th</sup></code> digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain a...
0
{ "code": "#include <vector>\n\nclass Solution {\npublic:\n std::vector<int> plusOne(std::vector<int>& digits) {\n // Start from the last digit and iterate backwards\n for (int i = digits.size() - 1; i >= 0; --i) {\n // If the current digit is less than 9, just increment and return the vec...
66
<p>You are given a <strong>large integer</strong> represented as an integer array <code>digits</code>, where each <code>digits[i]</code> is the <code>i<sup>th</sup></code> digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain a...
0
{ "code": "class Solution {\npublic:\n vector<int> plusOne(vector<int>& v) {\n int n = v.size();\n for(int i = n-1; i>=0;i--)\n {\n if(i == n-1) v[i]++;\n if(v[i] == 10)\n {\n v[i]= 0;\n if(i != 0){\n v[i-1]++;\n...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
0
{ "code": "class Solution final {\npublic:\n inline string addBinary(const string &a, const string &b) const noexcept {\n const auto aSize{size(a)}, bSize{size(b)};\n if (aSize < bSize)\n return addBinary(b, a);\n string result(aSize, '\\0');\n auto carry{false};\n for...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
0
{ "code": "class Solution final {\npublic:\n inline string addBinary(string &a, string &b) const noexcept {\n if (size(a) < size(b))\n return addBinary(b, a);\n\n auto carry{false};\n const auto lastA{rend(a)}, lastB{rend(b)};\n for (auto iterA{rbegin(a)}, iterB{rbegin(b)}; i...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
0
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string ans;\n int carry = 0;\n int i = a.size() - 1;\n int j = b.size() - 1;\n\n while (i >= 0 || j >= 0 || carry) {\n if (i >= 0)\n carry += a[i--] - '0';\n if (j >= 0)\n carry += b[j--] - '0';...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
0
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string result =\"\";\n int carry = 0;\n int i = a.size() - 1;\n int j = b.size() - 1;\n while(i >=0 || j >= 0 || carry)\n {\n if(i >= 0)\n {\n carry += a[i-...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
0
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string result = \"\";\n int carry = 0;\n int i = a.size() - 1;\n int j = b.size() - 1;\n \n // Iterate from the end of both strings\n while (i >= 0 || j >= 0 || carry) {\n //...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
0
{ "code": "class Solution {\n public:\n string addBinary(string a, string b) {\n string ans;\n int carry = 0;\n int i = a.length() - 1;\n int j = b.length() - 1;\n\n while (i >= 0 || j >= 0 || carry) {\n if (i >= 0)\n carry += a[i--] - '0';\n if (j >= 0)\n carry += b[j--] - '0'...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
0
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n int carry = 0;\n string ans;\n int t = 0;\n int n1=a.size(),n2=b.size();\n //*making the size same.*\n if (n1 > n2)\n while (t < n1 - n2)\n b.insert(b.begin(), '0'), t+...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
0
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string ans;\n int i = a.size()-1;\n int j = b.size()-1;\n int carry = 0;\n for(;i>=0 || j>=0 || carry > 0;){\n if(i>=0)\n carry += a[i--] - '0';\n if(j>=0)\n ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
0
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) \n {\n string res;\n int i = a.size() - 1, j = b.size() - 1, carry = 0;\n\n while (i >= 0 || j >= 0 || carry > 0)\n {\n carry += (i >= 0) ? (a[i] - '0') : 0;\n carry += (j >= 0) ? (b[j] ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
0
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string result = \"\";\n int carry = 0;\n int i = a.size() - 1, j = b.size() - 1;\n\n while (i >= 0 || j >= 0 || carry) {\n int sum_val = carry;\n\n if (i >= 0) {\n sum_val += a[i] - '0'; \n ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n int n = a.size();\n int m = b.size();\n reverse(a.begin(),a.end());\n reverse(b.begin(),b.end());\n if(n > m){\n int i = n - m;\n string s(i,'0');\n b = b + s;\n ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "\nclass Solution {\npublic:\n string addBinary(string a, string b) {\n int len_a = a.length();\n int len_b = b.length();\n int carry = 0;\n string result = \"\";\n\n while (len_a > 0 || len_b > 0 || carry) \n {\n len_a--;\n len_b--;\n ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n int na=a.length();\n int nb=b.length();\n int m=max(na,nb);\n a=string(m-na+1,'0')+a;\n b=string(m-nb+1,'0')+b;\n string ans(m+1,'0');\n int carry=0;\n for(int i=m;i>=0;i--){\n ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n int A = a.length();\n int B = b.length();\n string ans;\n int carry = 0;\n \n // Make both strings of equal length by padding with '0's on the left\n if (A < B) {\n a = string...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n int intC(char c) {\n return c-'0';\n }\n string addBi(string a, string b) {\n string ret;\n int n = b.size()-1;\n int m = a.size()-1;\n int tmp = 0;\n int i = 0;\n while (i <= n) {\n int tmpNum = intC(a[m-i]) +...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string arev = a; reverse(arev.begin(), arev.end());\n string brev = b; reverse(brev.begin(), brev.end());\n string ret = \"\";\n int carry = 0;\n for(int i = 0; i < max(arev.length(), brev.length()) +...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "#include <bits/stdc++.h> \nusing namespace std; \nclass Solution {\npublic:\n string addBinary(string a, string b) {\n \n if(a.length()>b.length())\n return addBinary(b,a);\n\n int diff =b.length()-a.length();\n\n string padding ;\n for(int i =0;i<diff;i++)\n {...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "#include <bits/stdc++.h> \nusing namespace std; \nclass Solution {\npublic:\n string addBinary(string a, string b) {\n \n if(a.length()>b.length())\n return addBinary(b,a);\n\n int diff =b.length()-a.length();\n\n string padding ;\n for(int i =0;i<diff;i++)\n {...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string lstr = a;\n string sstr = b;\n if (a.size() > b.size()) {\n lstr = a;\n sstr = b;\n } else {\n lstr = b;\n sstr = a;\n }\n int adder = 0;\n ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n while (a.length() > b.length()) {\n b.insert(0, \"0\");\n }\n while (b.length() > a.length()) {\n a.insert(0, \"0\");\n }\n\n vector<char> aStr;\n vector<char> bStr;\n\n ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n vector<int> v1, v2;\n reverse(a.begin(), a.end());\n reverse(b.begin(), b.end());\n if(b.size() > a.size()) \n swap(a, b);\n for(auto &c : a) \n v1.push_back(c - '0');\n f...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n deque<char> output;\n\n int i, j;\n\n i = a.size() - 1;\n j = b.size() - 1;\n\n int add = 0;\n\n while (i >= 0 || j >= 0 || add) {\n int sum = add;\n\n if (i >= 0) sum += ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n std::deque<char> ws;\n auto carry = 0;\n int i1 = a.size() - 1;\n int i2 = b.size() - 1;\n while (i1 >= 0 || i2 >= 0 || carry) {\n const auto sum = (i1 >= 0 ? a[i1] - '0' : 0) + (i2 >= 0 ? ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n std::deque<char> ws;\n auto carry = 0;\n int i1 = a.size() - 1;\n int i2 = b.size() - 1;\n while (i1 >= 0 || i2 >= 0 || carry) {\n const auto sum = (i1 >= 0 ? a[i1] - '0' : 0) + (i2 >= 0 ? ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n bool buf[10005];\n string addBinary(string a, string b) {\n int sz = 0, sza = a.size(), szb = b.size();\n int maxsz = max(sza, szb);\n int last = 0;\n int calc = 0;\n for (int i = 1; i <= maxsz; ++i) {\n calc = last;\n ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n vector<int> aa;\n vector<int> bb;\n for(int i=0; i< a.length(); i++)\n {\n int num = a[i] - '0';\n aa.push_back(num);\n \n }\n for(int i=0; i< b.length(); i++)\...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string result = \"\";\n string one = \"1\";\n string zero = \"0\";\n bool carryFlag = false;\n int i = 0;\n bool aBit, bBit;\n aBit = false;\n bBit = false;\n int alength ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n bool carry = false;\n string result;\n string sub; \n string ans;\n\n while (a.size() != 0 && b.size() != 0) {\n if (carry) result = binSum(\"1\" + string(1, a.back()) + string(1, b....
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n bool carry = false;\n string result;\n string sub; \n string ans;\n\n while (a.size() != 0 && b.size() != 0) {\n if (carry) result = binSum(\"1\" + string(1, a.back()) + string(1, b....
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string ans;\n int carry=0;\n int an, bn;\n unordered_map <char, int> intmap= {{'0',0}, {'1',1}};\n int i=a.size()-1,j=b.size()-1;\n while (i>=0 || j>=0){\n an=0; bn=0;\n i...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n std::stack<char> s;\n string res = \"\";\n int carry = 0;\n int i=0;\n for(i=0; i < a.length() || i < b.length(); i++) {\n int n0 = 0;\n int n1 = 0;\n\n if (i < a.leng...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n std::stack<char> s;\n string res = \"\";\n int carry = 0;\n int i=0;\n for(i=0; i < a.length() || i < b.length(); i++) {\n int n0 = 0;\n int n1 = 0;\n\n if (i < a.leng...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n std::stack<char> s;\n string res = \"\";\n int carry = 0;\n int i=0;\n for(i=0; i < a.length() || i < b.length(); i++) {\n int n0 = 0;\n int n1 = 0;\n\n if (i < a.leng...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n int carry=0,sum=0,p=0;\n string s;\n int i=a.length()-1,j=b.length()-1;\n\n while(i>=0&&j>=0){\n sum=carry+int(a[i])+int(b[j])-96;\n if(sum<2){\n carry=0;\n ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n\n\n string addBinary(string a, string b) {\n int n= a.length();\n int m= b.length();\n\n int carry=0;\n string res=\"\"; \n\n while(n>0 || m>0 || carry){\n\n int sum=0;\n\n if(n>0){\n sum+= a[n-1]-'0';\n ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string result = \"\";\n int carry = 0; // Initialize carry\n int i = a.size() - 1, j = b.size() - 1; // Pointers to the end of both strings\n\n // Traverse both strings from right to left\n while (i ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string result =\"\";\n int carry = 0;\n int i = a.size() - 1;\n int j = b.size() - 1;\n while(i >=0 || j >= 0 || carry)\n {\n int sum = carry;\n if(i >= 0)\n {\...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string res;\n int sum = 0, carry = 0;\n int i = a.size() - 1, j = b.size() - 1;\n while (i >= 0 && j >= 0) {\n sum = carry + (a[i] - '0') + (b[j] - '0');\n i--, j--;\n res +=...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n int c1 = a.length() - 1;\n int c2 = b.length() - 1;\n\n int carry = 0;\n string sum;\n\n while(c1 >= 0 || c2 >= 0 || carry)\n {\n if(c1 >= 0)\n {\n carry += a...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n std::string addBinary(std::string a, std::string b) {\n std::string result = \"\";\n int carry = 0; // Biến lưu phần nhớ khi cộng các bit\n int i = a.size() - 1, j = b.size() - 1;\n\n // Duyệt từ cuối chuỗi tới đầu\n while (i >= 0 || j >= 0 || carry) {\n ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n int m=a.size();\n int n=b.size();\n int carry=0;\n int i=0;\n string ans=\"\";\n while(i<m || i<n || carry!=0 ){\n int x=0;\n if(i<m && a[m-i-1]=='1'){\n x=...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n uint a_len = a.size();\n uint b_len = b.size();\n\n int ii = a_len - 1;\n int jj = b_len - 1;\n\n string ans{};\n\n int carry = 0;\n\n while (ii >=0 || jj >= 0)\n {\n i...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "#include <iostream>\n#include <string>\n#include <algorithm> // for reverse\n\nusing namespace std;\n\nclass Solution {\npublic:\n string addBinary(string a, string b) {\n string result = \"\";\n int carry = 0;\n int i = a.size() - 1;\n int j = b.size() - 1;\n\n // Tra...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n int carry = 0;\n stack<int> ansQ, A, B;\n for(auto it : a)\n A.push(it);\n for(auto it : b)\n B.push(it);\n\n while(!A.empty() || !B.empty()) {\n char v1 = '0', v2 = '...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string result = \"\";\n int i = a.size() - 1;\n int j = b.size() - 1;\n int carry = 0;\n\n \n while (i >= 0 || j >= 0 || carry == 1) {\n int sum = carry;\n\n if (i >= 0) s...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n int n = a.length();\n int m = b.length();\n int i = n-1, j = m-1, carry = 0;\n string ans;\n while(i >= 0 || j >= 0 || carry){\n int sum = 0;\n if(i >= 0){\n s...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string res;\n int i = a.size() - 1;\n int j = b.size() - 1;\n int carry = 0;\n\n while (i >= 0 || j >= 0 || carry) {\n int bitA = (i >= 0) ? a[i] - '0' : 0;\n int bitB = (j >= 0)...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string res;\n int i = a.size() - 1;\n int j = b.size() - 1;\n int carry = 0;\n\n while (i >= 0 || j >= 0 || carry) {\n int bitA = (i >= 0) ? a[i] - '0' : 0;\n int bitB = (j >= 0)...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string result = \"\";\n int i = a.size() - 1;\n int j = b.size() - 1;\n int carry = 0;\n \n while (i >= 0 || j >= 0 || carry == 1) {\n int sum = carry;\n if (i >= 0) sum +...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
2
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n int i = a.size() - 1;\n int j = b.size() - 1;\n bool carry = false;\n string result = \"\";\n \n while (i >= 0 || j >= 0 || carry) {\n int sum = carry;\n \n if ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
3
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n int carry = 0;\n int sum = 0;\n int a_int;\n int b_int;\n string answer = \"\";\n\n while((a.size() != 0 || b.size()!=0) || carry ==1){\n if (a.size() == 0){\n a_int =...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
3
{ "code": "/*\nwalk through the strings from right to left and construct a new string\n\nwhat are the cases?\n\na b c r nc\n0 0 1 1 0\n1 0 1 0 1\n1 1 1 1 1\n0 0 0 0 0\n0 1 0 1 0\n1 0 0 1 0\n1 1 0 0 1\n\n*/\n\nclass Solution {\npublic:\n\n char giveMeNextChar(char a, char b, bool& carry){\n if (carry){\n ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
3
{ "code": "/*\nwalk through the strings from right to left and construct a new string\n\nwhat are the cases?\n\na b c r nc\n0 0 1 1 0\n1 0 1 0 1\n1 1 1 1 1\n0 0 0 0 0\n0 1 0 1 0\n1 0 0 1 0\n1 1 0 0 1\n\n*/\n\nclass Solution {\npublic:\n\n char giveMeNextChar(char a, char b, bool& carry){\n if (carry){\n ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
3
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n int ai = a.length()-1;\n int bi = b.length()-1;\n string res;\n\n int c = 0;\n while (ai >= 0 || bi >= 0) {\n int anum = ai >= 0 ? (a[ai]-'0') : 0;\n int bnum = bi >= 0 ? (b[bi]-...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
3
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n int i = a.length()-1;\n int j = b.length()-1;\n int count = 0;\n string ans = \"\";\n while(i >= 0 && j>= 0){\n int x = a[i] - '0';\n int y = b[j] - '0';\n int sum = c...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
3
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n int c = a.length();\n int d = b.length();\n if (c < d) {\n a.insert(0, d - c, '0');\n } else {\n b.insert(0, c - d, '0');\n } \n string result = \"\";\n char carry = '0';\n \n for (int i = a....
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
3
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string ans;\n int i = a.size() - 1;\n int j = b.size() - 1;\n int carry = 0;\n while (i >= 0 && j >= 0) {\n if (a[i] - 48 + b[j] - 48 + carry == 1) {\n carry = 0;\n ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
3
{ "code": "class Solution {\npublic:\n\n unsigned long long convertTolong(string s){\n unsigned long long val = 0, multiple = 1, n = s.length();\n for(unsigned long long i = 0; i < n; i++)\n {\n val += (s[n - i - 1]=='1'?1:0) * multiple;\n multiple *= 2;\n }\n ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
3
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n \n \n int n=a.size();\n int m=b.size();\n \n int count=0;\n string ans;\n while(n>0 && m>0)\n {\n if(a[n-1]=='1' && b[m-1]=='1')\n {\n ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
3
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n int m=a.length()-1;\n int n=b.length()-1;\n string ans;\n int carry=0;\n while(m>=0&&n>=0)\n {\n int s1=a[m]-'0';\n int s2=b[n]-'0';\n cout<<s1<<s2<<carry<<endl;\n ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
3
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string s = \"\";\n int carry = 0;\n\n int i = a.size() - 1;\n int j = b.size() - 1;\n\n while (i >= 0 || j >= 0) {\n int x = (i >= 0 ? a[i] - '0' : 0);\n int y = (j >= 0 ? b[j] -...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
3
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n int N = a.size(), M = b.size();\n int i = N-1, j = M-1;\n int carry = 0;\n //int sum = 0;\n string ans;\n\n while (i >= 0 || j >= 0) {\n int sum = (i >= 0 ? (a[i] - '0') : 0) + (j >=...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
3
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string res = \"\";\n int i = a.size() - 1, j = b.size() - 1, carry = 0;\n while(i >= 0 || j >= 0 || carry){\n if(i >= 0){\n carry += a[i] - '0';\n i--;\n }\n ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
3
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string ans = \"\";\n int i = a.length() - 1;\n int j = b.length() - 1;\n int carry = 0;\n while(i>= 0 || j >= 0){\n int sum = carry;\n if(i >= 0){\n sum+= (a[i] - ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
3
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n int alen=a.length();\n int blen=b.length();\n int i=0, carry=0;\n string ans=\"\";\n\n while(i<alen || i<blen || carry !=0){\n int x=0;\n if(i<alen && a[alen-i-1]=='1')\n ...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
3
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string r=\"\";\n int carry=0,i=a.size()-1,j=b.size()-1;\n while(i>=0 || j>=0 || carry)\n {\nint sum=carry+(i>=0 ? a[i]-'0':0)+(j>=0 ? b[j]-'0':0);\nr=to_string(sum%2)+r;\ncarry=sum/2;\ni--;\nj--;\n }\n return r;...
67
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
3
{ "code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string str=\"\";\n int l1=a.length();int l2=b.length();\n int i=l1-1;int j=l2-1;int carry=0;\n while(i>=0 || j>=0 || carry>0)\n {\n int sum=carry;\n if(i>=0)\n {\n sum=sum+a[i...