Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc/lcof2 problems #636

Merged
merged 1 commit into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,28 @@ func pivotIndex(nums []int) int {
}
```

### **C++**

```cpp
class Solution {
public:
int pivotIndex(vector<int>& nums) {
int sum = 0;
int total = 0;
for (int num: nums)
sum += num;

for (int i = 0; i < nums.size(); i++) {
total += nums[i];
if (total - nums[i] == sum - total)
return i;
}

return -1;
}
};
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
int pivotIndex(vector<int>& nums) {
int sum = 0;
int total = 0;
for (int num: nums)
sum += num;

for (int i = 0; i < nums.size(); i++) {
total += nums[i];
if (total - nums[i] == sum - total)
return i;
}

return -1;
}
};
22 changes: 22 additions & 0 deletions solution/1900-1999/1991.Find the Middle Index in Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@

<!-- tabs:start -->

### **C++**

```cpp
class Solution {
public:
int findMiddleIndex(vector<int>& nums) {
int sum = 0;
int total = 0;
for(int num: nums)
sum += num;

for(int i = 0; i < nums.size(); i++) {
total += nums[i];
if(total - nums[i] == sum - total)
return i;
}

return -1;
}
};
```

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,28 @@ The sum of the numbers after index 0 is: 0

<!-- tabs:start -->

### **C++**

```cpp
class Solution {
public:
int findMiddleIndex(vector<int>& nums) {
int sum = 0;
int total = 0;
for(int num: nums)
sum += num;

for(int i = 0; i < nums.size(); i++) {
total += nums[i];
if(total - nums[i] == sum - total)
return i;
}

return -1;
}
};
```

### **Python3**

```python
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
int findMiddleIndex(vector<int>& nums) {
int sum = 0;
int total = 0;
for(int num: nums)
sum += num;

for(int i = 0; i < nums.size(); i++) {
total += nums[i];
if(total - nums[i] == sum - total)
return i;
}

return -1;
}
};