Skip to content

Commit a21101f

Browse files
Update
1 parent 9f6e4e9 commit a21101f

File tree

6 files changed

+75
-13
lines changed

6 files changed

+75
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@
342342
|[0707.设计链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0707.设计链表.md) |链表 |中等|**模拟**|
343343
|[0763.划分字母区间](https://github.com/youngyangyang04/leetcode/blob/master/problems/0763.划分字母区间.md) |贪心 |中等|**双指针/贪心** 体现贪心尽可能多的思想|
344344
|[0739.每日温度](https://github.com/youngyangyang04/leetcode/blob/master/problems/0739.每日温度.md) ||中等|**单调栈** 适合单调栈入门|
345+
|[0767.重构字符串](https://github.com/youngyangyang04/leetcode/blob/master/problems/0767.重构字符串.md) |字符串 |中等|**字符串** + 排序+一点贪心|
345346
|[0841.钥匙和房间](https://github.com/youngyangyang04/leetcode/blob/master/problems/0841.钥匙和房间.md) |孤岛问题 |中等|**bfs** **dfs**|
346347
|[0844.比较含退格的字符串](https://github.com/youngyangyang04/leetcode/blob/master/problems/0844.比较含退格的字符串.md) |字符串 |简单|**** **双指针优化** 使用栈的思路但没有必要使用栈|
347348
|[0925.长按键入](https://github.com/youngyangyang04/leetcode/blob/master/problems/0925.长按键入.md) |字符串 |简单|**双指针/模拟** 是一道模拟类型的题目|
116 KB
Loading

problems/0100.相同的树.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
## 题目地址
22
https://leetcode-cn.com/problems/same-tree/
33

4-
(没写完)
54

65
# 100. 相同的树
76

@@ -68,9 +67,9 @@ else if (tree1->val != tree2->val) return false; // 注意这里我没有
6867
代码如下:
6968

7069
```
71-
bool outside = compare(tree1->left, tree2->left); // 左子树:左、 右子树:左
72-
bool inside = compare(tree1->right, tree2->right); // 左子树:右、 右子树:右
73-
bool isSame = outside && inside; // 左子树:中、 右子树:中(逻辑处理)
70+
bool left = compare(tree1->left, tree2->left); // 左子树:左、 右子树:左
71+
bool right = compare(tree1->right, tree2->right); // 左子树:右、 右子树:右
72+
bool isSame = left && right; // 左子树:中、 右子树:中(逻辑处理)
7473
return isSame;
7574
```
7675
最后递归的C++整体代码如下:
@@ -86,9 +85,9 @@ public:
8685
8786
// 此时就是:左右节点都不为空,且数值相同的情况
8887
// 此时才做递归,做下一层的判断
89-
bool outside = compare(tree1->left, tree2->left); // 左子树:左、 右子树:左
90-
bool inside = compare(tree1->right, tree2->right); // 左子树:右、 右子树:右
91-
bool isSame = outside && inside; // 左子树:中、 右子树:中(逻辑处理)
88+
bool left = compare(tree1->left, tree2->left); // 左子树:左、 右子树:左
89+
bool right = compare(tree1->right, tree2->right); // 左子树:右、 右子树:右
90+
bool isSame = left && right; // 左子树:中、 右子树:中(逻辑处理)
9291
return isSame;
9392
9493
}
@@ -98,7 +97,6 @@ public:
9897
};
9998
```
10099

101-
-------------------------------------------------------------- 写到这
102100

103101
**我给出的代码并不简洁,但是把每一步判断的逻辑都清楚的描绘出来了。**
104102

@@ -107,7 +105,6 @@ public:
107105
**盲目的照着抄,结果就是:发现这是一道“简单题”,稀里糊涂的就过了,但是真正的每一步判断逻辑未必想到清楚。**
108106

109107
当然我可以把如上代码整理如下:
110-
这道题目本质上和[二叉树:我对称么?](https://mp.weixin.qq.com/s/Kgf0gjvlDlNDfKIH2b1Oxg)是一样,因为
111108

112109
## 递归
113110

@@ -157,9 +154,10 @@ public:
157154
}
158155
return true;
159156
}
160-
161-
162157
};
163158
```
164159

165-
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
160+
> **我是[程序员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),关注后就会发现和「代码随想录」相见恨晚!**
161+
162+
**如果感觉题解对你有帮助,不要吝啬给一个👍吧!**
163+

problems/0572.另一个树的子树.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22

3+
判断二叉树是否对称,是否相等,是否对称,都是一个套路
34

45
## C++代码
56

problems/0700.二叉搜索树中的搜索.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,6 @@ public:
132132
就酱,如果学到了,就转发给身边需要的同学吧!
133133

134134

135-
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
135+
> **我是[程序员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),关注后就会发现和「代码随想录」相见恨晚!**
136+
137+
**如果感觉题解对你有帮助,不要吝啬给一个👍吧!**

problems/0767.重构字符串.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
> **如果对做了一些题目,对字符串还没有整体了解的话,可以看这篇:[字符串经典题目大总结!](https://mp.weixin.qq.com/s/gtycjyDtblmytvBRFlCZJg),相信字符串各种操作就非常清晰了。**
3+
4+
扫了一圈题解,感觉大家的解答都比较高端,我来一个朴实无华的版本。
5+
6+
分为如下三步:
7+
8+
* 用map统计频率字符频率
9+
* 转为vector(即数组)按频率从大到小排序
10+
* 按奇数位顺序插入,插满之后按偶数位顺序插入
11+
12+
**为什么要先按奇数位插入呢?**
13+
14+
先按奇数位插入,保证最大的字符分散开,因为奇数位总是>=偶数位!
15+
16+
C++代码如下:
17+
18+
```
19+
class Solution {
20+
private:
21+
bool static cmp (const pair<int, int>& a, const pair<int, int>& b) {
22+
return a.second > b.second; // 按照频率从大到小排序
23+
}
24+
public:
25+
string reorganizeString(string S) {
26+
unordered_map<char, int> umap;
27+
int maxFreq = 0;
28+
for (char s : S) {
29+
umap[s]++;
30+
maxFreq = max(umap[s], maxFreq);
31+
}
32+
if (2 * maxFreq - 1 > S.size()) return "";
33+
34+
vector<pair<int, int>> vec(umap.begin(), umap.end());
35+
sort(vec.begin(), vec.end(), cmp); // 给频率排个序
36+
37+
string result(S);
38+
int index = 0;// 先按奇数位散开
39+
for (int i = 0; i < vec.size(); i++) {
40+
while (vec[i].second--) {
41+
result[index] = vec[i].first;
42+
index += 2;
43+
if (index >= S.size()) index = 1; // 奇数位插满了插偶数位
44+
}
45+
}
46+
return result;
47+
}
48+
};
49+
```
50+
51+
* 时间复杂度O(nlogn)
52+
* 空间复杂度O(n)
53+
54+
关于leetcode统计的击败多少多少用户,大家不必过于在意,想好代码的时间复杂度就够了,这个击败多少用户,多提交几次可能就击败100%了。
55+
![767. 重构字符串](https://img-blog.csdnimg.cn/202011301035035.png)
56+
57+
> **我是[程序员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),关注后就会发现和「代码随想录」相见恨晚!**
58+
59+
**如果感觉题解对你有帮助,不要吝啬给一个👍吧!**
60+

0 commit comments

Comments
 (0)