Skip to content

Conversation

@Ryotaro25
Copy link
Owner

問題へのリンク
https://leetcode.com/problems/3sum/description/

問題文(プレミアムの場合)

備考

次に解く問題の予告
Container With Most Water

フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cpp、brute_force.cpp、sort.cppとmemo.mdとなります。

memo.md内に各ステップで感じたことを追記します。

@nodchip
Copy link

nodchip commented Jul 31, 2025

2 つの問題のソースコードが 1 つの PR に含まれているように見えます。

while (left < right) {
int sum = nums[index] + nums[left] + nums[right];
if (sum < 0) {
left++;
Copy link

Choose a reason for hiding this comment

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

sum < 0 と sum > 0 の場合は continue し、 sum == 0 の場合のネストを下げたほうが読みやすいと思います。

right--;
} else {
triplets.push_back({nums[index], nums[left], nums[right]});
left++;
Copy link

Choose a reason for hiding this comment

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

do while 文で回すと 1 行減ります。読みやすさはあまり変わらないかもしれません。

}

private:
void FindTriplet(vector<int>& nums, int index, vector<vector<int>>& triplets) {
Copy link

Choose a reason for hiding this comment

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

nums が非 const 参照だと、読み手にとって中で変更されるのだと思わせてしまうと思います。 nums は変更されないため、 const 参照で渡すことをおすすめします。

LeetCode が指定した関数宣言については、仕方ないと思います。

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