Skip to content

Conversation

@Ryotaro25
Copy link
Owner

@Ryotaro25 Ryotaro25 commented May 1, 2025

問題へのリンク
https://leetcode.com/problems/product-of-array-except-self/description/

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

備考

次に解く問題の予告
Maximum Product Subarray

フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
PRに出したフォルダ内は、step1.cpp、step2.cpp、step3.cpp、memo.mdとmain/ディレクトリになります。
mainディレクトリ内では、ヘッダーファイルとソースファイルを分離しております。

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

resやlengthに関しても何が入っているのか分かるような変数名をつけたい
https://github.com/t-ooka/leetcode/pull/5

indexの管理方法を考えることで左側の積と右側の積を一度に計算することができる
Copy link

Choose a reason for hiding this comment

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

できますが、私はそんなに推奨しませんね。


vector<int>の初期化について
https://stackoverflow.com/questions/42743604/default-values-when-creating-a-vector-c
いつも忘れるので気になったら都度読む癖をつける
Copy link

Choose a reason for hiding this comment

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

2次元行列作るときに使っていますね。

vector<int>の初期化について
https://stackoverflow.com/questions/42743604/default-values-when-creating-a-vector-c
いつも忘れるので気になったら都度読む癖をつける

Copy link

Choose a reason for hiding this comment

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

vector<int> productExceptSelf(vector<int>& nums) {
    int n = nums.size();
    vector<int> prefix(n, 1);
    partial_sum(nums.begin(), nums.end() - 1, prefix.begin() + 1, multiplies<int>());
    vector<int> suffix(n, 1);
    partial_sum(nums.rbegin(), nums.rend() - 1, suffix.rbegin() + 1, multiplies<int>());
    vector<int> result(n);
    transform(prefix.begin(), prefix.end(), suffix.begin(), result.begin(), multiplies<int>());
    return result;
}

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.

@oda
レビューありがとうございます。
普段使っているもの(主にvector、map、set)以外に関しても足を運んで調べる姿勢をよりつけないとと感じました。
まずはpartial_sum、multiplies、transformをきちんと理解しておいて今後のコーディングで活かせるようにしたいと思います。


// Solution::をつけないとグローバル関数となる
std::vector<int> Solution::productExceptSelf(std::vector<int>& nums) {
if (nums.size() == 0) {
Copy link

Choose a reason for hiding this comment

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

nums.empty() のほうがシンプルだと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

@nodchip
レビューありがとうございます。確かにこちらの方が素直ですね。step4_2に追加しました。またこちらのもので3回書ける練習をしました。

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