forked from NilaakashSingh/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Because of The problem statement mentions that don't using divide , b…
…ut the solution use divide in last line in file, the advantages of the solution is use less space . So I add back solution that do not using divide as before. The time cost is almost same both solution.
- Loading branch information
Showing
1 changed file
with
60 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,63 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/product-of-array-except-self/ | ||
* Primary idea: Use two arrays to hold multiplication result from left and right sides | ||
* while iterating the original array | ||
* Time Complexity: O(n), Space Complexity: O(1) | ||
*/ | ||
* Question Link: https://leetcode.com/problems/product-of-array-except-self/ | ||
* Primary idea: Use two arrays to hold multiplication result from left and right sides | ||
* while iterating the original array | ||
* Time Complexity: O(n), Space Complexity: O(1) | ||
*/ | ||
|
||
class ProductExceptSelf { | ||
func productExceptSelf(_ nums: [Int]) -> [Int] { | ||
var zeroCount = 0 | ||
let total = nums.reduce(1) { | ||
if $1 == 0 { | ||
zeroCount += 1 | ||
} | ||
return $0 * ($1 == 0 ? 1 : $1) | ||
} | ||
if zeroCount > 1 {return nums.map({_ in return 0})} | ||
return nums.map({ | ||
if zeroCount == 1 { | ||
return ($0 == 0 ? total : 0) | ||
} else { | ||
return ($0 == 0 ? total : total / $0) | ||
} | ||
}) | ||
} | ||
} | ||
func productExceptSelf(_ nums: [Int]) -> [Int] { | ||
var zeroCount = 0 | ||
let total = nums.reduce(1) { | ||
if $1 == 0 { | ||
zeroCount += 1 | ||
} | ||
return $0 * ($1 == 0 ? 1 : $1) | ||
} | ||
if zeroCount > 1 {return nums.map({_ in return 0})} | ||
return nums.map({ | ||
if zeroCount == 1 { | ||
return ($0 == 0 ? total : 0) | ||
} else { | ||
return ($0 == 0 ? total : total / $0) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
/** | ||
* Question Link: https://leetcode.com/problems/product-of-array-except-self/ | ||
* Primary idea: Dynamic Programming, track Left and Right product lists at the same time and just use one additional array.The problem statement mentions that using theanswer array doesn't add to the space complexity. | ||
* Time Complexity: O(n), Space Complexity: O(1) | ||
*/ | ||
|
||
|
||
class ProductExceptSelfNotUseDivide{ | ||
|
||
func productExceptSelf(_ nums: [Int]) -> [Int] { | ||
|
||
var arr = Array.init(repeating: 1, count: nums.count) | ||
|
||
for i in (0..<(nums.count - 1)).reversed() { | ||
|
||
arr[i] = arr[i + 1] * nums[i + 1] | ||
|
||
} | ||
|
||
var left = 1 | ||
for i in 0..<nums.count { | ||
if i == 0 { | ||
arr[i] = left * arr[i] | ||
} else { | ||
left = left * nums[i - 1] | ||
arr[i] = left * arr[i] | ||
} | ||
} | ||
|
||
return arr | ||
} | ||
|
||
} |