Skip to content

53. Maximum Subarray#30

Open
5103246 wants to merge 1 commit intomainfrom
53-maximum-subarray
Open

53. Maximum Subarray#30
5103246 wants to merge 1 commit intomainfrom
53-maximum-subarray

Conversation

@5103246
Copy link
Copy Markdown
Owner

@5103246 5103246 commented Nov 1, 2025

Copy link
Copy Markdown

@naoto-iwase naoto-iwase left a comment

Choose a reason for hiding this comment

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

いずれも読みやすかったです。

int maxSubArray(const vector<int>& nums) {
vector<int> cumulative_sums{0};
int cumulative_min = 0;
int max_sum = nums[0];
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[0]にアクセスしているので先にnums.empty()でないかチェックしておくと安全かなと思いました。

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.

コメントありがとうございます!
たしかに、空でないかチェックする方が安全ですね。

vector<int> cumulative_sums{0};
int cumulative_min = 0;
int max_sum = nums[0];
for (auto num : nums) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

特に問題ないかと思います。私は int num: にしますが、古い C++ を書いていたからかもしれません。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

別の言語の話になりますが、一般的な C# のコード規則では、変数の型が代入の右側から明らかである場合、 var を使用する、そうでない場合は var を使用しないとなっています。

https://learn.microsoft.com/ja-jp/dotnet/csharp/fundamentals/coding-style/coding-conventions

変数の型が代入の右側から明らかである場合、ローカル変数の暗黙の型指定を使用します。

代入の右側から型が明らかではない場合、var を使用しないでください。 メソッド名から型が明らかであると想定しないでください。 変数型は、new 演算子、明示的なキャスト、またはリテラル値への割り当てである場合、明確と見なされます。

チームの平均的な書き方に合わせることをおすすめします。

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