Skip to content

Commit fc1606c

Browse files
committed
feat: update solutions to lc problems: No.2330,2347
* No.2330.Valid Palindrome IV * No.2347.Best Poker Hand
1 parent 3e9d114 commit fc1606c

File tree

14 files changed

+149
-93
lines changed

14 files changed

+149
-93
lines changed

solution/2300-2399/2330.Valid Palindrome IV/README.md

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656

5757
**方法一:双指针**
5858

59+
我们可以使用双指针 $i$ 和 $j$,分别指向字符串的头尾,然后向中间移动,统计不同字符的个数,如果不同字符的个数大于 $2$,则返回 `false`,否则返回 `true`
60+
61+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
62+
5963
<!-- tabs:start -->
6064

6165
### **Python3**
@@ -66,12 +70,11 @@
6670
class Solution:
6771
def makePalindrome(self, s: str) -> bool:
6872
i, j = 0, len(s) - 1
69-
t = 0
73+
cnt = 0
7074
while i < j:
71-
if s[i] != s[j]:
72-
t += 1
75+
cnt += s[i] != s[j]
7376
i, j = i + 1, j - 1
74-
return t <= 2
77+
return cnt <= 2
7578
```
7679

7780
### **Java**
@@ -81,13 +84,14 @@ class Solution:
8184
```java
8285
class Solution {
8386
public boolean makePalindrome(String s) {
84-
int t = 0;
85-
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
87+
int cnt = 0;
88+
int i = 0, j = s.length() - 1;
89+
for (; i < j; ++i, --j) {
8690
if (s.charAt(i) != s.charAt(j)) {
87-
++t;
91+
++cnt;
8892
}
8993
}
90-
return t <= 2;
94+
return cnt <= 2;
9195
}
9296
}
9397
```
@@ -98,9 +102,12 @@ class Solution {
98102
class Solution {
99103
public:
100104
bool makePalindrome(string s) {
101-
int t = 0;
102-
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) t += s[i] != s[j];
103-
return t <= 2;
105+
int cnt = 0;
106+
int i = 0, j = s.size() - 1;
107+
for (; i < j; ++i, --j) {
108+
cnt += s[i] != s[j];
109+
}
110+
return cnt <= 2;
104111
}
105112
};
106113
```
@@ -109,20 +116,31 @@ public:
109116
110117
```go
111118
func makePalindrome(s string) bool {
112-
t := 0
113-
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
119+
cnt := 0
120+
i, j := 0, len(s)-1
121+
for ; i < j; i, j = i+1, j-1 {
114122
if s[i] != s[j] {
115-
t++
123+
cnt++
116124
}
117125
}
118-
return t <= 2
126+
return cnt <= 2
119127
}
120128
```
121129

122130
### **TypeScript**
123131

124132
```ts
125-
133+
function makePalindrome(s: string): boolean {
134+
let cnt = 0;
135+
let i = 0;
136+
let j = s.length - 1;
137+
for (; i < j; ++i, --j) {
138+
if (s[i] != s[j]) {
139+
++cnt;
140+
}
141+
}
142+
return cnt <= 2;
143+
}
126144
```
127145

128146
### **...**

solution/2300-2399/2330.Valid Palindrome IV/README_EN.md

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,26 @@ Two operations could be performed to make s a palindrome so return true.
5656
class Solution:
5757
def makePalindrome(self, s: str) -> bool:
5858
i, j = 0, len(s) - 1
59-
t = 0
59+
cnt = 0
6060
while i < j:
61-
if s[i] != s[j]:
62-
t += 1
61+
cnt += s[i] != s[j]
6362
i, j = i + 1, j - 1
64-
return t <= 2
63+
return cnt <= 2
6564
```
6665

6766
### **Java**
6867

6968
```java
7069
class Solution {
7170
public boolean makePalindrome(String s) {
72-
int t = 0;
73-
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
71+
int cnt = 0;
72+
int i = 0, j = s.length() - 1;
73+
for (; i < j; ++i, --j) {
7474
if (s.charAt(i) != s.charAt(j)) {
75-
++t;
75+
++cnt;
7676
}
7777
}
78-
return t <= 2;
78+
return cnt <= 2;
7979
}
8080
}
8181
```
@@ -86,9 +86,12 @@ class Solution {
8686
class Solution {
8787
public:
8888
bool makePalindrome(string s) {
89-
int t = 0;
90-
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) t += s[i] != s[j];
91-
return t <= 2;
89+
int cnt = 0;
90+
int i = 0, j = s.size() - 1;
91+
for (; i < j; ++i, --j) {
92+
cnt += s[i] != s[j];
93+
}
94+
return cnt <= 2;
9295
}
9396
};
9497
```
@@ -97,20 +100,31 @@ public:
97100
98101
```go
99102
func makePalindrome(s string) bool {
100-
t := 0
101-
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
103+
cnt := 0
104+
i, j := 0, len(s)-1
105+
for ; i < j; i, j = i+1, j-1 {
102106
if s[i] != s[j] {
103-
t++
107+
cnt++
104108
}
105109
}
106-
return t <= 2
110+
return cnt <= 2
107111
}
108112
```
109113

110114
### **TypeScript**
111115

112116
```ts
113-
117+
function makePalindrome(s: string): boolean {
118+
let cnt = 0;
119+
let i = 0;
120+
let j = s.length - 1;
121+
for (; i < j; ++i, --j) {
122+
if (s[i] != s[j]) {
123+
++cnt;
124+
}
125+
}
126+
return cnt <= 2;
127+
}
114128
```
115129

116130
### **...**
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
class Solution {
22
public:
33
bool makePalindrome(string s) {
4-
int t = 0;
5-
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) t += s[i] != s[j];
6-
return t <= 2;
4+
int cnt = 0;
5+
int i = 0, j = s.size() - 1;
6+
for (; i < j; ++i, --j) {
7+
cnt += s[i] != s[j];
8+
}
9+
return cnt <= 2;
710
}
811
};
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
func makePalindrome(s string) bool {
2-
t := 0
3-
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
2+
cnt := 0
3+
i, j := 0, len(s)-1
4+
for ; i < j; i, j = i+1, j-1 {
45
if s[i] != s[j] {
5-
t++
6+
cnt++
67
}
78
}
8-
return t <= 2
9+
return cnt <= 2
910
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
class Solution {
22
public boolean makePalindrome(String s) {
3-
int t = 0;
4-
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
3+
int cnt = 0;
4+
int i = 0, j = s.length() - 1;
5+
for (; i < j; ++i, --j) {
56
if (s.charAt(i) != s.charAt(j)) {
6-
++t;
7+
++cnt;
78
}
89
}
9-
return t <= 2;
10+
return cnt <= 2;
1011
}
1112
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
class Solution:
22
def makePalindrome(self, s: str) -> bool:
33
i, j = 0, len(s) - 1
4-
t = 0
4+
cnt = 0
55
while i < j:
6-
if s[i] != s[j]:
7-
t += 1
6+
cnt += s[i] != s[j]
87
i, j = i + 1, j - 1
9-
return t <= 2
8+
return cnt <= 2
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function makePalindrome(s: string): boolean {
2+
let cnt = 0;
3+
let i = 0;
4+
let j = s.length - 1;
5+
for (; i < j; ++i, --j) {
6+
if (s[i] != s[j]) {
7+
++cnt;
8+
}
9+
}
10+
return cnt <= 2;
11+
}

solution/2300-2399/2347.Best Poker Hand/README.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363

6464
**方法一:计数**
6565

66-
我们可以用哈希表 $s$ 统计花色的种类,如果哈希表 $s$ 的大小为 $1$,说明五张牌的花色相同,返回 `"Flush"`
66+
我们可以先遍历数组 $suits$,判断相邻两个元素是否均相等,如果是,则返回 `"Flush"`
6767

6868
接下来,我们用哈希表或数组 $cnt$ 统计每张牌的数量:
6969

@@ -82,7 +82,7 @@
8282
```python
8383
class Solution:
8484
def bestHand(self, ranks: List[int], suits: List[str]) -> str:
85-
if len(set(suits)) == 1:
85+
if all(a == b for a, b in pairwise(suits)):
8686
return 'Flush'
8787
cnt = Counter(ranks)
8888
if any(v >= 3 for v in cnt.values()):
@@ -99,11 +99,11 @@ class Solution:
9999
```java
100100
class Solution {
101101
public String bestHand(int[] ranks, char[] suits) {
102-
Set<Character> s = new HashSet<>();
103-
for (char c : suits) {
104-
s.add(c);
102+
boolean flush = true;
103+
for (int i = 1; i < 5 && flush; ++i) {
104+
flush = suits[i] == suits[i - 1];
105105
}
106-
if (s.size() == 1) {
106+
if (flush) {
107107
return "Flush";
108108
}
109109
int[] cnt = new int[14];
@@ -125,8 +125,11 @@ class Solution {
125125
class Solution {
126126
public:
127127
string bestHand(vector<int>& ranks, vector<char>& suits) {
128-
unordered_set<char> s(suits.begin(), suits.end());
129-
if (s.size() == 1) {
128+
bool flush = true;
129+
for (int i = 1; i < 5 && flush; ++i) {
130+
flush = suits[i] == suits[i - 1];
131+
}
132+
if (flush) {
130133
return "Flush";
131134
}
132135
int cnt[14]{};
@@ -146,11 +149,11 @@ public:
146149
147150
```go
148151
func bestHand(ranks []int, suits []byte) string {
149-
s := map[byte]struct{}{}
150-
for _, c := range suits {
151-
s[c] = struct{}{}
152+
flush := true
153+
for i := 1; i < 5 && flush; i++ {
154+
flush = suits[i] == suits[i-1]
152155
}
153-
if len(s) == 1 {
156+
if flush {
154157
return "Flush"
155158
}
156159
cnt := [14]int{}
@@ -173,11 +176,11 @@ func bestHand(ranks []int, suits []byte) string {
173176

174177
```ts
175178
function bestHand(ranks: number[], suits: string[]): string {
176-
const s = new Set<string>();
177-
for (const c of suits) {
178-
s.add(c);
179+
let flush = true;
180+
for (let i = 1; i < 5 && flush; ++i) {
181+
flush = suits[i] == suits[i - 1];
179182
}
180-
if (s.size == 1) {
183+
if (flush) {
181184
return 'Flush';
182185
}
183186
const cnt = new Array(14).fill(0);

0 commit comments

Comments
 (0)