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 983251d

Browse files
committed
feat: add solutions to lc problems: No.1518,2094
1 parent 010a812 commit 983251d

File tree

10 files changed

+349
-31
lines changed

10 files changed

+349
-31
lines changed

‎solution/1500-1599/1518.Water Bottles/README.md‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,73 @@
5555
<li><code>2 &lt;=&nbsp;numExchange &lt;= 100</code></li>
5656
</ul>
5757

58-
5958
## 解法
6059

6160
<!-- 这里可写通用的实现逻辑 -->
6261

62+
直接模拟空瓶兑新酒即可。
63+
6364
<!-- tabs:start -->
6465

6566
### **Python3**
6667

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

6970
```python
70-
71+
class Solution:
72+
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
73+
ans = numBottles
74+
while numBottles >= numExchange:
75+
numBottles -= (numExchange - 1)
76+
ans += 1
77+
return ans
7178
```
7279

7380
### **Java**
7481

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

7784
```java
85+
class Solution {
86+
public int numWaterBottles(int numBottles, int numExchange) {
87+
int ans = numBottles;
88+
while (numBottles >= numExchange) {
89+
numBottles -= (numExchange - 1);
90+
++ans;
91+
}
92+
return ans;
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
int numWaterBottles(int numBottles, int numExchange) {
103+
int ans = numBottles;
104+
while (numBottles >= numExchange)
105+
{
106+
numBottles -= (numExchange - 1);
107+
++ans;
108+
}
109+
return ans;
110+
}
111+
};
112+
```
78113
114+
### **Go**
115+
116+
```go
117+
func numWaterBottles(numBottles int, numExchange int) int {
118+
ans := numBottles
119+
for numBottles >= numExchange {
120+
numBottles -= (numExchange - 1)
121+
ans++
122+
}
123+
return ans
124+
}
79125
```
80126

81127
### **...**

‎solution/1500-1599/1518.Water Bottles/README_EN.md‎

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,16 @@
66

77
<p>Given <code>numBottles</code>&nbsp;full water bottles, you can exchange <code>numExchange</code> empty water bottles for one full water bottle.</p>
88

9-
10-
119
<p>The operation of drinking a full water bottle turns it into an empty bottle.</p>
1210

13-
14-
1511
<p>Return the <strong>maximum</strong> number of water bottles you can&nbsp;drink.</p>
1612

17-
18-
1913
<p>&nbsp;</p>
2014

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

23-
24-
2517
<p><strong><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1500-1599/1518.Water%20Bottles/images/sample_1_1875.png" style="width: 480px; height: 240px;" /></strong></p>
2618

27-
28-
2919
<pre>
3020

3121
<strong>Input:</strong> numBottles = 9, numExchange = 3
@@ -38,16 +28,10 @@ Number of water bottles you can&nbsp;drink: 9 + 3 + 1 = 13.
3828

3929
</pre>
4030

41-
42-
4331
<p><strong>Example 2:</strong></p>
4432

45-
46-
4733
<p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1500-1599/1518.Water%20Bottles/images/sample_2_1875.png" style="width: 790px; height: 290px;" /></p>
4834

49-
50-
5135
<pre>
5236

5337
<strong>Input:</strong> numBottles = 15, numExchange = 4
@@ -60,12 +44,8 @@ Number of water bottles you can&nbsp;drink: 15 + 3 + 1 = 19.
6044

6145
</pre>
6246

63-
64-
6547
<p><strong>Example 3:</strong></p>
6648

67-
68-
6949
<pre>
7050

7151
<strong>Input:</strong> numBottles = 5, numExchange = 5
@@ -74,12 +54,8 @@ Number of water bottles you can&nbsp;drink: 15 + 3 + 1 = 19.
7454

7555
</pre>
7656

77-
78-
7957
<p><strong>Example 4:</strong></p>
8058

81-
82-
8359
<pre>
8460

8561
<strong>Input:</strong> numBottles = 2, numExchange = 3
@@ -88,14 +64,10 @@ Number of water bottles you can&nbsp;drink: 15 + 3 + 1 = 19.
8864

8965
</pre>
9066

91-
92-
9367
<p>&nbsp;</p>
9468

9569
<p><strong>Constraints:</strong></p>
9670

97-
98-
9971
<ul>
10072
<li><code>1 &lt;=&nbsp;numBottles &lt;= 100</code></li>
10173
<li><code>2 &lt;=&nbsp;numExchange &lt;= 100</code></li>
@@ -108,13 +80,58 @@ Number of water bottles you can&nbsp;drink: 15 + 3 + 1 = 19.
10880
### **Python3**
10981

11082
```python
111-
83+
class Solution:
84+
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
85+
ans = numBottles
86+
while numBottles >= numExchange:
87+
numBottles -= (numExchange - 1)
88+
ans += 1
89+
return ans
11290
```
11391

11492
### **Java**
11593

11694
```java
95+
class Solution {
96+
public int numWaterBottles(int numBottles, int numExchange) {
97+
int ans = numBottles;
98+
while (numBottles >= numExchange) {
99+
numBottles -= (numExchange - 1);
100+
++ans;
101+
}
102+
return ans;
103+
}
104+
}
105+
```
106+
107+
### **C++**
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
int numWaterBottles(int numBottles, int numExchange) {
113+
int ans = numBottles;
114+
while (numBottles >= numExchange)
115+
{
116+
numBottles -= (numExchange - 1);
117+
++ans;
118+
}
119+
return ans;
120+
}
121+
};
122+
```
117123
124+
### **Go**
125+
126+
```go
127+
func numWaterBottles(numBottles int, numExchange int) int {
128+
ans := numBottles
129+
for numBottles >= numExchange {
130+
numBottles -= (numExchange - 1)
131+
ans++
132+
}
133+
return ans
134+
}
118135
```
119136

120137
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int numWaterBottles(int numBottles, int numExchange) {
4+
int ans = numBottles;
5+
while (numBottles >= numExchange)
6+
{
7+
numBottles -= (numExchange - 1);
8+
++ans;
9+
}
10+
return ans;
11+
}
12+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func numWaterBottles(numBottles int, numExchange int) int {
2+
ans := numBottles
3+
for numBottles >= numExchange {
4+
numBottles -= (numExchange - 1)
5+
ans++
6+
}
7+
return ans
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public int numWaterBottles(int numBottles, int numExchange) {
3+
int ans = numBottles;
4+
while (numBottles >= numExchange) {
5+
numBottles -= (numExchange - 1);
6+
++ans;
7+
}
8+
return ans;
9+
}
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
3+
ans = numBottles
4+
while numBottles >= numExchange:
5+
numBottles -= (numExchange - 1)
6+
ans += 1
7+
return ans

‎solution/2000-2099/2094.Finding 3-Digit Even Numbers/README.md‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,82 @@ function check(target: Array<number>, digits: string): boolean {
178178
}
179179
```
180180

181+
### **C++**
182+
183+
```cpp
184+
class Solution {
185+
public:
186+
vector<int> findEvenNumbers(vector<int>& digits) {
187+
vector<int> counter = count(digits);
188+
vector<int> ans;
189+
for (int i = 100; i < 1000; i += 2)
190+
{
191+
vector<int> t(3);
192+
for (int j = 0, k = i; k > 0; ++j)
193+
{
194+
t[j] = k % 10;
195+
k /= 10;
196+
}
197+
vector<int> cnt = count(t);
198+
if (check(counter, cnt)) ans.push_back(i);
199+
}
200+
return ans;
201+
}
202+
203+
vector<int> count(vector<int>& nums) {
204+
vector<int> counter(10);
205+
for (int num : nums) ++counter[num];
206+
return counter;
207+
}
208+
209+
bool check(vector<int>& cnt1, vector<int>& cnt2) {
210+
for (int i = 0; i < 10; ++i)
211+
if (cnt1[i] < cnt2[i])
212+
return false;
213+
return true;
214+
}
215+
};
216+
```
217+
218+
### **Go**
219+
220+
```go
221+
func findEvenNumbers(digits []int) []int {
222+
counter := count(digits)
223+
var ans []int
224+
for i := 100; i < 1000; i += 2 {
225+
t := make([]int, 3)
226+
k := i
227+
for j := 0; k > 0; j++ {
228+
t[j] = k % 10
229+
k /= 10
230+
}
231+
cnt := count(t)
232+
if check(counter, cnt) {
233+
ans = append(ans, i)
234+
}
235+
}
236+
return ans
237+
}
238+
239+
func count(nums []int) []int {
240+
counter := make([]int, 10)
241+
for _, num := range nums {
242+
counter[num]++
243+
}
244+
return counter
245+
}
246+
247+
func check(cnt1, cnt2 []int) bool {
248+
for i := 0; i < 10; i++ {
249+
if cnt1[i] < cnt2[i] {
250+
return false
251+
}
252+
}
253+
return true
254+
}
255+
```
256+
181257
### **...**
182258

183259
```

0 commit comments

Comments
(0)

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