[LeetCode] 413. Arithmetic Slices

An integer array 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, [1,3,5,7,9][7,7,7,7], and [3,-1,-5,-9] are arithmetic sequences.

Given an integer array nums, return the number of arithmetic subarrays of nums.

A subarray is a contiguous subsequence of the array.

Example 1:

Input: nums = [1,2,3,4]
Output: 3
Explanation: We have 3 arithmetic slices in nums: [1, 2, 3], [2, 3, 4] and [1,2,3,4] itself.

Example 2:

Input: nums = [1]
Output: 0

Constraints:

  • 1 <= nums.length <= 5000
  • -1000 <= nums[i] <= 1000

等差数列划分。

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

例如,[1,3,5,7,9]、[7,7,7,7] 和 [3,-1,-5,-9] 都是等差数列。
给你一个整数数组 nums ,返回数组 nums 中所有为等差数组的 子数组 个数。

子数组 是数组中的一个连续序列。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/arithmetic-slices
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是动态规划。我们创建一个与 input 数组等长的 dp 数组,dp数组的定义是以 nums[i] 为结尾的最长的等差数列的长度是多少。这道题的思路类似53题。

时间O(n)

空间O(1) - 可以不需要额外空间

Java实现

 1 class Solution {
 2     public int numberOfArithmeticSlices(int[] nums) {
 3         int sum = 0;
 4         int[] dp = new int[nums.length];
 5         for (int i = 2; i < nums.length; i++) {
 6             if (nums[i] - nums[i - 1] == nums[i - 1] - nums[i - 2]) {
 7                 dp[i] = dp[i - 1] + 1;
 8                 sum += dp[i];
 9             }
10         }
11         return sum;
12     }
13 }

 

JavaScript实现

 1 /**
 2  * @param {number[]} nums
 3  * @return {number}
 4  */
 5  var numberOfArithmeticSlices = function(nums) {
 6     let sum = 0;
 7     let dp = new Array(nums.length).fill(0);
 8     for (let i = 2; i < nums.length; i++) {
 9         if (nums[i] - nums[i - 1] == nums[i - 1] - nums[i - 2]) {
10             dp[i] = dp[i - 1] + 1;
11             sum += dp[i];
12         }
13     }
14     return sum;
15 };

 

不需要额外空间的实现,其实用到的是数学思路。参考2348题

Java实现

 1 class Solution {
 2     public int numberOfArithmeticSlices(int[] nums) {
 3         int n = nums.length;
 4         // corner caes
 5         if (n < 3) {
 6             return 0;
 7         }
 8 
 9         // normal case
10         int res = 0;
11         int count = 0;
12         for (int i = 2; i < n; i++) {
13             if (nums[i] - nums[i - 1] == nums[i - 1] - nums[i - 2]) {
14                 count++;
15                 res += count;
16             } else {
17                 count = 0;
18             }
19         }
20         return res;
21     }
22 }

 

JavaScript实现

 1 /**
 2  * @param {number[]} nums
 3  * @return {number}
 4  */
 5 var numberOfArithmeticSlices = function(nums) {
 6     let sum = 0;
 7     let dp = new Array(nums.length).fill(0);
 8     for (let i = 2; i < nums.length; i++) {
 9         if (nums[i] - nums[i - 1] == nums[i - 1] - nums[i - 2]) {
10             dp[i] = dp[i - 1] + 1;
11             sum += dp[i];
12         }
13     }
14     return sum;
15 };

 

LeetCode 题目总结

posted @ 2021-02-19 02:09  CNoodle  阅读(252)  评论(1编辑  收藏  举报