Skip to content

1.two sum#11

Open
nicah4o wants to merge 1 commit into
mainfrom
1.two-sum
Open

1.two sum#11
nicah4o wants to merge 1 commit into
mainfrom
1.two-sum

Conversation

@nicah4o
Copy link
Copy Markdown
Owner

@nicah4o nicah4o commented May 13, 2026

Comment thread 1_two_sum/answer.md
for (int i = 0; i < nums.size(); i++) {
int complement = target - nums[i];
if (num_to_index.contains(complement)) {
vector<int> ans = {i, num_to_index[complement]};
Copy link
Copy Markdown

@TaisukeFujise TaisukeFujise May 13, 2026

Choose a reason for hiding this comment

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

以下のように直接returnをかけるので、ansのような一次変数におく必要はないと思います。

return {i, num_to_index[complement]};

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

レビューありがとうございます。
なるほど、なぜか3msから変わらないと思っていたらこの変数がボトルネックになっていました。
ご指摘助かりました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

コンパイラでおそらく最適化されますし、これがボトルネックになることはないと思います。実行時間のブレは、LeetCodeの環境によるものかと思われます。

Copy link
Copy Markdown

@jjysogfy jjysogfy left a comment

Choose a reason for hiding this comment

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

考えたことが書いてあり見やすかったです。何かあれば教えてください。

Comment thread 1_two_sum/answer.md
public:
vector<int> twoSum(vector<int>& nums, int target) {
int i, j;
for (i = 0; i <= (nums.size() - 1); i++) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この(nums.size() - 1)で、外側のかっこが無いほうが見慣れた感じで好みです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

レビューありがとうございます。
理解不足で括弧必要だと勘違いしていました。
i < nums.size()でもいけますね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

演算子の優先順位で調べてみると良いと思います。自明ではないなら、不要でも括弧をつけるのもありですね。

https://en.cppreference.com/cpp/language/operator_precedence

Comment thread 1_two_sum/answer.md
vector<int> ans = {i, num_to_index[complement]};
return ans;
}
else {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ifの中で returnしているので、このelseを消してインデントを下げられますね。どちらでも趣味の範囲だと思います。

Comment thread 1_two_sum/answer.md
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int i, j;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

for (int i = 0; i <= (nums.size() - 1); i++)のように使う直前に宣言すると良いと思います。

Comment thread 1_two_sum/answer.md

## step2
leetcodeの解法見た。unordered_mapをsum_to_pairでなくnum_to_indexで作る。
targer-nums[i] をnum_to_indexで検索すると一瞬で答えがわかる。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hash tableの仕組み(衝突回避やリアロケーションなど)についても、まだ調べていなかったら見てみると良いと思います。
https://en.wikipedia.org/wiki/Hash_table

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

レビューありがとうございます。
ハッシュテーブルの仕組みについては調べていなかったのでリンク参考になります。
挙げていただいた論点についても調べてみます。

Comment thread 1_two_sum/answer.md

arai60の分類だとhashmapに該当するのだが、どこでmapを使うべきかわからなかった。
今までの問題だとsum_to_pairのmapを作ることが多かった。
前回の問題でも(https://leetcode.com/problems/find-k-pairs-with-smallest-sums/) 、そのようなsum_to_pairで全走査する方式を使って上手くいかなかったが思いつかなかったので今回も全走査した。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらのことでしょうか?そうでしたら、mapではなくて、pairですね。
https://github.com/nicah4o/arai60/pull/10/files#diff-4441ba9bfe4587a375388da6cd11a2db15081cba994b37ca9f2ec8fddf3b327eR22-R23

        using pi = pair<int, vector<int>>;
        priority_queue<pi, vector<pi>, greater<pi>> sum_to_pair;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。

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.

4 participants