-
Notifications
You must be signed in to change notification settings - Fork 0
238. Product of Array Except Self #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| resやlengthに関しても何が入っているのか分かるような変数名をつけたい | ||
| https://github.com/t-ooka/leetcode/pull/5 | ||
|
|
||
| indexの管理方法を考えることで左側の積と右側の積を一度に計算することができる |
There was a problem hiding this comment.
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 | ||
| いつも忘れるので気になったら都度読む癖をつける |
There was a problem hiding this comment.
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 | ||
| いつも忘れるので気になったら都度読む癖をつける | ||
|
|
There was a problem hiding this comment.
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;
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こんなのもあります。
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nums.empty() のほうがシンプルだと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nodchip
レビューありがとうございます。確かにこちらの方が素直ですね。step4_2に追加しました。またこちらのもので3回書ける練習をしました。
問題へのリンク
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内に各ステップで感じたことを追記します。