Conversation
| ``` | ||
|
|
||
| -時間計算量 o(n) | ||
| -空間計算量 o(1) |
There was a problem hiding this comment.
res = [0] * len(nums) としているので、空間計算量は O(n) ではないでしょうか?
There was a problem hiding this comment.
レスポンスとして使用する値については空間計算量に含まない認識で書いておりました。
ソースコードベースですとおっしゃるとおり、O(n)です、、!
|
|
||
| left,rightはそれぞれ、各々のインデックスの数までの積を表しているので、両隣の数の積を取れば自身をのぞいた積にになる。 | ||
|
|
||
| - 時間計算量 o(n) |
| vector<int> productExceptSelf(vector<int>& nums) { | ||
| int length = nums.size(); | ||
|
|
||
| vector<int> L(length); |
There was a problem hiding this comment.
ローカル変数の名前は snake_case で命名することをお勧めします。
https://google.github.io/styleguide/cppguide.html#Variable_Names
The names of variables (including function parameters) and data members are snake_case (all lowercase, with underscores between words).
また、変数名には変数の値の目的や意図を端的に表した英単語・英語句を付けることをお勧めします。
https://google.github.io/styleguide/cppguide.html#General_Naming_Rules
Use names that describe the purpose or intent of the object.
| } | ||
|
|
||
| vector<int> R(length); | ||
| R[length-1] = 1; |
There was a problem hiding this comment.
- 等、 2 項演算子の左右に空白を一つずつ空けることをお勧めいたします。
https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace
Other binary operators usually have spaces around them,
| R[i] = R[i+1] * nums[i+1]; | ||
| } | ||
|
|
||
| vector<int> ans(length); |
There was a problem hiding this comment.
変数名には原則省略形は用いないことをお勧めいたします。
https://google.github.io/styleguide/cppguide.html#General_Naming_Rules
Minimize the use of abbreviations that would likely be unknown to someone outside your project (especially acronyms and initialisms). Do not abbreviate by deleting letters within a word. As a rule of thumb, an abbreviation is probably OK if it's listed in Wikipedia. Generally speaking, descriptiveness should be proportional to the name's scope of visibility.
|
数週間空けて解けたのでマージします。 |
問題リンク
問題文
Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in O(n) time and without using the division operation.
Example 1:
Example 2:
Constraints: