Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ddc13d6

Browse files
authored
feat: update solutions to lc problems: No.518,520 (doocs#2501)
1 parent 21f559f commit ddc13d6

File tree

9 files changed

+51
-32
lines changed

9 files changed

+51
-32
lines changed

solution/0500-0599/0518.Coin Change II/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class Solution {
129129
public:
130130
int change(int amount, vector<int>& coins) {
131131
int m = coins.size(), n = amount;
132-
int f[m + 1][n + 1];
132+
unsigned f[m + 1][n + 1];
133133
memset(f, 0, sizeof(f));
134134
f[0][0] = 1;
135135
for (int i = 1; i <= m; ++i) {
@@ -220,7 +220,7 @@ class Solution {
220220
public:
221221
int change(int amount, vector<int>& coins) {
222222
int n = amount;
223-
int f[n + 1];
223+
unsigned f[n + 1];
224224
memset(f, 0, sizeof(f));
225225
f[0] = 1;
226226
for (int x : coins) {

solution/0500-0599/0518.Coin Change II/README_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class Solution {
122122
public:
123123
int change(int amount, vector<int>& coins) {
124124
int m = coins.size(), n = amount;
125-
int f[m + 1][n + 1];
125+
unsigned f[m + 1][n + 1];
126126
memset(f, 0, sizeof(f));
127127
f[0][0] = 1;
128128
for (int i = 1; i <= m; ++i) {
@@ -213,7 +213,7 @@ class Solution {
213213
public:
214214
int change(int amount, vector<int>& coins) {
215215
int n = amount;
216-
int f[n + 1];
216+
unsigned f[n + 1];
217217
memset(f, 0, sizeof(f));
218218
f[0] = 1;
219219
for (int x : coins) {

solution/0500-0599/0518.Coin Change II/Solution.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Solution {
22
public:
33
int change(int amount, vector<int>& coins) {
44
int m = coins.size(), n = amount;
5-
int f[m + 1][n + 1];
5+
unsigned f[m + 1][n + 1];
66
memset(f, 0, sizeof(f));
77
f[0][0] = 1;
88
for (int i = 1; i <= m; ++i) {

solution/0500-0599/0518.Coin Change II/Solution2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Solution {
22
public:
33
int change(int amount, vector<int>& coins) {
44
int n = amount;
5-
int f[n + 1];
5+
unsigned f[n + 1];
66
memset(f, 0, sizeof(f));
77
f[0] = 1;
88
for (int x : coins) {

solution/0500-0599/0520.Detect Capital/README.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,22 @@
4545

4646
## 解法
4747

48-
### 方法一
48+
### 方法一:统计大写字母的个数
49+
50+
我们可以统计字符串中大写字母的个数,然后根据大写字母的个数判断是否符合题目要求。
51+
52+
- 如果大写字母的个数为 0 或者等于字符串的长度,那么返回 `true`
53+
- 如果大写字母的个数为 1 并且第一个字母是大写字母,那么返回 `true`
54+
- 否则返回 `false`
55+
56+
时间复杂度 $O(n)$,其中 $n$ 为字符串 `word` 的长度。空间复杂度 $O(1)$。
4957

5058
<!-- tabs:start -->
5159

5260
```python
5361
class Solution:
5462
def detectCapitalUse(self, word: str) -> bool:
55-
cnt = 0
56-
for c in word:
57-
if c.isupper():
58-
cnt += 1
63+
cnt = sum(c.isupper() for c in word)
5964
return cnt == 0 or cnt == len(word) or (cnt == 1 and word[0].isupper())
6065
```
6166

@@ -78,10 +83,8 @@ class Solution {
7883
class Solution {
7984
public:
8085
bool detectCapitalUse(string word) {
81-
int cnt = 0;
82-
for (char c : word)
83-
if (isupper(c)) ++cnt;
84-
return cnt == 0 || cnt == word.size() || (cnt == 1 && isupper(word[0]));
86+
int cnt = count_if(word.begin(), word.end(), [](char c) { return isupper(c); });
87+
return cnt == 0 || cnt == word.length() || (cnt == 1 && isupper(word[0]));
8588
}
8689
};
8790
```
@@ -98,6 +101,13 @@ func detectCapitalUse(word string) bool {
98101
}
99102
```
100103

104+
```ts
105+
function detectCapitalUse(word: string): boolean {
106+
const cnt = word.split('').reduce((acc, c) => acc + (c === c.toUpperCase() ? 1 : 0), 0);
107+
return cnt === 0 || cnt === word.length || (cnt === 1 && word[0] === word[0].toUpperCase());
108+
}
109+
```
110+
101111
<!-- tabs:end -->
102112

103113
<!-- end -->

solution/0500-0599/0520.Detect Capital/README_EN.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,22 @@
3434

3535
## Solutions
3636

37-
### Solution 1
37+
### Solution 1: Count the Number of Uppercase Letters
38+
39+
We can count the number of uppercase letters in the string, and then determine whether it meets the requirements of the problem based on the number of uppercase letters.
40+
41+
- If the number of uppercase letters is 0 or equal to the length of the string, then return `true`.
42+
- If the number of uppercase letters is 1 and the first letter is an uppercase letter, then return `true`.
43+
- Otherwise, return `false`.
44+
45+
The time complexity is $O(n)$, where $n$ is the length of the string `word`. The space complexity is $O(1)$.
3846

3947
<!-- tabs:start -->
4048

4149
```python
4250
class Solution:
4351
def detectCapitalUse(self, word: str) -> bool:
44-
cnt = 0
45-
for c in word:
46-
if c.isupper():
47-
cnt += 1
52+
cnt = sum(c.isupper() for c in word)
4853
return cnt == 0 or cnt == len(word) or (cnt == 1 and word[0].isupper())
4954
```
5055

@@ -67,10 +72,8 @@ class Solution {
6772
class Solution {
6873
public:
6974
bool detectCapitalUse(string word) {
70-
int cnt = 0;
71-
for (char c : word)
72-
if (isupper(c)) ++cnt;
73-
return cnt == 0 || cnt == word.size() || (cnt == 1 && isupper(word[0]));
75+
int cnt = count_if(word.begin(), word.end(), [](char c) { return isupper(c); });
76+
return cnt == 0 || cnt == word.length() || (cnt == 1 && isupper(word[0]));
7477
}
7578
};
7679
```
@@ -87,6 +90,13 @@ func detectCapitalUse(word string) bool {
8790
}
8891
```
8992

93+
```ts
94+
function detectCapitalUse(word: string): boolean {
95+
const cnt = word.split('').reduce((acc, c) => acc + (c === c.toUpperCase() ? 1 : 0), 0);
96+
return cnt === 0 || cnt === word.length || (cnt === 1 && word[0] === word[0].toUpperCase());
97+
}
98+
```
99+
90100
<!-- tabs:end -->
91101

92102
<!-- end -->
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
class Solution {
22
public:
33
bool detectCapitalUse(string word) {
4-
int cnt = 0;
5-
for (char c : word)
6-
if (isupper(c)) ++cnt;
7-
return cnt == 0 || cnt == word.size() || (cnt == 1 && isupper(word[0]));
4+
int cnt = count_if(word.begin(), word.end(), [](char c) { return isupper(c); });
5+
return cnt == 0 || cnt == word.length() || (cnt == 1 && isupper(word[0]));
86
}
97
};
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
class Solution:
22
def detectCapitalUse(self, word: str) -> bool:
3-
cnt = 0
4-
for c in word:
5-
if c.isupper():
6-
cnt += 1
3+
cnt = sum(c.isupper() for c in word)
74
return cnt == 0 or cnt == len(word) or (cnt == 1 and word[0].isupper())
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function detectCapitalUse(word: string): boolean {
2+
const cnt = word.split('').reduce((acc, c) => acc + (c === c.toUpperCase() ? 1 : 0), 0);
3+
return cnt === 0 || cnt === word.length || (cnt === 1 && word[0] === word[0].toUpperCase());
4+
}

0 commit comments

Comments
 (0)