Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e88c34c

Browse files
committed
feat: add solutions to lc problems: No.0575,0889
* Add solutions to lc problem: No.0575.Distribute Candies * Update python solution to lc problem: No.0889.Construct Binary Tree from Preorder and Postorder Traversal * Add new lc problems No.2051+
1 parent 68e6a90 commit e88c34c

File tree

47 files changed

+2307
-97
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2307
-97
lines changed

‎solution/0500-0599/0575.Distribute Candies/README.md‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
</li>
3636
</ol>
3737

38-
3938
## 解法
4039

4140
<!-- 这里可写通用的实现逻辑 -->
@@ -47,15 +46,57 @@
4746
<!-- 这里可写当前语言的特殊实现逻辑 -->
4847

4948
```python
50-
49+
class Solution:
50+
def distributeCandies(self, candyType: List[int]) -> int:
51+
return min(len(candyType) >> 1, len(set(candyType)))
5152
```
5253

5354
### **Java**
5455

5556
<!-- 这里可写当前语言的特殊实现逻辑 -->
5657

5758
```java
59+
class Solution {
60+
public int distributeCandies(int[] candyType) {
61+
Set<Integer> s = new HashSet<>();
62+
for (int c : candyType) {
63+
s.add(c);
64+
}
65+
return Math.min(candyType.length >> 1, s.size());
66+
}
67+
}
68+
```
69+
70+
### **C++**
71+
72+
```cpp
73+
class Solution {
74+
public:
75+
int distributeCandies(vector<int>& candyType) {
76+
unordered_set<int> s;
77+
for (int c : candyType) s.insert(c);
78+
return min(candyType.size() >> 1, s.size());
79+
}
80+
};
81+
```
5882
83+
### **Go**
84+
85+
```go
86+
func distributeCandies(candyType []int) int {
87+
s := hashset.New()
88+
for _, c := range candyType {
89+
s.Add(c)
90+
}
91+
return min(len(candyType)>>1, s.Size())
92+
}
93+
94+
func min(a, b int) int {
95+
if a < b {
96+
return a
97+
}
98+
return b
99+
}
59100
```
60101

61102
### **...**

‎solution/0500-0599/0575.Distribute Candies/README_EN.md‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,62 @@
4545
<li><code>-10<sup>5</sup> &lt;= candyType[i] &lt;= 10<sup>5</sup></code></li>
4646
</ul>
4747

48-
4948
## Solutions
5049

5150
<!-- tabs:start -->
5251

5352
### **Python3**
5453

5554
```python
56-
55+
class Solution:
56+
def distributeCandies(self, candyType: List[int]) -> int:
57+
return min(len(candyType) >> 1, len(set(candyType)))
5758
```
5859

5960
### **Java**
6061

6162
```java
63+
class Solution {
64+
public int distributeCandies(int[] candyType) {
65+
Set<Integer> s = new HashSet<>();
66+
for (int c : candyType) {
67+
s.add(c);
68+
}
69+
return Math.min(candyType.length >> 1, s.size());
70+
}
71+
}
72+
```
73+
74+
### **C++**
75+
76+
```cpp
77+
class Solution {
78+
public:
79+
int distributeCandies(vector<int>& candyType) {
80+
unordered_set<int> s;
81+
for (int c : candyType) s.insert(c);
82+
return min(candyType.size() >> 1, s.size());
83+
}
84+
};
85+
```
6286
87+
### **Go**
88+
89+
```go
90+
func distributeCandies(candyType []int) int {
91+
s := hashset.New()
92+
for _, c := range candyType {
93+
s.Add(c)
94+
}
95+
return min(len(candyType)>>1, s.Size())
96+
}
97+
98+
func min(a, b int) int {
99+
if a < b {
100+
return a
101+
}
102+
return b
103+
}
63104
```
64105

65106
### **...**
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
int distributeCandies(vector<int>& candyType) {
4+
unordered_set<int> s;
5+
for (int c : candyType) s.insert(c);
6+
return min(candyType.size() >> 1, s.size());
7+
}
8+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func distributeCandies(candyType []int) int {
2+
s := hashset.New()
3+
for _, c := range candyType {
4+
s.Add(c)
5+
}
6+
return min(len(candyType)>>1, s.Size())
7+
}
8+
9+
func min(a, b int) int {
10+
if a < b {
11+
return a
12+
}
13+
return b
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public int distributeCandies(int[] candyType) {
3+
Set<Integer> s = new HashSet<>();
4+
for (int c : candyType) {
5+
s.add(c);
6+
}
7+
return Math.min(candyType.length >> 1, s.size());
8+
}
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def distributeCandies(self, candyType: List[int]) -> int:
3+
return min(len(candyType) >> 1, len(set(candyType)))

‎solution/0800-0899/0889.Construct Binary Tree from Preorder and Postorder Traversal/README.md‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
<li>每个输入保证至少有一个答案。如果有多个答案,可以返回其中一个。</li>
2929
</ul>
3030

31-
3231
## 解法
3332

3433
<!-- 这里可写通用的实现逻辑 -->
@@ -40,7 +39,27 @@
4039
<!-- 这里可写当前语言的特殊实现逻辑 -->
4140

4241
```python
43-
42+
# Definition for a binary tree node.
43+
# class TreeNode:
44+
# def __init__(self, val=0, left=None, right=None):
45+
# self.val = val
46+
# self.left = left
47+
# self.right = right
48+
class Solution:
49+
def constructFromPrePost(self, preorder: List[int], postorder: List[int]) -> TreeNode:
50+
n = len(preorder)
51+
if n == 0:
52+
return None
53+
root = TreeNode(preorder[0])
54+
if n == 1:
55+
return root
56+
for i in range(n - 1):
57+
if postorder[i] == preorder[1]:
58+
root.left = self.constructFromPrePost(
59+
preorder[1: 1 + i + 1], postorder[: i + 1])
60+
root.right = self.constructFromPrePost(
61+
preorder[1 + i + 1:], postorder[i + 1: -1])
62+
return root
4463
```
4564

4665
### **Java**

‎solution/0800-0899/0889.Construct Binary Tree from Preorder and Postorder Traversal/README_EN.md‎

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,14 @@
66

77
<p>Return any binary tree that matches the given preorder and postorder traversals.</p>
88

9-
10-
119
<p>Values in the traversals&nbsp;<code>pre</code> and <code>post</code>&nbsp;are distinct&nbsp;positive integers.</p>
1210

13-
14-
1511
<p>&nbsp;</p>
1612

17-
18-
1913
<div>
2014

2115
<p><strong>Example 1:</strong></p>
2216

23-
24-
2517
<pre>
2618

2719
<strong>Input: </strong>pre = <span id="example-input-1-1">[1,2,4,5,3,6,7]</span>, post = <span id="example-input-1-2">[4,5,2,6,7,3,1]</span>
@@ -30,16 +22,10 @@
3022

3123
</pre>
3224

33-
34-
3525
<p>&nbsp;</p>
3626

37-
38-
3927
<p><strong><span>Note:</span></strong></p>
4028

41-
42-
4329
<ul>
4430
<li><code>1 &lt;= pre.length == post.length &lt;= 30</code></li>
4531
<li><code>pre[]</code> and <code>post[]</code>&nbsp;are both permutations of <code>1, 2, ..., pre.length</code>.</li>
@@ -48,16 +34,34 @@
4834

4935
</div>
5036

51-
52-
5337
## Solutions
5438

5539
<!-- tabs:start -->
5640

5741
### **Python3**
5842

5943
```python
60-
44+
# Definition for a binary tree node.
45+
# class TreeNode:
46+
# def __init__(self, val=0, left=None, right=None):
47+
# self.val = val
48+
# self.left = left
49+
# self.right = right
50+
class Solution:
51+
def constructFromPrePost(self, preorder: List[int], postorder: List[int]) -> TreeNode:
52+
n = len(preorder)
53+
if n == 0:
54+
return None
55+
root = TreeNode(preorder[0])
56+
if n == 1:
57+
return root
58+
for i in range(n - 1):
59+
if postorder[i] == preorder[1]:
60+
root.left = self.constructFromPrePost(
61+
preorder[1: 1 + i + 1], postorder[: i + 1])
62+
root.right = self.constructFromPrePost(
63+
preorder[1 + i + 1:], postorder[i + 1: -1])
64+
return root
6165
```
6266

6367
### **Java**
Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,21 @@
11
# Definition for a binary tree node.
22
# class TreeNode:
3-
# def __init__(self, x):
4-
# self.val = x
5-
# self.left = None
6-
# self.right = None
7-
8-
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
97
class Solution:
10-
def constructFromPrePost(self, pre, post):
11-
"""
12-
:type pre: List[int]
13-
:type post: List[int]
14-
:rtype: TreeNode
15-
"""
16-
if pre:
17-
root = TreeNode(pre[0])
18-
if len(pre) == 1:
19-
return root
20-
else:
21-
for i in range(0, len(pre) - 1):
22-
if post[i] == pre[1]:
23-
root.left = self.constructFromPrePost(
24-
pre[1:i + 1 + 1], post[:i + 1])
25-
root.right = self.constructFromPrePost(
26-
pre[i + 1 + 1:], post[i + 1:-1])
27-
break
28-
return root
29-
else:
8+
def constructFromPrePost(self, preorder: List[int], postorder: List[int]) -> TreeNode:
9+
n = len(preorder)
10+
if n == 0:
3011
return None
12+
root = TreeNode(preorder[0])
13+
if n == 1:
14+
return root
15+
for i in range(n - 1):
16+
if postorder[i] == preorder[1]:
17+
root.left = self.constructFromPrePost(
18+
preorder[1: 1 + i + 1], postorder[: i + 1])
19+
root.right = self.constructFromPrePost(
20+
preorder[1 + i + 1:], postorder[i + 1: -1])
21+
return root

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /