413:等差数列划分
This commit is contained in:
parent
a957bc7a6b
commit
8bd1475a8e
75
src/main/java/leetcode/editor/cn/ArithmeticSlices.java
Normal file
75
src/main/java/leetcode/editor/cn/ArithmeticSlices.java
Normal file
@ -0,0 +1,75 @@
|
||||
//如果一个数列 至少有三个元素 ,并且任意两个相邻元素之差相同,则称该数列为等差数列。
|
||||
//
|
||||
//
|
||||
// 例如,[1,3,5,7,9]、[7,7,7,7] 和 [3,-1,-5,-9] 都是等差数列。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 给你一个整数数组 nums ,返回数组 nums 中所有为等差数组的 子数组 个数。
|
||||
//
|
||||
// 子数组 是数组中的一个连续序列。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:nums = [1,2,3,4]
|
||||
//输出:3
|
||||
//解释:nums 中有三个子等差数组:[1, 2, 3]、[2, 3, 4] 和 [1,2,3,4] 自身。
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:nums = [1]
|
||||
//输出:0
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= nums.length <= 5000
|
||||
// -1000 <= nums[i] <= 1000
|
||||
//
|
||||
//
|
||||
//
|
||||
// Related Topics 数组 动态规划
|
||||
// 👍 309 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//413:等差数列划分
|
||||
class ArithmeticSlices {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new ArithmeticSlices().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int numberOfArithmeticSlices(int[] nums) {
|
||||
if (nums.length < 3) {
|
||||
return 0;
|
||||
}
|
||||
int sub = nums[0] - nums[1], count = 0;
|
||||
int sum = 0;
|
||||
for (int i = 2; i < nums.length; i++) {
|
||||
if (nums[i - 1] - nums[i] == sub) {
|
||||
count++;
|
||||
} else {
|
||||
sub = nums[i - 1] - nums[i];
|
||||
count = 0;
|
||||
}
|
||||
sum += count;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
40
src/main/java/leetcode/editor/cn/ArithmeticSlices.md
Normal file
40
src/main/java/leetcode/editor/cn/ArithmeticSlices.md
Normal file
@ -0,0 +1,40 @@
|
||||
<p>如果一个数列 <strong>至少有三个元素</strong> ,并且任意两个相邻元素之差相同,则称该数列为等差数列。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>[1,3,5,7,9]</code>、<code>[7,7,7,7]</code> 和 <code>[3,-1,-5,-9]</code> 都是等差数列。</li>
|
||||
</ul>
|
||||
|
||||
<div class="original__bRMd">
|
||||
<div>
|
||||
<p>给你一个整数数组 <code>nums</code> ,返回数组 <code>nums</code> 中所有为等差数组的 <strong>子数组</strong> 个数。</p>
|
||||
|
||||
<p><strong>子数组</strong> 是数组中的一个连续序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3,4]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>nums 中有三个子等差数组:[1, 2, 3]、[2, 3, 4] 和 [1,2,3,4] 自身。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1]
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 5000</code></li>
|
||||
<li><code>-1000 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div><div>Related Topics</div><div><li>数组</li><li>动态规划</li></div></div>\n<div><li>👍 309</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user