-
Notifications
You must be signed in to change notification settings - Fork 0
Add function to rotate array in-place #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Implement in-place rotation of an array by k steps.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
このPRは、LeetCode 189「配列の回転」問題の解答実装を追加するものです。配列を右にk回回転させるアルゴリズムとして、三回転法(配列全体を反転し、その後前半とk後半をそれぞれ反転する手法)を採用しています。
主な変更点:
rotate関数の実装: O(1)の空間計算量で配列をk回右に回転reverse補助関数の実装: 配列の指定範囲を反転
Comments suppressed due to low confidence (1)
src/leetcode/189_rotate-array/20251107.ts:10
- The value assigned to nums here is unused.
nums = reverse(nums, k, n - 1);
| nums = reverse(nums, 0, n - 1); | ||
| nums = reverse(nums, 0, k - 1); | ||
| nums = reverse(nums, k, n - 1); | ||
| }; | ||
|
|
||
| function reverse(nums: number[], startIndex: number, endIndex: number): number[] { | ||
| while (startIndex < endIndex) { | ||
| [nums[endIndex], nums[startIndex]] = [nums[startIndex], nums[endIndex]]; | ||
|
|
||
| startIndex++; | ||
| endIndex--; | ||
| } | ||
| return nums; | ||
| } |
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐛 重大なバグ: 配列の変更が反映されない(-3点)
問題点:
rotate関数内でnumsにreverseの戻り値を再代入していますが、これはローカル変数への再代入であり、元の配列への変更が反映されません。問題文では「in-place(その場で)配列を変更する」ことが求められています。
現在のコード:
nums = reverse(nums, 0, n - 1);
nums = reverse(nums, 0, k - 1);
nums = reverse(nums, k, n - 1);このコードでは、numsというローカル変数が新しい参照を指すようになりますが、呼び出し元の配列は変更されません。
改善案:
reverse関数は既に配列をin-placeで変更しているので、戻り値を代入する必要はありません:
reverse(nums, 0, n - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, n - 1);または、reverse関数からreturn nums;を削除してvoid型にすることもできます:
function reverse(nums: number[], startIndex: number, endIndex: number): void {
while (startIndex < endIndex) {
[nums[endIndex], nums[startIndex]] = [nums[startIndex], nums[endIndex]];
startIndex++;
endIndex--;
}
}テストケース例:
const nums = [1, 2, 3, 4, 5, 6, 7];
rotate(nums, 3);
// 期待: [5, 6, 7, 1, 2, 3, 4]
// 現在の実装: [1, 2, 3, 4, 5, 6, 7] (変更されない)| nums = reverse(nums, 0, n - 1); | |
| nums = reverse(nums, 0, k - 1); | |
| nums = reverse(nums, k, n - 1); | |
| }; | |
| function reverse(nums: number[], startIndex: number, endIndex: number): number[] { | |
| while (startIndex < endIndex) { | |
| [nums[endIndex], nums[startIndex]] = [nums[startIndex], nums[endIndex]]; | |
| startIndex++; | |
| endIndex--; | |
| } | |
| return nums; | |
| } | |
| reverse(nums, 0, n - 1); | |
| reverse(nums, 0, k - 1); | |
| reverse(nums, k, n - 1); | |
| }; | |
| function reverse(nums: number[], startIndex: number, endIndex: number): void { | |
| while (startIndex < endIndex) { | |
| [nums[endIndex], nums[startIndex]] = [nums[startIndex], nums[endIndex]]; | |
| startIndex++; | |
| endIndex--; | |
| } | |
| } | |
| } |
|
|
||
| nums = reverse(nums, 0, n - 1); | ||
| nums = reverse(nums, 0, k - 1); | ||
| nums = reverse(nums, k, n - 1); |
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⚠️ エッジケース: k = 0 の処理(-0.5点)
問題点:
k = 0の場合、reverse(nums, 0, -1)が呼び出されますが、これは意図しない動作を引き起こす可能性があります。k % nが0になる場合の処理を追加すべきです。
改善案:
export function rotate(nums: number[], k: number): void {
const n = nums.length;
k = k % n;
if (k === 0) return; // 早期リターン
reverse(nums, 0, n - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, n - 1);
}テストケース:
rotate([1, 2, 3], 0); // または rotate([1, 2, 3], 3);
// 期待: [1, 2, 3] (変更なし)| nums = reverse(nums, 0, n - 1); | |
| nums = reverse(nums, 0, k - 1); | |
| nums = reverse(nums, k, n - 1); | |
| if (k === 0) return; // 早期リターン | |
| reverse(nums, 0, n - 1); | |
| reverse(nums, 0, k - 1); | |
| reverse(nums, k, n - 1); |
| nums = reverse(nums, 0, k - 1); | ||
| nums = reverse(nums, k, n - 1); | ||
| }; | ||
|
|
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 ドキュメント: 関数の説明が不足(-0.5点)
問題点:
reverse関数にコメントがなく、その役割や使用方法が明確ではありません。特に、この関数は配列をin-placeで反転させる重要な補助関数です。
改善案:
/**
* 配列の指定された範囲を in-place で反転させる
* @param nums - 対象の配列
* @param startIndex - 開始インデックス
* @param endIndex - 終了インデックス
* @returns 反転後の配列(参照渡しのため同じ配列)
*/
function reverse(nums: number[], startIndex: number, endIndex: number): number[] {
while (startIndex < endIndex) {
[nums[endIndex], nums[startIndex]] = [nums[startIndex], nums[endIndex]];
startIndex++;
endIndex--;
}
return nums;
}| /** | |
| * 配列の指定された範囲を in-place で反転させる | |
| * @param nums - 対象の配列 | |
| * @param startIndex - 開始インデックス | |
| * @param endIndex - 終了インデックス | |
| * @returns 反転後の配列(参照渡しのため同じ配列) | |
| */ |
Status
Accepted
Runtime
3 ms (Beats 56.05%)
Memory
67.95 MB (Beats 27.54%)
Time Taken
17 m 45 s