Leetcode brush the daily problem and the next problem of the daily problem notes 30/30
Writing in the front
This is the 30th day of my participation in the Wenwen Challenge
About to graduate, only to find himself in the interview algorithm hanging hammer. There is no way to zero based identity and classmates to join the force buckle brush problem army. My classmates are very good, they are usually just modest, verbally said that they will not, and I really will not… Nuggets encourage new people to blog every day, I also join in a lively, record every day brush the first two questions, these two questions I do. I plan to brush five questions every day. For the other questions, I can only memorize the routine by force and will not post them in my blog.
I am really just a vegetable chicken, what do not want to solve the problem from my reference, coding habits also need to improve, if you want to find brush problem master ask questions, I think it is better to go to the palace water sanye brush problem diary this big guy. I try not to see the solution of the problem before I make it, so as not to collide with the content of the big guy.
In addition, I also hope to have the leisure of the big guy to provide some higher clear problem-solving ideas to me, welcome to discuss ha!
Good nonsense does not say to begin the first two questions of the thirtieth day!
2021.6.30 One question of the day
Offer 37. Serialize binary tree
This problem… There’s actually a way to cheat
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; * /
class Codec {
private:
TreeNode* root = new TreeNode(a);public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
if (root == nullptr) {
return "null";
}
return to_string(root->val) + ' ' + serialize(root->left) + ' ' + serialize(root->right);
}
TreeNode* dfs_deserialize(istringstream &iss){
string val;
iss >> val;
if(val == "null") return nullptr;
return new TreeNode(stoi(val), dfs_deserialize(iss), dfs_deserialize(iss));
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
istringstream iss(data);
return dfs_deserialize(iss); }};// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));
Copy the code
One of the following questions per day
1647. Character frequency Minimum number of unique deletions
Go straight to code
class Solution {
public:
int minDeletions(string s) {
int cnt[26];
memset(cnt, 0.sizeof(cnt));
for (char c : s)
{
++cnt[c-'a'];
}
// Sort from largest to smallest
sort(cnt, cnt+26, greater<int> ());int res = 0;
// Reduce the number of small numbers first
for (int i = 1; i < 26; ++i)
{
if (cnt[i] > 0 && cnt[i] >= cnt[i- 1])
{
// Compute the exact unequal case, i.e., the value greater than it minus 1
int target = cnt[i- 1] - 1> =0 ? cnt[i- 1] - 1 : 0; res += cnt[i] - target; cnt[i] = target; }}returnres; }};Copy the code
summary
How do you play o.J.
Refer to the link
There is no