File tree 6 files changed +317
-2
lines changed
solution/1000-1099/1013.Partition Array Into Three Parts With Equal Sum
6 files changed +317
-2
lines changed Original file line number Diff line number Diff line change 48
48
49
49
<!-- 这里可写通用的实现逻辑 -->
50
50
51
+ ** 方法一:双指针**
52
+
53
+ 先遍历数组 ` arr ` ,得到数组所有元素的和,记为 ` s ` 。如果 ` s ` 不能被 3 整除,那么数组 ` arr ` 不能被分成和相等的三个部分,直接返回 ` false ` 。
54
+
55
+ 接下来,利用双指针 ` i ` , ` j ` 找三等分和的边界,若成功找到,返回 ` true ` ,否则返回 ` false ` 。
56
+
57
+ 时间复杂度 $O(n)$,空间复杂度 $O(1)$,其中 $n$ 为数组 ` arr ` 的长度。
58
+
51
59
<!-- tabs:start -->
52
60
53
61
### ** Python3**
54
62
55
63
<!-- 这里可写当前语言的特殊实现逻辑 -->
56
64
57
65
``` python
58
-
66
+ class Solution :
67
+ def canThreePartsEqualSum (self , arr : List[int ]) -> bool :
68
+ s = sum (arr)
69
+ if s % 3 != 0 :
70
+ return False
71
+ i, j = 0 , len (arr) - 1
72
+ a = b = 0
73
+ while i < len (arr):
74
+ a += arr[i]
75
+ if a == s // 3 :
76
+ break
77
+ i += 1
78
+ while ~ j:
79
+ b += arr[j]
80
+ if b == s // 3 :
81
+ break
82
+ j -= 1
83
+ return i < j - 1
59
84
```
60
85
61
86
### ** Java**
62
87
63
88
<!-- 这里可写当前语言的特殊实现逻辑 -->
64
89
65
90
``` java
91
+ class Solution {
92
+ public boolean canThreePartsEqualSum (int [] arr ) {
93
+ int s = 0 ;
94
+ for (int v : arr) {
95
+ s += v;
96
+ }
97
+ if (s % 3 != 0 ) {
98
+ return false ;
99
+ }
100
+ int i = 0 , j = arr. length - 1 ;
101
+ int a = 0 , b = 0 ;
102
+ while (i < arr. length) {
103
+ a += arr[i];
104
+ if (a == s / 3 ) {
105
+ break ;
106
+ }
107
+ ++ i;
108
+ }
109
+ while (j >= 0 ) {
110
+ b += arr[j];
111
+ if (b == s / 3 ) {
112
+ break ;
113
+ }
114
+ -- j;
115
+ }
116
+ return i < j - 1 ;
117
+ }
118
+ }
119
+ ```
120
+
121
+ ### ** C++**
122
+
123
+ ``` cpp
124
+ class Solution {
125
+ public:
126
+ bool canThreePartsEqualSum(vector<int >& arr) {
127
+ int s = 0;
128
+ for (int v : arr) s += v;
129
+ if (s % 3) return false;
130
+ int i = 0, j = arr.size() - 1;
131
+ int a = 0, b = 0;
132
+ while (i < arr.size()) {
133
+ a += arr[ i] ;
134
+ if (a == s / 3) {
135
+ break;
136
+ }
137
+ ++i;
138
+ }
139
+ while (~ j) {
140
+ b += arr[ j] ;
141
+ if (b == s / 3) {
142
+ break;
143
+ }
144
+ --j;
145
+ }
146
+ return i < j - 1;
147
+ }
148
+ };
149
+ ```
66
150
151
+ ### **Go**
152
+
153
+ ```go
154
+ func canThreePartsEqualSum(arr []int) bool {
155
+ s := 0
156
+ for _, v := range arr {
157
+ s += v
158
+ }
159
+ if s%3 != 0 {
160
+ return false
161
+ }
162
+ i, j := 0, len(arr)-1
163
+ a, b := 0, 0
164
+ for i < len(arr) {
165
+ a += arr[i]
166
+ if a == s/3 {
167
+ break
168
+ }
169
+ i++
170
+ }
171
+ for j >= 0 {
172
+ b += arr[j]
173
+ if b == s/3 {
174
+ break
175
+ }
176
+ j--
177
+ }
178
+ return i < j-1
179
+ }
67
180
```
68
181
69
182
### ** ...**
Original file line number Diff line number Diff line change 47
47
### ** Python3**
48
48
49
49
``` python
50
-
50
+ class Solution :
51
+ def canThreePartsEqualSum (self , arr : List[int ]) -> bool :
52
+ s = sum (arr)
53
+ if s % 3 != 0 :
54
+ return False
55
+ i, j = 0 , len (arr) - 1
56
+ a = b = 0
57
+ while i < len (arr):
58
+ a += arr[i]
59
+ if a == s // 3 :
60
+ break
61
+ i += 1
62
+ while ~ j:
63
+ b += arr[j]
64
+ if b == s // 3 :
65
+ break
66
+ j -= 1
67
+ return i < j - 1
51
68
```
52
69
53
70
### ** Java**
54
71
55
72
``` java
73
+ class Solution {
74
+ public boolean canThreePartsEqualSum (int [] arr ) {
75
+ int s = 0 ;
76
+ for (int v : arr) {
77
+ s += v;
78
+ }
79
+ if (s % 3 != 0 ) {
80
+ return false ;
81
+ }
82
+ int i = 0 , j = arr. length - 1 ;
83
+ int a = 0 , b = 0 ;
84
+ while (i < arr. length) {
85
+ a += arr[i];
86
+ if (a == s / 3 ) {
87
+ break ;
88
+ }
89
+ ++ i;
90
+ }
91
+ while (j >= 0 ) {
92
+ b += arr[j];
93
+ if (b == s / 3 ) {
94
+ break ;
95
+ }
96
+ -- j;
97
+ }
98
+ return i < j - 1 ;
99
+ }
100
+ }
101
+ ```
102
+
103
+ ### ** C++**
104
+
105
+ ``` cpp
106
+ class Solution {
107
+ public:
108
+ bool canThreePartsEqualSum(vector<int >& arr) {
109
+ int s = 0;
110
+ for (int v : arr) s += v;
111
+ if (s % 3) return false;
112
+ int i = 0, j = arr.size() - 1;
113
+ int a = 0, b = 0;
114
+ while (i < arr.size()) {
115
+ a += arr[ i] ;
116
+ if (a == s / 3) {
117
+ break;
118
+ }
119
+ ++i;
120
+ }
121
+ while (~ j) {
122
+ b += arr[ j] ;
123
+ if (b == s / 3) {
124
+ break;
125
+ }
126
+ --j;
127
+ }
128
+ return i < j - 1;
129
+ }
130
+ };
131
+ ```
56
132
133
+ ### **Go**
134
+
135
+ ```go
136
+ func canThreePartsEqualSum(arr []int) bool {
137
+ s := 0
138
+ for _, v := range arr {
139
+ s += v
140
+ }
141
+ if s%3 != 0 {
142
+ return false
143
+ }
144
+ i, j := 0, len(arr)-1
145
+ a, b := 0, 0
146
+ for i < len(arr) {
147
+ a += arr[i]
148
+ if a == s/3 {
149
+ break
150
+ }
151
+ i++
152
+ }
153
+ for j >= 0 {
154
+ b += arr[j]
155
+ if b == s/3 {
156
+ break
157
+ }
158
+ j--
159
+ }
160
+ return i < j-1
161
+ }
57
162
```
58
163
59
164
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ bool canThreePartsEqualSum (vector<int >& arr) {
4
+ int s = 0 ;
5
+ for (int v : arr) s += v;
6
+ if (s % 3 ) return false ;
7
+ int i = 0 , j = arr.size () - 1 ;
8
+ int a = 0 , b = 0 ;
9
+ while (i < arr.size ()) {
10
+ a += arr[i];
11
+ if (a == s / 3 ) {
12
+ break ;
13
+ }
14
+ ++i;
15
+ }
16
+ while (~j) {
17
+ b += arr[j];
18
+ if (b == s / 3 ) {
19
+ break ;
20
+ }
21
+ --j;
22
+ }
23
+ return i < j - 1 ;
24
+ }
25
+ };
Original file line number Diff line number Diff line change
1
+ func canThreePartsEqualSum (arr []int ) bool {
2
+ s := 0
3
+ for _ , v := range arr {
4
+ s += v
5
+ }
6
+ if s % 3 != 0 {
7
+ return false
8
+ }
9
+ i , j := 0 , len (arr )- 1
10
+ a , b := 0 , 0
11
+ for i < len (arr ) {
12
+ a += arr [i ]
13
+ if a == s / 3 {
14
+ break
15
+ }
16
+ i ++
17
+ }
18
+ for j >= 0 {
19
+ b += arr [j ]
20
+ if b == s / 3 {
21
+ break
22
+ }
23
+ j --
24
+ }
25
+ return i < j - 1
26
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean canThreePartsEqualSum (int [] arr ) {
3
+ int s = 0 ;
4
+ for (int v : arr ) {
5
+ s += v ;
6
+ }
7
+ if (s % 3 != 0 ) {
8
+ return false ;
9
+ }
10
+ int i = 0 , j = arr .length - 1 ;
11
+ int a = 0 , b = 0 ;
12
+ while (i < arr .length ) {
13
+ a += arr [i ];
14
+ if (a == s / 3 ) {
15
+ break ;
16
+ }
17
+ ++i ;
18
+ }
19
+ while (j >= 0 ) {
20
+ b += arr [j ];
21
+ if (b == s / 3 ) {
22
+ break ;
23
+ }
24
+ --j ;
25
+ }
26
+ return i < j - 1 ;
27
+ }
28
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def canThreePartsEqualSum (self , arr : List [int ]) -> bool :
3
+ s = sum (arr )
4
+ if s % 3 != 0 :
5
+ return False
6
+ i , j = 0 , len (arr ) - 1
7
+ a = b = 0
8
+ while i < len (arr ):
9
+ a += arr [i ]
10
+ if a == s // 3 :
11
+ break
12
+ i += 1
13
+ while ~ j :
14
+ b += arr [j ]
15
+ if b == s // 3 :
16
+ break
17
+ j -= 1
18
+ return i < j - 1
You can’t perform that action at this time.
0 commit comments