Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0129620

Browse files
committed
add: Arithmetic Slices
1 parenta3eec7a commit0129620

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
5555
|371|[Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/)|[JavaScript](./src/sum-of-two-integers/res.js)|Easy|
5656
|384|[Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/)|[JavaScript](./src/shuffle-an-array/res.js)|Medium|
5757
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/)|[JavaScript](./src/sum-of-left-leaves/res.js)|Easy|
58+
|413|[Arithmetic Slices](https://leetcode.com/problems/arithmetic-slices/)|[JavaScript](./src/arithmetic-slices/res.js)|Medium|
5859
|416|[Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/)|[JavaScript](./src/partition-equal-subset-sum/res.js)|Medium|
5960
|434|[Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/)|[JavaScript](./src/number-of-segments-in-a-string/res.js)|Easy|
6061
|494|[Target Sum](https://leetcode.com/problems/target-sum/)|[JavaScript](./src/target-sum/res.js)|Medium|

‎src/arithmetic-slices/res.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* res.js
3+
*@authors Joe Jiang (hijiangtao@gmail.com)
4+
*@date 2017-04-13 11:02:22
5+
*
6+
* 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.
7+
* A slice (P, Q) of array A is called arithmetic if the sequence:
8+
* A[P], A[P + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q.
9+
* The function should return the number of arithmetic slices in the array A.
10+
*
11+
*@param {number[]} A
12+
*@return {number}
13+
*/
14+
letnumberOfArithmeticSlices=function(A){
15+
letlen=A.length;
16+
if(len<3){
17+
return0;
18+
}
19+
20+
letres=0,
21+
interval=[];
22+
23+
// 构建元素差数组
24+
for(leti=1;i<len;i++){
25+
interval.push(A[i]-A[i-1]);
26+
}
27+
28+
letcount=2,
29+
difPre=interval[0];
30+
31+
len--;
32+
for(leti=1;i<len;i++){
33+
letdifNow=interval[i];
34+
35+
if(difNow===difPre){
36+
count++;
37+
}else{
38+
res+=Sum(count);// 计算当前积攒元素个数下的 arithmetic 个数
39+
difPre=difNow;
40+
count=2;
41+
}
42+
}
43+
44+
if(count>2){
45+
res+=Sum(count);
46+
}
47+
48+
returnres;
49+
};
50+
51+
letSum=function(n){
52+
// console.log(n);
53+
return(n-2)*(n-1)/2;
54+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp