Skip to content

Commit 15edd0d

Browse files
authored
feat: add solutions to lc problems: No.2784,2785 (doocs#2442)
* No.2784.Check if Array is Good * No.2785.Sort Vowels in a String
1 parent 5749568 commit 15edd0d

File tree

10 files changed

+163
-79
lines changed

10 files changed

+163
-79
lines changed

solution/2700-2799/2784.Check if Array is Good/README.md

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,25 @@
5555

5656
## 解法
5757

58-
### 方法一
58+
### 方法一:计数
59+
60+
我们可以用一个哈希表或数组 $cnt$ 记录数组 $nums$ 中每个元素出现的次数。然后我们判断是否满足以下条件:
61+
62+
1. $cnt[n] = 2$,即数组中最大的元素出现了两次;
63+
2. 对于 $1 \leq i \leq n-1$,均满足 $cnt[i] = 1$,即数组中除了最大的元素外,其他元素都只出现了一次。
64+
65+
如果满足以上两个条件,那么数组 $nums$ 是一个好数组,否则不是。
66+
67+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
5968

6069
<!-- tabs:start -->
6170

6271
```python
6372
class Solution:
6473
def isGood(self, nums: List[int]) -> bool:
65-
n = len(nums) - 1
6674
cnt = Counter(nums)
67-
cnt[n] -= 2
68-
for i in range(1, n):
69-
cnt[i] -= 1
70-
return all(v == 0 for v in cnt.values())
75+
n = len(nums) - 1
76+
return cnt[n] == 2 and all(cnt[i] for i in range(1, n))
7177
```
7278

7379
```java
@@ -78,12 +84,11 @@ class Solution {
7884
for (int x : nums) {
7985
++cnt[x];
8086
}
81-
cnt[n] -= 2;
82-
for (int i = 1; i < n; ++i) {
83-
cnt[i] -= 1;
87+
if (cnt[n] != 2) {
88+
return false;
8489
}
85-
for (int x : cnt) {
86-
if (x != 0) {
90+
for (int i = 1; i < n; ++i) {
91+
if (cnt[i] != 1) {
8792
return false;
8893
}
8994
}
@@ -97,16 +102,15 @@ class Solution {
97102
public:
98103
bool isGood(vector<int>& nums) {
99104
int n = nums.size() - 1;
100-
vector<int> cnt(201);
105+
int cnt[201]{};
101106
for (int x : nums) {
102107
++cnt[x];
103108
}
104-
cnt[n] -= 2;
105-
for (int i = 1; i < n; ++i) {
106-
--cnt[i];
109+
if (cnt[n] != 2) {
110+
return false;
107111
}
108-
for (int x : cnt) {
109-
if (x) {
112+
for (int i = 1; i < n; ++i) {
113+
if (cnt[i] != 1) {
110114
return false;
111115
}
112116
}
@@ -122,12 +126,11 @@ func isGood(nums []int) bool {
122126
for _, x := range nums {
123127
cnt[x]++
124128
}
125-
cnt[n] -= 2
126-
for i := 1; i < n; i++ {
127-
cnt[i]--
129+
if cnt[n] != 2 {
130+
return false
128131
}
129-
for _, x := range cnt {
130-
if x != 0 {
132+
for i := 1; i < n; i++ {
133+
if cnt[i] != 1 {
131134
return false
132135
}
133136
}
@@ -138,15 +141,40 @@ func isGood(nums []int) bool {
138141
```ts
139142
function isGood(nums: number[]): boolean {
140143
const n = nums.length - 1;
141-
const cnt: number[] = new Array(201).fill(0);
144+
const cnt: number[] = Array(201).fill(0);
142145
for (const x of nums) {
143146
++cnt[x];
144147
}
145-
cnt[n] -= 2;
148+
if (cnt[n] !== 2) {
149+
return false;
150+
}
146151
for (let i = 1; i < n; ++i) {
147-
cnt[i]--;
152+
if (cnt[i] !== 1) {
153+
return false;
154+
}
155+
}
156+
return true;
157+
}
158+
```
159+
160+
```cs
161+
public class Solution {
162+
public bool IsGood(int[] nums) {
163+
int n = nums.Length - 1;
164+
int[] cnt = new int[201];
165+
foreach (int x in nums) {
166+
++cnt[x];
167+
}
168+
if (cnt[n] != 2) {
169+
return false;
170+
}
171+
for (int i = 1; i < n; ++i) {
172+
if (cnt[i] != 1) {
173+
return false;
174+
}
175+
}
176+
return true;
148177
}
149-
return cnt.every(x => x >= 0);
150178
}
151179
```
152180

solution/2700-2799/2784.Check if Array is Good/README_EN.md

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,25 @@
5555

5656
## Solutions
5757

58-
### Solution 1
58+
### Solution 1: Counting
59+
60+
We can use a hash table or array $cnt$ to record the number of occurrences of each element in the array $nums$. Then we determine whether the following conditions are met:
61+
62+
1. $cnt[n] = 2$, i.e., the largest element in the array appears twice;
63+
2. For $1 \leq i \leq n-1$, it holds that $cnt[i] = 1$, i.e., except for the largest element, all other elements appear only once.
64+
65+
If the above two conditions are met, then the array $nums$ is a good array, otherwise it is not.
66+
67+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $nums$.
5968

6069
<!-- tabs:start -->
6170

6271
```python
6372
class Solution:
6473
def isGood(self, nums: List[int]) -> bool:
65-
n = len(nums) - 1
6674
cnt = Counter(nums)
67-
cnt[n] -= 2
68-
for i in range(1, n):
69-
cnt[i] -= 1
70-
return all(v == 0 for v in cnt.values())
75+
n = len(nums) - 1
76+
return cnt[n] == 2 and all(cnt[i] for i in range(1, n))
7177
```
7278

7379
```java
@@ -78,12 +84,11 @@ class Solution {
7884
for (int x : nums) {
7985
++cnt[x];
8086
}
81-
cnt[n] -= 2;
82-
for (int i = 1; i < n; ++i) {
83-
cnt[i] -= 1;
87+
if (cnt[n] != 2) {
88+
return false;
8489
}
85-
for (int x : cnt) {
86-
if (x != 0) {
90+
for (int i = 1; i < n; ++i) {
91+
if (cnt[i] != 1) {
8792
return false;
8893
}
8994
}
@@ -97,16 +102,15 @@ class Solution {
97102
public:
98103
bool isGood(vector<int>& nums) {
99104
int n = nums.size() - 1;
100-
vector<int> cnt(201);
105+
int cnt[201]{};
101106
for (int x : nums) {
102107
++cnt[x];
103108
}
104-
cnt[n] -= 2;
105-
for (int i = 1; i < n; ++i) {
106-
--cnt[i];
109+
if (cnt[n] != 2) {
110+
return false;
107111
}
108-
for (int x : cnt) {
109-
if (x) {
112+
for (int i = 1; i < n; ++i) {
113+
if (cnt[i] != 1) {
110114
return false;
111115
}
112116
}
@@ -122,12 +126,11 @@ func isGood(nums []int) bool {
122126
for _, x := range nums {
123127
cnt[x]++
124128
}
125-
cnt[n] -= 2
126-
for i := 1; i < n; i++ {
127-
cnt[i]--
129+
if cnt[n] != 2 {
130+
return false
128131
}
129-
for _, x := range cnt {
130-
if x != 0 {
132+
for i := 1; i < n; i++ {
133+
if cnt[i] != 1 {
131134
return false
132135
}
133136
}
@@ -138,15 +141,40 @@ func isGood(nums []int) bool {
138141
```ts
139142
function isGood(nums: number[]): boolean {
140143
const n = nums.length - 1;
141-
const cnt: number[] = new Array(201).fill(0);
144+
const cnt: number[] = Array(201).fill(0);
142145
for (const x of nums) {
143146
++cnt[x];
144147
}
145-
cnt[n] -= 2;
148+
if (cnt[n] !== 2) {
149+
return false;
150+
}
146151
for (let i = 1; i < n; ++i) {
147-
cnt[i]--;
152+
if (cnt[i] !== 1) {
153+
return false;
154+
}
155+
}
156+
return true;
157+
}
158+
```
159+
160+
```cs
161+
public class Solution {
162+
public bool IsGood(int[] nums) {
163+
int n = nums.Length - 1;
164+
int[] cnt = new int[201];
165+
foreach (int x in nums) {
166+
++cnt[x];
167+
}
168+
if (cnt[n] != 2) {
169+
return false;
170+
}
171+
for (int i = 1; i < n; ++i) {
172+
if (cnt[i] != 1) {
173+
return false;
174+
}
175+
}
176+
return true;
148177
}
149-
return cnt.every(x => x >= 0);
150178
}
151179
```
152180

solution/2700-2799/2784.Check if Array is Good/Solution.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ class Solution {
22
public:
33
bool isGood(vector<int>& nums) {
44
int n = nums.size() - 1;
5-
vector<int> cnt(201);
5+
int cnt[201]{};
66
for (int x : nums) {
77
++cnt[x];
88
}
9-
cnt[n] -= 2;
10-
for (int i = 1; i < n; ++i) {
11-
--cnt[i];
9+
if (cnt[n] != 2) {
10+
return false;
1211
}
13-
for (int x : cnt) {
14-
if (x) {
12+
for (int i = 1; i < n; ++i) {
13+
if (cnt[i] != 1) {
1514
return false;
1615
}
1716
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Solution {
2+
public bool IsGood(int[] nums) {
3+
int n = nums.Length - 1;
4+
int[] cnt = new int[201];
5+
foreach (int x in nums) {
6+
++cnt[x];
7+
}
8+
if (cnt[n] != 2) {
9+
return false;
10+
}
11+
for (int i = 1; i < n; ++i) {
12+
if (cnt[i] != 1) {
13+
return false;
14+
}
15+
}
16+
return true;
17+
}
18+
}

solution/2700-2799/2784.Check if Array is Good/Solution.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ func isGood(nums []int) bool {
44
for _, x := range nums {
55
cnt[x]++
66
}
7-
cnt[n] -= 2
8-
for i := 1; i < n; i++ {
9-
cnt[i]--
7+
if cnt[n] != 2 {
8+
return false
109
}
11-
for _, x := range cnt {
12-
if x != 0 {
10+
for i := 1; i < n; i++ {
11+
if cnt[i] != 1 {
1312
return false
1413
}
1514
}

solution/2700-2799/2784.Check if Array is Good/Solution.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ public boolean isGood(int[] nums) {
55
for (int x : nums) {
66
++cnt[x];
77
}
8-
cnt[n] -= 2;
9-
for (int i = 1; i < n; ++i) {
10-
cnt[i] -= 1;
8+
if (cnt[n] != 2) {
9+
return false;
1110
}
12-
for (int x : cnt) {
13-
if (x != 0) {
11+
for (int i = 1; i < n; ++i) {
12+
if (cnt[i] != 1) {
1413
return false;
1514
}
1615
}
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
class Solution:
22
def isGood(self, nums: List[int]) -> bool:
3-
n = len(nums) - 1
43
cnt = Counter(nums)
5-
cnt[n] -= 2
6-
for i in range(1, n):
7-
cnt[i] -= 1
8-
return all(v == 0 for v in cnt.values())
4+
n = len(nums) - 1
5+
return cnt[n] == 2 and all(cnt[i] for i in range(1, n))
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
function isGood(nums: number[]): boolean {
22
const n = nums.length - 1;
3-
const cnt: number[] = new Array(201).fill(0);
3+
const cnt: number[] = Array(201).fill(0);
44
for (const x of nums) {
55
++cnt[x];
66
}
7-
cnt[n] -= 2;
7+
if (cnt[n] !== 2) {
8+
return false;
9+
}
810
for (let i = 1; i < n; ++i) {
9-
cnt[i]--;
11+
if (cnt[i] !== 1) {
12+
return false;
13+
}
1014
}
11-
return cnt.every(x => x >= 0);
15+
return true;
1216
}

solution/2700-2799/2785.Sort Vowels in a String/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@
4848

4949
## 解法
5050

51-
### 方法一
51+
### 方法一:排序
52+
53+
我们先将字符串中的所有元音字母存到数组或列表 $vs$ 中,然后我们对 $vs$ 进行排序。
54+
55+
接下来,我们遍历字符串 $s$,保持辅音字母不变,如果是元音字母,则依次按顺序替换为 $vs$ 数组中的字母。
56+
57+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。
5258

5359
<!-- tabs:start -->
5460

0 commit comments

Comments
 (0)