[Swift]LeetCode446. 等差數列劃分 II - 子序列 | Arithmetic Slices II - Subsequence

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

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

For example, these are arithmetic sequences:github

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

The following sequence is not arithmetic.數組

1, 1, 2, 5, 7

A zero-indexed array A consisting of N numbers is given. A subsequence slice of that array is any sequence of integers (P0, P1, ..., Pk) such that 0 ≤ P0 < P1 < ... < Pk < N.微信

A subsequence slice (P0, P1, ..., Pk) of array A is called arithmetic if the sequence A[P0], A[P1], ..., A[Pk-1], A[Pk] is arithmetic. In particular, this means that k ≥ 2.less

The function should return the number of arithmetic subsequence slices in the array A.函數

The input contains N integers. Every integer is in the range of -231 and 231-1 and 0 ≤ N ≤ 1000. The output is guaranteed to be less than 231-1.this

Example:spa

Input: [2, 4, 6, 8, 10]

Output: 7

Explanation:
All arithmetic subsequence slices are:
[2,4,6]
[4,6,8]
[6,8,10]
[2,4,6,8]
[4,6,8,10]
[2,4,6,8,10]
[2,6,10]

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

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

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

如下數列不是等差數列。

1, 1, 2, 5, 7 

數組 A 包含 N 個數,且索引從 0 開始。該數組子序列將劃分爲整數序列 (P0, P1, ..., Pk),P 與 Q 是整數且知足 0 ≤ P0 < P1 < ... < Pk < N。 

若是序列 A[P0],A[P1],...,A[Pk-1],A[Pk] 是等差的,那麼數組 A 的子序列 (P0,P1,…,PK) 稱爲等差序列。值得注意的是,這意味着 k ≥ 2。

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

輸入包含 N 個整數。每一個整數都在 -231 和 231-1 之間,另外 0 ≤ N ≤ 1000。保證輸出小於 231-1。 

示例: 

輸入:[2, 4, 6, 8, 10]

輸出:7

解釋:
全部的等差子序列爲:
[2,4,6]
[4,6,8]
[6,8,10]
[2,4,6,8]
[4,6,8,10]
[2,4,6,8,10]
[2,6,10]

1128ms
 1 class Solution {
 2     func numberOfArithmeticSlices(_ A: [Int]) -> Int {
 3         var res:Int = 0
 4         var n:Int = A.count
 5         var dp:[[Int:Int]] = [[Int:Int]](repeating:[Int:Int](),count:n)
 6         for i in 0..<n
 7         {
 8             for j in 0..<i
 9             {
10                 var delta:Int64 = Int64(A[i]) - Int64(A[j])
11                 if delta > Int64(Int.max) || delta < Int64(Int.min) {continue}
12                 var diff:Int = Int(delta)
13                 
14                 if dp[i][diff] == nil
15                 {
16                     dp[i][diff] = 1
17                 }
18                 else
19                 {
20                     dp[i][diff]! += 1                    
21                 }
22                 
23                 if dp[j][diff] != nil
24                 {
25                     res += dp[j][diff]!
26                     dp[i][diff]! += dp[j][diff]!
27                 }
28             }
29         }
30         return res
31     }
32 }
相關文章
相關標籤/搜索