Skip to content

Commit 65e5155

Browse files
committed
feat: add solutions to lc problem: No.0594
No.0594.Longest Harmonious Subsequence
1 parent 6417623 commit 65e5155

File tree

12 files changed

+466
-44
lines changed

12 files changed

+466
-44
lines changed

solution/0500-0599/0594.Longest Harmonious Subsequence/README.md

+22-14
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52-
先用哈希表统计每个元素出现的次数。然后遍历数组,判断比每个元素 `num` 大 1 的数字 `num + 1` 是否在哈希表中,若是,累计 `num``num + 1` 出现的次数,与最大值 res 比较。若更大,则替换。最后返回 res 即可。
52+
先用哈希表统计每个元素出现的次数。然后遍历数组,判断比每个元素 `num` 大 1 的数字 `num + 1` 是否在哈希表中,若是,累计 `num``num + 1` 出现的次数,与最大值 ans 比较。若更大,则替换。最后返回 ans 即可。
5353

5454
<!-- tabs:start -->
5555

@@ -60,12 +60,20 @@
6060
```python
6161
class Solution:
6262
def findLHS(self, nums: List[int]) -> int:
63-
counter = collections.Counter(nums)
64-
res = 0
63+
ans = 0
64+
counter = Counter(nums)
6565
for num in nums:
6666
if num + 1 in counter:
67-
res = max(res, counter[num] + counter[num + 1])
68-
return res
67+
ans = max(ans, counter[num] + counter[num + 1])
68+
return ans
69+
```
70+
71+
72+
```python
73+
class Solution:
74+
def findLHS(self, nums: List[int]) -> int:
75+
counter = Counter(nums)
76+
return max([counter[num] + counter[num + 1] for num in nums if num + 1 in counter], default=0)
6977
```
7078

7179
### **Java**
@@ -79,13 +87,13 @@ class Solution {
7987
for (int num : nums) {
8088
counter.put(num, counter.getOrDefault(num, 0) + 1);
8189
}
82-
int res = 0;
90+
int ans = 0;
8391
for (int num : nums) {
8492
if (counter.containsKey(num + 1)) {
85-
res = Math.max(res, counter.get(num) + counter.get(num + 1));
93+
ans = Math.max(ans, counter.get(num) + counter.get(num + 1));
8694
}
8795
}
88-
return res;
96+
return ans;
8997
}
9098
}
9199
```
@@ -100,13 +108,13 @@ public:
100108
for (int num : nums) {
101109
++counter[num];
102110
}
103-
int res = 0;
111+
int ans = 0;
104112
for (int num : nums) {
105113
if (counter.count(num + 1)) {
106-
res = max(res, counter[num] + counter[num + 1]);
114+
ans = max(ans, counter[num] + counter[num + 1]);
107115
}
108116
}
109-
return res;
117+
return ans;
110118
}
111119
};
112120
```
@@ -119,13 +127,13 @@ func findLHS(nums []int) int {
119127
for _, num := range nums {
120128
counter[num]++
121129
}
122-
res := 0
130+
ans := 0
123131
for _, num := range nums {
124132
if counter[num+1] > 0 {
125-
res = max(res, counter[num]+counter[num+1])
133+
ans = max(ans, counter[num]+counter[num+1])
126134
}
127135
}
128-
return res
136+
return ans
129137
}
130138
131139
func max(a, b int) int {

solution/0500-0599/0594.Longest Harmonious Subsequence/README_EN.md

+20-13
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,19 @@
5050
```python
5151
class Solution:
5252
def findLHS(self, nums: List[int]) -> int:
53-
counter = collections.Counter(nums)
54-
res = 0
53+
counter = Counter(nums)
54+
ans = 0
5555
for num in nums:
5656
if num + 1 in counter:
57-
res = max(res, counter[num] + counter[num + 1])
58-
return res
57+
ans = max(ans, counter[num] + counter[num + 1])
58+
return ans
59+
```
60+
61+
```python
62+
class Solution:
63+
def findLHS(self, nums: List[int]) -> int:
64+
counter = Counter(nums)
65+
return max([counter[num] + counter[num + 1] for num in nums if num + 1 in counter], default=0)
5966
```
6067

6168
### **Java**
@@ -67,13 +74,13 @@ class Solution {
6774
for (int num : nums) {
6875
counter.put(num, counter.getOrDefault(num, 0) + 1);
6976
}
70-
int res = 0;
77+
int ans = 0;
7178
for (int num : nums) {
7279
if (counter.containsKey(num + 1)) {
73-
res = Math.max(res, counter.get(num) + counter.get(num + 1));
80+
ans = Math.max(ans, counter.get(num) + counter.get(num + 1));
7481
}
7582
}
76-
return res;
83+
return ans;
7784
}
7885
}
7986
```
@@ -88,13 +95,13 @@ public:
8895
for (int num : nums) {
8996
++counter[num];
9097
}
91-
int res = 0;
98+
int ans = 0;
9299
for (int num : nums) {
93100
if (counter.count(num + 1)) {
94-
res = max(res, counter[num] + counter[num + 1]);
101+
ans = max(ans, counter[num] + counter[num + 1]);
95102
}
96103
}
97-
return res;
104+
return ans;
98105
}
99106
};
100107
```
@@ -107,13 +114,13 @@ func findLHS(nums []int) int {
107114
for _, num := range nums {
108115
counter[num]++
109116
}
110-
res := 0
117+
ans := 0
111118
for _, num := range nums {
112119
if counter[num+1] > 0 {
113-
res = max(res, counter[num]+counter[num+1])
120+
ans = max(ans, counter[num]+counter[num+1])
114121
}
115122
}
116-
return res
123+
return ans
117124
}
118125
119126
func max(a, b int) int {

solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ class Solution {
55
for (int num : nums) {
66
++counter[num];
77
}
8-
int res = 0;
8+
int ans = 0;
99
for (int num : nums) {
1010
if (counter.count(num + 1)) {
11-
res = max(res, counter[num] + counter[num + 1]);
11+
ans = max(ans, counter[num] + counter[num + 1]);
1212
}
1313
}
14-
return res;
14+
return ans;
1515
}
1616
};

solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ func findLHS(nums []int) int {
33
for _, num := range nums {
44
counter[num]++
55
}
6-
res := 0
6+
ans := 0
77
for _, num := range nums {
88
if counter[num+1] > 0 {
9-
res = max(res, counter[num]+counter[num+1])
9+
ans = max(ans, counter[num]+counter[num+1])
1010
}
1111
}
12-
return res
12+
return ans
1313
}
1414

1515
func max(a, b int) int {

solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ public int findLHS(int[] nums) {
44
for (int num : nums) {
55
counter.put(num, counter.getOrDefault(num, 0) + 1);
66
}
7-
int res = 0;
7+
int ans = 0;
88
for (int num : nums) {
99
if (counter.containsKey(num + 1)) {
10-
res = Math.max(res, counter.get(num) + counter.get(num + 1));
10+
ans = Math.max(ans, counter.get(num) + counter.get(num + 1));
1111
}
1212
}
13-
return res;
13+
return ans;
1414
}
1515
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution:
22
def findLHS(self, nums: List[int]) -> int:
3-
counter = collections.Counter(nums)
4-
res = 0
3+
counter = Counter(nums)
4+
ans = 0
55
for num in nums:
66
if num + 1 in counter:
7-
res = max(res, counter[num] + counter[num + 1])
8-
return res
7+
ans = max(ans, counter[num] + counter[num + 1])
8+
return ans

solution/0700-0799/0748.Shortest Completing Word/README.md

+140-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
<li><code>words[i]</code> 由小写英文字母组成</li>
7373
</ul>
7474

75-
7675
## 解法
7776

7877
<!-- 这里可写通用的实现逻辑 -->
@@ -84,15 +83,154 @@
8483
<!-- 这里可写当前语言的特殊实现逻辑 -->
8584

8685
```python
87-
86+
class Solution:
87+
def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str:
88+
def count(word):
89+
counter = [0] * 26
90+
for c in word:
91+
counter[ord(c) - ord('a')] += 1
92+
return counter
93+
94+
def check(counter1, counter2):
95+
for i in range(26):
96+
if counter1[i] > counter2[i]:
97+
return False
98+
return True
99+
100+
counter = count(c.lower() for c in licensePlate if c.isalpha())
101+
ans, n = None, 16
102+
for word in words:
103+
if n <= len(word):
104+
continue
105+
t = count(word)
106+
if check(counter, t):
107+
n = len(word)
108+
ans = word
109+
return ans
88110
```
89111

90112
### **Java**
91113

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

94116
```java
117+
class Solution {
118+
public String shortestCompletingWord(String licensePlate, String[] words) {
119+
int[] counter = count(licensePlate.toLowerCase());
120+
String ans = null;
121+
int n = 16;
122+
for (String word : words) {
123+
if (n <= word.length()) {
124+
continue;
125+
}
126+
int[] t = count(word);
127+
if (check(counter, t)) {
128+
n = word.length();
129+
ans = word;
130+
}
131+
}
132+
return ans;
133+
}
134+
135+
private int[] count(String word) {
136+
int[] counter = new int[26];
137+
for (char c : word.toCharArray()) {
138+
if (Character.isLetter(c)) {
139+
++counter[c - 'a'];
140+
}
141+
142+
}
143+
return counter;
144+
}
145+
146+
private boolean check(int[] counter1, int[] counter2) {
147+
for (int i = 0; i < 26; ++i) {
148+
if (counter1[i] > counter2[i]) {
149+
return false;
150+
}
151+
}
152+
return true;
153+
}
154+
}
155+
```
156+
157+
### **C++**
158+
159+
```cpp
160+
class Solution {
161+
public:
162+
string shortestCompletingWord(string licensePlate, vector<string>& words) {
163+
vector<int> counter = count(licensePlate);
164+
int n = 16;
165+
string ans;
166+
for (auto& word : words)
167+
{
168+
if (n <= word.size()) continue;
169+
vector<int> t = count(word);
170+
if (check(counter, t))
171+
{
172+
n = word.size();
173+
ans = word;
174+
}
175+
}
176+
return ans;
177+
}
178+
179+
vector<int> count(string& word) {
180+
vector<int> counter(26);
181+
for (char& c : word)
182+
if (isalpha(c))
183+
++counter[tolower(c) - 'a'];
184+
return counter;
185+
}
186+
187+
bool check(vector<int>& counter1, vector<int>& counter2) {
188+
for (int i = 0; i < 26; ++i)
189+
if (counter1[i] > counter2[i])
190+
return false;
191+
return true;
192+
}
193+
};
194+
```
95195
196+
### **Go**
197+
198+
```go
199+
func shortestCompletingWord(licensePlate string, words []string) string {
200+
count := func(word string) []int {
201+
counter := make([]int, 26)
202+
for _, c := range word {
203+
if unicode.IsLetter(c) {
204+
counter[c-'a']++
205+
}
206+
}
207+
return counter
208+
}
209+
210+
check := func(cnt1, cnt2 []int) bool {
211+
for i := 0; i < 26; i++ {
212+
if cnt1[i] > cnt2[i] {
213+
return false
214+
}
215+
}
216+
return true
217+
}
218+
219+
counter := count(strings.ToLower(licensePlate))
220+
var ans string
221+
n := 16
222+
for _, word := range words {
223+
if n <= len(word) {
224+
continue
225+
}
226+
t := count(word)
227+
if check(counter, t) {
228+
n = len(word)
229+
ans = word
230+
}
231+
}
232+
return ans
233+
}
96234
```
97235

98236
### **...**

0 commit comments

Comments
 (0)