Skip to content

1. Two Sum#11

Open
TakayaShirai wants to merge 1 commit intomainfrom
1_two_sum
Open

1. Two Sum#11
TakayaShirai wants to merge 1 commit intomainfrom
1_two_sum

Conversation

@TakayaShirai
Copy link
Owner

@TakayaShirai TakayaShirai self-assigned this Dec 4, 2025
continue
}

return [index, indexToFind]

Choose a reason for hiding this comment

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

好みの範囲かと思いますが、index[今みている要素], indexToFind[先に出てきた要素]で、読む人の脳内では、return [indexToFind, index]でもいいかと思いました。

}
}

// それぞれの手法でかかる時間を計測してみた。
Copy link

Choose a reason for hiding this comment

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

こんなものかもしれないです。
もしも、調べたかったらでいいのですが、これ、どういう入力でやりましたか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

レビューありがとうございます。

入力については、nums は 0 ~ 100,000 の値からランダムに抽出を繰り返して作成し、target は、解が必ず存在するように、二つのインデックスを用意し、nums でそれらのインデックスの位置にある数字を足し合わせることで作成しました。具体的に入力を生成する関数としては、以下を使用しました。

func generateData(count: Int) -> (nums: [Int], target: Int) {
    let nums = (0..<count).map { _ in Int.random(in: 0...100_000) }
    
    let idx1 = Int.random(in: 0..<count)
    var idx2 = Int.random(in: 0..<count)
    while idx1 == idx2 { idx2 = Int.random(in: 0..<count) }
    
    let target = nums[idx1] + nums[idx2]
    return (nums, target)
}

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.

3 participants