|
22160 | 22160 | <ul class="md-nav__list">
|
22161 | 22161 |
|
22162 | 22162 | <li class="md-nav__item">
|
22163 |
| - <a href="#solution-1" class="md-nav__link"> |
| 22163 | + <a href="#solution-1-prefix-sum" class="md-nav__link"> |
22164 | 22164 | <span class="md-ellipsis">
|
22165 |
| - Solution 1 |
| 22165 | + Solution 1: Prefix Sum |
22166 | 22166 | </span>
|
22167 | 22167 | </a>
|
22168 | 22168 |
|
@@ -77186,7 +77186,16 @@ <h2 id="description">Description</h2>
|
77186 | 77186 | <h2 id="solutions">Solutions</h2>
|
77187 | 77187 | <!-- solution:start -->
|
77188 | 77188 |
|
77189 |
| -<h3 id="solution-1">Solution 1</h3> |
| 77189 | +<h3 id="solution-1-prefix-sum">Solution 1: Prefix Sum</h3> |
| 77190 | +<p>We define a variable $left$ to represent the sum of elements to the left of index $i$ in the array $\textit{nums}$, and a variable $right$ to represent the sum of elements to the right of index $i$ in the array $\textit{nums}$. Initially, $left = 0$, $right = \sum_{i = 0}^{n - 1} nums[i]$.</p> |
| 77191 | +<p>We traverse the array $\textit{nums}$. For the current number $x$ being traversed, we update $right = right - x$. At this point, if $left = right$, it indicates that the current index $i$ is the middle position, and we can return it directly. Otherwise, we update $left = left + x$ and continue to traverse the next number.</p> |
| 77192 | +<p>If the middle position is not found by the end of the traversal, return $-1$.</p> |
| 77193 | +<p>The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array $\textit{nums}$.</p> |
| 77194 | +<p>Similar Problems:</p> |
| 77195 | +<ul> |
| 77196 | +<li><a href="https://github.com/doocs/leetcode/blob/main/solution/1900-1999/1991.Find%20the%20Middle%20Index%20in%20Array/README_EN.md">1991. Find the Middle Index in Array</a></li> |
| 77197 | +<li><a href="https://github.com/doocs/leetcode/blob/main/solution/2500-2599/2574.Left%20and%20Right%20Sum%20Differences/README_EN.md">2574. Left and Right Sum Differences</a></li> |
| 77198 | +</ul> |
77190 | 77199 | <div class="tabbed-set tabbed-alternate" data-tabs="1:7"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><input id="__tabbed_1_7" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python3</label><label for="__tabbed_1_2">Java</label><label for="__tabbed_1_3">C++</label><label for="__tabbed_1_4">Go</label><label for="__tabbed_1_5">TypeScript</label><label for="__tabbed_1_6">Rust</label><label for="__tabbed_1_7">JavaScript</label></div>
|
77191 | 77200 | <div class="tabbed-content">
|
77192 | 77201 | <div class="tabbed-block">
|
|
0 commit comments