Skip to content

Implement moveZeroes method to shift zeros#35

Merged
argondev22 merged 1 commit intomainfrom
20251209/34-283-move-zeroes
Dec 9, 2025
Merged

Implement moveZeroes method to shift zeros#35
argondev22 merged 1 commit intomainfrom
20251209/34-283-move-zeroes

Conversation

@argondev22
Copy link
Copy Markdown
Owner

@argondev22 argondev22 commented Dec 9, 2025

Status

Accepted

Runtime

2 ms

Order

O(n)

Memory

47.65 MB

Order

O(1)

Time Taken

21 m 59 s

@argondev22 argondev22 self-assigned this Dec 9, 2025
Copilot AI review requested due to automatic review settings December 9, 2025 09:41
@argondev22 argondev22 linked an issue Dec 9, 2025 that may be closed by this pull request
Copy link
Copy Markdown
Contributor

Copilot AI left a 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問題283「Move Zeroes」の解答を実装しています。配列内のすべてのゼロを末尾に移動させながら、非ゼロ要素の相対的な順序を保持するアルゴリズムです。提出結果はAcceptedで、O(n)の時間計算量とO(1)の空間計算量を達成しています。

主な変更点

  • 2ポインタ方式を使用したin-place配列操作の実装
  • 非ゼロ要素を前に詰め、ゼロを後ろに移動させるスワップロジック
  • 追加メモリを使用しない効率的な実装

Comment on lines +3 to +14
int j = 0;

for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
continue;
}

int tmp = nums[j];
nums[j] = nums[i];
nums[i] = tmp;

j++;
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

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

変数名jが不明確です。この変数は非ゼロ要素を配置する位置を追跡するため、insertPosnonZeroIndexのようなより説明的な名前を使用することをお勧めします。

改善案:

int insertPos = 0;  // 非ゼロ要素を配置する位置
Suggested change
int j = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
continue;
}
int tmp = nums[j];
nums[j] = nums[i];
nums[i] = tmp;
j++;
int insertPos = 0; // 非ゼロ要素を配置する位置
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
continue;
}
int tmp = nums[insertPos];
nums[insertPos] = nums[i];
nums[i] = tmp;
insertPos++;

Copilot uses AI. Check for mistakes.
Comment thread src/leetcode/283_move-zeroes/20251209.java
Comment on lines +1 to +17
class Solution {
public void moveZeroes(int[] nums) {
int j = 0;

for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
continue;
}

int tmp = nums[j];
nums[j] = nums[i];
nums[i] = tmp;

j++;
}
}
}
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

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

📊 総合評価:8点 / 10点

良い点(+8点)

1. 正しく動作する(+3点)

  • アルゴリズムは正確で、全てのテストケースで期待通りの結果を出力します
  • 非ゼロ要素の相対的な順序を保持しながら、ゼロを配列の末尾に移動しています
  • 例: [0,1,0,3,12][1,3,12,0,0] が正しく動作

2. 効率的な実装(+3点)

  • 時間計算量: O(n) - 配列を1回だけトラバース
  • 空間計算量: O(1) - 追加の配列を使用せず、in-placeで操作
  • 2ポインタ方式で効率的に実装されています

3. 簡潔な実装(+2点)

  • コードが短く、理解しやすい構造
  • 不要な処理を避けている

改善が必要な点(-2点)

1. 変数名の不明確さ(-1点)

問題点: 変数jの役割が一目で分かりにくい

int j = 0;  // 何のインデックスか不明確

改善案:

int insertPos = 0;  // 非ゼロ要素を配置する位置
// または
int nonZeroIndex = 0;  // 次の非ゼロ要素の挿入位置

2. 不要なスワップ処理(-1点)

問題点: i == jの場合、自分自身とスワップする無駄な処理が発生

// i == j の場合、以下は不要
int tmp = nums[j];
nums[j] = nums[i];
nums[i] = tmp;

改善案:

if (nums[i] != 0) {
    if (i != j) {  // i != j の場合のみスワップ
        int tmp = nums[j];
        nums[j] = nums[i];
        nums[i] = tmp;
    }
    j++;
}

🎯 最適解との比較

推奨解法(改善版)

class Solution {
    public void moveZeroes(int[] nums) {
        int insertPos = 0;  // より明確な変数名
        
        // 非ゼロ要素を前に詰める
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                if (i != insertPos) {  // 不要なスワップを避ける
                    nums[insertPos] = nums[i];
                    nums[i] = 0;
                }
                insertPos++;
            }
        }
    }
}

メリット:

  • ✅ 変数名がより分かりやすい(insertPosは非ゼロ要素の挿入位置を明示)
  • i == insertPosの場合の不要なスワップを回避
  • ✅ スワップではなく直接代入することで、よりシンプル

📊 計算量の比較

解法 時間計算量 空間計算量 不要な処理
現在の解法 O(n) O(1) i==jの場合の無駄なスワップあり
推奨解法 O(n) O(1) 不要な処理を回避

💡 学習ポイント

1. 2ポインタ方式の理解

  • i: 配列全体を走査するポインタ
  • j (または insertPos): 非ゼロ要素を配置する位置を追跡

2. 変数命名の重要性

  • jよりもinsertPosnonZeroIndexの方が意図が明確
  • 可読性の向上はバグの削減につながる

3. エッジケースの確認

  • 全てゼロの配列: [0,0,0] → 正しく動作 ✅
  • ゼロがない配列: [1,2,3] → 正しく動作(ただし不要なスワップが発生)
  • 単一要素: [0] または [1] → 正しく動作 ✅

📝 まとめ

良かった点

  • ✅ アルゴリズムの正しさ:完璧に動作
  • ✅ 時間・空間計算量:最適なO(n)とO(1)
  • ✅ in-place操作:追加のメモリを使用しない

改善点

  • ❌ 変数名:jinsertPosなどに変更すると可読性向上
  • ❌ 最適化:i == jの場合の不要なスワップを回避

次のステップ

  1. 変数名をより説明的なものに変更する
  2. 不要な処理を削減する条件分岐を追加
  3. 他の2ポインタ問題(例:Remove Duplicates from Sorted Array)にも挑戦

全体的に非常に良い実装です! アルゴリズムの核心を理解しており、効率的なソリューションを実装できています。細かい最適化と可読性の向上で、さらに優れたコードになります。引き続き頑張ってください!🎉

Copilot generated this review using guidance from repository custom instructions.
@argondev22 argondev22 merged commit 37b9b85 into main Dec 9, 2025
6 checks passed
@argondev22 argondev22 deleted the 20251209/34-283-move-zeroes branch December 9, 2025 09:57
@github-project-automation github-project-automation Bot moved this from Todo to Done in My Learnings Dec 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

283. Move Zeroes

2 participants