[Swift]LeetCode303. 區域和檢索 - 數組不可變 | Range Sum Query - Immutable

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-xnfjkiqm-ks.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

Example:github

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 兩點。微信

示例:app

給定 nums = [-2, 0, 3, -5, 2, -1],求和函數爲 sumRange()

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

說明:函數

  1. 你能夠假設數組不可變。
  2. 會屢次調用 sumRange 方法。

 132msspa

 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 }

132mscode

 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  

172mshtm

 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 }
相關文章
相關標籤/搜索