Skip to content

Commit 81e19c8

Browse files
committed
feat: add new lc problems and update solutions
1 parent 66c6b09 commit 81e19c8

File tree

15 files changed

+348
-74
lines changed

15 files changed

+348
-74
lines changed

solution/0700-0799/0700.Search in a Binary Search Tree/README.md

+7-22
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,9 @@
5151
# self.right = right
5252
class Solution:
5353
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
54-
if root is None:
55-
return None
56-
if root.val == val:
54+
if root is None or root.val == val:
5755
return root
58-
if root.val < val:
59-
return self.searchBST(root.right, val)
60-
return self.searchBST(root.left, val)
56+
return self.searchBST(root.right, val) if root.val < val else self.searchBST(root.left, val)
6157
```
6258

6359
### **Java**
@@ -82,16 +78,10 @@ class Solution:
8278
*/
8379
class Solution {
8480
public TreeNode searchBST(TreeNode root, int val) {
85-
if (root == null) {
86-
return null;
87-
}
88-
if (root.val == val) {
81+
if (root == null || root.val == val) {
8982
return root;
9083
}
91-
if (root.val < val) {
92-
return searchBST(root.right, val);
93-
}
94-
return searchBST(root.left, val);
84+
return root.val < val ? searchBST(root.right, val) : searchBST(root.left, val);
9585
}
9686
}
9787
```
@@ -113,10 +103,8 @@ class Solution {
113103
class Solution {
114104
public:
115105
TreeNode* searchBST(TreeNode* root, int val) {
116-
if (root == nullptr) return nullptr;
117-
if (root->val == val) return root;
118-
if (root->val < val) return searchBST(root->right, val);
119-
return searchBST(root->left, val);
106+
if (!root || root->val == val) return root;
107+
return root->val < val ? searchBST(root->right, val) : searchBST(root->left, val);
120108
}
121109
};
122110
```
@@ -133,10 +121,7 @@ public:
133121
* }
134122
*/
135123
func searchBST(root *TreeNode, val int) *TreeNode {
136-
if root == nil {
137-
return nil
138-
}
139-
if root.Val == val {
124+
if root == nil || root.Val == val {
140125
return root
141126
}
142127
if root.Val < val {

solution/0700-0799/0700.Search in a Binary Search Tree/README_EN.md

+7-22
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,9 @@
4848
# self.right = right
4949
class Solution:
5050
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
51-
if root is None:
52-
return None
53-
if root.val == val:
51+
if root is None or root.val == val:
5452
return root
55-
if root.val < val:
56-
return self.searchBST(root.right, val)
57-
return self.searchBST(root.left, val)
53+
return self.searchBST(root.right, val) if root.val < val else self.searchBST(root.left, val)
5854
```
5955

6056
### **Java**
@@ -77,16 +73,10 @@ class Solution:
7773
*/
7874
class Solution {
7975
public TreeNode searchBST(TreeNode root, int val) {
80-
if (root == null) {
81-
return null;
82-
}
83-
if (root.val == val) {
76+
if (root == null || root.val == val) {
8477
return root;
8578
}
86-
if (root.val < val) {
87-
return searchBST(root.right, val);
88-
}
89-
return searchBST(root.left, val);
79+
return root.val < val ? searchBST(root.right, val) : searchBST(root.left, val);
9080
}
9181
}
9282
```
@@ -108,10 +98,8 @@ class Solution {
10898
class Solution {
10999
public:
110100
TreeNode* searchBST(TreeNode* root, int val) {
111-
if (root == nullptr) return nullptr;
112-
if (root->val == val) return root;
113-
if (root->val < val) return searchBST(root->right, val);
114-
return searchBST(root->left, val);
101+
if (!root || root->val == val) return root;
102+
return root->val < val ? searchBST(root->right, val) : searchBST(root->left, val);
115103
}
116104
};
117105
```
@@ -128,10 +116,7 @@ public:
128116
* }
129117
*/
130118
func searchBST(root *TreeNode, val int) *TreeNode {
131-
if root == nil {
132-
return nil
133-
}
134-
if root.Val == val {
119+
if root == nil || root.Val == val {
135120
return root
136121
}
137122
if root.Val < val {

solution/0700-0799/0700.Search in a Binary Search Tree/Solution.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
class Solution {
1313
public:
1414
TreeNode* searchBST(TreeNode* root, int val) {
15-
if (root == nullptr) return nullptr;
16-
if (root->val == val) return root;
17-
if (root->val < val) return searchBST(root->right, val);
18-
return searchBST(root->left, val);
15+
if (!root || root->val == val) return root;
16+
return root->val < val ? searchBST(root->right, val) : searchBST(root->left, val);
1917
}
2018
};

solution/0700-0799/0700.Search in a Binary Search Tree/Solution.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
* }
88
*/
99
func searchBST(root *TreeNode, val int) *TreeNode {
10-
if root == nil {
11-
return nil
12-
}
13-
if root.Val == val {
10+
if root == nil || root.Val == val {
1411
return root
1512
}
1613
if root.Val < val {

solution/0700-0799/0700.Search in a Binary Search Tree/Solution.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,9 @@
1515
*/
1616
class Solution {
1717
public TreeNode searchBST(TreeNode root, int val) {
18-
if (root == null) {
19-
return null;
20-
}
21-
if (root.val == val) {
18+
if (root == null || root.val == val) {
2219
return root;
2320
}
24-
if (root.val < val) {
25-
return searchBST(root.right, val);
26-
}
27-
return searchBST(root.left, val);
21+
return root.val < val ? searchBST(root.right, val) : searchBST(root.left, val);
2822
}
2923
}

solution/0700-0799/0700.Search in a Binary Search Tree/Solution.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
# self.right = right
77
class Solution:
88
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
9-
if root is None:
10-
return None
11-
if root.val == val:
9+
if root is None or root.val == val:
1210
return root
13-
if root.val < val:
14-
return self.searchBST(root.right, val)
15-
return self.searchBST(root.left, val)
11+
return self.searchBST(root.right, val) if root.val < val else self.searchBST(root.left, val)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# [2082. The Number of Rich Customers](https://leetcode-cn.com/problems/the-number-of-rich-customers)
2+
3+
[English Version](/solution/2000-2099/2082.The%20Number%20of%20Rich%20Customers/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code>Store</code></p>
10+
11+
<pre>
12+
+-------------+------+
13+
| Column Name | Type |
14+
+-------------+------+
15+
| bill_id | int |
16+
| customer_id | int |
17+
| amount | int |
18+
+-------------+------+
19+
bill_id is the primary key for this table.
20+
Each row contains information about the amount of one bill and the customer associated with it.
21+
</pre>
22+
23+
<p>&nbsp;</p>
24+
25+
<p>Write an SQL query to report the number of customers who had <strong>at least one</strong> bill with an amount <strong>strictly greater</strong> than <code>500</code>.</p>
26+
27+
<p>The query result format is in the following example.</p>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Example 1:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong>
34+
Store table:
35+
+---------+-------------+--------+
36+
| bill_id | customer_id | amount |
37+
+---------+-------------+--------+
38+
| 6 | 1 | 549 |
39+
| 8 | 1 | 834 |
40+
| 4 | 2 | 394 |
41+
| 11 | 3 | 657 |
42+
| 13 | 3 | 257 |
43+
+---------+-------------+--------+
44+
<strong>Output:</strong>
45+
+------------+
46+
| rich_count |
47+
+------------+
48+
| 2 |
49+
+------------+
50+
<strong>Explanation:</strong>
51+
Customer 1 has two bills with amounts strictly greater than 500.
52+
Customer 2 does not have any bills with an amount strictly greater than 500.
53+
Customer 3 has one bill with an amount strictly greater than 500.
54+
</pre>
55+
56+
## 解法
57+
58+
<!-- 这里可写通用的实现逻辑 -->
59+
60+
<!-- tabs:start -->
61+
62+
### **SQL**
63+
64+
<!-- 这里可写当前语言的特殊实现逻辑 -->
65+
66+
```sql
67+
# Write your MySQL query statement below
68+
SELECT
69+
COUNT(DISTINCT(customer_id)) AS rich_count
70+
FROM
71+
Store
72+
WHERE
73+
amount > 500;
74+
```
75+
76+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# [2082. The Number of Rich Customers](https://leetcode.com/problems/the-number-of-rich-customers)
2+
3+
[中文文档](/solution/2000-2099/2082.The%20Number%20of%20Rich%20Customers/README.md)
4+
5+
## Description
6+
7+
<p>Table: <code>Store</code></p>
8+
9+
<pre>
10+
+-------------+------+
11+
| Column Name | Type |
12+
+-------------+------+
13+
| bill_id | int |
14+
| customer_id | int |
15+
| amount | int |
16+
+-------------+------+
17+
bill_id is the primary key for this table.
18+
Each row contains information about the amount of one bill and the customer associated with it.
19+
</pre>
20+
21+
<p>&nbsp;</p>
22+
23+
<p>Write an SQL query to report the number of customers who had <strong>at least one</strong> bill with an amount <strong>strictly greater</strong> than <code>500</code>.</p>
24+
25+
<p>The query result format is in the following example.</p>
26+
27+
<p>&nbsp;</p>
28+
<p><strong>Example 1:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong>
32+
Store table:
33+
+---------+-------------+--------+
34+
| bill_id | customer_id | amount |
35+
+---------+-------------+--------+
36+
| 6 | 1 | 549 |
37+
| 8 | 1 | 834 |
38+
| 4 | 2 | 394 |
39+
| 11 | 3 | 657 |
40+
| 13 | 3 | 257 |
41+
+---------+-------------+--------+
42+
<strong>Output:</strong>
43+
+------------+
44+
| rich_count |
45+
+------------+
46+
| 2 |
47+
+------------+
48+
<strong>Explanation:</strong>
49+
Customer 1 has two bills with amounts strictly greater than 500.
50+
Customer 2 does not have any bills with an amount strictly greater than 500.
51+
Customer 3 has one bill with an amount strictly greater than 500.
52+
</pre>
53+
54+
## Solutions
55+
56+
<!-- tabs:start -->
57+
58+
### **SQL**
59+
60+
```sql
61+
# Write your MySQL query statement below
62+
SELECT
63+
COUNT(DISTINCT(customer_id)) AS rich_count
64+
FROM
65+
Store
66+
WHERE
67+
amount > 500;
68+
```
69+
70+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Write your MySQL query statement below
2+
SELECT
3+
COUNT(DISTINCT(customer_id)) AS rich_count
4+
FROM
5+
Store
6+
WHERE
7+
amount > 500;

0 commit comments

Comments
 (0)