为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode303. 区域和检索 - 数组不可变 | Range Sum Query - Immutable

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9757168.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.

Example:

Given nums = [-2, 0, 3, -5, 2, -1]

sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3 

Note:

  1. You may assume that the array does not change.
  2. There are many calls to sumRange function.

给定一个整数数组  nums,求出数组从索引 到 j  (i ≤ j) 范围内元素的总和,包含 i,  j 两点。

示例:

给定 nums = [-2, 0, 3, -5, 2, -1],求和函数为 sumRange()

sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3

说明:

  1. 你可以假设数组不可变。
  2. 会多次调用 sumRange 方法。

 132ms

 1 class NumArray {
 2     let sum: [Int]
 3     init(_ nums: [Int]) {
 4         var s = 0
 5         var sum = [0]
 6         for n in nums {
 7             s += n
 8             sum.append(s)
 9         }
10         self.sum = sum
11     }
12 
13     func sumRange(_ i: Int, _ j: Int) -> Int {
14         return sum[j + 1] - sum[i]
15     }
16 }

132ms

 1 class NumArray {
 2     
 3     let _nums : [Int]
 4     var sums : [Int]
 5     
 6     init(_ nums: [Int]) {
 7         _nums = nums
 8         
 9         sums = [Int]()
10         if nums.isEmpty {
11             return
12         }
13         sums.append(_nums[0])
14         for i in 1..<nums.count {
15             sums.append(sums[i-1] + _nums[i])
16         }
17         
18     }
19     
20     @inline(__always) func sumRange(_ i: Int, _ j: Int) -> Int {
21         if i == 0 {
22             return sums[j]
23         }
24         return sums[j]-sums[i-1]
25     }
26 }
27 
28 /**
29  * Your NumArray object will be instantiated and called as such:
30  * let obj = NumArray(nums)
31  * let ret_1: Int = obj.sumRange(i, j)
32  */
33  

172ms

 1 class NumArray {
 2     
 3     private var sums: [Int] = []
 4 
 5     init(_ nums: [Int]) {
 6         guard !nums.isEmpty else {
 7             return
 8         }
 9         
10         var sums = Array(repeating: 0, count: nums.count)
11         sums[0] = nums[0]
12         for index in 1..<nums.count {
13             let num = nums[index]
14             sums[index] = sums[index - 1] + num
15         }
16     
17         self.sums = sums
18     }
19     
20     func sumRange(_ i: Int, _ j: Int) -> Int {
21         if i == 0 {
22             return sums[j]
23         } else {
24             return sums[j] - sums[i - 1]
25         }
26     }
27 }

2444ms

 1 class NumArray {
 2     let nums: [Int]
 3     
 4     init(_ nums: [Int]) {
 5         self.nums = nums
 6     }
 7     
 8     func sumRange(_ i: Int, _ j: Int) -> Int
 9     {
10         var sum = 0
11         for index in i...j
12         {
13             sum += nums[index]
14         }
15         return sum
16     }
17 }

3328ms

 1 class NumArray {
 2 
 3     var nums: [Int]
 4     
 5     init(_ nums: [Int]) {
 6         self.nums = nums
 7     }
 8     
 9     func sumRange(_ i: Int, _ j: Int) -> Int {
10         
11         guard i <= j && i < nums.count else {
12             return 0
13         }
14         
15         let i = i < 0 ? 0 : i
16         let j = j < nums.count ? j : nums.count-1
17         
18         var sum = 0
19         for k in i...j {
20             sum += nums[k]
21         }
22 
23         return sum
24     }
25 }

 

posted @ 2018-10-08 21:06  为敢技术  阅读(272)  评论(0编辑  收藏  举报