Skip to content

Commit ad2158e

Browse files
realDuYuanChaogithub-actions
and
github-actions
authored
leetcode-2021.3.13 (#71)
* running-sum-of-1d-array * shuffle-the-array * richest-customer-wealth * number-of-good-pairs * Formatted with Google Java Formatter Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent a3b58a9 commit ad2158e

9 files changed

+139
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/build/
33
.idea/
44
target
5+
*.iml
56

67
# Ignore Gradle GUI config
78
gradle-app.setting
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
/** https://leetcode.com/problems/number-of-good-pairs/ */
4+
public class NumberOfGoodPairs {
5+
public static int solution1(int[] nums) {
6+
int goodPairs = 0;
7+
for (int i = 0; i < nums.length; ++i) {
8+
for (int j = i + 1; j < nums.length; j++) {
9+
if (nums[i] == nums[j]) {
10+
goodPairs++;
11+
}
12+
}
13+
}
14+
return goodPairs;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
import com.examplehub.maths.SumOfArray;
4+
5+
/** https://leetcode.com/problems/richest-customer-wealth/ */
6+
public class RichestCustomerWealth {
7+
public static int solution1(int[][] accounts) {
8+
int max = SumOfArray.sum(accounts[0]);
9+
for (int i = 1; i < accounts.length; ++i) {
10+
int temp = SumOfArray.sum(accounts[i]);
11+
if (temp > max) {
12+
max = temp;
13+
}
14+
}
15+
return max;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
/** https://leetcode.com/problems/running-sum-of-1d-array/ */
4+
public class RunningSumOf1dArray {
5+
public static int[] solution1(int[] nums) {
6+
for (int i = 1; i < nums.length; ++i) {
7+
nums[i] += nums[i - 1];
8+
}
9+
return nums;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
/** https://leetcode.com/problems/shuffle-the-array/ */
4+
public class ShuffleTheArray {
5+
public static int[] solution1(int[] nums, int n) {
6+
int[] result = new int[nums.length];
7+
int index = 0;
8+
for (int i = 0, j = n; i < n; i++, j++) {
9+
result[index++] = nums[i];
10+
result[index++] = nums[j];
11+
}
12+
System.arraycopy(result, 0, nums, 0, result.length);
13+
return nums;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class NumberOfGoodPairsTest {
8+
@Test
9+
void testSolution1() {
10+
int[] numbers = new int[] {1, 2, 3, 1, 1, 3};
11+
assertEquals(4, NumberOfGoodPairs.solution1(numbers));
12+
13+
numbers = new int[] {1, 1, 1, 1};
14+
assertEquals(6, NumberOfGoodPairs.solution1(numbers));
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class RichestCustomerWealthTest {
8+
@Test
9+
void test() {
10+
int[][] account =
11+
new int[][] {
12+
{1, 2, 3},
13+
{3, 2, 1}
14+
};
15+
assertEquals(RichestCustomerWealth.solution1(account), 6);
16+
17+
account =
18+
new int[][] {
19+
{1, 5},
20+
{7, 3},
21+
{3, 5}
22+
};
23+
assertEquals(RichestCustomerWealth.solution1(account), 10);
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.util.Arrays;
6+
import org.junit.jupiter.api.Test;
7+
8+
class RunningSumOf1dArrayTest {
9+
@Test
10+
void testSolution1() {
11+
int[] nums = {1, 2, 3, 4};
12+
RunningSumOf1dArray.solution1(nums);
13+
assertEquals(Arrays.toString(nums), Arrays.toString(new int[] {1, 3, 6, 10}));
14+
15+
nums = new int[] {1, 1, 1, 1, 1};
16+
RunningSumOf1dArray.solution1(nums);
17+
assertEquals(Arrays.toString(nums), Arrays.toString(new int[] {1, 2, 3, 4, 5}));
18+
19+
nums = new int[] {3, 1, 2, 10, 1};
20+
RunningSumOf1dArray.solution1(nums);
21+
assertEquals(Arrays.toString(nums), Arrays.toString(new int[] {3, 4, 6, 16, 17}));
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.util.Arrays;
6+
import org.junit.jupiter.api.Test;
7+
8+
class ShuffleTheArrayTest {
9+
@Test
10+
void testSolution1() {
11+
int[] nums = {2, 5, 1, 3, 4, 7};
12+
ShuffleTheArray.solution1(nums, 3);
13+
assertEquals(Arrays.toString(nums), Arrays.toString(new int[] {2, 3, 5, 4, 1, 7}));
14+
}
15+
}

0 commit comments

Comments
 (0)