Skip to content
Merged
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
16 changes: 16 additions & 0 deletions src/leetcode/26_remove-duplicates-from-sorted-array/20251029.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
func removeDuplicates(nums []int) int {
if len(nums) == 0 {
return 0
}

k := 1

for i := 0; i < len(nums) - 1; i++ {
if nums[i] != nums[i + 1] {
Comment on lines +8 to +9
Copy link

Copilot AI Oct 29, 2025

Choose a reason for hiding this comment

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

より効率的なアプローチとして、ii+1を比較するのではなく、kとの比較を行う方法があります。これにより、ループの範囲もlen(nums)全体に拡張でき、ロジックがよりシンプルになります。

推奨解法:

k := 1
for i := 1; i < len(nums); i++ {
if nums[i] != nums[k-1] {
nums[k] = nums[i]
k++
}
}

メリット:

  • ✅ インデックス計算(i+1)が不要になり、可読性が向上
  • ✅ ループ範囲が0からlen(nums)-1ではなく1からlen(nums)-1となり、意図がより明確

Copilot generated this review using guidance from repository custom instructions.
nums[k] = nums[i + 1]
k++
}
}

return k
}
Loading