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

Commitb504cf5

Browse files
[LEET-523] add 523
1 parent5f38e99 commitb504cf5

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

‎leetcode-algorithms/src/main/java/com/stevesun/solutions/ContinuousSubarraySum.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
packagecom.stevesun.solutions;
22

33
/**
4-
* Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.
4+
* Given a list of non-negative numbers and a target integer k,
5+
* write a function to check if the array has a continuous subarray of size at least 2
6+
* that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.
57
68
Example 1:
79
Input: [23, 2, 4, 6, 7], k=6
@@ -30,17 +32,16 @@ public boolean checkSubarraySum(int[] nums, int k) {
3032
}
3133

3234
//then k cannot be zero any more
33-
if (k ==0)returnfalse;
35+
if (k ==0 ||nums.length <2)returnfalse;
3436

35-
int[]preSums =newint[nums.length];
36-
preSums[0] =nums[0];
37-
for (inti =1;i <nums.length;i++) {
38-
preSums[i] =preSums[i-1] +nums[i];
37+
int[]preSums =newint[nums.length+1];
38+
for (inti =1;i <=nums.length;i++) {
39+
preSums[i] =preSums[i-1] +nums[i-1];
3940
}
4041

41-
for (inti =1;i <nums.length;i++) {
42-
for (intj =0;j <=i-1;j++) {
43-
if ((preSums[i] -nums[j]) %k ==0)returntrue;
42+
for (inti =1;i <=nums.length;i++) {
43+
for (intj =0;j <i-1;j++) {
44+
if ((preSums[i] -preSums[j]) %k ==0)returntrue;
4445
}
4546
}
4647
returnfalse;

‎leetcode-algorithms/src/test/java/com/stevesun/ContinuousSubarraySumTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,49 @@ public void test5(){
6767
assertEquals(expected,actual);
6868
}
6969

70+
@Test
71+
publicvoidtest6(){
72+
nums =newint[]{1,1};
73+
expected =true;
74+
k =2;
75+
actual =test.checkSubarraySum(nums,k);
76+
assertEquals(expected,actual);
77+
}
78+
79+
@Test
80+
publicvoidtest7(){
81+
nums =newint[]{0};
82+
expected =false;
83+
k = -1;
84+
actual =test.checkSubarraySum(nums,k);
85+
assertEquals(expected,actual);
86+
}
87+
88+
@Test
89+
publicvoidtest8(){
90+
nums =newint[]{23,2,4,6,7};
91+
expected =true;
92+
k = -6;
93+
actual =test.checkSubarraySum(nums,k);
94+
assertEquals(expected,actual);
95+
}
96+
97+
@Test
98+
publicvoidtest9(){
99+
nums =newint[]{1,2,3};
100+
expected =false;
101+
k =4;
102+
actual =test.checkSubarraySum(nums,k);
103+
assertEquals(expected,actual);
104+
}
105+
106+
@Test
107+
publicvoidtest10(){
108+
nums =newint[]{5,2,4};
109+
expected =false;
110+
k =5;
111+
actual =test.checkSubarraySum(nums,k);
112+
assertEquals(expected,actual);
113+
}
114+
70115
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp