Skip to content

Commit 4452d94

Browse files
committed
feat: update leetcode solutions: No.0724. Find Pivot Index
1 parent 76c6663 commit 4452d94

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

solution/0700-0799/0724.Find Pivot Index/README.md

+26-2
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,39 @@ nums = [1, 2, 3]
4949
<!-- 这里可写当前语言的特殊实现逻辑 -->
5050

5151
```python
52-
52+
class Solution:
53+
def pivotIndex(self, nums: List[int]) -> int:
54+
sums = sum(nums)
55+
pre_sum = 0
56+
for i, v in enumerate(nums):
57+
if (pre_sum << 1) == sums - v:
58+
return i
59+
pre_sum += v
60+
return -1
5361
```
5462

5563
### **Java**
5664

5765
<!-- 这里可写当前语言的特殊实现逻辑 -->
5866

5967
```java
60-
68+
class Solution {
69+
public int pivotIndex(int[] nums) {
70+
int sums = 0;
71+
for (int e : nums) {
72+
sums += e;
73+
}
74+
int preSum = 0;
75+
for (int i = 0; i < nums.length; ++i) {
76+
// preSum == sums - nums[i] - preSum
77+
if (preSum << 1 == sums - nums[i]) {
78+
return i;
79+
}
80+
preSum += nums[i];
81+
}
82+
return -1;
83+
}
84+
}
6185
```
6286

6387
### **...**

solution/0700-0799/0724.Find Pivot Index/README_EN.md

+26-2
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,37 @@ There is no index that satisfies the conditions in the problem statement.
6464
### **Python3**
6565

6666
```python
67-
67+
class Solution:
68+
def pivotIndex(self, nums: List[int]) -> int:
69+
sums = sum(nums)
70+
pre_sum = 0
71+
for i, v in enumerate(nums):
72+
if (pre_sum << 1) == sums - v:
73+
return i
74+
pre_sum += v
75+
return -1
6876
```
6977

7078
### **Java**
7179

7280
```java
73-
81+
class Solution {
82+
public int pivotIndex(int[] nums) {
83+
int sums = 0;
84+
for (int e : nums) {
85+
sums += e;
86+
}
87+
int preSum = 0;
88+
for (int i = 0; i < nums.length; ++i) {
89+
// preSum == sums - nums[i] - preSum
90+
if (preSum << 1 == sums - nums[i]) {
91+
return i;
92+
}
93+
preSum += nums[i];
94+
}
95+
return -1;
96+
}
97+
}
7498
```
7599

76100
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
class Solution {
22
public int pivotIndex(int[] nums) {
3-
int sum = Arrays.stream(nums).sum();
4-
int s = 0;
3+
int sums = 0;
4+
for (int e : nums) {
5+
sums += e;
6+
}
7+
int preSum = 0;
58
for (int i = 0; i < nums.length; ++i) {
6-
if (s << 1 == sum - nums[i]) {
9+
// preSum == sums - nums[i] - preSum
10+
if (preSum << 1 == sums - nums[i]) {
711
return i;
812
}
9-
s += nums[i];
13+
preSum += nums[i];
1014
}
1115
return -1;
1216
}
13-
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def pivotIndex(self, nums: List[int]) -> int:
3+
sums = sum(nums)
4+
pre_sum = 0
5+
for i, v in enumerate(nums):
6+
if (pre_sum << 1) == sums - v:
7+
return i
8+
pre_sum += v
9+
return -1

0 commit comments

Comments
 (0)