Open
Conversation
naoto-iwase
reviewed
Nov 1, 2025
| int maxSubArray(const vector<int>& nums) { | ||
| vector<int> cumulative_sums{0}; | ||
| int cumulative_min = 0; | ||
| int max_sum = nums[0]; |
There was a problem hiding this comment.
問題の入力制約ではあり得ないですが、nums[0]にアクセスしているので先にnums.empty()でないかチェックしておくと安全かなと思いました。
Owner
Author
There was a problem hiding this comment.
コメントありがとうございます!
たしかに、空でないかチェックする方が安全ですね。
oda
reviewed
Nov 18, 2025
| vector<int> cumulative_sums{0}; | ||
| int cumulative_min = 0; | ||
| int max_sum = nums[0]; | ||
| for (auto num : nums) { |
There was a problem hiding this comment.
特に問題ないかと思います。私は int num: にしますが、古い C++ を書いていたからかもしれません。
There was a problem hiding this comment.
別の言語の話になりますが、一般的な C# のコード規則では、変数の型が代入の右側から明らかである場合、 var を使用する、そうでない場合は var を使用しないとなっています。
https://learn.microsoft.com/ja-jp/dotnet/csharp/fundamentals/coding-style/coding-conventions
変数の型が代入の右側から明らかである場合、ローカル変数の暗黙の型指定を使用します。
代入の右側から型が明らかではない場合、var を使用しないでください。 メソッド名から型が明らかであると想定しないでください。 変数型は、new 演算子、明示的なキャスト、またはリテラル値への割り当てである場合、明確と見なされます。
チームの平均的な書き方に合わせることをおすすめします。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
今回の問題
https://leetcode.com/problems/maximum-subarray/
次の問題
https://leetcode.com/problems/unique-paths/