Skip to content

Commit b614e8d

Browse files
add 1991
1 parent eee5a93 commit b614e8d

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1992|[Find All Groups of Farmland](https://leetcode.com/problems/find-all-groups-of-farmland/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1992.java) ||Medium||
12+
|1991|[Find the Middle Index in Array](https://leetcode.com/problems/find-the-middle-index-in-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1991.java) ||Easy||
1113
|1985|[Find the Kth Largest Integer in the Array](https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1985.java) ||Medium||
1214
|1984|[Minimum Difference Between Highest and Lowest of K Scores](https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1984.java) ||Easy||
1315
|1981|[Minimize the Difference Between Target and Chosen Elements](https://leetcode.com/problems/minimize-the-difference-between-target-and-chosen-elements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1981.java) ||Medium|DP|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1991 {
4+
public static class Solution1 {
5+
public int findMiddleIndex(int[] nums) {
6+
int middleIndex = -1;
7+
long sum = 0;
8+
for (int i = 0; i < nums.length; i++) {
9+
sum += nums[i];
10+
}
11+
long leftSum = 0;
12+
for (int i = 0; i < nums.length; i++) {
13+
sum -= nums[i];
14+
if (i > 0) {
15+
leftSum += nums[i - 1];
16+
}
17+
if (sum == leftSum) {
18+
return i;
19+
}
20+
}
21+
return middleIndex;
22+
}
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1991;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1991Test {
10+
private static _1991.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1991.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(3, solution1.findMiddleIndex(new int[]{2, 3, -1, 8, 4}));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)