Skip to content

Commit ac869f2

Browse files
committed
feat: add solutions to lc problems: No.0526,1592
- No.0526.Beautiful Arrangement - No.1592.Rearrange Spaces Between Words
1 parent 5b6d020 commit ac869f2

File tree

8 files changed

+358
-0
lines changed

8 files changed

+358
-0
lines changed

solution/0500-0599/0526.Beautiful Arrangement/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,76 @@ func countArrangement(n int) int {
225225
}
226226
```
227227

228+
### **TypeScript**
229+
230+
```ts
231+
function countArrangement(n: number): number {
232+
const vis = new Array(n + 1).fill(0);
233+
const match = Array.from({ length: n + 1 }, () => []);
234+
for (let i = 1; i <= n; i++) {
235+
for (let j = 1; j <= n; j++) {
236+
if (i % j === 0 || j % i === 0) {
237+
match[i].push(j);
238+
}
239+
}
240+
}
241+
242+
let res = 0;
243+
const dfs = (i: number, n: number) => {
244+
if (i === n + 1) {
245+
res++;
246+
return;
247+
}
248+
for (const j of match[i]) {
249+
if (!vis[j]) {
250+
vis[j] = true;
251+
dfs(i + 1, n);
252+
vis[j] = false;
253+
}
254+
}
255+
};
256+
dfs(1, n);
257+
return res;
258+
}
259+
```
260+
261+
### **Rust**
262+
263+
```rust
264+
impl Solution {
265+
fn dfs(i: usize, n: usize, mat: &Vec<Vec<usize>>, vis: &mut Vec<bool>, res: &mut i32) {
266+
if i == n + 1 {
267+
*res += 1;
268+
return;
269+
}
270+
for &j in mat[i].iter() {
271+
if !vis[j] {
272+
vis[j] = true;
273+
Self::dfs(i + 1, n, mat, vis, res);
274+
vis[j] = false;
275+
}
276+
}
277+
}
278+
279+
pub fn count_arrangement(n: i32) -> i32 {
280+
let n = n as usize;
281+
let mut vis = vec![false; n + 1];
282+
let mut mat = vec![Vec::new(); n + 1];
283+
for i in 1..=n {
284+
for j in 1..=n {
285+
if i % j == 0 || j % i == 0 {
286+
mat[i].push(j);
287+
}
288+
}
289+
}
290+
291+
let mut res = 0;
292+
Self::dfs(1, n, &mat, &mut vis, &mut res);
293+
res
294+
}
295+
}
296+
```
297+
228298
### **...**
229299

230300
```

solution/0500-0599/0526.Beautiful Arrangement/README_EN.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,76 @@ func countArrangement(n int) int {
213213
}
214214
```
215215

216+
### **TypeScript**
217+
218+
```ts
219+
function countArrangement(n: number): number {
220+
const vis = new Array(n + 1).fill(0);
221+
const match = Array.from({ length: n + 1 }, () => []);
222+
for (let i = 1; i <= n; i++) {
223+
for (let j = 1; j <= n; j++) {
224+
if (i % j === 0 || j % i === 0) {
225+
match[i].push(j);
226+
}
227+
}
228+
}
229+
230+
let res = 0;
231+
const dfs = (i: number, n: number) => {
232+
if (i === n + 1) {
233+
res++;
234+
return;
235+
}
236+
for (const j of match[i]) {
237+
if (!vis[j]) {
238+
vis[j] = true;
239+
dfs(i + 1, n);
240+
vis[j] = false;
241+
}
242+
}
243+
};
244+
dfs(1, n);
245+
return res;
246+
}
247+
```
248+
249+
### **Rust**
250+
251+
```rust
252+
impl Solution {
253+
fn dfs(i: usize, n: usize, mat: &Vec<Vec<usize>>, vis: &mut Vec<bool>, res: &mut i32) {
254+
if i == n + 1 {
255+
*res += 1;
256+
return;
257+
}
258+
for &j in mat[i].iter() {
259+
if !vis[j] {
260+
vis[j] = true;
261+
Self::dfs(i + 1, n, mat, vis, res);
262+
vis[j] = false;
263+
}
264+
}
265+
}
266+
267+
pub fn count_arrangement(n: i32) -> i32 {
268+
let n = n as usize;
269+
let mut vis = vec![false; n + 1];
270+
let mut mat = vec![Vec::new(); n + 1];
271+
for i in 1..=n {
272+
for j in 1..=n {
273+
if i % j == 0 || j % i == 0 {
274+
mat[i].push(j);
275+
}
276+
}
277+
}
278+
279+
let mut res = 0;
280+
Self::dfs(1, n, &mat, &mut vis, &mut res);
281+
res
282+
}
283+
}
284+
```
285+
216286
### **...**
217287

218288
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
impl Solution {
2+
fn dfs(i: usize, n: usize, mat: &Vec<Vec<usize>>, vis: &mut Vec<bool>, res: &mut i32) {
3+
if i == n + 1 {
4+
*res += 1;
5+
return;
6+
}
7+
for &j in mat[i].iter() {
8+
if !vis[j] {
9+
vis[j] = true;
10+
Self::dfs(i + 1, n, mat, vis, res);
11+
vis[j] = false;
12+
}
13+
}
14+
}
15+
16+
pub fn count_arrangement(n: i32) -> i32 {
17+
let n = n as usize;
18+
let mut vis = vec![false; n + 1];
19+
let mut mat = vec![Vec::new(); n + 1];
20+
for i in 1..=n {
21+
for j in 1..=n {
22+
if i % j == 0 || j % i == 0 {
23+
mat[i].push(j);
24+
}
25+
}
26+
}
27+
28+
let mut res = 0;
29+
Self::dfs(1, n, &mat, &mut vis, &mut res);
30+
res
31+
}
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function countArrangement(n: number): number {
2+
const vis = new Array(n + 1).fill(0);
3+
const match = Array.from({ length: n + 1 }, () => []);
4+
for (let i = 1; i <= n; i++) {
5+
for (let j = 1; j <= n; j++) {
6+
if (i % j === 0 || j % i === 0) {
7+
match[i].push(j);
8+
}
9+
}
10+
}
11+
12+
let res = 0;
13+
const dfs = (i: number, n: number) => {
14+
if (i === n + 1) {
15+
res++;
16+
return;
17+
}
18+
for (const j of match[i]) {
19+
if (!vis[j]) {
20+
vis[j] = true;
21+
dfs(i + 1, n);
22+
vis[j] = false;
23+
}
24+
}
25+
};
26+
dfs(1, n);
27+
return res;
28+
}

solution/1500-1599/1592.Rearrange Spaces Between Words/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,62 @@ func reorderSpaces(text string) string {
128128
}
129129
```
130130

131+
### **TypeScript**
132+
133+
```ts
134+
function reorderSpaces(text: string): string {
135+
let count = 0;
136+
for (const c of text) {
137+
if (c === ' ') {
138+
count++;
139+
}
140+
}
141+
142+
const words = text.trim().split(/\s+/g);
143+
const n = words.length;
144+
if (n === 1) {
145+
return words.join('') + ''.padStart(count);
146+
}
147+
148+
const rest = count % (words.length - 1);
149+
const per = (count - rest) / (words.length - 1);
150+
return words.join(''.padStart(per)) + ''.padStart(rest);
151+
}
152+
```
153+
154+
### **Rust**
155+
156+
```rust
157+
impl Solution {
158+
fn create_spaces(n: usize) -> String {
159+
let mut res = String::new();
160+
for _ in 0..n {
161+
res.push(' ');
162+
}
163+
res
164+
}
165+
166+
pub fn reorder_spaces(text: String) -> String {
167+
let count = {
168+
let mut res = 0;
169+
for c in text.as_bytes() {
170+
if c == &b' ' {
171+
res += 1;
172+
}
173+
}
174+
res
175+
};
176+
177+
let works = text.split_whitespace().collect::<Vec<&str>>();
178+
let n = works.len();
179+
if n == 1 {
180+
return works[0].to_string() + &Self::create_spaces(count);
181+
}
182+
works.join(&Self::create_spaces((count / (n - 1)))) + &Self::create_spaces(count % (n - 1))
183+
}
184+
}
185+
```
186+
131187
### **...**
132188

133189
```

solution/1500-1599/1592.Rearrange Spaces Between Words/README_EN.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,62 @@ func reorderSpaces(text string) string {
9696
}
9797
```
9898

99+
### **TypeScript**
100+
101+
```ts
102+
function reorderSpaces(text: string): string {
103+
let count = 0;
104+
for (const c of text) {
105+
if (c === ' ') {
106+
count++;
107+
}
108+
}
109+
110+
const words = text.trim().split(/\s+/g);
111+
const n = words.length;
112+
if (n === 1) {
113+
return words.join('') + ''.padStart(count);
114+
}
115+
116+
const rest = count % (words.length - 1);
117+
const per = (count - rest) / (words.length - 1);
118+
return words.join(''.padStart(per)) + ''.padStart(rest);
119+
}
120+
```
121+
122+
### **Rust**
123+
124+
```rust
125+
impl Solution {
126+
fn create_spaces(n: usize) -> String {
127+
let mut res = String::new();
128+
for _ in 0..n {
129+
res.push(' ');
130+
}
131+
res
132+
}
133+
134+
pub fn reorder_spaces(text: String) -> String {
135+
let count = {
136+
let mut res = 0;
137+
for c in text.as_bytes() {
138+
if c == &b' ' {
139+
res += 1;
140+
}
141+
}
142+
res
143+
};
144+
145+
let works = text.split_whitespace().collect::<Vec<&str>>();
146+
let n = works.len();
147+
if n == 1 {
148+
return works[0].to_string() + &Self::create_spaces(count);
149+
}
150+
works.join(&Self::create_spaces((count / (n - 1)))) + &Self::create_spaces(count % (n - 1))
151+
}
152+
}
153+
```
154+
99155
### **...**
100156

101157
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
impl Solution {
2+
fn create_spaces(n: usize) -> String {
3+
let mut res = String::new();
4+
for _ in 0..n {
5+
res.push(' ');
6+
}
7+
res
8+
}
9+
10+
pub fn reorder_spaces(text: String) -> String {
11+
let count = {
12+
let mut res = 0;
13+
for c in text.as_bytes() {
14+
if c == &b' ' {
15+
res += 1;
16+
}
17+
}
18+
res
19+
};
20+
21+
let works = text.split_whitespace().collect::<Vec<&str>>();
22+
let n = works.len();
23+
if n == 1 {
24+
return works[0].to_string() + &Self::create_spaces(count);
25+
}
26+
works.join(&Self::create_spaces((count / (n - 1)))) + &Self::create_spaces(count % (n - 1))
27+
}
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function reorderSpaces(text: string): string {
2+
let count = 0;
3+
for (const c of text) {
4+
if (c === ' ') {
5+
count++;
6+
}
7+
}
8+
9+
const words = text.trim().split(/\s+/g);
10+
const n = words.length;
11+
if (n === 1) {
12+
return words.join('') + ''.padStart(count);
13+
}
14+
15+
const rest = count % (words.length - 1);
16+
const per = (count - rest) / (words.length - 1);
17+
return words.join(''.padStart(per)) + ''.padStart(rest);
18+
}

0 commit comments

Comments
 (0)