Force deduction Hot100 single person plan c + + Version (I)

Force deduction Hot100 single personal plan c + + Version (II)

Force deduction Hot100 single personal plan c + + Version (III)

Force deduction Hot100 single personal plan c + + Version (IV)

Force deduction Hot100 single personal plan c + + Version (V)

Link to brush questions: Force buckle Hot 100

One question per day, one watch per day, whiteboard handwriting.

# 81. House raiding III

11.22 punch in

Tree dp, the introductory question is a dance without a boss. This question is similar to the introductory question. That is, for a node, either select the node or not, and both statuses will be returned.

struct selectnode{ int s0,s1; }; class Solution { public: selectnode dfs(TreeNode* p){ if(p==nullptr)return {0,0}; auto l=dfs(p->left); auto r=dfs(p->right); int s0=max(l.s0,l.s1)+max(r.s0,r.s1); int s1=p->val+l.s0+r.s0; return {s0,s1}; } int rob(TreeNode* root) { selectnode r=dfs(root); return max(r.s0,r.s1); } };

# 82. Bit count

11.24 punch in

The feeling is to find a regular problem. Every time you pass the nth power of 2, it is a cycle. Code 8421 similar to digital. Because the highest order of the nth power of every 2 becomes 1, and the number of 0-1 is 0,1, then 2-3 is to add one on the basis of 0-1, and 4-7 is to add one on the basis of 0-3. So you can cycle each cycle. The corresponding problem solution is method 2. It is normal to fail to understand it. After all, the officials have not spoken human words, so they terminalize the simple understanding. To understand the official solution, you only need to understand the binary representation of the original code. Although the computer saves and processes data in the form of complement, the complement of positive numbers is the same as the original code, so you only need to understand
i
&
(
i
−
1
)
i\&(i-1)
i & (i − 1) is actually a number in which the last 1 in the binary number i is changed to 0.

class Solution { public: vector<int> countBits(int n) { if(n==0)return {0}; vector<int> nums(n+1); nums[0]=0;nums[1]=1; int round=1; for(int i=2;i<=n;++i){ if(i==2*round)round=round<<1;//The official writing here: if (I & (i-1) = = 0) round = I; //Actually, the two are the same. I & (i-1) is actually the number whose last 1 in I binary is changed to 0 nums[i]=1+nums[i-round]; } return nums; } };

In addition, from the solution of the law dp of bit system, the last digit of even number must be 0, so the number of even number 1 is the same as that after dividing by two, and the last digit of odd number is 1, so the number of 1 is the number of digits after dividing by two plus 1.

class Solution { public: vector<int> countBits(int n) { vector<int> nums(n+1); nums[0]=0; for(int i=1;i<=n;++i){ if(i%2)nums[i]=nums[i>>1]+1;//The remainder representing i divided by 2 can also be used for bit operation i & 1 else nums[i]=nums[i>>1]; } return nums; } };

There is also a clever solution. For each positive number, the number of 1 is equal to the number of 1 whose lowest 1 becomes 0 plus 1.

class Solution { public: vector<int> countBits(int n) { vector<int> nums(n+1); nums[0]=0; for(int i=1;i<=n;++i){ nums[i]=nums[i&(i-1)]+1; //cout<<i<<" "<<(i&(i-1))<<endl; } return nums; } };

# 83. Top K high frequency elements

11.25 punch in

and 63. The kth largest element in the array Almost as like as two peas, the difference lies in the question of judging priority, namely, to change the comparison of the larger than the number to the size of the quantity. No more here.

# 84. String decoding

11.25 punch in

Huawei was asked about the deformation of the question. At that time, it was also written recursively, but some small problems led to the non execution of recursive numbers. When time was limited, it was roughly said, and the idea ended hastily. The interview manager said that it could also be realized by stack. The problem is very clear. The difficulty is to pay attention to some details of recursion.

Today, I finally know the reason for the failure. If recursion is used, you only need to judge whether the current character is a number or a letter. If it is a letter, give the next character to the next recursion for execution. Don't judge whether the next letter is'] 'or a number. In this way, recursion is complicated. You need to always understand that the defined recursion returns the decoded string after the current character. You only need to splice it with the currently decoded character.

class Solution { public: int cur; int getdigit(const string s){ int num=0; while(isdigit(s[cur])){ num*=10; num+=s[cur++]-'0'; } return num; } string dfs(const string s,const int n){ if(cur==n||s[cur]==']')return ""; string ret=""; if(isdigit(s[cur])){ int repnum=getdigit(s); char c=s[cur]; ++cur;//Skip left parenthesis string tmp=dfs(s,n); ++cur;//Skip closing bracket while(repnum--)ret+=tmp; }else if(isalpha(s[cur])){ ret+=s[cur++]; } return ret+dfs(s,n); } string decodeString(string s) { int n=s.size(); cur=0; return dfs(s,n); } };

# 85. Division evaluation

11.26 punch in

Without thinking, I didn't expect that the solution to the problem was transformed into graph theory to construct a directed graph, which was wonderful. Union algorithm, including path compression. To be supplemented.