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

Commit63da990

Browse files
refactor 413
1 parent38753de commit63da990

File tree

1 file changed

+28
-22
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+28
-22
lines changed

‎src/main/java/com/fishercoder/solutions/_413.java

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
packagecom.fishercoder.solutions;
22

3-
/**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.
3+
/**
4+
* 413. Arithmetic Slices
5+
*
6+
* 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.
47
58
For example, these are arithmetic sequence:
69
710
1, 3, 5, 7, 9
811
7, 7, 7, 7
912
3, -1, -5, -9
13+
1014
The following sequence is not arithmetic.
1115
1216
1, 1, 2, 5, 7
@@ -18,40 +22,42 @@ A slice (P, Q) of array A is called arithmetic if the sequence:
1822
1923
The function should return the number of arithmetic slices in the array A.
2024
21-
2225
Example:
2326
2427
A = [1, 2, 3, 4]
2528
26-
return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.*/
29+
return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.
30+
*/
2731
publicclass_413 {
2832

29-
//copied this solution: https://discuss.leetcode.com/topic/62884/2ms-java-o-n-time-o-1-space-solution
30-
publicintnumberOfArithmeticSlices(int[]A) {
31-
intsum =0;
32-
intlen =2;
33-
for (inti =2;i <A.length;i++) {
34-
if (A[i] -A[i -1] ==A[i -1] -A[i -2]) {
35-
len++;
36-
}else {
37-
if (len >2) {
38-
sum +=calculateSlices(len);
33+
publicstaticclassSolution1 {
34+
//credit: https://discuss.leetcode.com/topic/62884/2ms-java-o-n-time-o-1-space-solution
35+
publicintnumberOfArithmeticSlices(int[]A) {
36+
intsum =0;
37+
intlen =2;
38+
for (inti =2;i <A.length;i++) {
39+
if (A[i] -A[i -1] ==A[i -1] -A[i -2]) {
40+
len++;
41+
}else {
42+
if (len >2) {
43+
sum +=calculateSlices(len);
44+
}
45+
len =2;//reset it to 2
3946
}
40-
len =2;//reset it to 2
4147
}
48+
if (len >2) {
49+
sum +=calculateSlices(len);
50+
}
51+
returnsum;
4252
}
43-
if (len >2) {
44-
sum +=calculateSlices(len);
45-
}
46-
returnsum;
47-
}
4853

49-
intcalculateSlices(intlen) {
50-
return (len -1) * (len -2) /2;
54+
intcalculateSlices(intlen) {
55+
return (len -1) * (len -2) /2;
56+
}
5157
}
5258

5359
classSolution2 {
54-
//a more clear solution: https://discuss.leetcode.com/topic/63302/simple-java-solution-9-lines-2ms
60+
//credit: https://discuss.leetcode.com/topic/63302/simple-java-solution-9-lines-2ms
5561
publicintnumberOfArithmeticSlices(int[]A) {
5662
intsum =0;
5763
intcurr =0;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp