File tree Expand file tree Collapse file tree 6 files changed +156
-33
lines changed
solution/1900-1999/1991.Find the Middle Index in Array Expand file tree Collapse file tree 6 files changed +156
-33
lines changed Original file line number Diff line number Diff line change 67
67
68
68
<!-- tabs:start -->
69
69
70
+ ### ** Python3**
71
+
72
+ <!-- 这里可写当前语言的特殊实现逻辑 -->
73
+
74
+ ``` python
75
+ class Solution :
76
+ def findMiddleIndex (self , nums : List[int ]) -> int :
77
+ s = sum (nums)
78
+ total = 0
79
+ for i, num in enumerate (nums):
80
+ total += num
81
+ if total - num == s - total:
82
+ return i
83
+ return - 1
84
+ ```
85
+
86
+ ### ** Java**
87
+
88
+ <!-- 这里可写当前语言的特殊实现逻辑 -->
89
+
90
+ ``` java
91
+ class Solution {
92
+ public int findMiddleIndex (int [] nums ) {
93
+ int s = 0 ;
94
+ for (int num : nums) {
95
+ s += num;
96
+ }
97
+ int total = 0 ;
98
+ for (int i = 0 ; i < nums. length; ++ i) {
99
+ total += nums[i];
100
+ if (total - nums[i] == s - total) {
101
+ return i;
102
+ }
103
+ }
104
+ return - 1 ;
105
+ }
106
+ }
107
+ ```
108
+
70
109
### ** C++**
71
110
72
111
``` cpp
@@ -75,12 +114,12 @@ public:
75
114
int findMiddleIndex(vector<int >& nums) {
76
115
int sum = 0;
77
116
int total = 0;
78
- for(int num: nums)
117
+ for (int num: nums)
79
118
sum += num;
80
119
81
- for(int i = 0; i < nums.size(); i++) {
120
+ for (int i = 0; i < nums.size(); i++) {
82
121
total += nums[i];
83
- if(total - nums[i] == sum - total)
122
+ if (total - nums[i] == sum - total)
84
123
return i;
85
124
}
86
125
@@ -89,20 +128,23 @@ public:
89
128
};
90
129
```
91
130
92
- ### ** Python3**
93
-
94
- <!-- 这里可写当前语言的特殊实现逻辑 -->
95
-
96
- ``` python
97
-
98
- ```
99
-
100
- ### ** Java**
101
-
102
- <!-- 这里可写当前语言的特殊实现逻辑 -->
103
-
104
- ``` java
105
-
131
+ ### ** Go**
132
+
133
+ ``` go
134
+ func findMiddleIndex (nums []int ) int {
135
+ s := 0
136
+ for _ , num := range nums {
137
+ s += num
138
+ }
139
+ total := 0
140
+ for i , num := range nums {
141
+ total += num
142
+ if total-num == s-total {
143
+ return i
144
+ }
145
+ }
146
+ return -1
147
+ }
106
148
```
107
149
108
150
### ** ...**
Original file line number Diff line number Diff line change @@ -65,6 +65,41 @@ The sum of the numbers after index 0 is: 0
65
65
66
66
<!-- tabs:start -->
67
67
68
+ ### ** Python3**
69
+
70
+ ``` python
71
+ class Solution :
72
+ def findMiddleIndex (self , nums : List[int ]) -> int :
73
+ s = sum (nums)
74
+ total = 0
75
+ for i, num in enumerate (nums):
76
+ total += num
77
+ if total - num == s - total:
78
+ return i
79
+ return - 1
80
+ ```
81
+
82
+ ### ** Java**
83
+
84
+ ``` java
85
+ class Solution {
86
+ public int findMiddleIndex (int [] nums ) {
87
+ int s = 0 ;
88
+ for (int num : nums) {
89
+ s += num;
90
+ }
91
+ int total = 0 ;
92
+ for (int i = 0 ; i < nums. length; ++ i) {
93
+ total += nums[i];
94
+ if (total - nums[i] == s - total) {
95
+ return i;
96
+ }
97
+ }
98
+ return - 1 ;
99
+ }
100
+ }
101
+ ```
102
+
68
103
### ** C++**
69
104
70
105
``` cpp
@@ -73,12 +108,12 @@ public:
73
108
int findMiddleIndex(vector<int >& nums) {
74
109
int sum = 0;
75
110
int total = 0;
76
- for(int num: nums)
111
+ for (int num: nums)
77
112
sum += num;
78
113
79
- for(int i = 0; i < nums.size(); i++) {
114
+ for (int i = 0; i < nums.size(); i++) {
80
115
total += nums[i];
81
- if(total - nums[i] == sum - total)
116
+ if (total - nums[i] == sum - total)
82
117
return i;
83
118
}
84
119
@@ -87,16 +122,23 @@ public:
87
122
};
88
123
```
89
124
90
- ### ** Python3**
91
-
92
- ``` python
93
-
94
- ```
95
-
96
- ### ** Java**
97
-
98
- ``` java
99
-
125
+ ### ** Go**
126
+
127
+ ``` go
128
+ func findMiddleIndex (nums []int ) int {
129
+ s := 0
130
+ for _ , num := range nums {
131
+ s += num
132
+ }
133
+ total := 0
134
+ for i , num := range nums {
135
+ total += num
136
+ if total-num == s-total {
137
+ return i
138
+ }
139
+ }
140
+ return -1
141
+ }
100
142
```
101
143
102
144
### ** ...**
Original file line number Diff line number Diff line change @@ -3,12 +3,12 @@ class Solution {
3
3
int findMiddleIndex (vector<int >& nums) {
4
4
int sum = 0 ;
5
5
int total = 0 ;
6
- for (int num: nums)
6
+ for (int num: nums)
7
7
sum += num;
8
8
9
- for (int i = 0 ; i < nums.size (); i++) {
9
+ for (int i = 0 ; i < nums.size (); i++) {
10
10
total += nums[i];
11
- if (total - nums[i] == sum - total)
11
+ if (total - nums[i] == sum - total)
12
12
return i;
13
13
}
14
14
Original file line number Diff line number Diff line change
1
+ func findMiddleIndex (nums []int ) int {
2
+ s := 0
3
+ for _ , num := range nums {
4
+ s += num
5
+ }
6
+ total := 0
7
+ for i , num := range nums {
8
+ total += num
9
+ if total - num == s - total {
10
+ return i
11
+ }
12
+ }
13
+ return - 1
14
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int findMiddleIndex (int [] nums ) {
3
+ int s = 0 ;
4
+ for (int num : nums ) {
5
+ s += num ;
6
+ }
7
+ int total = 0 ;
8
+ for (int i = 0 ; i < nums .length ; ++i ) {
9
+ total += nums [i ];
10
+ if (total - nums [i ] == s - total ) {
11
+ return i ;
12
+ }
13
+ }
14
+ return -1 ;
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def findMiddleIndex (self , nums : List [int ]) -> int :
3
+ s = sum (nums )
4
+ total = 0
5
+ for i , num in enumerate (nums ):
6
+ total += num
7
+ if total - num == s - total :
8
+ return i
9
+ return - 1
You can’t perform that action at this time.
0 commit comments