Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions solution/2500-2599/2582.Pass the Pillow/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,42 @@ function passThePillow(n: number, time: number): number {
}
```

### **Rust**

```rust
impl Solution {
pub fn pass_the_pillow(n: i32, time: i32) -> i32 {
let mut ans = 1;
let mut k = 1;

for i in 1..=time {
ans += k;

if ans == 1 || ans == n {
k *= -1;
}
}

ans
}
}
```

```rust
impl Solution {
pub fn pass_the_pillow(n: i32, time: i32) -> i32 {
let mut k = time / (n - 1);
let mut _mod = time % (n - 1);

if k & 1 == 1 {
return n - _mod
}

_mod + 1
}
}
```

### **...**

```
Expand Down
36 changes: 36 additions & 0 deletions solution/2500-2599/2582.Pass the Pillow/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,42 @@ function passThePillow(n: number, time: number): number {
}
```

### **Rust**

```rust
impl Solution {
pub fn pass_the_pillow(n: i32, time: i32) -> i32 {
let mut ans = 1;
let mut k = 1;

for i in 1..=time {
ans += k;

if ans == 1 || ans == n {
k *= -1;
}
}

ans
}
}
```

```rust
impl Solution {
pub fn pass_the_pillow(n: i32, time: i32) -> i32 {
let mut k = time / (n - 1);
let mut _mod = time % (n - 1);

if k & 1 == 1 {
return n - _mod
}

_mod + 1
}
}
```

### **...**

```
Expand Down
12 changes: 12 additions & 0 deletions solution/2500-2599/2582.Pass the Pillow/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
impl Solution {
pub fn pass_the_pillow(n: i32, time: i32) -> i32 {
let mut k = time / (n - 1);
let mut _mod = time % (n - 1);

if k & 1 == 1 {
return n - _mod
}

_mod + 1
}
}