Skip to content

Commit c90cd44

Browse files
committed
feat: add solutions to lc problem: No.2000
No.2000.Reverse Prefix of Word
1 parent 8393d6e commit c90cd44

File tree

8 files changed

+167
-30
lines changed

8 files changed

+167
-30
lines changed

solution/2000-2099/2000.Reverse Prefix of Word/README.md

+36-10
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454

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

57+
**方法一:模拟**
58+
59+
我们先找到字符 $ch$ 第一次出现的下标 $i$,然后反转从下标 $0$ 开始、直到下标 $i$ 结束(含下标 $i$)的那段字符,最后将反转后的字符串与下标 $i + 1$ 开始的字符串拼接即可。
60+
61+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $word$ 的长度。
62+
5763
<!-- tabs:start -->
5864

5965
### **Python3**
@@ -73,14 +79,30 @@ class Solution:
7379

7480
```java
7581
class Solution {
82+
public String reversePrefix(String word, char ch) {
83+
int j = word.indexOf(ch);
84+
if (j == -1) {
85+
return word;
86+
}
87+
char[] cs = word.toCharArray();
88+
for (int i = 0; i < j; ++i, --j) {
89+
char t = cs[i];
90+
cs[i] = cs[j];
91+
cs[j] = t;
92+
}
93+
return String.valueOf(cs);
94+
}
95+
}
96+
```
7697

98+
```java
99+
class Solution {
77100
public String reversePrefix(String word, char ch) {
78-
int i = word.indexOf(ch);
79-
return i == -1 ? word
80-
: new StringBuilder(word.substring(0, i + 1))
81-
.reverse()
82-
.append(word.substring(i + 1))
83-
.toString();
101+
int j = word.indexOf(ch);
102+
if (j == -1) {
103+
return word;
104+
}
105+
return new StringBuilder(word.substring(0, j + 1)).reverse().append(word.substring(j + 1)).toString();
84106
}
85107
}
86108
```
@@ -92,7 +114,9 @@ class Solution {
92114
public:
93115
string reversePrefix(string word, char ch) {
94116
int i = word.find(ch);
95-
if (i != string::npos) reverse(word.begin(), word.begin() + i + 1);
117+
if (i != string::npos) {
118+
reverse(word.begin(), word.begin() + i + 1);
119+
}
96120
return word;
97121
}
98122
};
@@ -119,9 +143,11 @@ func reversePrefix(word string, ch byte) string {
119143

120144
```ts
121145
function reversePrefix(word: string, ch: string): string {
122-
let idx = word.indexOf(ch) + 1;
123-
if (!idx) return word;
124-
return [...word.substring(0, idx)].reverse().join('') + word.substring(idx);
146+
const i = word.indexOf(ch) + 1;
147+
if (!i) {
148+
return word;
149+
}
150+
return [...word.slice(0, i)].reverse().join('') + word.slice(i);
125151
}
126152
```
127153

solution/2000-2099/2000.Reverse Prefix of Word/README_EN.md

+30-10
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,30 @@ class Solution:
6666

6767
```java
6868
class Solution {
69+
public String reversePrefix(String word, char ch) {
70+
int j = word.indexOf(ch);
71+
if (j == -1) {
72+
return word;
73+
}
74+
char[] cs = word.toCharArray();
75+
for (int i = 0; i < j; ++i, --j) {
76+
char t = cs[i];
77+
cs[i] = cs[j];
78+
cs[j] = t;
79+
}
80+
return String.valueOf(cs);
81+
}
82+
}
83+
```
6984

85+
```java
86+
class Solution {
7087
public String reversePrefix(String word, char ch) {
71-
int i = word.indexOf(ch);
72-
return i == -1 ? word
73-
: new StringBuilder(word.substring(0, i + 1))
74-
.reverse()
75-
.append(word.substring(i + 1))
76-
.toString();
88+
int j = word.indexOf(ch);
89+
if (j == -1) {
90+
return word;
91+
}
92+
return new StringBuilder(word.substring(0, j + 1)).reverse().append(word.substring(j + 1)).toString();
7793
}
7894
}
7995
```
@@ -85,7 +101,9 @@ class Solution {
85101
public:
86102
string reversePrefix(string word, char ch) {
87103
int i = word.find(ch);
88-
if (i != string::npos) reverse(word.begin(), word.begin() + i + 1);
104+
if (i != string::npos) {
105+
reverse(word.begin(), word.begin() + i + 1);
106+
}
89107
return word;
90108
}
91109
};
@@ -112,9 +130,11 @@ func reversePrefix(word string, ch byte) string {
112130

113131
```ts
114132
function reversePrefix(word: string, ch: string): string {
115-
let idx = word.indexOf(ch) + 1;
116-
if (!idx) return word;
117-
return [...word.substring(0, idx)].reverse().join('') + word.substring(idx);
133+
const i = word.indexOf(ch) + 1;
134+
if (!i) {
135+
return word;
136+
}
137+
return [...word.slice(0, i)].reverse().join('') + word.slice(i);
118138
}
119139
```
120140

solution/2000-2099/2000.Reverse Prefix of Word/Solution.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ class Solution {
22
public:
33
string reversePrefix(string word, char ch) {
44
int i = word.find(ch);
5-
if (i != string::npos) reverse(word.begin(), word.begin() + i + 1);
5+
if (i != string::npos) {
6+
reverse(word.begin(), word.begin() + i + 1);
7+
}
68
return word;
79
}
810
};
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
class Solution {
22
public String reversePrefix(String word, char ch) {
3-
int i = word.indexOf(ch);
4-
return i == -1 ? word
5-
: new StringBuilder(word.substring(0, i + 1))
6-
.reverse()
7-
.append(word.substring(i + 1))
8-
.toString();
3+
int j = word.indexOf(ch);
4+
if (j == -1) {
5+
return word;
6+
}
7+
char[] cs = word.toCharArray();
8+
for (int i = 0; i < j; ++i, --j) {
9+
char t = cs[i];
10+
cs[i] = cs[j];
11+
cs[j] = t;
12+
}
13+
return String.valueOf(cs);
914
}
1015
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
function reversePrefix(word: string, ch: string): string {
2-
let idx = word.indexOf(ch) + 1;
3-
if (!idx) return word;
4-
return [...word.substring(0, idx)].reverse().join('') + word.substring(idx);
2+
const i = word.indexOf(ch) + 1;
3+
if (!i) {
4+
return word;
5+
}
6+
return [...word.slice(0, i)].reverse().join('') + word.slice(i);
57
}

solution/2000-2099/2001.Number of Pairs of Interchangeable Rectangles/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,35 @@ func gcd(a, b int) int {
153153
}
154154
```
155155

156+
### **JavaScript**
157+
158+
```js
159+
/**
160+
* @param {number[][]} rectangles
161+
* @return {number}
162+
*/
163+
var interchangeableRectangles = function (rectangles) {
164+
const cnt = new Map();
165+
let ans = 0;
166+
for (let [w, h] of rectangles) {
167+
const g = gcd(w, h);
168+
w = Math.floor(w / g);
169+
h = Math.floor(h / g);
170+
const x = w * (rectangles.length + 1) + h;
171+
ans += cnt.get(x) | 0;
172+
cnt.set(x, (cnt.get(x) | 0) + 1);
173+
}
174+
return ans;
175+
};
176+
177+
function gcd(a, b) {
178+
if (b == 0) {
179+
return a;
180+
}
181+
return gcd(b, a % b);
182+
}
183+
```
184+
156185
### **...**
157186

158187
```

solution/2000-2099/2001.Number of Pairs of Interchangeable Rectangles/README_EN.md

+29
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,35 @@ func gcd(a, b int) int {
137137
}
138138
```
139139

140+
### **JavaScript**
141+
142+
```js
143+
/**
144+
* @param {number[][]} rectangles
145+
* @return {number}
146+
*/
147+
var interchangeableRectangles = function (rectangles) {
148+
const cnt = new Map();
149+
let ans = 0;
150+
for (let [w, h] of rectangles) {
151+
const g = gcd(w, h);
152+
w = Math.floor(w / g);
153+
h = Math.floor(h / g);
154+
const x = w * (rectangles.length + 1) + h;
155+
ans += cnt.get(x) | 0;
156+
cnt.set(x, (cnt.get(x) | 0) + 1);
157+
}
158+
return ans;
159+
};
160+
161+
function gcd(a, b) {
162+
if (b == 0) {
163+
return a;
164+
}
165+
return gcd(b, a % b);
166+
}
167+
```
168+
140169
### **...**
141170

142171
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[][]} rectangles
3+
* @return {number}
4+
*/
5+
var interchangeableRectangles = function (rectangles) {
6+
const cnt = new Map();
7+
let ans = 0;
8+
for (let [w, h] of rectangles) {
9+
const g = gcd(w, h);
10+
w = Math.floor(w / g);
11+
h = Math.floor(h / g);
12+
const x = w * (rectangles.length + 1) + h;
13+
ans += cnt.get(x) | 0;
14+
cnt.set(x, (cnt.get(x) | 0) + 1);
15+
}
16+
return ans;
17+
};
18+
19+
function gcd(a, b) {
20+
if (b == 0) {
21+
return a;
22+
}
23+
return gcd(b, a % b);
24+
}

0 commit comments

Comments
 (0)