Skip to content

leetcode/1260: rotate_right#386

Merged
tony84727 merged 1 commit into
mainfrom
leetcode/1260
Jul 20, 2026
Merged

leetcode/1260: rotate_right#386
tony84727 merged 1 commit into
mainfrom
leetcode/1260

Conversation

@tony84727

Copy link
Copy Markdown
Owner

No description provided.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements the solution for LeetCode 1260, "Shift 2D Grid", in Rust. The feedback suggests adding a guard clause to handle empty grids safely, preventing potential panics from division by zero or unwrapping empty options. Additionally, it recommends using chunks_exact to group the flattened vector into rows, which is a more idiomatic and efficient approach than using nested loops with pre-allocated vectors.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +5 to +15
let n = grid.len();
let m = grid.first().map(|row| row.len()).unwrap();
let mut elements: Vec<i32> = grid.into_iter().flatten().collect();
let mut shifted = vec![vec![0; m]; n];
elements.rotate_right(k as usize % (m * n));
for r in 0..n {
for c in 0..m {
shifted[r][c] = elements[r * m + c];
}
}
shifted

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation has a few areas for improvement:

  1. Safety: If grid is empty or contains empty rows, calling .unwrap() on grid.first() or performing modulo division by m * n (which would be 0) will cause a panic. Adding a guard clause makes the function robust.
  2. Efficiency & Idiomatic Rust: Instead of pre-allocating shifted with zeros and manually copying elements using nested loops, you can use chunks_exact to group the flat vector into rows and collect them directly. This avoids double-initialization and is much more idiomatic.
        if grid.is_empty() || grid[0].is_empty() {
            return grid;
        }
        let m = grid[0].len();
        let mut elements: Vec<i32> = grid.into_iter().flatten().collect();
        elements.rotate_right(k as usize % elements.len());
        elements.chunks_exact(m).map(|chunk| chunk.to_vec()).collect()

@tony84727
tony84727 merged commit c069b15 into main Jul 20, 2026
4 checks passed
@tony84727
tony84727 deleted the leetcode/1260 branch July 20, 2026 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant