File tree 2 files changed +37
-1
lines changed
2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,672 LeetCode solutions in JavaScript
1
+ # 1,673 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1526
1526
1985|[ Find the Kth Largest Integer in the Array] ( ./solutions/1985-find-the-kth-largest-integer-in-the-array.js ) |Medium|
1527
1527
1986|[ Minimum Number of Work Sessions to Finish the Tasks] ( ./solutions/1986-minimum-number-of-work-sessions-to-finish-the-tasks.js ) |Medium|
1528
1528
1987|[ Number of Unique Good Subsequences] ( ./solutions/1987-number-of-unique-good-subsequences.js ) |Hard|
1529
+ 1991|[ Find the Middle Index in Array] ( ./solutions/1991-find-the-middle-index-in-array.js ) |Easy|
1529
1530
1996|[ The Number of Weak Characters in the Game] ( ./solutions/1996-the-number-of-weak-characters-in-the-game.js ) |Medium|
1530
1531
2000|[ Reverse Prefix of Word] ( ./solutions/2000-reverse-prefix-of-word.js ) |Easy|
1531
1532
2011|[ Final Value of Variable After Performing Operations] ( ./solutions/2011-final-value-of-variable-after-performing-operations.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1991. Find the Middle Index in Array
3
+ * https://leetcode.com/problems/find-the-middle-index-in-array/
4
+ * Difficulty: Easy
5
+ *
6
+ * Given a 0-indexed integer array nums, find the leftmost middleIndex (i.e., the smallest amongst
7
+ * all the possible ones).
8
+ *
9
+ * A middleIndex is an index where nums[0] + nums[1] + ... +
10
+ * nums[middleIndex-1] == nums[middleIndex+1] + nums[middleIndex+2] + ... + nums[nums.length-1].
11
+ *
12
+ * If middleIndex == 0, the left side sum is considered to be 0. Similarly, if
13
+ * middleIndex == nums.length - 1, the right side sum is considered to be 0.
14
+ *
15
+ * Return the leftmost middleIndex that satisfies the condition, or -1 if there is no such index.
16
+ */
17
+
18
+ /**
19
+ * @param {number[] } nums
20
+ * @return {number }
21
+ */
22
+ var findMiddleIndex = function ( nums ) {
23
+ let left = 0 ;
24
+ let right = nums . reduce ( ( sum , n ) => sum + n , 0 ) ;
25
+
26
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
27
+ left += nums [ i ] ;
28
+ right -= nums [ i ] ;
29
+ if ( left - nums [ i ] === right ) {
30
+ return i ;
31
+ }
32
+ }
33
+
34
+ return - 1 ;
35
+ } ;
You can’t perform that action at this time.
0 commit comments