Skip to content

Commit 3803e27

Browse files
committed
feat: add solutions to lc problems: No.2729,2730
* No.2729.Check if The Number is Fascinating * No.2730.Find the Longest Semi-Repetitive Substring
1 parent a7f9b88 commit 3803e27

File tree

16 files changed

+425
-16
lines changed

16 files changed

+425
-16
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"prettier": "^2.8.8"
1717
},
1818
"lint-staged": {
19-
"*.{js,ts,md}": "prettier --write",
19+
"*.{js,ts,php,md}": "prettier --write",
2020
"*.py": "black -S"
2121
}
2222
}

solution/1100-1199/1171.Remove Zero Sum Consecutive Nodes from Linked List/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@
5252

5353
若链表节点的两个前缀和相等,说明两个前缀和之间的连续节点序列的和为 $0$,那么可以消去这部分连续节点。
5454

55-
第一次遍历链表,用哈希表 `last` 记录前缀和以及对应的链表节点,同一前缀和 $s$,**后者的链表节点覆盖前者**
55+
我们第一次遍历链表,用哈希表 $last$ 记录前缀和以及对应的链表节点,对于同一前缀和 $s$,后面出现的节点覆盖前面的节点
5656

57-
第二次遍历链表,若当前节点 `cur` 的前缀和 $s$ 在 `last` 出现,说明 `cur``last[s]` 之间的所有节点和为 $0$,直接修改 `cur` 的指向,`cur.next = last[s].next`,就删去了这部分和为 $0$ 的连续节点。继续往后遍历,删除所有和为 $0$ 的连续节点。
57+
接下来,我们再次遍历链表,若当前节点 $cur$ 的前缀和 $s$ 在 $last$ 出现,说明 $cur$$last[s]$ 之间的所有节点和为 $0$,我们直接修改 $cur$ 的指向,即 $cur.next = last[s].next$,这样就删去了这部分和为 $0$ 的连续节点。继续往后遍历,删除所有和为 $0$ 的连续节点。
5858

59-
最后返回链表的头节点 `dummy.next`
59+
最后返回链表的头节点 $dummy.next$
6060

6161
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为链表的长度。
6262

solution/2700-2799/2729.Check if The Number is Fascinating/README.md

+49-3
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,80 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49+
**方法一:模拟**
50+
51+
我们根据题目描述,将数字 $n$ 与 $2 \times n$ 和 $3 \times n$ 连接,得到字符串 $s$,然后判断 $s$ 是否包含数字 $1$ 到 $9$ 各一次且不包含任何 $0$ 即可。
52+
53+
时间复杂度 $O(\log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为题目给定的整数。
54+
4955
<!-- tabs:start -->
5056

5157
### **Python3**
5258

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

5561
```python
56-
62+
class Solution:
63+
def isFascinating(self, n: int) -> bool:
64+
s = str(n) + str(2 * n) + str(3 * n)
65+
return "".join(sorted(s)) == "123456789"
5766
```
5867

5968
### **Java**
6069

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

6372
```java
64-
73+
class Solution {
74+
public boolean isFascinating(int n) {
75+
String s = "" + n + (2 * n) + (3 * n);
76+
int[] cnt = new int[10];
77+
for (char c : s.toCharArray()) {
78+
if (++cnt[c - '0'] > 1) {
79+
return false;
80+
}
81+
}
82+
return cnt[0] == 0 && s.length() == 9;
83+
}
84+
}
6585
```
6686

6787
### **C++**
6888

6989
```cpp
70-
90+
class Solution {
91+
public:
92+
bool isFascinating(int n) {
93+
string s = to_string(n) + to_string(n * 2) + to_string(n * 3);
94+
sort(s.begin(), s.end());
95+
return s == "123456789";
96+
}
97+
};
7198
```
7299
73100
### **Go**
74101
75102
```go
103+
func isFascinating(n int) bool {
104+
s := strconv.Itoa(n) + strconv.Itoa(n*2) + strconv.Itoa(n*3)
105+
cnt := [10]int{}
106+
for _, c := range s {
107+
cnt[c-'0']++
108+
if cnt[c-'0'] > 1 {
109+
return false
110+
}
111+
}
112+
return cnt[0] == 0 && len(s) == 9
113+
}
114+
```
115+
116+
### **TypeScript**
76117

118+
```ts
119+
function isFascinating(n: number): boolean {
120+
const s = `${n}${n * 2}${n * 3}`;
121+
return s.split('').sort().join('') === '123456789';
122+
}
77123
```
78124

79125
### **...**

solution/2700-2799/2729.Check if The Number is Fascinating/README_EN.md

+43-3
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,65 @@
4747
### **Python3**
4848

4949
```python
50-
50+
class Solution:
51+
def isFascinating(self, n: int) -> bool:
52+
s = str(n) + str(2 * n) + str(3 * n)
53+
return "".join(sorted(s)) == "123456789"
5154
```
5255

5356
### **Java**
5457

5558
```java
56-
59+
class Solution {
60+
public boolean isFascinating(int n) {
61+
String s = "" + n + (2 * n) + (3 * n);
62+
int[] cnt = new int[10];
63+
for (char c : s.toCharArray()) {
64+
if (++cnt[c - '0'] > 1) {
65+
return false;
66+
}
67+
}
68+
return cnt[0] == 0 && s.length() == 9;
69+
}
70+
}
5771
```
5872

5973
### **C++**
6074

6175
```cpp
62-
76+
class Solution {
77+
public:
78+
bool isFascinating(int n) {
79+
string s = to_string(n) + to_string(n * 2) + to_string(n * 3);
80+
sort(s.begin(), s.end());
81+
return s == "123456789";
82+
}
83+
};
6384
```
6485
6586
### **Go**
6687
6788
```go
89+
func isFascinating(n int) bool {
90+
s := strconv.Itoa(n) + strconv.Itoa(n*2) + strconv.Itoa(n*3)
91+
cnt := [10]int{}
92+
for _, c := range s {
93+
cnt[c-'0']++
94+
if cnt[c-'0'] > 1 {
95+
return false
96+
}
97+
}
98+
return cnt[0] == 0 && len(s) == 9
99+
}
100+
```
101+
102+
### **TypeScript**
68103

104+
```ts
105+
function isFascinating(n: number): boolean {
106+
const s = `${n}${n * 2}${n * 3}`;
107+
return s.split('').sort().join('') === '123456789';
108+
}
69109
```
70110

71111
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
bool isFascinating(int n) {
4+
string s = to_string(n) + to_string(n * 2) + to_string(n * 3);
5+
sort(s.begin(), s.end());
6+
return s == "123456789";
7+
}
8+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func isFascinating(n int) bool {
2+
s := strconv.Itoa(n) + strconv.Itoa(n*2) + strconv.Itoa(n*3)
3+
cnt := [10]int{}
4+
for _, c := range s {
5+
cnt[c-'0']++
6+
if cnt[c-'0'] > 1 {
7+
return false
8+
}
9+
}
10+
return cnt[0] == 0 && len(s) == 9
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public boolean isFascinating(int n) {
3+
String s = "" + n + (2 * n) + (3 * n);
4+
int[] cnt = new int[10];
5+
for (char c : s.toCharArray()) {
6+
if (++cnt[c - '0'] > 1) {
7+
return false;
8+
}
9+
}
10+
return cnt[0] == 0 && s.length() == 9;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def isFascinating(self, n: int) -> bool:
3+
s = str(n) + str(2 * n) + str(3 * n)
4+
return "".join(sorted(s)) == "123456789"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function isFascinating(n: number): boolean {
2+
const s = `${n}${n * 2}${n * 3}`;
3+
return s.split('').sort().join('') === '123456789';
4+
}

solution/2700-2799/2730.Find the Longest Semi-Repetitive Substring/README.md

+102-3
Original file line numberDiff line numberDiff line change
@@ -53,34 +53,133 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:双指针**
57+
58+
我们用双指针维护一个区间 $s[j..i]$,使得区间内最多只有一个相邻字符相等。我们用 $cnt$ 记录区间内相邻字符相等的个数,如果 $cnt \gt 1$,那么我们就需要移动左指针 $j$,直到 $cnt \le 1$。每一次,我们更新答案为 $ans = \max(ans, i - j + 1)$。
59+
60+
时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。
61+
5662
<!-- tabs:start -->
5763

5864
### **Python3**
5965

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

6268
```python
63-
69+
class Solution:
70+
def longestSemiRepetitiveSubstring(self, s: str) -> int:
71+
n = len(s)
72+
ans = cnt = j = 0
73+
for i in range(n):
74+
if i and s[i] == s[i - 1]:
75+
cnt += 1
76+
while cnt > 1:
77+
if s[j] == s[j + 1]:
78+
cnt -= 1
79+
j += 1
80+
ans = max(ans, i - j + 1)
81+
return ans
6482
```
6583

6684
### **Java**
6785

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

7088
```java
71-
89+
class Solution {
90+
public int longestSemiRepetitiveSubstring(String s) {
91+
int n = s.length();
92+
int ans = 0;
93+
for (int i = 0, j = 0, cnt = 0; i < n; ++i) {
94+
if (i > 0 && s.charAt(i) == s.charAt(i - 1)) {
95+
++cnt;
96+
}
97+
while (cnt > 1) {
98+
if (s.charAt(j) == s.charAt(j + 1)) {
99+
--cnt;
100+
}
101+
++j;
102+
}
103+
ans = Math.max(ans, i - j + 1);
104+
}
105+
return ans;
106+
}
107+
}
72108
```
73109

74110
### **C++**
75111

76112
```cpp
77-
113+
class Solution {
114+
public:
115+
int longestSemiRepetitiveSubstring(string s) {
116+
int n = s.size();
117+
int ans = 0;
118+
for (int i = 0, j = 0, cnt = 0; i < n; ++i) {
119+
if (i && s[i] == s[i - 1]) {
120+
++cnt;
121+
}
122+
while (cnt > 1) {
123+
if (s[j] == s[j + 1]) {
124+
--cnt;
125+
}
126+
++j;
127+
}
128+
ans = max(ans, i - j + 1);
129+
}
130+
return ans;
131+
}
132+
};
78133
```
79134
80135
### **Go**
81136
82137
```go
138+
func longestSemiRepetitiveSubstring(s string) (ans int) {
139+
n := len(s)
140+
for i, j, cnt := 0, 0, 0; i < n; i++ {
141+
if i > 0 && s[i] == s[i-1] {
142+
cnt++
143+
}
144+
for cnt > 1 {
145+
if s[j] == s[j+1] {
146+
cnt--
147+
}
148+
j++
149+
}
150+
ans = max(ans, i-j+1)
151+
}
152+
return
153+
}
154+
155+
func max(a, b int) int {
156+
if a > b {
157+
return a
158+
}
159+
return b
160+
}
161+
```
83162

163+
### **TypeScript**
164+
165+
```ts
166+
function longestSemiRepetitiveSubstring(s: string): number {
167+
const n = s.length;
168+
let ans = 0;
169+
for (let i = 0, j = 0, cnt = 0; i < n; ++i) {
170+
if (i > 0 && s[i] === s[i - 1]) {
171+
++cnt;
172+
}
173+
while (cnt > 1) {
174+
if (s[j] === s[j + 1]) {
175+
--cnt;
176+
}
177+
++j;
178+
}
179+
ans = Math.max(ans, i - j + 1);
180+
}
181+
return ans;
182+
}
84183
```
85184

86185
### **...**

0 commit comments

Comments
 (0)