Skip to content

Commit 0bf2e51

Browse files
authored
feat: add rust solution to lc problem: No.3307 (#4530)
No.3307.Find the K-th Character in String Game II
1 parent 74b8a69 commit 0bf2e51

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

solution/3300-3399/3307.Find the K-th Character in String Game II/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,31 @@ function kthCharacter(k: number, operations: number[]): string {
226226
}
227227
```
228228

229+
#### Rust
230+
231+
```rust
232+
impl Solution {
233+
pub fn kth_character(mut k: i64, operations: Vec<i32>) -> char {
234+
let mut n = 1i64;
235+
let mut i = 0;
236+
while n < k {
237+
n *= 2;
238+
i += 1;
239+
}
240+
let mut d = 0;
241+
while n > 1 {
242+
if k > n / 2 {
243+
k -= n / 2;
244+
d += operations[i - 1] as i64;
245+
}
246+
n /= 2;
247+
i -= 1;
248+
}
249+
((b'a' + (d % 26) as u8) as char)
250+
}
251+
}
252+
```
253+
229254
<!-- tabs:end -->
230255

231256
<!-- solution:end -->

solution/3300-3399/3307.Find the K-th Character in String Game II/README_EN.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,31 @@ function kthCharacter(k: number, operations: number[]): string {
223223
}
224224
```
225225

226+
#### Rust
227+
228+
```rust
229+
impl Solution {
230+
pub fn kth_character(mut k: i64, operations: Vec<i32>) -> char {
231+
let mut n = 1i64;
232+
let mut i = 0;
233+
while n < k {
234+
n *= 2;
235+
i += 1;
236+
}
237+
let mut d = 0;
238+
while n > 1 {
239+
if k > n / 2 {
240+
k -= n / 2;
241+
d += operations[i - 1] as i64;
242+
}
243+
n /= 2;
244+
i -= 1;
245+
}
246+
((b'a' + (d % 26) as u8) as char)
247+
}
248+
}
249+
```
250+
226251
<!-- tabs:end -->
227252

228253
<!-- solution:end -->
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
pub fn kth_character(mut k: i64, operations: Vec<i32>) -> char {
3+
let mut n = 1i64;
4+
let mut i = 0;
5+
while n < k {
6+
n *= 2;
7+
i += 1;
8+
}
9+
let mut d = 0;
10+
while n > 1 {
11+
if k > n / 2 {
12+
k -= n / 2;
13+
d += operations[i - 1] as i64;
14+
}
15+
n /= 2;
16+
i -= 1;
17+
}
18+
((b'a' + (d % 26) as u8) as char)
19+
}
20+
}

solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Due to the movement rules given in the problem, in fact, only the following thre
8585

8686
For all other cases, it is impossible to reach the target cell, so return $-1$.
8787

88-
The time complexity is $O(1)$, and the space complexity is
88+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
8989

9090
<!-- tabs:start -->
9191

0 commit comments

Comments
 (0)