id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n unordered_map<string,bool> mp;\n bool isScramble(string s1, string s2) {\n if(s1==s2)\n return true;\n if(s1.size()==0 and s2.size()==0)\n return true;\n if(s1.size() != s2.size())\n return false;\n\n if(s1.size()<=1)\n ...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n unordered_map<string, bool> m;\n bool isScramble(string s1, string s2) {\n if(s1 == s2) return true;\n if(s1.size() <= 1 || s2.size() <= 1 || s1.size() != s2.size()) return false;\n\n if(m.find(s1 + \" \" + s2) != m.end()) return m[s1 + \" \" + s2];\n\...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n bool isScramble(string s1, string s2) {\n map<pair<string, string>, int> dp; \n auto dfs = [&](auto &self, string a, string b) -> bool\n {\n // cout << a << ' ' << b << endl; \n if(a == b)\n return 1; \n if(...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n bool isScramble(string s1, string s2) {\n if (s1 == s2)\n return true;\n int n = s1.size(), m = s2.size();\n // will cache the result for a given string, as there may be repeated strings\n unordered_map<string, bool> cache;\n retu...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\n // int n;\n unordered_map<string, bool> mp;\n bool f(string s1, string s2, int n) {\n // cout << s1 << \" \" << s2 << endl;\n\n if (s1 == s2)\n return mp[s1 + \"#\" + s2] = true;\n if (n == 1)\n return mp[s1 + \"#\" + s2] = false;\n\n ...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n unordered_map<string,bool> mp;\n bool isScramble(string s1, string s2) {\n int n = s1.size();\n if(s2.size()!=n)\n return false;\n\n if(s1==s2)\n return true; \n \n if(n==1)\n return false;\n \n ...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n\n unordered_map<string, bool> dp;\n\n bool isScramble(string s1, string s2) {\n if(s1.compare(s2)==0) return true;\n else if(s1.size()==1) return false;\n\n if(dp.find(s1+\",\"+s2)!=dp.end()) return dp[s1+\",\"+s2];\n\n for(int i=1;i<s1.size();i...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n bool isScramble(string s1, string s2) {\n unordered_map<string, bool> dp;\n auto dfs = [&](auto &self, string a, string b) -> bool\n {\n if(a == b)\n return 1; \n if(a.length() != b.length())\n return 0; ...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\nunordered_map<string,bool> mp;\n bool solve(string s1, string s2)\n {\n if(s1==s2)\n return 1;\n if(s1.size()!=s2.size())\n return 0;\n int n = s1.size();\n if(mp.find(s1+s2)!=mp.end())\n return mp[s1+s2];\n bool notSwap = 0, swap = 0;\n ...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\n unordered_map<string,bool>mpp;\nprivate:\n bool f(string s1, string s2){\n int n =s1.size();\n if(s1==s2){\n return true;\n }\n if(s1.size()!=s2.size()){\n return false;\n }\n\n string key=s1+s2;\n\n if(mpp.find...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\n unordered_map<string,bool>mpp;\nprivate:\n bool f(string s1, string s2){\n int n =s1.size();\n if(s1==s2){\n return true;\n }\n if(s1.size()!=s2.size()){\n return false;\n }\n\n string key=s1+s2;\n\n if(mpp.find...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\nmap<string,int>mp;\nbool solve(string s1,string s2)\n{\n int n=s1.length();\n if(s1==s2)\n return true;\n if(mp.find(s1+s2)!=mp.end())\n return mp[s1+s2];\n string key=s1+s2;\n bool result =false;\n for(int i=1;i<s1.length();i++)\n {\n bool notswa...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n map<pair<string, string>, bool> mp;\n bool checkScramble(string s1, string s2) {\n if (s1 == \"\" || s1.length() <=0 ) return true;\n if (s1 == s2) return true;\n if (s1.length() == 1) return false;\n // cout << s1 << \" \" << s2 << \"\\n\";\n ...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n map<pair<string, string>, bool> mp;\n bool checkScramble(string s1, string s2) {\n if (s1 == \"\" || s1.length() <=0 ) return true;\n if (s1 == s2) return true;\n if (s1.length() == 1) return false;\n // cout << s1 << \" \" << s2 << \"\\n\";\n ...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n bool func(string s1, string s2, map<pair<string, string>, bool>&dp){\n if(dp.find({s1, s2}) != dp.end())\n return dp[{s1, s2}];\n bool ans = false;\n if(s1.size() == 1){\n return s1 == s2;\n }\n else{\n for(i...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\n map<string,bool> dp;\n bool solve(string a , string b) {\n int n = a.length();\n if(n == 1) {\n return a == b;\n }\n string temp = a + \"#\" + b;\n if(dp.count(temp)) return dp[temp];\n bool ans = false;\n for(int i = 0; i...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n bool solve(string s1, string s2, unordered_map<string,int>&dp){\n if(s1==s2) return true;\n int n=s1.length();\n string k=s1+\":\"+s2;\n if(dp.count(k)!=0) return dp[k];\n if(s1.length()==1) return false;\n bool ans=false;\n fo...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\n unordered_map<string , bool>temp;\n bool solve(string s1, string s2, int i){\n if(s1.size() == 1){\n return s1[0] == s2[i];\n }\n string key = s1 + '#' +to_string(i) ;\n if(temp.count(key))return temp[key];\n bool ans = false;\n for(int k = 0;k<...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n unordered_map<string,bool> dp;\n int calc(string s1,string s2)\n {\n if(s1.size() != s2.size())\n {\n return 0;\n }\n if(s1 == s2)\n {\n return 1;\n }\n string key;\n key+=s1;\n key+=\"...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\n map<string, bool> cache;\n\npublic:\n bool isScramble(string s1, string s2) {\n /*cout << \"s1: \" << s1 << \", s2: \" << s2 << endl;*/\n int len = s1.length();\n if (len != s2.length())\n return false;\n if (len == 1)\n return s1 == s2;\n auto r = cache.find(s...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n unordered_map<string, bool> map{};\n bool isScramble(string s1, string s2) {\n if(map.find(s1 + \" \" + s2) != map.end()) return map[s1 + \" \" + s2];\n if(s1.length() != s2.length()) return map[s1 + \" \" + s2] = false;\n if(s1 == s2) return map[s1 + ...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n unordered_map<string, bool> mp;\n bool setAndReturn(string key, bool b) {\n mp[key] = b;\n return b;\n }\n bool isScramble(string s1, string s2) {\n if (s1 > s2) return isScramble(s2, s1);\n int len1 = s1.length();\n int len2 = s2.l...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n unordered_map<string, bool> mp;\n bool setAndReturn(string key, bool b) {\n mp[key] = b;\n return b;\n }\n bool isScramble(string s1, string s2) {\n if (s1 > s2) return isScramble(s2, s1);\n int len1 = s1.length();\n int len2 = s2.l...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n\n int dp[31][31][31];\n int fr(int i, int j, int k, string s1, string s2){\n if(i==j){\n if(s1[i] == s2[k])return 1;\n return 0;\n }\n if(dp[i][j][k]!=-1)return dp[i][j][k];\n int ans = 0;\n for(int ind = i; ind < j;...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n\n int m[40][40][40];\n\n bool dfs(string s1, string s2, int l, int l1, int len) {\n if (len == 1) {\n return s1[l] == s2[l1];\n }\n if (m[l][l1][len] != -1) {\n return m[l][l1][len];\n }\n bool isSame = true;\n ...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n int check[30][30][31] = {}; \n\n bool _isScramble(string s1, string s2, int b1, int b2, int l) {\n if (check[b1][b2][l] != 0) return (check[b1][b2][l] - 1);\n if (s1.substr(b1, l) == s2.substr(b2, l)) return true;\n if (l == 1) return false;\n f...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n\n int m[40][40][40];\n\n bool dfs(string s1, string s2, int l, int l1, int len) {\n if (len == 1) {\n return s1[l] == s2[l1];\n }\n if (m[l][l1][len] != -1) {\n return m[l][l1][len];\n }\n bool isSame = true;\n ...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n int check[40][40][40] = {}; \n\n bool _isScramble(string s1, string s2, int b1, int b2, int l) {\n if (check[b1][b2][l] != 0) return (check[b1][b2][l] - 1);\n if (s1.substr(b1, l) == s2.substr(b2, l)) return true;\n if (l == 1) return false;\n f...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n int check[40][40][40] = {}; \n\n bool _isScramble(string s1, string s2, int b1, int b2, int l) {\n if (check[b1][b2][l] != 0) return (check[b1][b2][l] - 1);\n if (s1.substr(b1, l) == s2.substr(b2, l)) return true;\n if (l == 1) return false;\n f...
87
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
3
{ "code": "class Solution {\npublic:\n\n bool help2(string s1, string s2, unordered_map<string, bool> &dp){\n if(s1 == s2) return true;\n if(s1.length() <= 1)\n return false;\n // if(dp.find({s1, {i,j}}) != dp.end()) return dp[{s1, {i,j}}];\n string temp = s1 + \" \" + s2;\n...
88
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>, sorted in <strong>non-decreasing order</strong>, and two integers <code>m</code> and <code>n</code>, representing the number of elements in <code>nums1</code> and <code>nums2</code> respectively.</p> <p><strong>Merge</strong> <code>nums1</c...
0
{ "code": "int32_t intercept = [] {\n std::string str{};\n std::string str2{};\n std::priority_queue<int16_t, std::vector<int16_t>, std::greater<int16_t>>\n pq{};\n\n {\n std::ofstream out{\"user.out\"};\n std::string dummy1{};\n std::string dummy2{};\n while (std::getli...
88
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>, sorted in <strong>non-decreasing order</strong>, and two integers <code>m</code> and <code>n</code>, representing the number of elements in <code>nums1</code> and <code>nums2</code> respectively.</p> <p><strong>Merge</strong> <code>nums1</c...
0
{ "code": "int32_t intercept = [] {\n std::string str{};\n std::string str2{};\n std::priority_queue<int16_t, std::vector<int16_t>, std::greater<int16_t>>\n pq{};\n\n {\n std::ofstream out{\"user.out\"};\n std::string dummy1{};\n std::string dummy2{};\n while (std::getli...
88
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>, sorted in <strong>non-decreasing order</strong>, and two integers <code>m</code> and <code>n</code>, representing the number of elements in <code>nums1</code> and <code>nums2</code> respectively.</p> <p><strong>Merge</strong> <code>nums1</c...
0
{ "code": "\nclass Solution {\npublic:\n void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {\n int i = m-1;\n int j = n-1;\n int k = m+n-1;\n while(j>=0) {\n if(i>=0 && nums1[i]>nums2[j]){\n nums1[k--]=nums1[i--];\n\n }\n ...
88
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>, sorted in <strong>non-decreasing order</strong>, and two integers <code>m</code> and <code>n</code>, representing the number of elements in <code>nums1</code> and <code>nums2</code> respectively.</p> <p><strong>Merge</strong> <code>nums1</c...
0
{ "code": "class Solution {\npublic:\n void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {\n int i = m - 1; \n int j = n - 1; \n int k = m + n - 1; \n\n \n while (i >= 0 && j >= 0) {\n if (nums1[i] >= nums2[j]) {\n nums1[k] = nums1[i];\n ...
88
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>, sorted in <strong>non-decreasing order</strong>, and two integers <code>m</code> and <code>n</code>, representing the number of elements in <code>nums1</code> and <code>nums2</code> respectively.</p> <p><strong>Merge</strong> <code>nums1</c...
0
{ "code": "class Solution {\npublic:\n void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {\n // Start from the last positions of nums1 and nums2\n int i = m - 1; // Pointer for nums1 (excluding the zeros at the end)\n int j = n - 1; // Pointer for nums2\n int k = m + n ...
88
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>, sorted in <strong>non-decreasing order</strong>, and two integers <code>m</code> and <code>n</code>, representing the number of elements in <code>nums1</code> and <code>nums2</code> respectively.</p> <p><strong>Merge</strong> <code>nums1</c...
0
{ "code": "class Solution {\npublic:\n void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {\n int i = m, j = 0;\n while (i < m + n && j < n) {\n nums1[i] = nums2[j];\n i++;\n j++;\n }\n sort(nums1.begin(), nums1.end());\n }\n};", "memo...
88
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>, sorted in <strong>non-decreasing order</strong>, and two integers <code>m</code> and <code>n</code>, representing the number of elements in <code>nums1</code> and <code>nums2</code> respectively.</p> <p><strong>Merge</strong> <code>nums1</c...
0
{ "code": "class Solution {\npublic:\n void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {\n // Pointers for nums1, nums2, and the merged array\n int i = m - 1; // Last valid element in nums1\n int j = n - 1; // Last element in nums2\n int k = m + n - 1; // Last positi...
88
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>, sorted in <strong>non-decreasing order</strong>, and two integers <code>m</code> and <code>n</code>, representing the number of elements in <code>nums1</code> and <code>nums2</code> respectively.</p> <p><strong>Merge</strong> <code>nums1</c...
0
{ "code": "class Solution {\npublic:\n void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {\n int k = n + m - 1;\n m--;\n n--;\n while (k >= 0)\n {\n if (n < 0)\n {\n break;\n }\n\n if (m >= 0 && nums1[m] > ...
88
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>, sorted in <strong>non-decreasing order</strong>, and two integers <code>m</code> and <code>n</code>, representing the number of elements in <code>nums1</code> and <code>nums2</code> respectively.</p> <p><strong>Merge</strong> <code>nums1</c...
1
{ "code": "class Solution {\npublic:\n void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {\n for (int i = m - 1, j = n - 1, k = m + n - 1; k >= 0 && j >= 0; k--) {\n nums1[k] = (i < 0 || nums1[i] < nums2[j]) ? nums2[j--] : nums1[i--];\n }\n }\n};", "memory": "11300" }
88
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>, sorted in <strong>non-decreasing order</strong>, and two integers <code>m</code> and <code>n</code>, representing the number of elements in <code>nums1</code> and <code>nums2</code> respectively.</p> <p><strong>Merge</strong> <code>nums1</c...
1
{ "code": "class Solution {\npublic:\n void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {\n // Start merging from the end of nums1 array\n int i = m - 1; // Index for the last element of the actual nums1\n int j = n - 1; // Index for the last element of nums2\n int k = m...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
0
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n int mask=1<<n;\n vector<int>R(mask);\n for(int i=0;i<mask;i++){\n int toggled=i^(i>>1);\n R[i]=toggled;\n }\n return R;\n }\n};", "memory": "12514" }
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
0
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n vector<int> ans(1<<n);\n for (int i=0; i<(1<<n); i++) \n ans[i] = i^(i>>1);\n return ans;\n }\n};", "memory": "12514" }
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
0
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n vector<int> res;\n for (int i = 0; i < 1 << n; i++)\n res.push_back(i ^ (i >> 1));\n return res;\n }\n};\n", "memory": "12943" }
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
0
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n //Checks \n // Every integer is between 0 and 2^n\n // First integer is 0\n // Unique Integers\n // Binary representation of every pair of adjacent integers differs by exactly one bit\n ...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
1
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n vector<int>ans;\n for(int i = 0; i < pow(2,n); i++){\n ans.push_back(i ^ (i >> 1));\n }\n return ans;\n }\n};", "memory": "13371" }
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
1
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n int count=0;\n vector<int> ans;\n ans.push_back(0);\n ans.push_back(1);\n while(count<n-1){\n int j=ans.size()-1;\n int add=pow(2,count+1);\n while(j>=0){\n ans....
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
2
{ "code": "\n class Solution {\npublic:\n vector<int> vect;\n void helper(int &a,int n)\n {\n a^=1<<n;\n vect.push_back(a);\n for(int c=0;c<n;c++)\n helper(a,c);\n }\n vector<int> grayCode(int n) \n {\n int a=0;\n vect.push_back(a);\n for(i...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
2
{ "code": "class Solution {\npublic:\n vector<int> vect;\n void helper(int &a,int n)\n {\n a^=1<<n;\n vect.push_back(a);\n for(int c=0;c<n;c++)\n helper(a,c);\n }\n vector<int> grayCode(int n) \n {\n int a=0;\n vect.push_back(a);\n for(int c=0;c<n...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n if (n == 1) return vector<int> {0, 1};\n vector<int> prev = grayCode(n - 1);\n vector<int> nxt(1 << (n - 1));\n int len = 1 << (n - 1);\n for (int i = 0; i < len; i++)\n nxt[i] = prev[len-1-i] + len...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> ret;\n\n void helper(int n)\n {\n if (n == 1)\n {\n ret.push_back(0);\n ret.push_back(1);\n return;\n }\n\n helper(n-1);\n int i = ret.size() - 1;\n\n while (i >= 0)\n {\n ...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n //TC:O(2^n)\n vector<int> grayCode(int n) {\n vector<int> res;\n res.push_back(0);\n int t = 1;\n while (n -- != 0) {\n vector<int> newRes;\n for (int i = 0; i < res.size(); i++) newRes.push_back(res[i]);\n for (...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n vector<int>v;\n v.push_back(0);\n n=pow(2,n);\n for(int i=1;i<=n-1;i++){\n string str=\"\";\nint j=i;\nwhile(j>0){\n if(j%2==0){\n str='0'+str;\n }\n else{\n str='1'+str;\n }\n j=j/2;\...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n vector<int>v;\n v.push_back(0);\n n=pow(2,n);\n for(int i=1;i<=n-1;i++){\n string str=\"\";\nint j=i;\nwhile(j>0){\n if(j%2==0){\n str='0'+str;\n }\n else{\n str='1'+str;\n }\n j=j/2;\...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n vector<int>v;\n v.push_back(0);\n n=pow(2,n);\n for(int i=1;i<=n-1;i++){\n string str=\"\";\nint j=i;\nwhile(j>0){\n if(j%2==0){\n str='0'+str;\n }\n else{\n str='1'+str;\n }\n j=j/2;\...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n vector<int>ans;\n for(int i=0;i<(pow(2,n));i++){\n // convering the first number that is 0\n // since there are n bits we can do by\n string graybits;\n int lastbit=0;\n for(i...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n vector<int>ans;\n for(int i=0;i<(pow(2,n));i++){\n // convering the first number that is 0\n // since there are n bits we can do by\n string graybits;\n int lastbit=0;\n for(i...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n vector<int> res;\n vector<int> him(2);\n vector<int> tmp=him;\n for(int i=1;i<=n;i++){\n int mid=(pow(2,i)-1)/2;\n int j=0;\n for(;j<=mid;j++){\n tmp[j]=(tmp[j]|(0<<(i-...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n\n vector<int> grayCode(int n) {\n int k = 1;\n for(int i=1;i<=n;i++) k*=2;\n string s[k+3];\n s[1]=\"0\";\n s[2]=\"1\";\n int t=2;\n for(int i=2;i<=n;i++){\n int l=1;\n for(int j=1;j<=i;j++){\n ...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n vector<int> result(pow(2, n));\n result[0] = 0; \n result[1] = 1; \n\n if(n == 1){return result;}\n\n for(int i = 2; i <= n; i++)\n {\n result = append_array_in_reverse(result, i);\n }...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n vector<int> result(pow(2, n)); \n\n result[0] = 0; \n result[1] = 1;\n \n if(n == 1) {return result;}\n \n for(int i = 2; i <= n; i++)\n {\n result = append_array_in_reverse(res...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n\n int count(int x)\n {\n int ans = 0;\n while(x) {\n if(x&1)ans++;\n x >>= 1; \n }\n\n return ans;\n }\n\n bool solve(vector<int>& visit, vector<int>& ans, int l, int n, int ind)\n {\n // cout << l << endl;\n ...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n vector<int> result;\n for (int i = 0; i < pow(2 , 16); ++i) { \n result.push_back(i ^ (i >> 1));\n }\n return vector<int>(result.begin(), result.begin() + pow(2, n));\n }\n};", "memory": "19803" }
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n)\n {\n std::unordered_set<int> table{0};\n int size = std::pow(2, n);\n std::vector<int> results(size, 0);\n int idx = 1;\n while (table.size()!=size)\n {\n int previousValue = results[idx-1];\...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n)\n {\n std::unordered_set<int> table{0};\n int size = std::pow(2, n);\n std::vector<int> results(size, 0);\n int idx = 1;\n while (table.size()!=size)\n {\n int previousValue = results[idx-1];\...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n)\n {\n std::unordered_set<int> table{0};\n int size = std::pow(2, n);\n std::vector<int> results(size, 0);\n int idx = 1;\n while (table.size()!=size)\n {\n int previousValue = results[idx-1];\...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n)\n {\n std::unordered_set<int> table{0};\n int size = std::pow(2, n);\n std::vector<int> results(size, 0);\n int idx = 1;\n while (table.size()!=size)\n {\n int previousValue = results[idx-1];\...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n string binary(int n){\n if(n==0)\n return \"0\";\n \n string ans=\"\";\n while(n){\n if(n%2==0)\n ans+='0';\n else ans+='1';\n n/=2;\n }\n reverse(ans.begin(), ans.end());\n re...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "#include <vector>\n#include <unordered_map>\n#include <cmath>\n\nclass Solution {\npublic:\n std::vector<int> grayCode(int n) {\n std::vector<int> v;\n v.push_back(0);\n std::unordered_map<int, int> mp;\n mp[0] = 1;\n while (v.size() < (1 << n)) {\n int last...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n \n vector<int> grayCode(int n) {\n int sz = pow(2, n), i=1;\n vector<int> ans;\n unordered_map<int, int> mp;\n mp[0]++;\n ans.push_back(0);\n int graycode = 0;\n\n while(ans.size() < sz){\n int changedMSB = grayco...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n \n unordered_set<int> ans;\n vector<int> ans1;\n int num=0;\n ans1.push_back(0);\n ans.insert(0);\n int power=pow(2,n);\n while(ans.size()!=power){\n \n for(int i=0;i...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n vector<int> ans;\n ans.push_back(0);\n\n set<int> s;\n s.insert(0);\n\n for (int i = 1;i< (1<<n); i++)\n {\n int x = ans.back();\n for (int j = 0;j<n;j++)\n {\n ...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n set<int> st;\n vector<int> ans;\n int t=0;\n st.insert(t);ans.push_back(t);\n \n int op=1,sh=0;\n int i=1;\n i=(i<<n)-1;\n while(i>0){\n t^=op<<sh;\n if(st.fin...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n set<int> st;\n vector<int> ans;\n int t=0;\n st.insert(t);ans.push_back(t);\n \n int sh=0;\n int i=1;\n i=(i<<n)-1;\n while(i>0){\n t^=1<<sh;\n if(st.find(t)==...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n#define FOR(i,a,b) for(int i=a;i<b;++i)\n#define FOr(j,a,b) for(int j=a;j>=b;--j)\n#define ce cout << endl;\n#define cs cout << \" \";\n\n \n\n vector<int> grayCode(int n) {\n vector<string> a(pow(2 , n));\n fill(a.begin() , a.end() , \"\");\n \n a[...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n if(n==1){\n return {0,1};\n }\n if(n==2){\n return {0,1,3,2};\n }\n vector<int>ans = {0,1,3,2};\n int cur = 2;\n vector<int>temp;\n while(cur < 16){\n temp...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n\n string toBinary(int i) {\n string s;\n while(i) {\n if(i&1)\n s = \"1\"+s;\n else \n s = \"0\"+s;\n \n i>>=1;\n }\n return s;\n }\n\n string binaryToGray(string b) {\...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> ans = {0};\n void graycode(int n, int high, int i){\n if(i == high) return;\n int two = 1, temp = n, j = i;\n bool val = 1;\n while(j%2 == 0){\n j /= 2;\n two *= 2;\n temp /= 2;\n }\n ...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> ans; \n void greycode(int i, int n){\n if(i == n) return;\n ans.push_back(i^(i/2));\n greycode(i+1, n);\n }\n vector<int> grayCode(int n) {\n int x = pow(2, n);\n greycode(0,x);\n return ans;\n\n ...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> ans; \n void greycode(int i, int n){\n if(i == n) return;\n ans.push_back(i^(i>>1));\n greycode(i+1, n);\n }\n vector<int> grayCode(int n) {\n int x = pow(2, n);\n greycode(0,x);\n return ans;\n ...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\n\tvector<string> gen_code(int n) {\n\t\tvector<string> sol {\"\"};\n\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tint s = sol.size();\n\n\t\t\tfor (int j = s - 1; j >= 0; j--)\n\t\t\t\tsol.push_back(sol[j]);\n\n\t\t\ts *= 2;\n\t\t\tfor (int k = 0; k < s; k++)\n\t\t\t\tif (k < s / 2)\n\t\t\t\...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n if(n==1){\n return {0,1};\n }\n int cnt=2;\n vector<string> v={\"00\",\"01\",\"11\",\"10\"};\n while(cnt<n){\n int x=v.size();\n for(int i=x-1;i>=0;i--){\n strin...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\n void DFS(int num, int places, vector<int> &visited, vector<int> &ret){\n visited[num]=1;\n ret.push_back(num);\n for(int i=0;i<places;i++){\n int val=num^(1<<i);\n if(!visited[val]){\n DFS(val,places,visited,ret);\n ...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n this->n = n;\n vector<int> curr;\n backtrack(0, curr);\n return res;\n }\nprivate:\n int n;\n vector<int> res;\n bool s[1 << 16] = {};\n bool backtrack(int x, vector<int>& curr){\n if (curr.size...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n vector<string> v_str = solve(n);\n vector<int> v(v_str.size());\n for (int i = 0; i < v_str.size(); i++)\n {\n v[i] = binary_str_to_int(v_str[i]);\n }\n return v;\n }\n vector<int> gray...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\nprivate:\nstring get(char a, char b){\n return (a == b) ? \"0\" : \"1\";\n}\nstring binarytoGray(string binary)\n {\n string gray = \"\";\n gray += binary[0];\n for (int i = 1; i < binary.length(); i++)\n gray += get(binary[i - 1], binary[i]);\n ...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n vector<string> curStrings{\"0\", \"1\"};\n n--;\n \n while (n > 0) {\n for (int i = curStrings.size()-1; i >= 0; i--) {\n curStrings.emplace_back(curStrings[i]);\n }\n\n ...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n bool check[1000000] = {false};\n\n void Try(int n, vector<int>& result) {\n\n int pre = result.back();\n for (int i = 0; i < n; i++) {\n int next = pre ^ (1 << i);\n if (!check[next]) {\n check[next] = true;\n ...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n\n int convert(string str)\n {\n int rs=0;\n int cnt=0;\n for(int i=str.length()-1; i>= 0; i--)\n {\n rs=rs+(int)(str[i]-'0')*pow(2,cnt);\n cnt++;\n }\n return rs;\n }\n \n vector<string> recursion(int ...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n vector<string> results = {\"0\"};\n if(n == 0) return {0};\n int start = n;\n while(n > 0){\n cout << \"n is \" << n << endl;\n int size = results.size();\n\n for(int res = 0; res < (...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n\n int binpow(int a, int b){\n if(b == 0) return 1;\n int v = binpow(a, b / 2);\n if(b & 1) return a * v * v;\n else return v * v;\n }\n\n // Variables to track state\n vector<int> numbers;\n vector<int> sol, ans;\n bool done = false;\n i...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n\n int binpow(int a, int b){\n if(b == 0) return 1;\n int v = binpow(a, b / 2);\n if(b & 1) return a * v * v;\n else return v * v;\n }\n\n // Variables to track state\n vector<int> numbers;\n vector<int> sol, ans;\n bool done = false;\n i...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n\n int binpow(int a, int b){\n if(b == 0) return 1;\n int v = binpow(a, b / 2);\n if(b & 1) return a * v * v;\n else return v * v;\n }\n\n // Variables to track state\n vector<int> numbers;\n vector<int> sol, ans;\n bool done = false;\n i...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n vector<int> ans;\n vector<string> b_ver_ans;\n int size = 2;\n string str(n, '0');\n int decimal = stoi(str, nullptr, 2);\n ans.push_back(decimal);\n b_ver_ans.push_back(str);\n str[n-1] =...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n\n //1: normal node\n //2: end node\n //-1: passed normal node\n //-2: passed end node\n //0: start node\n bool dfs(vector<int>& path, int states[], int n, int num){\n // for(int p: path){\n // printf(\"%d, \", p);\n // }\n // pri...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n\n //1: normal node\n //2: end node\n //-1: passed normal node\n //-2: passed end node\n //0: start node\n bool dfs(vector<int>& path, int states[], int n, int num){\n // for(int p: path){\n // printf(\"%d, \", p);\n // }\n // pri...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n\n //1: normal node\n //2: end node\n //-1: passed normal node\n //-2: passed end node\n //0: start node\n bool dfs(vector<int>& path, int states[], int n, int num){\n // for(int p: path){\n // printf(\"%d, \", p);\n // }\n // pri...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n int nn; int lim;\n vector<int>ans;\n void solve(vector<int>&res,vector<int>&vis,int u){\n if(res.size()==lim) return;\n for(int i=0;i<nn;i++){\n int v = (1<<i)^u;\n if(!vis[v]){\n vis[v] = 1; res.push_back(v);\n ...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n vector<int> grayCode(int n) {\n const int sz = 1 << n;\n vector<vector<int>> arr(1, vector<int>(n, 0));\n \n for (int i = 0, idx = n - 1; i < n; ++i, --idx)\n {\n const int j_start = arr.size() - 1;\n for (int j = j_sta...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n\n int binpow(int a, int b){\n if(b == 0)return 1;\n int v = binpow(a, b/2);\n if(b&1)return a*v*v;\n else return v*v;\n }\n\n //state - which elements are taken\n vector<int>numbers;\n vector<int>sol, ans;\n\n bool done = false;\n int ...
89
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
3
{ "code": "class Solution {\npublic:\n int nn;\n vector<int>ans;\n void solve(vector<int>&res,vector<int>&vis,int u){\n if(!ans.empty()) return;\n if(res.size()==pow(2,nn)) {\n ans = res;\n return;\n }\n for(int i=0;i<nn;i++){\n int v = (1<<i)^u;\n...