Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c8fea0e

Browse files
committedAug 6, 2021
feat: add solutions to lc problems: No.0654,1295
1 parent e2d8ac6 commit c8fea0e

File tree

12 files changed

+440
-27
lines changed

12 files changed

+440
-27
lines changed
 

‎solution/0600-0699/0654.Maximum Binary Tree/README.md

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,144 @@
5252
<li><code>nums</code> 中的所有整数 <strong>互不相同</strong></li>
5353
</ul>
5454

55-
5655
## 解法
5756

5857
<!-- 这里可写通用的实现逻辑 -->
5958

59+
先找到数组的最大元素所在的位置,作为根节点,然后递归左右两侧的子数组,构建左右子树。
60+
6061
<!-- tabs:start -->
6162

6263
### **Python3**
6364

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

6667
```python
67-
68+
# Definition for a binary tree node.
69+
# class TreeNode:
70+
# def __init__(self, val=0, left=None, right=None):
71+
# self.val = val
72+
# self.left = left
73+
# self.right = right
74+
class Solution:
75+
def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode:
76+
def inner(nums, l, r):
77+
if l > r:
78+
return None
79+
mx = l
80+
for i in range(l + 1, r + 1):
81+
if nums[mx] < nums[i]:
82+
mx = i
83+
return TreeNode(nums[mx], inner(nums, l, mx - 1), inner(nums, mx + 1, r))
84+
85+
return inner(nums, 0, len(nums) - 1)
6886
```
6987

7088
### **Java**
7189

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

7492
```java
93+
/**
94+
* Definition for a binary tree node.
95+
* public class TreeNode {
96+
* int val;
97+
* TreeNode left;
98+
* TreeNode right;
99+
* TreeNode() {}
100+
* TreeNode(int val) { this.val = val; }
101+
* TreeNode(int val, TreeNode left, TreeNode right) {
102+
* this.val = val;
103+
* this.left = left;
104+
* this.right = right;
105+
* }
106+
* }
107+
*/
108+
class Solution {
109+
public TreeNode constructMaximumBinaryTree(int[] nums) {
110+
return construct(nums, 0, nums.length - 1);
111+
}
112+
113+
private TreeNode construct(int[] nums, int l, int r) {
114+
if (l > r) {
115+
return null;
116+
}
117+
int mx = l;
118+
for (int i = l + 1; i <= r; ++i) {
119+
if (nums[mx] < nums[i]) {
120+
mx = i;
121+
}
122+
}
123+
return new TreeNode(nums[mx], construct(nums, l, mx - 1), construct(nums, mx + 1, r));
124+
}
125+
}
126+
```
127+
128+
### **C++**
129+
130+
```cpp
131+
/**
132+
* Definition for a binary tree node.
133+
* struct TreeNode {
134+
* int val;
135+
* TreeNode *left;
136+
* TreeNode *right;
137+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
138+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
139+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
140+
* };
141+
*/
142+
class Solution {
143+
public:
144+
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
145+
return construct(nums, 0, nums.size() - 1);
146+
}
147+
148+
TreeNode* construct(vector<int>& nums, int l, int r) {
149+
if (l > r) return nullptr;
150+
int mx = l;
151+
for (int i = l + 1; i <= r; ++i) {
152+
if (nums[mx] < nums[i]) mx = i;
153+
}
154+
TreeNode* root = new TreeNode(nums[mx]);
155+
root->left = construct(nums, l, mx - 1);
156+
root->right = construct(nums, mx + 1, r);
157+
return root;
158+
}
159+
};
160+
```
75161

162+
### **Go**
163+
164+
```go
165+
/**
166+
* Definition for a binary tree node.
167+
* type TreeNode struct {
168+
* Val int
169+
* Left *TreeNode
170+
* Right *TreeNode
171+
* }
172+
*/
173+
func constructMaximumBinaryTree(nums []int) *TreeNode {
174+
return construct(nums, 0, len(nums)-1)
175+
}
176+
177+
func construct(nums []int, l, r int) *TreeNode {
178+
if l > r {
179+
return nil
180+
}
181+
mx := l
182+
for i := l + 1; i <= r; i++ {
183+
if nums[mx] < nums[i] {
184+
mx = i
185+
}
186+
}
187+
return &TreeNode{
188+
Val: nums[mx],
189+
Left: construct(nums, l, mx-1),
190+
Right: construct(nums, mx+1, r),
191+
}
192+
}
76193
```
77194

78195
### **...**

‎solution/0600-0699/0654.Maximum Binary Tree/README_EN.md

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,136 @@
4848
<li>All integers in <code>nums</code> are <strong>unique</strong>.</li>
4949
</ul>
5050

51-
5251
## Solutions
5352

5453
<!-- tabs:start -->
5554

5655
### **Python3**
5756

5857
```python
59-
58+
# Definition for a binary tree node.
59+
# class TreeNode:
60+
# def __init__(self, val=0, left=None, right=None):
61+
# self.val = val
62+
# self.left = left
63+
# self.right = right
64+
class Solution:
65+
def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode:
66+
def inner(nums, l, r):
67+
if l > r:
68+
return None
69+
mx = l
70+
for i in range(l + 1, r + 1):
71+
if nums[mx] < nums[i]:
72+
mx = i
73+
return TreeNode(nums[mx], inner(nums, l, mx - 1), inner(nums, mx + 1, r))
74+
75+
return inner(nums, 0, len(nums) - 1)
6076
```
6177

6278
### **Java**
6379

6480
```java
81+
/**
82+
* Definition for a binary tree node.
83+
* public class TreeNode {
84+
* int val;
85+
* TreeNode left;
86+
* TreeNode right;
87+
* TreeNode() {}
88+
* TreeNode(int val) { this.val = val; }
89+
* TreeNode(int val, TreeNode left, TreeNode right) {
90+
* this.val = val;
91+
* this.left = left;
92+
* this.right = right;
93+
* }
94+
* }
95+
*/
96+
class Solution {
97+
public TreeNode constructMaximumBinaryTree(int[] nums) {
98+
return construct(nums, 0, nums.length - 1);
99+
}
100+
101+
private TreeNode construct(int[] nums, int l, int r) {
102+
if (l > r) {
103+
return null;
104+
}
105+
int mx = l;
106+
for (int i = l + 1; i <= r; ++i) {
107+
if (nums[mx] < nums[i]) {
108+
mx = i;
109+
}
110+
}
111+
return new TreeNode(nums[mx], construct(nums, l, mx - 1), construct(nums, mx + 1, r));
112+
}
113+
}
114+
```
115+
116+
### **C++**
117+
118+
```cpp
119+
/**
120+
* Definition for a binary tree node.
121+
* struct TreeNode {
122+
* int val;
123+
* TreeNode *left;
124+
* TreeNode *right;
125+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
126+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
127+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
128+
* };
129+
*/
130+
class Solution {
131+
public:
132+
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
133+
return construct(nums, 0, nums.size() - 1);
134+
}
135+
136+
TreeNode* construct(vector<int>& nums, int l, int r) {
137+
if (l > r) return nullptr;
138+
int mx = l;
139+
for (int i = l + 1; i <= r; ++i) {
140+
if (nums[mx] < nums[i]) mx = i;
141+
}
142+
TreeNode* root = new TreeNode(nums[mx]);
143+
root->left = construct(nums, l, mx - 1);
144+
root->right = construct(nums, mx + 1, r);
145+
return root;
146+
}
147+
};
148+
```
65149

150+
### **Go**
151+
152+
```go
153+
/**
154+
* Definition for a binary tree node.
155+
* type TreeNode struct {
156+
* Val int
157+
* Left *TreeNode
158+
* Right *TreeNode
159+
* }
160+
*/
161+
func constructMaximumBinaryTree(nums []int) *TreeNode {
162+
return construct(nums, 0, len(nums)-1)
163+
}
164+
165+
func construct(nums []int, l, r int) *TreeNode {
166+
if l > r {
167+
return nil
168+
}
169+
mx := l
170+
for i := l + 1; i <= r; i++ {
171+
if nums[mx] < nums[i] {
172+
mx = i
173+
}
174+
}
175+
return &TreeNode{
176+
Val: nums[mx],
177+
Left: construct(nums, l, mx-1),
178+
Right: construct(nums, mx+1, r),
179+
}
180+
}
66181
```
67182

68183
### **...**
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
15+
return construct(nums, 0, nums.size() - 1);
16+
}
17+
18+
TreeNode* construct(vector<int>& nums, int l, int r) {
19+
if (l > r) return nullptr;
20+
int mx = l;
21+
for (int i = l + 1; i <= r; ++i) {
22+
if (nums[mx] < nums[i]) mx = i;
23+
}
24+
TreeNode* root = new TreeNode(nums[mx]);
25+
root->left = construct(nums, l, mx - 1);
26+
root->right = construct(nums, mx + 1, r);
27+
return root;
28+
}
29+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func constructMaximumBinaryTree(nums []int) *TreeNode {
10+
return construct(nums, 0, len(nums)-1)
11+
}
12+
13+
func construct(nums []int, l, r int) *TreeNode {
14+
if l > r {
15+
return nil
16+
}
17+
mx := l
18+
for i := l + 1; i <= r; i++ {
19+
if nums[mx] < nums[i] {
20+
mx = i
21+
}
22+
}
23+
return &TreeNode{
24+
Val: nums[mx],
25+
Left: construct(nums, l, mx-1),
26+
Right: construct(nums, mx+1, r),
27+
}
28+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public TreeNode constructMaximumBinaryTree(int[] nums) {
18+
return construct(nums, 0, nums.length - 1);
19+
}
20+
21+
private TreeNode construct(int[] nums, int l, int r) {
22+
if (l > r) {
23+
return null;
24+
}
25+
int mx = l;
26+
for (int i = l + 1; i <= r; ++i) {
27+
if (nums[mx] < nums[i]) {
28+
mx = i;
29+
}
30+
}
31+
return new TreeNode(nums[mx], construct(nums, l, mx - 1), construct(nums, mx + 1, r));
32+
}
33+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode:
9+
def inner(nums, l, r):
10+
if l > r:
11+
return None
12+
mx = l
13+
for i in range(l + 1, r + 1):
14+
if nums[mx] < nums[i]:
15+
mx = i
16+
return TreeNode(nums[mx], inner(nums, l, mx - 1), inner(nums, mx + 1, r))
17+
18+
return inner(nums, 0, len(nums) - 1)

‎solution/1200-1299/1295.Find Numbers with Even Number of Digits/README.md

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
<li><code>1 &lt;= nums[i] &lt;= 10^5</code></li>
4141
</ul>
4242

43-
4443
## 解法
4544

4645
<!-- 这里可写通用的实现逻辑 -->
@@ -56,10 +55,7 @@
5655
```python
5756
class Solution:
5857
def findNumbers(self, nums: List[int]) -> int:
59-
res = 0
60-
for num in nums:
61-
res += (len(str(num)) & 1) == 0
62-
return res
58+
return sum(1 for num in nums if (len(str(num)) & 1) == 0)
6359
```
6460

6561
### **Java**
@@ -69,15 +65,46 @@ class Solution:
6965
```java
7066
class Solution {
7167
public int findNumbers(int[] nums) {
72-
int res = 0;
68+
int s = 0;
7369
for (int num : nums) {
74-
res += (String.valueOf(num).length() & 1) == 0 ? 1 : 0;
70+
if ((String.valueOf(num).length() & 1) == 0) {
71+
++s;
72+
}
7573
}
76-
return res;
74+
return s;
7775
}
7876
}
7977
```
8078

79+
### **C++**
80+
81+
```cpp
82+
class Solution {
83+
public:
84+
int findNumbers(vector<int>& nums) {
85+
int s = 0;
86+
for (int num : nums) {
87+
s += (to_string(num).size() & 1) == 0;
88+
}
89+
return s;
90+
}
91+
};
92+
```
93+
94+
### **Go**
95+
96+
```go
97+
func findNumbers(nums []int) int {
98+
s := 0
99+
for _, num := range nums {
100+
if (len(strconv.Itoa(num)) & 1) == 0 {
101+
s++
102+
}
103+
}
104+
return s
105+
}
106+
```
107+
81108
### **...**
82109

83110
```

‎solution/1200-1299/1295.Find Numbers with Even Number of Digits/README_EN.md

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## Description
66

77
Given an array <code>nums</code> of integers, return how many of them contain an <strong>even number</strong> of digits.
8+
89
<p>&nbsp;</p>
910
<p><strong>Example 1:</strong></p>
1011

@@ -37,7 +38,6 @@ Only 1771 contains an even number of digits.
3738
<li><code>1 &lt;= nums[i] &lt;= 10^5</code></li>
3839
</ul>
3940

40-
4141
## Solutions
4242

4343
<!-- tabs:start -->
@@ -47,23 +47,51 @@ Only 1771 contains an even number of digits.
4747
```python
4848
class Solution:
4949
def findNumbers(self, nums: List[int]) -> int:
50-
res = 0
51-
for num in nums:
52-
res += (len(str(num)) & 1) == 0
53-
return res
50+
return sum(1 for num in nums if (len(str(num)) & 1) == 0)
5451
```
5552

5653
### **Java**
5754

5855
```java
5956
class Solution {
6057
public int findNumbers(int[] nums) {
61-
int res = 0;
58+
int s = 0;
59+
for (int num : nums) {
60+
if ((String.valueOf(num).length() & 1) == 0) {
61+
++s;
62+
}
63+
}
64+
return s;
65+
}
66+
}
67+
```
68+
69+
### **C++**
70+
71+
```cpp
72+
class Solution {
73+
public:
74+
int findNumbers(vector<int>& nums) {
75+
int s = 0;
6276
for (int num : nums) {
63-
res += (String.valueOf(num).length() & 1) == 0 ? 1 : 0;
77+
s += (to_string(num).size() & 1) == 0;
6478
}
65-
return res;
79+
return s;
6680
}
81+
};
82+
```
83+
84+
### **Go**
85+
86+
```go
87+
func findNumbers(nums []int) int {
88+
s := 0
89+
for _, num := range nums {
90+
if (len(strconv.Itoa(num)) & 1) == 0 {
91+
s++
92+
}
93+
}
94+
return s
6795
}
6896
```
6997

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
int findNumbers(vector<int>& nums) {
4+
int s = 0;
5+
for (int num : nums) {
6+
s += (to_string(num).size() & 1) == 0;
7+
}
8+
return s;
9+
}
10+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func findNumbers(nums []int) int {
2+
s := 0
3+
for _, num := range nums {
4+
if (len(strconv.Itoa(num)) & 1) == 0 {
5+
s++
6+
}
7+
}
8+
return s
9+
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
class Solution {
22
public int findNumbers(int[] nums) {
3-
int res = 0;
3+
int s = 0;
44
for (int num : nums) {
5-
res += (String.valueOf(num).length() & 1) == 0 ? 1 : 0;
5+
if ((String.valueOf(num).length() & 1) == 0) {
6+
++s;
7+
}
68
}
7-
return res;
9+
return s;
810
}
911
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
class Solution:
22
def findNumbers(self, nums: List[int]) -> int:
3-
res = 0
4-
for num in nums:
5-
res += (len(str(num)) & 1) == 0
6-
return res
3+
return sum(1 for num in nums if (len(str(num)) & 1) == 0)

0 commit comments

Comments
 (0)
Please sign in to comment.