id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
538 | <p>Given the <code>root</code> of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.</p>
<p>As a reminder, a <em>binary search tree</em> is a tree that satisfies these constraints... | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod... |
538 | <p>Given the <code>root</code> of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.</p>
<p>As a reminder, a <em>binary search tree</em> is a tree that satisfies these constraints... | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod... |
538 | <p>Given the <code>root</code> of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.</p>
<p>As a reminder, a <em>binary search tree</em> is a tree that satisfies these constraints... | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod... |
538 | <p>Given the <code>root</code> of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.</p>
<p>As a reminder, a <em>binary search tree</em> is a tree that satisfies these constraints... | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod... |
538 | <p>Given the <code>root</code> of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.</p>
<p>As a reminder, a <em>binary search tree</em> is a tree that satisfies these constraints... | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod... |
538 | <p>Given the <code>root</code> of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.</p>
<p>As a reminder, a <em>binary search tree</em> is a tree that satisfies these constraints... | 3 | {
"code": "class Solution {\npublic:\nvector<int> v;\nvoid helper(TreeNode* root){\n if (!root) return ;\n helper(root->left);\n v.push_back(root->val);\n helper(root->right);\n}\nvoid inorder(TreeNode* root,map<int,int>& mp){\n if (!root) return;\n inorder(root->left,mp);\n root->val=mp[root->va... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\nprivate:\n int n, target;\n\n void helper(string &num, string &expression, int idx, long long current, long long num1, long long num2, char sign, bool multiply, vector<string> &res) {\n // Extend the current number (num2)\n num2 = num2 * 10 + (num[idx] - '0');\n ... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n vector<string> addOperators(string num, int target) {\n vector<string> result;\n string expression;\n backtrack(num,target,0,0,0,expression,result);\n return result;\n }\nprivate:\n void backtrack(const string& num,int target,int index,long l... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "int speedUp = [] {\n std::ios::sync_with_stdio(0);\n std::cin.tie(0);\n return 0;\n}();\n\n#define opt() ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n\nclass Solution {\n using ll = long long;\npublic:\n string num;\n vector<string> ret;\n int n, target;\n\n // Depth-first searc... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": " class Solution {\n typedef long long int i64;\n \n string myS;\n const char* s;\n i64 target;\n int slen;\n \n public:\n \n vector<string> addOperators(const string& num, int t) {\n myS = num;\n slen = myS.size();\n ... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n\n string join(vector<string>& vs) {\n string res = \"\";\n for (int i = 1; i < vs.size(); i++) res += vs[i];\n return res;\n }\n\n void recurse(string& num, int index, int target, vector<string>& curr, long long value, long long currOperand, long long lastOperand, ve... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\n vector<string> output;\npublic:\n vector<string> addOperators(string num, int target) {\n string expression;\n expression.reserve(2 * num.size()); // 預分配空間\n DFS(num, (long long)target, 0, 0, 0, expression);\n return output;\n }\n \nprivate:\n v... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n void solve(vector<string> &ans, string &path, string &num, int target, int pos, long long curr, long long prev)\n {\n if (pos == num.size())\n {\n if (curr == target)\n {\n ans.push_back(path);\n }\n ... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n void solve(vector<string>& ans, string& path, string& num, int target,\n int pos, long long curr, long long prev) {\n if (pos == num.size()) {\n if (curr == target) {\n ans.push_back(path);\n }\n return;\n ... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n void solve(int ind, long long cur, string &num, int target, vector<string> &ans, string &temp, long long last) {\n if (ind == num.size()) {\n if (cur == target) {\n ans.push_back(temp);\n }\n return;\n }\n \... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\nprivate:\n\tvector<long long> nums;\n\tvector<string> mem;\n\tvector<string> answer;\n\tint target;\n\tlong long sum = 0;\npublic:\n\tvoid add()\n\t{\n\t\tstring ans;\n\t\tans.reserve(nums.size() * 2);\n\t\tfor (int i = 0; i < (int)nums.size() - 1; i++)\n\t\t{\n\t\t\tans.append(to_string(... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\nprivate:\n string toStr(vector<string> expr) {\n string out = \"\";\n for (int i = 0; i < expr.size(); i++) {\n out += expr[i]; \n }\n return out;\n }\n\n void calculate(string& num, int start, int target, long curr, long prevTerm, vector<st... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n\nvector<string> func(int i, long long cursum, long long curr, long long tar, string s)\n{\n string p;\n int n = s.size();\n vector<string>ans;\n long long d = 0;\n if(i == n)\n {\n if(cursum == tar)\n {\n return {{\"\"}};\n }\n ... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n vector<string> result;\n int target;\n\n void addResult(vector<long long> &digits, vector<char> &ops){\n string s = \"\";\n for(int i=0; i < digits.size()-1; i++){\n s += to_string(digits[i]) + ops[i];\n }\n s += to_string(digits.b... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n\nvector<string> func(int i, long long cursum, long long curr, long long tar, string s)\n{\n string p;\n int n = s.size();\n vector<string>ans;\n long long d = 0;\n if(i == n)\n {\n if(cursum == tar)\n {\n return {{\"\"}};\n }\n ... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "/*\n Brute force -> try every single operator at every single between\n position in the array, and for each possible solution, evaluate\n\n Lots of annoyance -> doable.\n\n Order of operations land:\n A*B*C +D+E+ F*H*I*J\n\n A+B+ => waitingAdd = (A+B)\n\n waitingAdd = a, wa... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n \n int n;\n int targetSum;\n vector<string> addOperators(string num, int target) {\n \n n = num.size();\n targetSum = target;\n\n string currAns = \"\";\n vector<string> ans;\n\n getAns(0, 404, 0, num, currAns, ans);\n\n ... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n vector<string> addOperators(string num, int target) {\n vector<string> res;\n int opts = 1 << (num.size() - 1);\n\n for(int mask = 0; mask < opts; mask++)\n {\n vector<long long> vals;\n long long val = num[0] - '0', pos = 0;\... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n vector<string> addOperators(string num, int target) {\n vector<string> exps;\n string exp = \"\";\n dfs(num, exps, exp, 0, 0, 0, target);\n return exps;\n }\n\nprivate:\n void dfs(const string& num, vector<string>& exps, string& exp, int star... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n // opt1\n // backtrack\n // time=4^n\n vector<string> results;\n\n auto eval(const string& expression) {\n auto res = 0ll;\n auto prev = 0ll;\n auto curr = 0ll;\n auto idx = 0ll;\n auto nextNegative = false;\n auto mult = ... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\n typedef long long ll;\n void dfs(int pos, string &num, string &curr, ll tar, ll last_mul, vector<string> &ans) {\n if(pos == num.size()) {\n if(last_mul == tar) ans.push_back(curr);\n return;\n }\n\n string curr_num;\n for (int j = ... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n vector<string> expressions;\n\n void generateExpressions(int index, int n, string path, string currentNumStr, long processedValue, long currentNum, string num, int target) {\n // Base case: All digits have been processed\n if (index == n) {\n // If... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n int atoi(const string s)\n {\n long long tmp = 0;\n for (auto c : s)\n {\n tmp *= 10;\n tmp += c-'0';\n if (tmp > INT_MAX)\n return -1;\n }\n return tmp;\n }\n bool check(const string&... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\n typedef long long ll;\n void dfs(int pos, string &num, string &curr, ll tar, ll last_mul, ll last_num, vector<string> &ans) {\n if(pos == num.size()) {\n if(last_mul == tar) ans.push_back(curr);\n return;\n }\n\n auto ncurr = curr;\n ... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\n typedef long long ll;\n void dfs(int pos, string &num, string &curr, ll tar, ll last_mul, ll last_num, vector<string> &ans) {\n if(pos == num.size()) {\n if(last_mul == tar) ans.push_back(curr);\n return;\n }\n\n auto ncurr = curr;\n ... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "\nclass Solution {\npublic:\n\n\nlong long calc(string& s){\n long long ans = 0;\n long long ls = 1;\n long long nu = 0;\n for(int i=0; i<s.size(); i++){\n if(s[i]>='0' && s[i]<='9'){\n nu *= 10;\n nu += s[i] - '0';\n } else if (s[i] == '*') {\n ls... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "\nclass Solution {\npublic:\n\n\nlong long calc(string& s){\n long long ans = 0;\n long long ls = 1;\n long long nu = 0;\n for(int i=0; i<s.size(); i++){\n if(s[i]>='0' && s[i]<='9'){\n nu *= 10;\n nu += s[i] - '0';\n } else if (s[i] == '*') {\n ls... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "\nclass Solution {\npublic:\n\n\nlong long calc(string& s){\n // vector<long long> nums;\n long long ans = 0;\n long long ls = 1;\n long long nu = 0;\n for(int i=0; i<s.size(); i++){\n if(s[i]>='0' && s[i]<='9'){\n nu *= 10;\n nu += s[i] - '0';\n } else if... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n void Recur(string rem_str, string op_string, long long eval, long long to_add, int target, vector<string>& ans) {\n if (rem_str.empty()) {\n if (eval + to_add == target) {\n ans.push_back(op_string);\n }\n return;\n ... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n vector<string> addOperators(string num, int target) {\n // DFS\n // accumulated num, cur num\n vector<string> res;\n for(int i = 1; i <= num.size(); i++) {\n if (num[0] == '0' && i > 1) break;\n recur(res, 0, atol(num.substr(0... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\n \nprivate:\n int target;\n string num;\n void dfs(int pos, long last, long sum, string path, vector<string>& res){\n \n if (pos == num.size())\n {\n if (sum == target)\n {\n res.push_back(path);\n }\n ... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n int target;\n string num;\n vector<string> addOperators(string num, int target) {\n vector<string> res;\n this->target = target;\n this->num = num;\n dfs(0, \"\", 0, 0, res);\n return res;\n }\n\n void dfs(int i, string path, lon... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n // This is the recursive helper function\n void aORec(string num, int target, vector<string>& ans, int ind, string curr, long prev, long res) {\n // Base case: if we've reached the end of the string\n if (ind == num.size()) {\n // Check if the curr... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n\n\nvoid helper( string s,int index,int target,int prev,long sum,string temp,vector<string>&ans){\nif(s.size()==index){\n\n // cout<<temp<<','<<sum<<' ';\n\n if(target==sum)\n ans.push_back(temp);\n return ;\n}\n\n\n long num=0;\n string curr=\"\";\n\nfor(int i=index;... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n\n\nvoid helper( string s,int index,int target,int prev,long sum,string temp,vector<string>&ans){\nif(s.size()==index){\n\n // cout<<temp<<','<<sum<<' ';\n\n if(target==sum)\n ans.push_back(temp);\n return ;\n}\n\n\n long num=0;\n string curr=\"\";\n\nfor(int i=index;... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n vector<string> addOperators(string num, int target) {\n vector<string> ans;\n recursion(ans, num, target, \"\", 0, 0, 0 );\n return ans;\n }\n \n void recursion(vector<string>& ans, const string& num, const int& target, string expression, int idx... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 0 | {
"code": "class Solution {\npublic:\n // This is the recursive helper function\n void aORec(string num, int target, vector<string>& ans, int ind, string curr, long prev, long res) {\n // Base case: if we've reached the end of the string\n if (ind == num.size()) {\n // Check if the curr... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 1 | {
"code": "class Solution {\npublic:\n typedef long long ll;\n\n void f(int idx, string& num, int& target, ll val, ll pv, string ds, vector<string>& res) {\n if (idx == num.size()) {\n if (val == target) {\n res.push_back(ds);\n }\n return;\n }\n\n ... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 1 | {
"code": "class Solution {\npublic:\n vector<string> ans;\n\n void helper(string s, int target,int i, const string path, long eval, long residual) {\n // bc\n if(i==s.length()){\n if(eval==target){\n ans.push_back(path);\n return;\n }\n ... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 1 | {
"code": "class Solution {\npublic:\n vector<string> ans;\n string s;\n int target;\n vector<string> addOperators(string s, int target) {\n this->s = s;\n this->target = target;\n backtrack( 0, \"\", 0, 0);\n return ans;\n }\n void backtrack(int i, const string& path, lo... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 1 | {
"code": "class Solution {\npublic:\n vector<string> ans;\n \n vector<string> addOperators(string s, int target) {\n helper(s,target, 0, \"\", 0, 0);\n return ans;\n }\n void helper(string s, int target,int i, const string& path, long eval, long residual) {\n // bc\n if(i==... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 2 | {
"code": "class Solution {\npublic:\n vector<string> addOperators(string num, int target) {\n vector<string> ans;\n long long dig = 0;\n for (int i = 0; i < num.size(); i++) {\n dig = dig * 10 + num[i]-'0';\n addOpHelper(num, i+1, dig, to_string(dig), target, ans, dig, 1... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 2 | {
"code": "class Solution {\n\n void help(string num, int target, int i, long sum, string temp, vector<string> &ans, long prevNo){\n if(i == num.size()){\n if(sum == target){\n ans.push_back(temp);\n return;\n }\n }\n for(int j = i; j<num.siz... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 2 | {
"code": "class Solution {\npublic:\n void helper(string &num, int target, long long sum, long long prev, int i, string curr, vector<string> &ans) {\n if (i >= num.size()) {\n if (sum == target) {\n ans.push_back(curr);\n }\n return;\n }\n\n for... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 2 | {
"code": "class Solution {\npublic:\n vector<string> addOperators(string num, int target) {\n vector<string> result;\n if (num.empty()) return result;\n helper(result, num, \"\", target, 0, 0, 0);\n return result;\n }\n void helper(vector<string>& result, const string& num, strin... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 2 | {
"code": "class Solution {\npublic:\n void helper(vector<string>& res, const string& num, string expr, int pos, long prevOperand, long currentValue, int target) {\n if (pos == num.size()) {\n if (currentValue == target) {\n res.push_back(expr);\n }\n return;\... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 2 | {
"code": "#include <string>\n#include <vector>\nusing namespace std;\n\nclass Solution {\npublic:\n vector<string> addOperators(string num, int target) {\n vector<string> result;\n helper(result, \"\", num, target, 0, 0, 0);\n return result;\n }\n\nprivate:\n void helper(vector<string>&... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 3 | {
"code": "class Solution {\npublic:\n Solution(){\n ios_base :: sync_with_stdio(false);\n }\n void solve(string num, int target, string helper, int index, long long curValue, long long prevValue, vector<string>& ans) {\n if (index == num.size()) {\n if (curValue == target) {\n ... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 3 | {
"code": "class Solution {\n string s;\n int target;\n vector<string>res;\n\n void helper(int idx = 0, long long score = 0, string curr = \"\", long long prev_num = -1) {\n if (idx == s.length()) {\n if (score == target) res.push_back(curr);\n return;\n }\n long... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 3 | {
"code": "class Solution {\npublic:\nvector<string>sa;\nstring s;\n \n void help(string num,int target,int i,long long prev,long long cur){\n if(i==num.size()){\n if(cur==target){\n sa.push_back(s);\n }\n return;\n }\n for(int start=i;start<num.size();start++){\n i... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 3 | {
"code": "class Solution {\nprivate:\n vector<string> answer;\npublic:\n void dfs(string num, int target, int idx, long long curr_val, long long prev_val, string path) {\n if(idx == num.size()) {\n if(curr_val == target) {\n answer.push_back(path);\n }\n r... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 3 | {
"code": "class Solution {\npublic:\n vector<string>ans;\n void helper(int ind,long long prev,long long sum,string &s,int k, string temp)\n {\n if(ind==s.length())\n {\n if(sum==k)\n {\n ans.push_back(temp);\n }\n return;\n }\n ... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 3 | {
"code": "class Solution {\npublic:\n vector<string> ans;\n vector<string> addOperators(string num, int target) {\n dfs(0,0,0,\"\",num,target);\n return ans;\n }\n\n void dfs(int idx,long long pre,long long sum,string path,string num,int target){\n if(idx==num.size()){\n i... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 3 | {
"code": "class Solution {\npublic:\n vector<string> addOperators(string num, int target) {\n vector<string> res;\n if (num.empty()) return res;\n\n for (int i = 1; i <= num.size(); i++) {\n string s = num.substr(0, i);\n long cur = stol(s);\n if (to_string(cu... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 3 | {
"code": "class Solution {\npublic:\n vector<string> addOperators(string num, int target) {\n vector<string> res;\n if (num.empty()) return res;\n\n for (int i = 1; i <= num.size(); i++) {\n string s = num.substr(0, i);\n long cur = stol(s);\n if (to_string(cu... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 3 | {
"code": "class Solution {\npublic:\n void backtrack(vector<string> &res, const string &num, const int target, string cur, int pos, const long cv, const long pv,\n const char op) {\n // cur: {string} expression generated so far.\n // pos: {int} current visiting position of num.\n // cv: {lon... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 3 | {
"code": "class Solution {\nprivate:\n // cur: {string} expression generated so far.\n // pos: {int} current visiting position of num.\n // cv: {long} cumulative value so far.\n // pv: {long} previous operand value.\n // op: {char} previous operator used.\n void dfs(std::vector<string>&... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 3 | {
"code": "class Solution {\npublic:\n vector<string> addOperators(string num, int target) {\n vector<string> ans;\n f(num, target, ans, 0, \"\", 0, 0);\n return ans;\n }\n\n void f(string num, int target, vector<string>& ans, int ind, string asf, long prev, long curr){\n if(ind =... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 3 | {
"code": "// class Solution {\n// public:\n// void check(string num, int target, vector<string>& result, string curr_res, int curr_index, int current_sum, int prev)\n// {\n// if(curr_index>=num.size())\n// {\n// if(current_sum == target)\n// result.push_back(curr_r... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 3 | {
"code": "class Solution {\npublic:\n vector<string> addOperators(string num, int target) {\n vector<string> ans;\n f(num, target, ans, 0, \"\", 0, 0);\n return ans;\n }\n\n void f(string num, int target, vector<string>& ans, int ind, string asf, long prev, long curr){\n if(ind =... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 3 | {
"code": "class Solution {\npublic:\n vector<string> addOperators(string num, int target) {\n vector<string> ans;\n f(num, target, ans, 0, \"\", 0, 0);\n return ans;\n }\n\n void f(string num, int target, vector<string>& ans, int ind, string asf, long prev, long curr){\n if(ind =... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 3 | {
"code": "class Solution {\npublic:\n vector<string> addOperators(string num, int target) {\n\tvector<string> res;\n\tdfs(num, 0, target, \"\", 0, 0, res);\n\treturn res;\n}\n\nvoid dfs(string &num, int start, int target, string path, long prev, long cur, vector<string> &res) {\n\tif(start == num.size() && prev +... |
282 | <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>'+'</code><em>, </em><code>'-'</code><em>, and/or </em><code>'*'</code><em> between the digits of </em><code>nu... | 3 | {
"code": "class Solution {\npublic:\n \n // recursion\n \n vector<string> res;\n \n void dfs(string num, int target, int ind, string path, long prev, long cur) {\n if(ind == num.size() && cur + prev == target) {\n res.push_back(path);\n }\n \n for(int i=1; ind... |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 0 | {
"code": "class Solution {\npublic:\n void moveZeroes(vector<int>& nums) {\n int n=nums.size();\n int l=0;\n int r=0;\n while(r<n)\n {\n if(nums[l]==0)\n {\n while(r<n && nums[l]==0)\n {\n if(nums[r]!=0)\n ... |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 0 | {
"code": "class Solution {\npublic:\n void moveZeroes(vector<int>& nums) {\n int n = nums.size();\n int zeroInd = -1;\n\n for(int i=0; i<n; i++){\n if(nums[i]==0){zeroInd = i;break;}\n }\n int j = zeroInd + 1;\n\n for(; j<n; j++){\n if(nums[j]!=0 and... |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 0 | {
"code": "class Solution {\npublic:\n void moveZeroes(vector<int>& nums) {\n\n // if(nums.size() == 1)\n for(int i = 0; i < nums.size(); i++) {\n for(int j = i+1; j < nums.size(); j++) {\n if(nums[i] == 0) {\n swap(nums[i], nums[j]);\n }\n ... |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 0 | {
"code": "class Solution {\npublic:\n void moveZeroes(vector<int>& nums) {\n int n=nums.size();\n int i=0;\n for(int j=0;j<n;j++){\n if(nums[j]!=0){\n swap(nums[i],nums[j]);\n i++;\n }\n \n }\n \n }\n};",
"memor... |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 0 | {
"code": "class Solution {\npublic:\n void moveZeroes(vector<int>& nums) {\n int nonZero = 0;\n for(int j=0; j<nums.size(); j++){\n if(nums[j] != 0){\n swap(nums[j], nums[nonZero]);\n nonZero++;\n }\n }\n }\n};",
"memory": "21700"
} |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 0 | {
"code": "class Solution {\npublic:\n void moveZeroes(vector<int>& nums) {\n int last = 0;\n \n for (int i = 0; i < nums.size(); i++) {\n if (nums[i] != 0) {\n swap(nums[i],nums[last]);\n last++;\n }\n }\n }\n};\n",
"memory": "21... |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 0 | {
"code": "class Solution {\npublic:\n void moveZeroes(vector<int>& nums) {\n int n = nums.size();\n int startIdx = 0;\n for(int i=0;i<n;i++){\n if(nums[i]!=0){\n nums[startIdx] = nums[i];\n startIdx++;\n }\n }\n \n while... |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 0 | {
"code": "class Solution {\npublic:\n void moveZeroes(vector<int>& nums) {\n int n = nums.size();\n int startIdx = 0;\n for(int i=0;i<n;i++){\n if(nums[i]!=0){\n swap(nums[startIdx++], nums[i]);\n }\n }\n \n return;\n }\n};",
"mem... |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 1 | {
"code": "class Solution {\npublic:\n void moveZeroes(vector<int>& nums) {\n int left = 0;\n\n for (int right = 0; right < nums.size(); right++) {\n if (nums[right] != 0) {\n swap(nums[right], nums[left]);\n left++;\n }\n } \n }\n}... |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 1 | {
"code": "class Solution {\npublic:\n void moveZeroes(vector<int>& nums) {\n int n = nums.size(),i=0;\n while(n--){\n if(nums[i]==0) {\n \n nums.erase(nums.begin() + i);\n nums.push_back(0);\n }\n else i++;\n }\n ... |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 2 | {
"code": "class Solution {\npublic:\n void moveZeroes(vector<int>& nums) {\n // We are shifting all non-zero elements to the left\n\n int nonZeroIndex = 0; // This will keep track of the position of non-zero elements\n\n for(int i=0; i<nums.size(); i++){\n if(nums[i] != 0){\n ... |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 2 | {
"code": "class Solution {\npublic:\n void moveZeroes(vector<int>& nums) {\n int i = 0;\n for(int j = 0; j < nums.size(); j++) {\n if(nums[j] != 0) swap(nums[i++], nums[j]);\n }\n }\n};",
"memory": "22000"
} |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 3 | {
"code": "class Solution {\npublic:\n void moveZeroes(vector<int>& nums) {\n stack<int>st;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==0){\n st.push(nums[i]);\n nums.erase(nums.begin()+i);\n i--;\n }\n }while(!st.empty()){\n nums.push_bac... |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 3 | {
"code": "class Solution {\npublic:\n void moveZeroes(std::vector<int>& nums) {\n stable_partition(nums.begin(), nums.end(), [](int x) { return x != 0; });\n }\n};",
"memory": "22100"
} |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 3 | {
"code": "class Solution {\npublic:\n void moveZeroes(vector<int>& nums) {\n int n=nums.size(),c=0;\n vector<int> temp(n);\n for(int i=0;i<n;i++){\n if(nums[i]!=0){\n temp[c]=nums[i];\n c++;\n }\n }\n while(c<n){\n temp[c]=0;\n c++;\n }\n std:... |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 3 | {
"code": "class Solution {\npublic:\n void moveZeroes(vector<int>& nums) {\n int n = nums.size();\n vector<int> arr(n, 0); // Create a vector with the same size and initialize with 0\n int j = 0;\n for (int i = 0; i < n; i++) {\n if (nums[i] != 0) {\n arr[j] =... |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 3 | {
"code": "class Solution {\npublic:\n void moveZeroes(vector<int>& nums) {\n\n //brute approach TC=O(2N) sc=O(N) extra space\n int j,n=nums.size();\n vector<int> arr(n,0);\n j=0;\n for(int i=0;i<n;++i)\n {\n if(nums[i])\n {\n arr[j++]=... |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 3 | {
"code": "#include<bits/stdc++.h>\nclass Solution {\npublic:\n vector<int> moveZeroes(vector<int>& nums) {\n int n = nums.size();\n int j =-1;\n for(int i =0;i<n;i++){\n if(nums[i]==0){\n j=i;\n break;\n }\n }\n if(j==-1) retur... |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 3 | {
"code": "class Solution {\npublic:\n void moveZeroes(vector<int>& nums) {\n int n = nums.size();\n vector<int> neww(n,0);\n int j = 0;\n\n for(int i=0;i<n;i++){\n if(nums[i] != 0){\n neww[j] = nums[i];\n j++;\n }\n }\n\n ... |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 3 | {
"code": "class Solution {\npublic:\n void moveZeroes(vector<int>& nums) {\n int n=nums.end()-nums.begin();\n queue<int> q;\n int i=0;\n int j=0;\n while(i<n){\n if(nums[i]!=0){\n q.push(nums[i]);\n j++;\n }\n i++;\n... |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 3 | {
"code": "class Solution {\npublic:\n void moveZeroes(vector<int>& nums) {\n queue<int> q;\n\n for(int i = 0; i < nums.size(); i++) {\n if(nums[i] == 0) {\n q.push(i);\n }\n else if(!q.empty()){\n nums[q.front()] = nums[i];\n ... |
283 | <p>Given an integer array <code>nums</code>, move all <code>0</code>'s to the end of it while maintaining the relative order of the non-zero elements.</p>
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p... | 3 | {
"code": "class Solution {\npublic:\n void moveZeroes(vector<int>& nums) {\n queue<int>q;\n int count = 0;\n for(int i = 0 ; i < nums.size(); i++)\n {\n if(nums[i] != 0)\n {\n q.push(nums[i]);\n count++;\n }\n }\n ... |
191 | <p>Write a function that takes the binary representation of a positive integer and returns the number of <span data-keyword="set-bit">set bits</span> it has (also known as the <a href="http://en.wikipedia.org/wiki/Hamming_weight" target="_blank">Hamming weight</a>).</p>
<p> </p>
<p><strong class="example">Example... | 0 | {
"code": "class Solution {\npublic:\n int hammingWeight(int n) {\n int count = 0;\n while(n!=0){\n if(n&1){\n count++;\n }\n n=n>>1;\n }\n return count;\n \n }\n};",
"memory": "7500"
} |
191 | <p>Write a function that takes the binary representation of a positive integer and returns the number of <span data-keyword="set-bit">set bits</span> it has (also known as the <a href="http://en.wikipedia.org/wiki/Hamming_weight" target="_blank">Hamming weight</a>).</p>
<p> </p>
<p><strong class="example">Example... | 0 | {
"code": "// --------------->>>>> C W M\n\n\nclass Solution {\npublic:\n int hammingWeight(uint32_t n) {\n int count = 0;\n \n while(n)\n {\n count += n & 1;\n n >>= 1;\n }\n \n return count;\n }\n};\n\n\n\n\n\n\n\n\n\n\n\n\n",
"memory": ... |
191 | <p>Write a function that takes the binary representation of a positive integer and returns the number of <span data-keyword="set-bit">set bits</span> it has (also known as the <a href="http://en.wikipedia.org/wiki/Hamming_weight" target="_blank">Hamming weight</a>).</p>
<p> </p>
<p><strong class="example">Example... | 0 | {
"code": "class Solution {\npublic:\n int hammingWeight(uint32_t n) {\n int ans=0;\n while(n>0){\n if(n&1){\n ans++;\n }\n n=n>>1;\n } \n return ans;\n }\n};",
"memory": "7600"
} |
191 | <p>Write a function that takes the binary representation of a positive integer and returns the number of <span data-keyword="set-bit">set bits</span> it has (also known as the <a href="http://en.wikipedia.org/wiki/Hamming_weight" target="_blank">Hamming weight</a>).</p>
<p> </p>
<p><strong class="example">Example... | 0 | {
"code": "class Solution {\npublic:\n int hammingWeight(int n) {\n int count=0;\n while(n){\n n=(n&(n-1));\n count++;\n }\n return count;\n }\n};",
"memory": "7700"
} |
191 | <p>Write a function that takes the binary representation of a positive integer and returns the number of <span data-keyword="set-bit">set bits</span> it has (also known as the <a href="http://en.wikipedia.org/wiki/Hamming_weight" target="_blank">Hamming weight</a>).</p>
<p> </p>
<p><strong class="example">Example... | 0 | {
"code": "class Solution {\npublic:\n int hammingWeight(int n) {\n int count=0;\n int m=n;\n while (m>0){\n int rem= m%2;\n if (rem ==1){\n count = count+1 ;\n }\n else {\n count=count+0;\n }\n m=m/2;... |
191 | <p>Write a function that takes the binary representation of a positive integer and returns the number of <span data-keyword="set-bit">set bits</span> it has (also known as the <a href="http://en.wikipedia.org/wiki/Hamming_weight" target="_blank">Hamming weight</a>).</p>
<p> </p>
<p><strong class="example">Example... | 0 | {
"code": "class Solution {\npublic:\n int hammingWeight(int n) {\n int ans = 0;\n while(n>0){\n n&=(n-1);\n ans++;\n \n }\n return ans;\n }\n};",
"memory": "7800"
} |
191 | <p>Write a function that takes the binary representation of a positive integer and returns the number of <span data-keyword="set-bit">set bits</span> it has (also known as the <a href="http://en.wikipedia.org/wiki/Hamming_weight" target="_blank">Hamming weight</a>).</p>
<p> </p>
<p><strong class="example">Example... | 0 | {
"code": "class Solution {\npublic:\n int hammingWeight(int n) {\n\n int count=0;\n while(n!=0){\n if(n&1){\n count++;\n }\n n=n>>1;\n }\n return count;\n }\n};",
"memory": "7800"
} |
191 | <p>Write a function that takes the binary representation of a positive integer and returns the number of <span data-keyword="set-bit">set bits</span> it has (also known as the <a href="http://en.wikipedia.org/wiki/Hamming_weight" target="_blank">Hamming weight</a>).</p>
<p> </p>
<p><strong class="example">Example... | 0 | {
"code": "class Solution {\npublic:\n int hammingWeight(int n) {\n int count=0;\n while(n){\n count++;\n n=n&(n-1);\n }\n return count;\n }\n};",
"memory": "7900"
} |
191 | <p>Write a function that takes the binary representation of a positive integer and returns the number of <span data-keyword="set-bit">set bits</span> it has (also known as the <a href="http://en.wikipedia.org/wiki/Hamming_weight" target="_blank">Hamming weight</a>).</p>
<p> </p>
<p><strong class="example">Example... | 0 | {
"code": "class Solution {\npublic:\n int hammingWeight(int n) {\n int count = 0;\n \n while (n) {\n n &= (n - 1);\n ++count;\n }\n return count;\n }\n};\n\n// O(1) time and O(1) space",
"memory": "7900"
} |
202 | <p>Write an algorithm to determine if a number <code>n</code> is happy.</p>
<p>A <strong>happy number</strong> is a number defined by the following process:</p>
<ul>
<li>Starting with any positive integer, replace the number by the sum of the squares of its digits.</li>
<li>Repeat the process until the number equal... | 0 | {
"code": "class Solution {\nprivate:\n int square(int a){\n int ans=0;\n while(a > 0){\n ans += (a%10)*(a%10);\n a =a /10;\n }\n return ans ;\n }\npublic:\n bool isHappy(int n) {\n int slow =n;\n int fast=n;\n do{\n slow = squ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.