由于我正在學習資料結構和演算法,我對計算演算法的時間和空間復雜度感到非??常困惑。
這個問題來自leetcode。
def product_except_self(nums):
result = []
start = 0
while start != len(nums):
total = 1
for i in nums:
if i != nums[start]:
total *= i
result.append(total)
start = 1
return result
所以,我的計算復雜度是,
時間復雜度:O(n^2)
空間復雜度:O(n)
uj5u.com熱心網友回復:
def product_except_self(nums):
result = [] // in the worst case you would need to put all array in the result array, what give O(n) space complexity.
start = 0
while start != len(nums): // O(n) complexity
total = 1
for i in nums: // One more loop that gave additional O(n) complexity, in result we have O(n^2)
if i != nums[start]:
total *= i
result.append(total)
start = 1
return result
是的,你有正確的假設
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/489539.html
