Skip to content

Commit 2fa9738

Browse files
committed
feat: add solutions to lc problems
* No.1952.Three Divisors * No.1974.Minimum Time to Type Word Using Special Typewriter * No.2255.Count Prefixes of a Given String * No.2256.Minimum Average Difference * No.2257.Count Unguarded Cells in the Grid * No.2258.Escape the Spreading Fire * No.2259.Remove Digit From Number to Maximize Result * No.2260.Minimum Consecutive Cards to Pick Up * No.2261.K Divisible Elements Subarrays * No.2262.Total Appeal of A String
1 parent da89fc3 commit 2fa9738

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+8584
-4622
lines changed

solution/1900-1999/1952.Three Divisors/README.md

+21-8
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@
4646
```python
4747
class Solution:
4848
def isThree(self, n: int) -> bool:
49-
cnt = 0
50-
for i in range(2, n):
51-
if n % i == 0:
52-
cnt += 1
53-
return cnt == 1
49+
return sum(n % i == 0 for i in range(2, n)) == 1
5450
```
5551

5652
### **Java**
@@ -78,9 +74,8 @@ class Solution {
7874
public:
7975
bool isThree(int n) {
8076
int cnt = 0;
81-
for (int i = 2; i < n; ++i) {
82-
if (n % i == 0) ++cnt;
83-
}
77+
for (int i = 2; i < n; ++i)
78+
cnt += n % i == 0;
8479
return cnt == 1;
8580
}
8681
};
@@ -100,6 +95,24 @@ func isThree(n int) bool {
10095
}
10196
```
10297

98+
### **JavaScript**
99+
100+
```js
101+
/**
102+
* @param {number} n
103+
* @return {boolean}
104+
*/
105+
var isThree = function (n) {
106+
let cnt = 0;
107+
for (let i = 2; i < n; ++i) {
108+
if (n % i == 0) {
109+
++cnt;
110+
}
111+
}
112+
return cnt == 1;
113+
};
114+
```
115+
103116
### **...**
104117

105118
```

solution/1900-1999/1952.Three Divisors/README_EN.md

+21-8
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@
4141
```python
4242
class Solution:
4343
def isThree(self, n: int) -> bool:
44-
cnt = 0
45-
for i in range(2, n):
46-
if n % i == 0:
47-
cnt += 1
48-
return cnt == 1
44+
return sum(n % i == 0 for i in range(2, n)) == 1
4945
```
5046

5147
### **Java**
@@ -71,9 +67,8 @@ class Solution {
7167
public:
7268
bool isThree(int n) {
7369
int cnt = 0;
74-
for (int i = 2; i < n; ++i) {
75-
if (n % i == 0) ++cnt;
76-
}
70+
for (int i = 2; i < n; ++i)
71+
cnt += n % i == 0;
7772
return cnt == 1;
7873
}
7974
};
@@ -93,6 +88,24 @@ func isThree(n int) bool {
9388
}
9489
```
9590

91+
### **JavaScript**
92+
93+
```js
94+
/**
95+
* @param {number} n
96+
* @return {boolean}
97+
*/
98+
var isThree = function (n) {
99+
let cnt = 0;
100+
for (let i = 2; i < n; ++i) {
101+
if (n % i == 0) {
102+
++cnt;
103+
}
104+
}
105+
return cnt == 1;
106+
};
107+
```
108+
96109
### **...**
97110

98111
```

solution/1900-1999/1952.Three Divisors/Solution.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ class Solution {
22
public:
33
bool isThree(int n) {
44
int cnt = 0;
5-
for (int i = 2; i < n; ++i) {
6-
if (n % i == 0) ++cnt;
7-
}
5+
for (int i = 2; i < n; ++i)
6+
cnt += n % i == 0;
87
return cnt == 1;
98
}
109
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @param {number} n
3+
* @return {boolean}
4+
*/
5+
var isThree = function (n) {
6+
let cnt = 0;
7+
for (let i = 2; i < n; ++i) {
8+
if (n % i == 0) {
9+
++cnt;
10+
}
11+
}
12+
return cnt == 1;
13+
};
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
class Solution:
22
def isThree(self, n: int) -> bool:
3-
cnt = 0
4-
for i in range(2, n):
5-
if n % i == 0:
6-
cnt += 1
7-
return cnt == 1
3+
return sum(n % i == 0 for i in range(2, n)) == 1

solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/README.md

+74-1
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,88 @@
8585
<!-- 这里可写当前语言的特殊实现逻辑 -->
8686

8787
```python
88-
88+
class Solution:
89+
def minTimeToType(self, word: str) -> int:
90+
ans = prev = 0
91+
for c in word:
92+
curr = ord(c) - ord('a')
93+
t = abs(prev - curr)
94+
t = min(t, 26 - t)
95+
ans += t + 1
96+
prev = curr
97+
return ans
8998
```
9099

91100
### **Java**
92101

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

95104
```java
105+
class Solution {
106+
public int minTimeToType(String word) {
107+
int ans = 0;
108+
int prev = 0;
109+
for (char c : word.toCharArray()) {
110+
int curr = c - 'a';
111+
int t = Math.abs(prev - curr);
112+
t = Math.min(t, 26 - t);
113+
ans += t + 1;
114+
prev = curr;
115+
}
116+
return ans;
117+
}
118+
}
119+
```
120+
121+
### **C++**
122+
123+
```cpp
124+
class Solution {
125+
public:
126+
int minTimeToType(string word) {
127+
int ans = 0;
128+
int prev = 0;
129+
for (char& c : word)
130+
{
131+
int curr = c - 'a';
132+
int t = abs(prev - curr);
133+
t = min(t, 26 - t);
134+
ans += t + 1;
135+
prev = curr;
136+
}
137+
return ans;
138+
}
139+
};
140+
```
96141
142+
### **Go**
143+
144+
```go
145+
func minTimeToType(word string) int {
146+
ans, prev := 0, 0
147+
for _, c := range word {
148+
curr := int(c - 'a')
149+
t := abs(prev - curr)
150+
t = min(t, 26-t)
151+
ans += t + 1
152+
prev = curr
153+
}
154+
return ans
155+
}
156+
157+
func abs(x int) int {
158+
if x < 0 {
159+
return -x
160+
}
161+
return x
162+
}
163+
164+
func min(a, b int) int {
165+
if a < b {
166+
return a
167+
}
168+
return b
169+
}
97170
```
98171

99172
### **...**

solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/README_EN.md

+74-1
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,86 @@ The characters are printed as follows:
7777
### **Python3**
7878

7979
```python
80-
80+
class Solution:
81+
def minTimeToType(self, word: str) -> int:
82+
ans = prev = 0
83+
for c in word:
84+
curr = ord(c) - ord('a')
85+
t = abs(prev - curr)
86+
t = min(t, 26 - t)
87+
ans += t + 1
88+
prev = curr
89+
return ans
8190
```
8291

8392
### **Java**
8493

8594
```java
95+
class Solution {
96+
public int minTimeToType(String word) {
97+
int ans = 0;
98+
int prev = 0;
99+
for (char c : word.toCharArray()) {
100+
int curr = c - 'a';
101+
int t = Math.abs(prev - curr);
102+
t = Math.min(t, 26 - t);
103+
ans += t + 1;
104+
prev = curr;
105+
}
106+
return ans;
107+
}
108+
}
109+
```
110+
111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
int minTimeToType(string word) {
117+
int ans = 0;
118+
int prev = 0;
119+
for (char& c : word)
120+
{
121+
int curr = c - 'a';
122+
int t = abs(prev - curr);
123+
t = min(t, 26 - t);
124+
ans += t + 1;
125+
prev = curr;
126+
}
127+
return ans;
128+
}
129+
};
130+
```
86131
132+
### **Go**
133+
134+
```go
135+
func minTimeToType(word string) int {
136+
ans, prev := 0, 0
137+
for _, c := range word {
138+
curr := int(c - 'a')
139+
t := abs(prev - curr)
140+
t = min(t, 26-t)
141+
ans += t + 1
142+
prev = curr
143+
}
144+
return ans
145+
}
146+
147+
func abs(x int) int {
148+
if x < 0 {
149+
return -x
150+
}
151+
return x
152+
}
153+
154+
func min(a, b int) int {
155+
if a < b {
156+
return a
157+
}
158+
return b
159+
}
87160
```
88161

89162
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int minTimeToType(string word) {
4+
int ans = 0;
5+
int prev = 0;
6+
for (char& c : word)
7+
{
8+
int curr = c - 'a';
9+
int t = abs(prev - curr);
10+
t = min(t, 26 - t);
11+
ans += t + 1;
12+
prev = curr;
13+
}
14+
return ans;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func minTimeToType(word string) int {
2+
ans, prev := 0, 0
3+
for _, c := range word {
4+
curr := int(c - 'a')
5+
t := abs(prev - curr)
6+
t = min(t, 26-t)
7+
ans += t + 1
8+
prev = curr
9+
}
10+
return ans
11+
}
12+
13+
func abs(x int) int {
14+
if x < 0 {
15+
return -x
16+
}
17+
return x
18+
}
19+
20+
func min(a, b int) int {
21+
if a < b {
22+
return a
23+
}
24+
return b
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int minTimeToType(String word) {
3+
int ans = 0;
4+
int prev = 0;
5+
for (char c : word.toCharArray()) {
6+
int curr = c - 'a';
7+
int t = Math.abs(prev - curr);
8+
t = Math.min(t, 26 - t);
9+
ans += t + 1;
10+
prev = curr;
11+
}
12+
return ans;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def minTimeToType(self, word: str) -> int:
3+
ans = prev = 0
4+
for c in word:
5+
curr = ord(c) - ord('a')
6+
t = abs(prev - curr)
7+
t = min(t, 26 - t)
8+
ans += t + 1
9+
prev = curr
10+
return ans

0 commit comments

Comments
 (0)