zoukankan      html  css  js  c++  java
  • [Swift]LeetCode413. 等差数列划分 | Arithmetic Slices

    原文地址:https://www.cnblogs.com/strengthen/p/10330630.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.

    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.

    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.

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

    Example:

    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.

    如果一个数列至少有三个元素,并且任意两个相邻元素之差相同,则称该数列为等差数列。

    例如,以下数列为等差数列:

    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 }
  • 相关阅读:
    HDU 4393 Throw nails(贪心加模拟,追及问题)
    【Add Two Numbers】
    【Single Num II】cpp
    【Single Number】cpp
    【Candy】cpp
    【Gas Station】cpp
    【Set Matrix Zeros】cpp
    【Gray Code】cpp
    【Climbing Stairs】cpp
    【Plus One】cpp
  • 原文地址:https://www.cnblogs.com/strengthen/p/10330630.html
Copyright © 2011-2022 走看看