成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

leetcode413. Arithmetic Slices

piglei / 3519人閱讀

摘要:題目要求將包含大于等于三個(gè)元素且任意相鄰兩個(gè)元素之間的差相等的數(shù)組成為等差數(shù)列?,F(xiàn)在輸入一個(gè)隨機(jī)數(shù)組,問(wèn)該數(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.

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

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

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

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

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

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

因此,我們只需要找到以當(dāng)前起點(diǎn)為開(kāi)始的最長(zhǎng)的等差數(shù)列,計(jì)算該等差數(shù)列的長(zhǎng)度并根據(jù)其長(zhǎng)度得出其共包含多少個(gè)子等差數(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)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/74259.html

相關(guān)文章

  • Leetcode[413] Arithmetic Slices

    摘要:復(fù)雜度思路找數(shù)組里面的等差數(shù)列的個(gè)數(shù)。想法是如果一開(kāi)始三個(gè)數(shù)就滿足是等差數(shù)列的話,就在當(dāng)前已有的數(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 評(píng)論0 收藏0
  • LeetCode 3

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

    rainyang 評(píng)論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 評(píng)論0 收藏0
  • [Leetcode] Evaluate Reverse Polish Notation 計(jì)算逆波蘭表

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

    ephererid 評(píng)論0 收藏0
  • leetcode150. Evaluate Reverse Polish Notation

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

    bitkylin 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<