成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

leetcode413. Arithmetic Slices

piglei / 3552人閱讀

摘要:題目要求將包含大于等于三個元素且任意相鄰兩個元素之間的差相等的數(shù)組成為等差數(shù)列?,F(xiàn)在輸入一個隨機數(shù)組,問該數(shù)組中一共可以找出多少組等差數(shù)列。

題目要求
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.

將包含大于等于三個元素且任意相鄰兩個元素之間的差相等的數(shù)組成為等差數(shù)列。現(xiàn)在輸入一個隨機數(shù)組,問該數(shù)組中一共可以找出多少組等差數(shù)列。

思路一:動態(tài)規(guī)劃

假設(shè)已經(jīng)知道以第i-1個數(shù)字為結(jié)尾有k個等差數(shù)列,且第i個元素與i-1號元素和i-2號元素構(gòu)成了等差數(shù)列,則第i個數(shù)字為結(jié)尾的等差數(shù)列個數(shù)為k+1。因此我們可以自底向上動態(tài)規(guī)劃,記錄每一位作為結(jié)尾的等差數(shù)列的個數(shù),并最終得出整個數(shù)列中等差數(shù)列的個數(shù)。代碼如下:

    public int numberOfArithmeticSlices(int[] A) {
        int[] dp = new int[A.length];
        int count = 0;
        for(int i = 2 ; i
思路二:算數(shù)方法

首先看一個簡單的等差數(shù)列1 2 3, 可知該數(shù)列中一共有1個等差數(shù)列
再看1 2 3 4, 可知該數(shù)列中一共有3個等差數(shù)列,其中以3為結(jié)尾的1個,以4為結(jié)尾的2個
再看1 2 3 4 5, 可知該數(shù)列中一共有6個等差數(shù)列,其中以3為結(jié)尾的1個,4為結(jié)尾的2個,5為結(jié)尾的3個。

綜上,我們可以得出,如果是一個最大長度為n的等差數(shù)列,則該等差數(shù)列中一共包含的等差數(shù)列個數(shù)為(n-2+1)*(n-2)/2,即(n-1)*(n-2)/2

因此,我們只需要找到以當前起點為開始的最長的等差數(shù)列,計算該等差數(shù)列的長度并根據(jù)其長度得出其共包含多少個子等差數(shù)列。

代碼如下:

    public int numberOfArithmeticSlices2(int[] A) {
        if(A.length <3) return 0;
        int diff = A[1]-A[0];
        int left = 0;
        int right = 2;
        int count = 0;
        while(right < A.length) {
            if(A[right] - A[right-1] != diff) {
                count += (right-left-1) * (right-left-2) / 2;
                diff = A[right] - A[right-1];
                left = right-1;
            }
            right++;
        }
        count += (right-left-1) * (right-left-2) / 2;
        return count;
    }

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/74259.html

相關(guān)文章

  • Leetcode[413] Arithmetic Slices

    摘要:復雜度思路找數(shù)組里面的等差數(shù)列的個數(shù)。想法是如果一開始三個數(shù)就滿足是等差數(shù)列的話,就在當前已有的數(shù)目上不斷的累加的結(jié)果。 Leetcode[413] Arithmetic Slices A sequence of number is called arithmetic if it consists of at least three elements and if the diffe...

    _ipo 評論0 收藏0
  • LeetCode 3

    摘要:這個題沒什么好說的,用棧就可以了,注意一下兩個數(shù)計算的時候誰前誰后就行了。 Evaluate Reverse Polish Notation https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ Evaluate the value of an arithmetic expression in Reve...

    rainyang 評論0 收藏0
  • [LeetCode] 150. Evaluate Reverse Polish Notation

    Problem Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Note: Division between two inte...

    KoreyLee 評論0 收藏0
  • [Leetcode] Evaluate Reverse Polish Notation 計算逆波蘭表

    摘要:棧法復雜度時間空間思路逆波蘭表達式的計算十分方便,對于運算符,其運算的兩個數(shù)就是這個運算符前面的兩個數(shù)。注意對于減法,先彈出的是減號后面的數(shù)。 Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operato...

    ephererid 評論0 收藏0
  • leetcode150. Evaluate Reverse Polish Notation

    摘要:我們一般看到的數(shù)學表達式就是中綴表達式,也就是將符號放在兩個數(shù)字之間。后綴表達式也就是將運算符放在相應數(shù)字的后面。后綴表達式相當于樹中的后序遍歷。通過獲得對應位置的操作符。如果對應的還是操作符,則繼續(xù)遞歸往前計算。 題目要求 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid...

    bitkylin 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<