@@ -63,13 +63,17 @@ tags:
6363
6464<!-- solution:start -->
6565
66- ### 方法一:双指针
66+ ### 方法一:遍历求和
6767
68- 先遍历数组 ` arr ` ,得到数组所有元素的和,记为 ` s ` 。如果 ` s ` 不能被 3 整除,那么数组 ` arr ` 不能被分成和相等的三个部分 ,直接返回 ` false ` 。
68+ 我们先求出整个数组的和,然后判断和是否能被 3 整除,如果不能 ,直接返回 $\textit{ false}$ 。
6969
70- 接下来,利用双指针 ` i ` , ` j ` 找三等分和的边界,若成功找到,返回 ` true ` ,否则返回 ` false ` 。
70+ 否则,我们记 $\textit{s}$ 表示每部分的和,用一个变量 $\textit{cnt}$ 记录当前已经找到的部分数,另一个变量 $\textit{t}$ 记录当前部分的和。初始时 $\textit{cnt} = 0$, $t = 0$ 。
7171
72- 时间复杂度 $O(n)$,空间复杂度 $O(1)$,其中 $n$ 为数组 ` arr ` 的长度。
72+ 然后我们遍历数组,对于每个元素 $x$,我们将 $t$ 加上 $x$,如果 $t$ 等于 $s$,说明找到了一部分,将 $\textit{cnt}$ 加一,然后将 $t$ 置为 0。
73+
74+ 最后判断 $\textit{cnt}$ 是否大于等于 3 即可。
75+
76+ 时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{arr}$ 的长度。空间复杂度 $O(1)$。
7377
7478<!-- tabs:start -->
7579
@@ -78,53 +82,37 @@ tags:
7882``` python
7983class Solution :
8084 def canThreePartsEqualSum (self , arr : List[int ]) -> bool :
81- s = sum (arr)
82- if s % 3 != 0 :
85+ s, mod = divmod ( sum (arr), 3 )
86+ if mod :
8387 return False
84- i, j = 0 , len (arr) - 1
85- a = b = 0
86- while i < len (arr):
87- a += arr[i]
88- if a == s // 3 :
89- break
90- i += 1
91- while ~ j:
92- b += arr[j]
93- if b == s // 3 :
94- break
95- j -= 1
96- return i < j - 1
88+ cnt = t = 0
89+ for x in arr:
90+ t += x
91+ if t == s:
92+ cnt += 1
93+ t = 0
94+ return cnt >= 3
9795```
9896
9997#### Java
10098
10199``` java
102100class Solution {
103101 public boolean canThreePartsEqualSum (int [] arr ) {
104- int s = 0 ;
105- for (int v : arr) {
106- s += v;
107- }
102+ int s = Arrays . stream(arr). sum();
108103 if (s % 3 != 0 ) {
109104 return false ;
110105 }
111- int i = 0 , j = arr. length - 1 ;
112- int a = 0 , b = 0 ;
113- while (i < arr. length) {
114- a += arr[i];
115- if (a == s / 3 ) {
116- break ;
106+ s /= 3 ;
107+ int cnt = 0 , t = 0 ;
108+ for (int x : arr) {
109+ t += x;
110+ if (t == s) {
111+ cnt++ ;
112+ t = 0 ;
117113 }
118- ++ i;
119114 }
120- while (j >= 0 ) {
121- b += arr[j];
122- if (b == s / 3 ) {
123- break ;
124- }
125- -- j;
126- }
127- return i < j - 1 ;
115+ return cnt >= 3 ;
128116 }
129117}
130118```
@@ -135,26 +123,20 @@ class Solution {
135123class Solution {
136124public:
137125 bool canThreePartsEqualSum(vector<int >& arr) {
138- int s = 0;
139- for (int v : arr) s += v;
140- if (s % 3) return false;
141- int i = 0, j = arr.size() - 1;
142- int a = 0, b = 0;
143- while (i < arr.size()) {
144- a += arr[ i] ;
145- if (a == s / 3) {
146- break;
147- }
148- ++i;
126+ int s = accumulate(arr.begin(), arr.end(), 0);
127+ if (s % 3) {
128+ return false;
149129 }
150- while (~ j) {
151- b += arr[ j] ;
152- if (b == s / 3) {
153- break;
130+ s /= 3;
131+ int cnt = 0, t = 0;
132+ for (int x : arr) {
133+ t += x;
134+ if (t == s) {
135+ t = 0;
136+ cnt++;
154137 }
155- --j;
156138 }
157- return i < j - 1 ;
139+ return cnt >= 3 ;
158140 }
159141};
160142```
@@ -164,29 +146,70 @@ public:
164146```go
165147func canThreePartsEqualSum(arr []int) bool {
166148 s := 0
167- for _, v := range arr {
168- s += v
149+ for _, x := range arr {
150+ s += x
169151 }
170152 if s%3 != 0 {
171153 return false
172154 }
173- i, j := 0, len(arr)-1
174- a, b := 0, 0
175- for i < len(arr) {
176- a += arr[i]
177- if a == s/3 {
178- break
155+ s /= 3
156+ cnt, t := 0, 0
157+ for _, x := range arr {
158+ t += x
159+ if t == s {
160+ cnt++
161+ t = 0
179162 }
180- i++
181163 }
182- for j >= 0 {
183- b += arr[j]
184- if b == s/3 {
185- break
186- }
187- j--
188- }
189- return i < j-1
164+ return cnt >= 3
165+ }
166+ ```
167+
168+ #### TypeScript
169+
170+ ``` ts
171+ function canThreePartsEqualSum(arr : number []): boolean {
172+ let s = arr .reduce ((a , b ) => a + b );
173+ if (s % 3 ) {
174+ return false ;
175+ }
176+ s = (s / 3 ) | 0 ;
177+ let [cnt, t] = [0 , 0 ];
178+ for (const x of arr ) {
179+ t += x ;
180+ if (t == s ) {
181+ cnt ++ ;
182+ t = 0 ;
183+ }
184+ }
185+ return cnt >= 3 ;
186+ }
187+ ```
188+
189+ #### Rust
190+
191+ ``` rust
192+ impl Solution {
193+ pub fn can_three_parts_equal_sum (arr : Vec <i32 >) -> bool {
194+ let sum : i32 = arr . iter (). sum ();
195+ let s = sum / 3 ;
196+ let mod_val = sum % 3 ;
197+ if mod_val != 0 {
198+ return false ;
199+ }
200+
201+ let mut cnt = 0 ;
202+ let mut t = 0 ;
203+ for & x in & arr {
204+ t += x ;
205+ if t == s {
206+ cnt += 1 ;
207+ t = 0 ;
208+ }
209+ }
210+
211+ cnt >= 3
212+ }
190213}
191214```
192215
0 commit comments