[Swift]LeetCode238. 除自身以外数组的乘积 | Product of Array Except Self

[Swift]LeetCode238. 除自身以外数组的乘积 | Product of Array Except Self,第1张

概述Given an array nums of n integers where n > 1,  return an array outputsuch that output[i] is equal to the product of all the elements of numsexcept nums[i]. Example: Input: Output: [1,2,3,4][24,1

Given an array nums of n integers where n > 1, return an array outputsuch that output[i] is equal to the product of all the elements of numsexcept nums[i].

Example:

input:  Output: [1,2,3,4][24,12,8,6]

Note: Please solve it without division and in O(n).

Follow up:
Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)

给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积

示例:

输入: 输出: [1,6]

说明: 请不要使用除法,且在 O(n) 时间复杂度内完成此题。

进阶:
你可以在常数空间复杂度内完成这个题目吗?( 出于对空间复杂度分析的目的,输出数组不被视为额外空间。)

116ms

 1 class Solution { 2     func productExceptSelf(_ nums: [Int]) -> [Int] { 3  4     var result : [Int] = [] 5     var mutiplIEr = 1 6  7     for num in nums 8     { 9         result.append(mutiplIEr)10         mutiplIEr *= num11     }12 13     mutiplIEr = 114     var index = nums.count - 115     while index>=016     {17         result[index] *= mutiplIEr18         mutiplIEr *= nums[index]19         index -= 120     }21 22     return result23 }24 }

124ms

 1 class Solution { 2     func productExceptSelf(_ nums: [Int]) -> [Int] { 3         guard !nums.isEmpty else { return [] } 4  5         var output = [Int](repeating: 1,count: nums.count) 6         for i in nums.indices.dropFirst() { 7             output[i] = output[i - 1] * nums[i - 1] 8         } 9         10         var right = nums[nums.count - 1]11         for i in nums.indices.dropLast().reversed() {12             output[i] *= right13             right *= nums[i]14         }15         16         return output17     }18 }

124ms

 1 class Solution { 2     func productExceptSelf(_ nums: [Int]) -> [Int] { 3         let count = nums.count 4         if count == 0 { 5             return [Int]() 6         } 7         if count == 1 { 8             return [1] 9         }10         var result = nums11         for i in 1..<count - 1 {12             result[i] *= result[i - 1]13         }14         15         var temp = 116         for i in (1..<count).reversed() {17             result[i] = temp * result[i - 1]18             temp *= nums[i]19         }20         result[0] = temp21         return result22     }23 }

125ms

 1 class Solution { 2     func productExceptSelf(_ nums: [Int]) -> [Int] { 3         let total = nums.filter({ $0 != 0 }).reduce(1,{ $0 * $1 }) 4         let zero_count = nums.filter({ $0 == 0}).count 5         return nums.map { number -> Int in 6             if zero_count == 0 { 7                 return total / number 8             } else if zero_count == 1 { 9                 return number == 0 ? total : 010             } else {11                 return 012             }13         }14     }15 }

136ms

 1 class Solution { 2     func productExceptSelf(_ nums: [Int]) -> [Int] { 3         guard nums.count > 1 else { return [0]} 4         let letfProduct = nums.reduce(into:[]) { $0.append(($0.last ?? 1) * $1) } 5         let rightProduct = Array(Array(nums.reversed().reduce(into:[]) { $0.append(($0.last ?? 1) * $1) }).reversed()) 6         var result = [Int](repeating:0,count:nums.count) 7         for i in 0..<nums.count { 8             if i == 0 { 9                 result[i] = rightProduct[1]10             } else if i == nums.count-1 {11                 result[i] = letfProduct[nums.count-2]12             } else {13                 result[i] = letfProduct[i-1] * rightProduct[i+1]14             }15         }16         return result17     }18 }

160ms

 1 class Solution { 2     func productExceptSelf(_ nums: [Int]) -> [Int] { 3         let n = nums.count 4         var result = [Int](repeating: 1,count: n) 5         guard n > 0 else { return result } 6         //设left[i]为nums[i]左边的所有元素的乘积 7         var left = [Int](repeating: 1,count: n)  8         //设right[i]为nums[i]右边的所有元素的乘积 9         var right = [Int](repeating: 1,count: n) 10         for i in 1 ..< n {11             left[i] = left[i - 1] * nums[i - 1]12         }13         for i in (0 ..< n - 1).reversed() {14             right[i] = right[i + 1] * nums[i + 1]15         }16         for i in 0 ..< n {17             result[i] = left[i] * right[i]18         }19         return result20     }21 }

188ms

 1 class Solution { 2     func productExceptSelf(_ nums: [Int]) -> [Int] { 3         var res = [Int]() 4         var leftValue = 1 5         for i in 0 ..< nums.count { 6             res.append(leftValue) 7             leftValue *= nums[i] 8         } 9         var rightValue = 110         for i in strIDe(from: nums.count - 1,to: -1,by: -1) {11             res[i] = res[i] * rightValue12             rightValue *= nums[i]13         }14         return res15     }16 }
总结

以上是内存溢出为你收集整理的[Swift]LeetCode238. 除自身以外数组的乘积 | Product of Array Except Self全部内容,希望文章能够帮你解决[Swift]LeetCode238. 除自身以外数组的乘积 | Product of Array Except Self所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1021205.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存