You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add solutions to lc problems: No.3210~3212 (#3219)
* No.3210.Find the Encrypted String
* No.3211.Generate Binary Strings Without Adjacent Zeros
* No.3212.Count Submatrices With Equal Frequency of X and Y
Copy file name to clipboardexpand all lines: solution/0700-0799/0724.Find Pivot Index/README_EN.md
+14-1
Original file line number
Diff line number
Diff line change
@@ -73,7 +73,20 @@ Right sum = nums[1] + nums[2] = 1 + -1 = 0
73
73
74
74
<!-- solution:start -->
75
75
76
-
### Solution 1
76
+
### Solution 1: Prefix Sum
77
+
78
+
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]$.
79
+
80
+
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.
81
+
82
+
If the middle position is not found by the end of the traversal, return $-1$.
83
+
84
+
The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array $\textit{nums}$.
85
+
86
+
Similar Problems:
87
+
88
+
-[1991. Find the Middle Index in Array](https://github.com/doocs/leetcode/blob/main/solution/1900-1999/1991.Find%20the%20Middle%20Index%20in%20Array/README_EN.md)
89
+
-[2574. Left and Right Sum Differences](https://github.com/doocs/leetcode/blob/main/solution/2500-2599/2574.Left%20and%20Right%20Sum%20Differences/README_EN.md)
0 commit comments