Skip to content

Commit 46148e4

Browse files
authored
feat: add rust solution to lc problem: No.2582 (#1183)
1 parent e55e434 commit 46148e4

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

solution/2500-2599/2582.Pass the Pillow/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,42 @@ function passThePillow(n: number, time: number): number {
197197
}
198198
```
199199

200+
### **Rust**
201+
202+
```rust
203+
impl Solution {
204+
pub fn pass_the_pillow(n: i32, time: i32) -> i32 {
205+
let mut ans = 1;
206+
let mut k = 1;
207+
208+
for i in 1..=time {
209+
ans += k;
210+
211+
if ans == 1 || ans == n {
212+
k *= -1;
213+
}
214+
}
215+
216+
ans
217+
}
218+
}
219+
```
220+
221+
```rust
222+
impl Solution {
223+
pub fn pass_the_pillow(n: i32, time: i32) -> i32 {
224+
let mut k = time / (n - 1);
225+
let mut _mod = time % (n - 1);
226+
227+
if k & 1 == 1 {
228+
return n - _mod
229+
}
230+
231+
_mod + 1
232+
}
233+
}
234+
```
235+
200236
### **...**
201237

202238
```

solution/2500-2599/2582.Pass the Pillow/README_EN.md

+36
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,42 @@ function passThePillow(n: number, time: number): number {
184184
}
185185
```
186186

187+
### **Rust**
188+
189+
```rust
190+
impl Solution {
191+
pub fn pass_the_pillow(n: i32, time: i32) -> i32 {
192+
let mut ans = 1;
193+
let mut k = 1;
194+
195+
for i in 1..=time {
196+
ans += k;
197+
198+
if ans == 1 || ans == n {
199+
k *= -1;
200+
}
201+
}
202+
203+
ans
204+
}
205+
}
206+
```
207+
208+
```rust
209+
impl Solution {
210+
pub fn pass_the_pillow(n: i32, time: i32) -> i32 {
211+
let mut k = time / (n - 1);
212+
let mut _mod = time % (n - 1);
213+
214+
if k & 1 == 1 {
215+
return n - _mod
216+
}
217+
218+
_mod + 1
219+
}
220+
}
221+
```
222+
187223
### **...**
188224

189225
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
impl Solution {
2+
pub fn pass_the_pillow(n: i32, time: i32) -> i32 {
3+
let mut k = time / (n - 1);
4+
let mut _mod = time % (n - 1);
5+
6+
if k & 1 == 1 {
7+
return n - _mod
8+
}
9+
10+
_mod + 1
11+
}
12+
}

0 commit comments

Comments
 (0)