diff --git a/README.md b/README.md index aae26979..28127c61 100644 --- a/README.md +++ b/README.md @@ -342,6 +342,7 @@ |[0707.设计链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0707.设计链表.md) |链表 |中等|**模拟**| |[0763.划分字母区间](https://github.com/youngyangyang04/leetcode/blob/master/problems/0763.划分字母区间.md) |贪心 |中等|**双指针/贪心** 体现贪心尽可能多的思想| |[0739.每日温度](https://github.com/youngyangyang04/leetcode/blob/master/problems/0739.每日温度.md) |栈 |中等|**单调栈** 适合单调栈入门| +|[0767.重构字符串](https://github.com/youngyangyang04/leetcode/blob/master/problems/0767.重构字符串.md) |字符串 |中等|**字符串** + 排序+一点贪心| |[0841.钥匙和房间](https://github.com/youngyangyang04/leetcode/blob/master/problems/0841.钥匙和房间.md) |孤岛问题 |中等|**bfs** **dfs**| |[0844.比较含退格的字符串](https://github.com/youngyangyang04/leetcode/blob/master/problems/0844.比较含退格的字符串.md) |字符串 |简单|**栈** **双指针优化** 使用栈的思路但没有必要使用栈| |[0925.长按键入](https://github.com/youngyangyang04/leetcode/blob/master/problems/0925.长按键入.md) |字符串 |简单|**双指针/模拟** 是一道模拟类型的题目| diff --git a/pics/700.二叉搜索树中的搜索1.png b/pics/700.二叉搜索树中的搜索1.png new file mode 100644 index 00000000..037627b1 Binary files /dev/null and b/pics/700.二叉搜索树中的搜索1.png differ diff --git a/problems/0100.相同的树.md b/problems/0100.相同的树.md index b596010a..ac495f51 100644 --- a/problems/0100.相同的树.md +++ b/problems/0100.相同的树.md @@ -1,7 +1,6 @@ ## 题目地址 https://leetcode-cn.com/problems/same-tree/ -(没写完) # 100. 相同的树 @@ -68,9 +67,9 @@ else if (tree1->val != tree2->val) return false; // 注意这里我没有 代码如下: ``` -bool outside = compare(tree1->left, tree2->left); // 左子树:左、 右子树:左 -bool inside = compare(tree1->right, tree2->right); // 左子树:右、 右子树:右 -bool isSame = outside && inside; // 左子树:中、 右子树:中(逻辑处理) +bool left = compare(tree1->left, tree2->left); // 左子树:左、 右子树:左 +bool right = compare(tree1->right, tree2->right); // 左子树:右、 右子树:右 +bool isSame = left && right; // 左子树:中、 右子树:中(逻辑处理) return isSame; ``` 最后递归的C++整体代码如下: @@ -86,9 +85,9 @@ public: // 此时就是:左右节点都不为空,且数值相同的情况 // 此时才做递归,做下一层的判断 - bool outside = compare(tree1->left, tree2->left); // 左子树:左、 右子树:左 - bool inside = compare(tree1->right, tree2->right); // 左子树:右、 右子树:右 - bool isSame = outside && inside; // 左子树:中、 右子树:中(逻辑处理) + bool left = compare(tree1->left, tree2->left); // 左子树:左、 右子树:左 + bool right = compare(tree1->right, tree2->right); // 左子树:右、 右子树:右 + bool isSame = left && right; // 左子树:中、 右子树:中(逻辑处理) return isSame; } @@ -98,7 +97,6 @@ public: }; ``` --------------------------------------------------------------- 写到这 **我给出的代码并不简洁,但是把每一步判断的逻辑都清楚的描绘出来了。** @@ -107,7 +105,6 @@ public: **盲目的照着抄,结果就是:发现这是一道“简单题”,稀里糊涂的就过了,但是真正的每一步判断逻辑未必想到清楚。** 当然我可以把如上代码整理如下: -这道题目本质上和[二叉树:我对称么?](https://mp.weixin.qq.com/s/Kgf0gjvlDlNDfKIH2b1Oxg)是一样,因为 ## 递归 @@ -157,9 +154,10 @@ public: } return true; } - - }; ``` -> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。 +> **我是[程序员Carl](https://github.com/youngyangyang04),可以找我[组队刷题](https://img-blog.csdnimg.cn/20201115103410182.png),也可以在[B站上找到我](https://space.bilibili.com/525438321),本文[leetcode刷题攻略](https://github.com/youngyangyang04/leetcode-master)已收录,更多[精彩算法文章](https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzUxNjY5NTYxNA==&action=getalbum&album_id=1485825793120387074&scene=173#wechat_redirect)尽在公众号:[代码随想录](https://img-blog.csdnimg.cn/20201124161234338.png),关注后就会发现和「代码随想录」相见恨晚!** + +**如果感觉题解对你有帮助,不要吝啬给一个👍吧!** + diff --git a/problems/0572.另一个树的子树.md b/problems/0572.另一个树的子树.md index fdac6177..7b787736 100644 --- a/problems/0572.另一个树的子树.md +++ b/problems/0572.另一个树的子树.md @@ -1,5 +1,6 @@ +判断二叉树是否对称,是否相等,是否对称,都是一个套路 ## C++代码 diff --git a/problems/0700.二叉搜索树中的搜索.md b/problems/0700.二叉搜索树中的搜索.md index cb716034..cdf713be 100644 --- a/problems/0700.二叉搜索树中的搜索.md +++ b/problems/0700.二叉搜索树中的搜索.md @@ -132,4 +132,6 @@ public: 就酱,如果学到了,就转发给身边需要的同学吧! -> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。 +> **我是[程序员Carl](https://github.com/youngyangyang04),可以找我[组队刷题](https://img-blog.csdnimg.cn/20201115103410182.png),也可以在[B站上找到我](https://space.bilibili.com/525438321),本文[leetcode刷题攻略](https://github.com/youngyangyang04/leetcode-master)已收录,更多[精彩算法文章](https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzUxNjY5NTYxNA==&action=getalbum&album_id=1485825793120387074&scene=173#wechat_redirect)尽在公众号:[代码随想录](https://img-blog.csdnimg.cn/20201124161234338.png),关注后就会发现和「代码随想录」相见恨晚!** + +**如果感觉题解对你有帮助,不要吝啬给一个👍吧!** diff --git a/problems/0767.重构字符串.md b/problems/0767.重构字符串.md new file mode 100644 index 00000000..3498f9af --- /dev/null +++ b/problems/0767.重构字符串.md @@ -0,0 +1,60 @@ + +> **如果对做了一些题目,对字符串还没有整体了解的话,可以看这篇:[字符串经典题目大总结!](https://mp.weixin.qq.com/s/gtycjyDtblmytvBRFlCZJg),相信字符串各种操作就非常清晰了。** + +扫了一圈题解,感觉大家的解答都比较高端,我来一个朴实无华的版本。 + +分为如下三步: + +* 用map统计频率字符频率 +* 转为vector(即数组)按频率从大到小排序 +* 按奇数位顺序插入,插满之后按偶数位顺序插入 + +**为什么要先按奇数位插入呢?** + +先按奇数位插入,保证最大的字符分散开,因为奇数位总是>=偶数位! + +C++代码如下: + +``` +class Solution { +private: + bool static cmp (const pair& a, const pair& b) { + return a.second > b.second; // 按照频率从大到小排序 + } +public: + string reorganizeString(string S) { + unordered_map umap; + int maxFreq = 0; + for (char s : S) { + umap[s]++; + maxFreq = max(umap[s], maxFreq); + } + if (2 * maxFreq - 1 > S.size()) return ""; + + vector> vec(umap.begin(), umap.end()); + sort(vec.begin(), vec.end(), cmp); // 给频率排个序 + + string result(S); + int index = 0;// 先按奇数位散开 + for (int i = 0; i < vec.size(); i++) { + while (vec[i].second--) { + result[index] = vec[i].first; + index += 2; + if (index >= S.size()) index = 1; // 奇数位插满了插偶数位 + } + } + return result; + } +}; +``` + +* 时间复杂度O(nlogn) +* 空间复杂度O(n) + +关于leetcode统计的击败多少多少用户,大家不必过于在意,想好代码的时间复杂度就够了,这个击败多少用户,多提交几次可能就击败100%了。 +![767. 重构字符串](https://img-blog.csdnimg.cn/202011301035035.png) + +> **我是[程序员Carl](https://github.com/youngyangyang04),可以找我[组队刷题](https://img-blog.csdnimg.cn/20201115103410182.png),也可以在[B站上找到我](https://space.bilibili.com/525438321),本文[leetcode刷题攻略](https://github.com/youngyangyang04/leetcode-master)已收录,更多[精彩算法文章](https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzUxNjY5NTYxNA==&action=getalbum&album_id=1485825793120387074&scene=173#wechat_redirect)尽在公众号:[代码随想录](https://img-blog.csdnimg.cn/20201124161234338.png),关注后就会发现和「代码随想录」相见恨晚!** + +**如果感觉题解对你有帮助,不要吝啬给一个👍吧!** +