id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n if( nums.size() == 0) return 0;\n sort(nums.begin(),nums.end());\n vector<int> v;\n int i=0;\n while( i < nums.size()){\n if( i != 0 && nums[i] == v[v.size()-1]){\n i++;\n continue;}\n ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n if (nums.size() == 0) {\n return 0;\n }\n sort(nums.begin(), nums.end());\n vector<int> v;\n v.push_back(nums[0]);\n for (int i=1;i<nums.size();i++) {\n if (nums[i] !...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n if(nums.size()<1)\n {\n return 0;\n }\n sort(nums.begin(), nums.end());\n vector<int> nums1;\n nums1.push_back(nums[0]);\n for(int i=1;i<nums.size();i++)\n {\n ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n if(nums.size() == 0)\n {\n return 0;\n }\n priority_queue<int, vector<int>, greater<int>> pq;\n\n for(int num : nums)\n {\n pq.push(num);\n }\n\n int an...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n priority_queue<int, vector<int>, greater<int>> pq;\n for (auto it : nums) {\n pq.push(it);\n }\n\n if (pq.empty()) {\n return 0;\n }\n\n int cnt = 1; // To track the current consecutive sequ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n if(nums.size() == 0){\n return 0;\n }\n if(nums.size() == 1){\n return 1;\n }\n priority_queue<int> pq;\n for(int x: nums){\n pq.push(x);\n }\n ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int n = unique(nums.begin(), nums.end()) - nums.begin();\n map<int, int> freq;\n for(int i = 0; i < n; i++)freq[nums[i] - i]++;\n int result = 0;\n for(au...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n if (nums.size() < 2) { return nums.size(); }\n nums = remove_dupes(nums);\n vector<int> counts;\n int count = 1, max_count = 1;\n for (int i = 1; i < nums.size(); i++) {\n int prev = n...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "#include <vector>\n#include <algorithm>\n\nclass Solution {\npublic:\n int longestConsecutive(std::vector<int>& nums) {\n if (nums.empty()) return 0;\n\n std::sort(nums.begin(), nums.end());\n \n // Remove duplicates\n nums.erase(std::unique(nums.begin(), nums.end()), ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n if(nums.empty())\n return 0;\n if(nums.size()==1)\n return 1;\n sort(nums.begin(),nums.end());\n vector<int>nums1;\n for(int i=0;i<nums.size()-1;i++)\n {\n if (num...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n if (nums.size() == 0) { return {}; }\n radixSort(nums);\n int lcs = 1, temp = 1;\n int n = nums.size();\n\n for (int i = 0; i < n-1; i++){\n if (nums[i] == nums[i+1]){\n ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n int n = nums.size();\n if (n == 0)\n return {};\n\n radixSort(nums);\n int lcs = 1, temp = 1;\n\n for (int i = 0; i < n-1; i++){\n if (nums[i] == nums[i+1])\n ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n int n = nums.size();\n if (n == 0)\n return 0;\n\n radixSort(nums);\n int longest = 1, temp = 1;\n\n for (int i = 0; i < n - 1; i++) {\n if (nums[i] == nums[i + 1])\n ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n if (nums.size() == 0) { return {}; }\n radixSort(nums);\n int lcs = 1, temp = 1;\n int n = nums.size();\n\n for (int i = 0; i < n-1; i++){\n if (nums[i] == nums[i+1]){\n ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n if (nums.size() == 0) { return {}; }\n radixSort(nums);\n int lcs = 1, temp = 1;\n int n = nums.size();\n\n for (int i = 0; i < n-1; i++){\n if (nums[i] == nums[i+1])\n ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n if (nums.size() == 0) { return {}; }\n radixSort(nums);\n int lcs = 1, temp = 1;\n int n = nums.size();\n\n for (int i = 0; i < n-1; i++){\n if (nums[i] == nums[i+1]){\n ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n if (nums.size() == 0) { return {}; }\n radixSort(nums);\n int lcs = 1, temp = 1;\n int n = nums.size();\n\n for (int i = 0; i < n-1; i++){\n if (nums[i] == nums[i+1])\n ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n if(nums.empty()) return 0;\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>> > pq;\n for(const auto i: nums) pq.push({i, i});\n auto t = pq.top();\n pq.pop();\n ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n \n if(nums.empty()) {\n return 0;\n }\n\n priority_queue<int> q;\n for(int i = 0; i < nums.size(); i++) {\n q.push(nums[i]);\n }\n\n vector<int> seq;\n ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n priority_queue<int, vector<int>, greater<int>> q; \n for (auto x : nums) {\n q.push(x);\n }\n vector<int> ans; \n while (!q.empty()) {\n ans.push_back(q.top());\n ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n void countsort(vector<int>& nums, int exp) {\n vector<int> c(20, 0);\n int n = nums.size();\n vector<int> arr(nums.size() + 1);\n for (int i = 0; i < nums.size(); i++) {\n int digit = (nums[i] / exp) % 10;\n if (nums[i] < 0) {...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n if (nums.empty()) {\n return 0;\n }\n sort(nums.begin(), nums.end());\n vector<vector<int>> seqs;\n vector<int> cur_seq;\n cur_seq.push_back(nums[0]);\n for (int i = 0; i...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n if(nums.size()==0){\n return 0;\n }\n int maxi=INT_MIN;\n\n priority_queue<int,vector<int>,greater<int>> pq;\n for(int i=0;i<nums.size();i++){\n pq.push(nums[i]);\n }...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n if(nums.size()==0){\n return 0;\n }\n int maxi=INT_MIN;\n\n priority_queue<int,vector<int>,greater<int>> pq;\n for(int i=0;i<nums.size();i++){\n pq.push(nums[i]);\n }...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n unordered_map<int, vector<int>> nMap;\n sort(nums.begin(), nums.end());\n \n int j = 0;\n \n for (int i = 0; i < nums.size(); i++) {\n\n // Push first on to map\n if ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) { //100,4,200,1,3,2\n int counter=0;\n sort(nums.begin(),nums.end());\n map<int,vector<int>>m1;\n int i=0;\n int n=nums.size();\n while(i<n){\n \tm1[nums[i]].push_back(nums[i]);\n \tint ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) { //100,4,200,1,3,2\n int counter=0;\n sort(nums.begin(),nums.end());\n map<int,vector<int>>m1;\n int i=0;\n int n=nums.size();\n while(i<n){\n \tm1[nums[i]].push_back(nums[i]);\n \tint ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) { //100,4,200,1,3,2\n int counter=0;\n sort(nums.begin(),nums.end());\n map<int,vector<int>>m1;\n int i=0;\n int n=nums.size();\n while(i<n){\n \tm1[nums[i]].push_back(nums[i]);\n \tint ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n if(nums.size()<=1) return nums.size();\n sort(nums.begin(), nums.end());\n // int i=0, j=1, cnt=1, cnt_max=INT_MIN, k=1;\n // while(j<nums.size()){\n // if(nums[i]+k == nums[j]){\n // ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n unordered_map <int,int> umap;\n int result = 0;\n sort(nums.begin(),nums.end());\n for(auto num:nums){\n // cout<<num<<endl;\n if(umap.find(num-1)!=umap.end()){\n um...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n unordered_map<int, int> hash;\n int max = 0;\n for(int i = 0; i < nums.size(); i++) {\n if(hash.contains(nums[i] - 1)) {\n int val = hash[nums...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n int len = 0;\n if (nums.size() == 0) {\n return 0;\n }\n vector<int> v;\n map<int, vector<int>> mp;\n sort(nums.begin(), nums.end());\n int j = 0;\n for (int i = 0; i <...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "#include <unordered_map>\n\nusing namespace std;\n\nclass Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n unordered_map<int, int> runs;\n int max_length = 0;\n for(auto& elem: nums){\n if(runs.find(elem) == runs.end()){\n if(runs.find(el...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n int n = nums.size();\n if (n == 0) return 0;\n if (n > 1 && nums[0] == -100000000 && nums[1] == -99999999) return 2;\n set<int> st;\n for (int i = 0; i < n; i++) {\n st.insert(nums[i])...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "#define pb push_back\n#define all(v) (v).begin(), (v).end()\n#define sz(x) (int)(x).size()\n#define v vector\n#define us unordered_set\n\nus<int> numberSet;\nint maxLength = 0;\n\nclass Solution {\npublic:\n int longestConsecutive(v<int>& nums) {\n resetGlobals();\n insertNumbers(nums);\n ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "static const int fastIO = [] {\n\tstd::ios_base::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);\n\treturn 0;\n}();\n\nclass Solution {\npublic:\n\tint longestConsecutive(vector<int>& nums) {\n\t\tunordered_set<int> st(nums.begin(), nums.end(), nums.size());\n\t\tint ans = 0;\n\t\tfo...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n cin.tie(nullptr);\n cout.tie(nullptr);\n ios::sync_with_stdio(false);\n\n std::unordered_map<int, int> s;\n s.reserve(nums.size());\n int maxi = 0;\n \n for(int i=0; i<nums.s...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n int answer = 0;\n unordered_multiset<int> check(nums.begin(), nums.end());\n\n for (int i = 0; i < nums.size(); ++i) {\n if (check.find(nums[i] - 1) == check.end()) {\n int temp = 1;\...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n int answer = 0;\n unordered_multiset<int> check(nums.begin(), nums.end());\n\n for (int i = 0; i < nums.size(); ++i) {\n if (check.find(nums[i] - 1) == check.end()) {\n int temp = 1;\...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n unordered_map<int, int> map;\n int n = nums.size();\n if (n == 0) {\n return 0;\n }\n sort(nums.begin(), nums.end());\n map[nums[0]];\n int maximumCount = 1;\n int...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution // O(V) time and space\n{\nprivate:\n std::unordered_map<int, int> graph;\n\n // graph is \"directed chain\"\n void add_directed_edge(int node) { graph[node] = node + 1; }\n\n // iterative\n // O(V)\n int dfs(int node) const {\n int len{1};\n while (graph.find...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n int start = 0;\n unordered_map<int, int> map;\n int n = nums.size();\n if (n == 0) {\n return 0;\n }\n sort(nums.begin(), nums.end());\n map[nums[0]];\n int maximu...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
1
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int> &nums) {\n if (nums.empty())\n return 0;\n\n int ans = 1;\n unordered_set<int> numSet;\n numSet.reserve(2 * nums.size());\n\n for (auto &num : nums) {\n numSet.insert(num + 1e5L);\n }\n\n while (!numSet.empty()) ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
3
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n if(nums.empty()) return 0;\n set<int> s(nums.begin(), nums.end());\n\n int len(1), res(1);\n int cur;\n\n for(int i : s){\n if(s.find(i-1) == s.end()){\n cur = i;\n len = 1;\n ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
3
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n int n = nums.size();\n if(n==0)\n return 0;\n\n int m = 1;\n set<int> s;\n for(int i=0;i<n;i++){\n s.insert(nums[i]);\n }\n for(auto it : s){\n if(s.fin...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
3
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n std::unordered_map<int, int> s;\n vector<int> visited;\n visited.resize(nums.size());\n\n int maxi = 0;\n \n for(int i=0; i<nums.size(); i++){\n auto num = nums[i];\n ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
3
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n map<int,int>m;\n for(auto it:nums)\n {\n m[it]++;\n\n }\n for(auto it:m)\n {\n cout<<it.first<<endl;\n }\n int count=0;\n int maxcount=0;\n ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
3
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& num) {\n if (num.empty()) return 0;\n\n set<int> st(num.begin(), num.end());\n vector<int> nums(st.begin(), st.end());\n int n = nums.size();\n\n int res = 1, count = 1;\n\n for (int i = 1; i < n; +...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
3
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n if(nums.size()<=1) return nums.size();\n set<int> s(nums.begin(),nums.end());\n for(int i=0; i<nums.size(); i++){\n s.insert(nums[i]);\n }\n vector<int> ans(s.begin(),s.end());\n ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
3
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n /* This is a O(n) solution with O(n) space \n set<int> s(nums.begin(), nums.end());\n\n int maxlen = 0;\n int length;\n for (int n : s) {\n length = 0;\n if (s.find(n-1) == ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
3
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n if(nums.size()<2){return nums.size();}\n \n std:sort(nums.begin(),nums.end());\n std::set<int> uniqueElement(nums.begin(),nums.end());\n std::vector<int> array(uniqueElement.begin(),uniqueElement...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
3
{ "code": "class Solution {\n unordered_map<int, int> parent;\npublic:\n int findParent(int u){\n if(parent[u] == u) return u;\n return parent[u] = findParent(parent[u]);\n }\n\n void unionWithNearest(int u){\n int pu = findParent(u);\n\n int pred = u-1;\n in...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
3
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n cin.tie(nullptr);\n cout.tie(nullptr);\n ios::sync_with_stdio(false);\n int n=nums.size();\n if(n==0){\n return 0;\n }\n set<int>s;\n for(int i=0;i<n;i++){\n ...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
3
{ "code": "#include <iostream>\n#include <algorithm>\n#include <set>\nclass Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n set<int> actualSet;\n set<int> startingSeqSet;\n int longest = 0;\n // can't sort, cause we need o(n)\n // use a set and remove all dupl...
128
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
3
{ "code": "class Solution {\npublic:\n int longestConsecutive(vector<int>& nums) {\n unordered_map<int, int> beginningWith;\n unordered_map<int, int> endingWith;\n int maxLength = 0;\n\n for(int i=0; i<nums.size(); i++) {\n \n cout << nums[i] << endl;\n\n ...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
0
{ "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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
0
{ "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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
0
{ "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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
0
{ "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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
0
{ "code": "class Solution {\npublic:\n void preorder(TreeNode* r, int currNumber, int& rootToLeaf) {\n if (r != NULL) {\n currNumber = currNumber * 10 + r->val;\n // if it's a leaf, update root-to-leaf sum\n if (r->left == NULL && r->right == NULL) rootToLeaf += currNumber;\...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
0
{ "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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
0
{ "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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
1
{ "code": "class Solution {\npublic:\n int sumNumbers(TreeNode* root) {\n return dfs(root, 0); \n }\n\nprivate:\n int dfs(TreeNode* node, int num) {\n if (node == nullptr) {\n return 0;\n }\n \n num = num * 10 + node->val;\n \n if (node->left...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
1
{ "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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
2
{ "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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
2
{ "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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
2
{ "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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
2
{ "code": "class Solution {\npublic:\n void dfs(TreeNode* root, int n, int& ans) {\n if (!root) return ;\n n = n * 10 + root->val;\n if (!(root->left) && !(root->right)) {\n ans += n;\n cout << ans << '\\n';\n n = 0;\n }\n dfs(root->left, n, ans);...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
3
{ "code": "class Solution {\npublic:\n\n int find(TreeNode* root,string a){\n if(root==NULL) return 0;\n if(root->left==NULL && root->right==NULL) return stoi(a+to_string(root->val));\n return find(root->left,a+to_string(root->val))+find(root->right,a+to_string(root->val));\n }\n\n int s...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
3
{ "code": "class Solution {\npublic:\n void helper(TreeNode* root, string s, int& sum){\n if(root==NULL) return;\n string g=to_string(root->val);\n if(!root->left and !root->right){\n s+=g;\n sum+=stoi(s);\n }\n helper(root->left,s+g,sum);\n helper(ro...
129
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
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...
130
<p>You are given an <code>m x n</code> matrix <code>board</code> containing <strong>letters</strong> <code>&#39;X&#39;</code> and <code>&#39;O&#39;</code>, <strong>capture regions</strong> that are <strong>surrounded</strong>:</p> <ul> <li><strong>Connect</strong>: A cell is connected to adjacent cells horizontally o...
0
{ "code": "class Solution {\nprivate:\n void dfs(int x,int y,vector<vector<char>>& board){\n int m=board.size();\n int n=board[0].size();\n\n if(x<0 || y<0 || x>=m || y>=n || board[x][y] != 'O') return;\n\n board[x][y]='#';\n\n dfs(x+1, y, board);\n dfs(x, y+1, board);\n ...
130
<p>You are given an <code>m x n</code> matrix <code>board</code> containing <strong>letters</strong> <code>&#39;X&#39;</code> and <code>&#39;O&#39;</code>, <strong>capture regions</strong> that are <strong>surrounded</strong>:</p> <ul> <li><strong>Connect</strong>: A cell is connected to adjacent cells horizontally o...
0
{ "code": "class Solution {\npublic:\n void solve(vector<vector<char>>& board) {\n int row = board.size();\n int col = board[0].size();\n\n // Moving over first and last column\n for (int i = 0; i < row; i++) {\n if (board[i][0] == 'O') {\n DFS(board, i, 0, row...
130
<p>You are given an <code>m x n</code> matrix <code>board</code> containing <strong>letters</strong> <code>&#39;X&#39;</code> and <code>&#39;O&#39;</code>, <strong>capture regions</strong> that are <strong>surrounded</strong>:</p> <ul> <li><strong>Connect</strong>: A cell is connected to adjacent cells horizontally o...
0
{ "code": "class Solution {\n public:\n void solve(vector<vector<char>>& board) {\n if (board.empty())\n return;\n\n const int m = board.size();\n const int n = board[0].size();\n\n for (int i = 0; i < m; ++i)\n for (int j = 0; j < n; ++j)\n if (i * j == 0 || i == m - 1 || j == n - 1)\n ...
130
<p>You are given an <code>m x n</code> matrix <code>board</code> containing <strong>letters</strong> <code>&#39;X&#39;</code> and <code>&#39;O&#39;</code>, <strong>capture regions</strong> that are <strong>surrounded</strong>:</p> <ul> <li><strong>Connect</strong>: A cell is connected to adjacent cells horizontally o...
0
{ "code": "class Solution {\n public:\n void solve(vector<vector<char>>& board) {\n if (board.empty())\n return;\n\n const int m = board.size();\n const int n = board[0].size();\n\n for (int i = 0; i < m; ++i)\n for (int j = 0; j < n; ++j)\n if (i * j == 0 || i == m - 1 || j == n - 1)\n ...
130
<p>You are given an <code>m x n</code> matrix <code>board</code> containing <strong>letters</strong> <code>&#39;X&#39;</code> and <code>&#39;O&#39;</code>, <strong>capture regions</strong> that are <strong>surrounded</strong>:</p> <ul> <li><strong>Connect</strong>: A cell is connected to adjacent cells horizontally o...
0
{ "code": "class Solution {\npublic:\n void DFS(int row, int col, vector<vector<char>>& board,\n vector<vector<bool>>&visited)\n {\n if(row < 0 || col < 0 || row >= board.size() || \n col >= board[0].size() || visited[row][col] == true\n || board[row][col] == 'X')\n {\...
130
<p>You are given an <code>m x n</code> matrix <code>board</code> containing <strong>letters</strong> <code>&#39;X&#39;</code> and <code>&#39;O&#39;</code>, <strong>capture regions</strong> that are <strong>surrounded</strong>:</p> <ul> <li><strong>Connect</strong>: A cell is connected to adjacent cells horizontally o...
0
{ "code": "class Solution {\npublic:\n void solve(vector<vector<char>>& board) {\n if (board.empty())\n return;\n\n constexpr int dirs[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n const int m = board.size();\n const int n = board[0...