执行用时:160 ms, 在所有 Swift 提交中击败了100.00%的用户 内存消耗:19.2 MB, 在所有 Swift 提交中击败了100.00%的用户
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution{ funcproductExceptSelf(_nums: [Int]) -> [Int] { let n = nums.count var res = [Int]() varL= [Int].init(repeating: 1, count: n) varR= [Int].init(repeating: 1, count: n) for i in1..< n { L[i] =L[i-1] * nums[i-1] } for j in (0..< n-1).reversed() { R[j] =R[j+1] * nums[j+1] } for i in0..< n { res.append(L[i] *R[i]) } return res } }