The title is as follows: An array, cut three knives, an integer of four slices from these three incisions, and whether it is possible to have four equal parts, and if so, return true, not return false.
The main idea is to remember three tangent points for m1,m2,m3 (and is not including M1 M2 and M3), then find m1,m2, and the tail and the equal, count, using count and m1,m3 to find M2. The detail is that the first count is not necessarily right, so you need to expand count to continue looking. The specific code is as follows.
The code is implemented as follows
1 PackageP2;2 3 Public classAli {4 5 //array slices. 6 Public BooleanFun (int[] A) {7 intlen=a.length;8 if(len<=3)return false;9 inti=0;intJ=len-1;Ten intcount1=A[i]; One intCount2=A[j]; A Booleancheck; - while(i<j) { - if(count1<Count2) { thei++; -count1+=A[i]; - } - if(count1>Count2) { +j--; -count2+=A[j]; + } A Else{ ati++; -j--; -check=check (a,i,j,count1); - if(check) {//If found M2 directly returns True - returncheck; - } in Else{ -count1+=A[i]; Increase count to continue lookup if no qualifying m2 are found tocount2+=A[j]; + - } the } * $ }Panax Notoginseng return false; - } the Public BooleanCheckint[] A,intIintJintsum) { + intL =i+1; A intR=j-1; the intLeft=a[l],right =A[r]; + while(l<R) { - if(left<Right ) { $l++; $left+=A[l]; - } - if(left>Right ) { ther++; -right+=A[r];Wuyi } the Else if(left==Right ) { - if(left==sum&&r-l==2{//Note that the conditions found here are R and L equivalent to m2+1 and m2-1 2 This condition needs to be met Wu return true; - } About if(left<sum) {//If it is smaller than the sum passed in, increase and $l++;left+=A[l]; -r--;right+=A[r]; - } - Else return false; is not satisfied, it means that you cannot find the count that may need to be added to the upper level or the condition is not satisfied. A } + } the return false; - $ } the}
Array slicing algorithm