国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

LeetCode 2765. Longest Alternating Subarray

2023-07-09 08:05 作者:您是打尖兒還是住店呢  | 我要投稿

You are given a?0-indexed?integer array?nums. A subarray?s?of length?m?is called?alternating?if:

  • m?is greater than?1.

  • s1?= s0?+ 1.

  • The 0-indexed subarray?s?looks like?[s0, s1, s0, s1,...,s(m-1) % 2]. In other words,?s1?- s0?= 1,?s2?- s1?= -1,?s3?- s2?= 1,?s4?- s3?= -1, and so on up to?s[m - 1] - s[m - 2] = (-1)m.

Return?the maximum length of all?alternating?subarrays present in?nums?or?-1?if no such subarray exists.

A subarray is a contiguous?non-empty?sequence of elements within an array.

?

Example 1:

Input: nums = [2,3,4,3,4]

Output: 4

Explanation: The alternating subarrays are [3, 4], [3, 4, 3], and [3, 4, 3, 4]. The longest of these is [3,4,3,4], which is of length 4.

Example 2:

Input: nums = [4,5,6]

Output: 2

Explanation: [4,5] and [5,6] are the only two alternating subarrays. They are both of length 2.

?

Constraints:

  • 2 <= nums.length <= 100

  • 1 <= nums[i] <= 104

-------------------------------

解:先判斷第一項(xiàng)是否符合,如果不符合,直接返回,然后去遍歷每個(gè)元素是否跟它后面第2個(gè)數(shù)字是否一致即可。這個(gè)函數(shù)寫(xiě)好后,再去依次遍歷,判斷是否符合。下面是代碼;

Runtime:?37 ms, faster than?33.33%?of?Java?online submissions for?Longest Alternating Subarray.

Memory Usage:?43.5 MB, less than?66.67%?of?Java?online submissions for?Longest Alternating Subarray.


LeetCode 2765. Longest Alternating Subarray的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
恩施市| 佛教| 新密市| 南漳县| 神木县| 西盟| 安远县| 博客| 右玉县| 南部县| 类乌齐县| 博客| 红安县| 磐安县| 禄丰县| 延长县| 霞浦县| 武宁县| 三原县| 五家渠市| 潜江市| 抚顺县| SHOW| 西吉县| 怀集县| 兴文县| 那坡县| 繁昌县| 温泉县| 建瓯市| 武义县| 云阳县| 厦门市| 固阳县| 阳城县| 樟树市| 晋宁县| 星子县| 壤塘县| 长泰县| 舟山市|