[Swift]LeetCode413. 等差數列劃分 | Arithmetic Slices

原文地址:http://www.javashuo.com/article/p-vjxhvnwu-dt.html html

A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.數組

For example, these are arithmetic sequence:函數

1, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9

The following sequence is not arithmetic.this

1, 1, 2, 5, 7

A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 <= P < Q < N.spa

A slice (P, Q) of array A is called arithmetic if the sequence:
A[P], A[p + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q.code

The function should return the number of arithmetic slices in the array A.htm

Example:blog

A = [1, 2, 3, 4]

return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.

若是一個數列至少有三個元素,而且任意兩個相鄰元素之差相同,則稱該數列爲等差數列。索引

例如,如下數列爲等差數列:three

1, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9

如下數列不是等差數列。

1, 1, 2, 5, 7 

數組 A 包含 N 個數,且索引從0開始。數組 A 的一個子數組劃分爲數組 (P, Q),P 與 Q 是整數且知足 0<=P<Q<N 。

若是知足如下條件,則稱子數組(P, Q)爲等差數組:

元素 A[P], A[p + 1], ..., A[Q - 1], A[Q] 是等差的。而且 P + 1 < Q 。

函數要返回數組 A 中全部爲等差數組的子數組個數。 

示例:

A = [1, 2, 3, 4]

返回: 3, A 中有三個子等差數組: [1, 2, 3], [2, 3, 4] 以及自身 [1, 2, 3, 4]。

8ms
 1 class Solution {
 2     func numberOfArithmeticSlices(_ A: [Int]) -> Int {
 3         guard A.count >= 3 else {
 4             return 0
 5         }
 6 
 7         var result = 0
 8         for i in 2..<A.count {
 9             if A[i] - A[i-1] == A[i-1] - A[i-2] {
10                 result += 1
11                 for j in i + 1..<A.count {
12                     if A[j] - A[j - 1] == A[j - 1] - A[j - 2] {
13                         result += 1
14                     } else {
15                         break
16                     }
17                 }
18             }
19         }
20 
21         return result
22     }
23 }

16ms

 1 class Solution {
 2     func numberOfArithmeticSlices(_ A: [Int]) -> Int {
 3         if A.count < 3 {return 0}
 4         var res:Int = 0
 5         var len:Int = 2
 6         var n:Int = A.count
 7         for i in 2..<n
 8         {
 9             if A[i] - A[i - 1] == A[i - 1] - A[i - 2]
10             {
11                 len += 1
12             }
13             else
14             {
15                 if len > 2
16                 {
17                     res += (len - 1) * (len - 2) / 2
18                     len = 2
19                 }
20             }
21         }
22         if len > 2
23         {
24             res += (len - 1) * (len - 2) / 2
25         }
26         return res
27     }
28 }

20ms

 1 class Solution {
 2     func numberOfArithmeticSlices(_ A: [Int]) -> Int {
 3         guard A.count >= 3 else {
 4             return 0
 5         }
 6         
 7         var P = 0
 8         var res = 0
 9         var diff = A[1] - A[0]
10         var count = 0
11         var i = 2
12         while i < A.count {
13             if A[i] - A[i-1] == diff {
14                 count += i - P - 1
15             }
16             else {
17                 res += count
18                 count = 0
19                 diff = A[i] - A[i-1]
20                 P = i - 1
21             }
22             i += 1
23         }
24         res += count        
25         return res
26     }
27 }

24ms

 1 class Solution {
 2     func numberOfArithmeticSlices(_ A: [Int]) -> Int {
 3         let length = A.count
 4         guard length > 2 else {
 5             return 0
 6         }
 7         
 8         var count = 1
 9         var dis = A[1] - A[0]
10         var num = 0
11         
12         for index in 2..<length {
13             let curDis = A[index] - A[index - 1]
14             if curDis == dis {
15                 count += 1
16             } else {
17                 
18                 if count >= 2 {
19                     num += (1 + (count - 1)) * (count - 1) / 2
20                 }
21                 dis = curDis
22                 count = 1
23             }
24         }
25         if count >= 2 {
26             num += (1 + (count - 1)) * (count - 1) / 2
27         }
28         return num
29     }
30 }
相關文章
相關標籤/搜索