The first time I played leetcode, I played virtual, and briefly talked about the ideas of each question.
Number reversed twice
Main idea: judge whether a number is still the original number after being flipped twice, and remove the leading zero after flipping.
As long as the last bit is not zero, there must be no change in flipping twice. It is necessary to judge 0
code:
class Solution { public: bool isSameAfterReversals(int num) { if(num == 0) return true; return num % 10; } };
Execute all suffix instructions
General idea: give a URDL operation sequence with length N and a starting point. Ask how many times you can move according to the operation sequence from 0~n-1 without crossing the boundary.
I didn't expect a simple way. The violent simulation doesn't time out. The complexity is O (n2).
code:
class Solution { public: vector<int> executeInstructions(int n, vector<int>& startPos, string s) { vector<int> ans; for(int i = 0;i < s.size();++i){ int r = startPos[0]; int c = startPos[1]; for(int j = i; j < s.size();++j){ if(s[j] == 'L'){ c -= 1; }else if(s[j] == 'R'){ c += 1; }else if(s[j] == 'U'){ r -= 1; }else{ r += 1; } if(r >= n || c >= n || r < 0 || c < 0){ ans.push_back(j-i); break; } if(j == s.size()-1){ ans.push_back(j-i+1); } } } return ans; } };
Sum of intervals of the same elements
Give an array and ask the sum of the distance between each number and the subscript of the same number.
I didn't have much time to think. I wrote a method with explosive spatial complexity. Each element is divided into two types of statistics, one is in front of the element and the other is behind the element. For example, the subscript of the element is I, sum[i] represents the prefix and of the subscript set of the element, and j represents the number of the same element. M represents the number of elements of this type, so the first type is J × i - sum[j-1], the second type is (sum[m-1] - sum[j]) - 1LL × (m-j-1) × i. The specific implementation depends on the code. Time complexity O (N), space complexity O (N), but constants are extremely explosive. It may be better to change pos array to hash table.
code:
class Solution { public: vector<long long> getDistances(vector<int>& a) { int n = a.size(); vector<long long> ans = vector<long long>(n,0); vector<vector<int>> pos = vector<vector<int>>(1e5+1,vector<int>()); vector<long long> sum = vector<long long>(1e5+1,0); for(int i = 0 ; i < n; ++ i) pos[a[i]].push_back(i); for(int i = 0 ;i < 1e5+1;++i){ int m = pos[i].size(); sum[0] = m?pos[i][0]:0; for(int j = 1;j < m; j++) sum[j] = 1LL*pos[i][j]+sum[j-1]; for(int j = m-1;j >= 0;-- j){ long long pre = 0,aft = 0; if(j > 0) pre = 1LL*j*pos[i][j] - sum[j-1]; if(j < m-1) aft = (sum[m-1] - sum[j]) - 1LL*(m-j-1) * pos[i][j]; ans[pos[i][j]] = pre + aft; } } return ans; } };
Restore original array
It turns out that there is an array A and a k, and two arrays are generated: b[i] = a[i] + k,c[i] = c[i] - k. Now there are only mixed b,c, and d. Find a and k.
Continue the violence. Consider the relationship between b and c. obviously, b[i] = c[i] + 2 × k. Then first order the array. Assuming K is determined, you only need to start from 0 and find d[i] + 2*k for each number. If it is not found or has been used up by the previous numbers, then this k is illegal. In fact, this k is also very good for enumeration. After sorting, d[0], other N-1 numbers and his only n-1 combinations, K also has only n-1, and the time complexity is O (n2).
code:
class Solution { public: vector<int> recoverArray(vector<int>& nums) { int n = nums.size(); sort(nums.begin(),nums.end()); unordered_map<int,int> mp; vector<int> ans; int kk = 0; for(int i = 0 ; i< n; ++ i) mp[nums[i]]++; for(int i = 1; i <= n/2; ++ i){ int k = (nums[i] - nums[0])/2; if(!k || (nums[i]-nums[0])%2) continue; bool flag = 1; mp[nums[i]]--; mp[nums[0]]--; vector<int> temp; for(int j = 0;j < n; ++ j){ if(!mp[nums[j]]) continue; int next = nums[j] + 2*k; if(!mp[next]){ flag = false; break; } mp[next]--; mp[nums[j]]--; temp.push_back(next); temp.push_back(nums[j]); } for(auto x:temp) mp[x]++; mp[nums[i]]++; mp[nums[0]]++; if(flag){ kk = k; break; } } for(int i = 0;i < n; ++ i){ if(mp[nums[i]] && mp[nums[i]+2*kk]){ ans.push_back(nums[i]+kk); mp[nums[i]]--; mp[nums[i]+2*kk]--; } } return ans; } };