Skip to content

Commit 43dcd0c

Browse files
authored
feat: add solutions to lc/lcof2 problems (doocs#636)
1 parent 78f17c1 commit 43dcd0c

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

lcof2/剑指 Offer II 012. 左右两边子数组的和相等/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,28 @@ func pivotIndex(nums []int) int {
122122
}
123123
```
124124

125+
### **C++**
126+
127+
```cpp
128+
class Solution {
129+
public:
130+
int pivotIndex(vector<int>& nums) {
131+
int sum = 0;
132+
int total = 0;
133+
for (int num: nums)
134+
sum += num;
135+
136+
for (int i = 0; i < nums.size(); i++) {
137+
total += nums[i];
138+
if (total - nums[i] == sum - total)
139+
return i;
140+
}
141+
142+
return -1;
143+
}
144+
};
145+
```
146+
125147
### **...**
126148

127149
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int pivotIndex(vector<int>& nums) {
4+
int sum = 0;
5+
int total = 0;
6+
for (int num: nums)
7+
sum += num;
8+
9+
for (int i = 0; i < nums.size(); i++) {
10+
total += nums[i];
11+
if (total - nums[i] == sum - total)
12+
return i;
13+
}
14+
15+
return -1;
16+
}
17+
};

solution/1900-1999/1991.Find the Middle Index in Array/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,28 @@
6767

6868
<!-- tabs:start -->
6969

70+
### **C++**
71+
72+
```cpp
73+
class Solution {
74+
public:
75+
int findMiddleIndex(vector<int>& nums) {
76+
int sum = 0;
77+
int total = 0;
78+
for(int num: nums)
79+
sum += num;
80+
81+
for(int i = 0; i < nums.size(); i++) {
82+
total += nums[i];
83+
if(total - nums[i] == sum - total)
84+
return i;
85+
}
86+
87+
return -1;
88+
}
89+
};
90+
```
91+
7092
### **Python3**
7193

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

solution/1900-1999/1991.Find the Middle Index in Array/README_EN.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@ The sum of the numbers after index 0 is: 0
6565

6666
<!-- tabs:start -->
6767

68+
### **C++**
69+
70+
```cpp
71+
class Solution {
72+
public:
73+
int findMiddleIndex(vector<int>& nums) {
74+
int sum = 0;
75+
int total = 0;
76+
for(int num: nums)
77+
sum += num;
78+
79+
for(int i = 0; i < nums.size(); i++) {
80+
total += nums[i];
81+
if(total - nums[i] == sum - total)
82+
return i;
83+
}
84+
85+
return -1;
86+
}
87+
};
88+
```
89+
6890
### **Python3**
6991

7092
```python
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int findMiddleIndex(vector<int>& nums) {
4+
int sum = 0;
5+
int total = 0;
6+
for(int num: nums)
7+
sum += num;
8+
9+
for(int i = 0; i < nums.size(); i++) {
10+
total += nums[i];
11+
if(total - nums[i] == sum - total)
12+
return i;
13+
}
14+
15+
return -1;
16+
}
17+
};

0 commit comments

Comments
 (0)