The array is divided into three equal parts and explanations

This topic is a relatively simple topic, blogger in leetcode brush above problems encountered, so this record a bit, hoping to help the students in question.

First of all I want to talk about ideas:

1. Assuming array has n elements, the summing element when this n is not 0 mod 3 indicates that the array is not necessarily equally divided by three.

2. We use all the elements and divided by 3 to get the average

3. left indicates the beginning of the array, right part indicates the end of the array, and the array represents a left portion and a left_sum, right_sum portion and a right array represents a set value indicating whether a bool left_sum and right_sum equal, and the default is false

4. When the pointer is less than the right hand left, we continue iterations, and each iteration from the left and right ends of the accumulation value and the left_sum right_sum above, until the two are equal, and equal to the average, then we set the predefined value bool It is true and exit the loop

5. After cycle we have determined whether the left and right between the at least one spatial ensure equal and the third value and left_sum right_sum, if it indicates that the array can be three equal, if not, the array can not be Trisection.

Well, the general idea of ​​the algorithm is the case, I will put the code below, it is worth noting that vector <int> v = {1,2,3,4,5} Such a statement can only be run correctly after c ++ 11 I hope you pay attention.

 1 #include <iostream>
 2 #include <numeric>
 3 #include <stdio.h>
 4 #include <vector>
 5 using namespace std;
 6 
 7 class Solution
 8 {
 9 public:
10     bool canThreePartsEqualSum(vector<int> &A)
11     {
12         int sum = accumulate(A.begin(), A.end(), 0); // 求和
13         if (sum % 3 != 0)
14             return to false ;                    // to three modulo the array can not be equal to 0 trisected 
15          int AVG = SUM / . 3 ;                   // get Mean 
16          int left = 0 , right = a.size () - . 1 ; // get around pointer 
. 17          int left_sum = 0 , right_sum = 0 ;     // initialize and left blank 
18 is          BOOL in Flag = to false ;                   // determine whether the left and right and can be calculated equal to each other 
. 19          the while (left < right)
 20 is          { //When the pointer is less than the left to the right continues to loop 
21 is              IF (! Left_sum = AVG)
 22 is              { // if the average is less than the left, right cumulative 
23 is                  left_sum + = A.at (left ++ );
 24              }
 25              IF (right_sum =! AVG)
 26 is              { // if the average is less than the right and left cumulative 
27                  right_sum A.at + = (right - );
 28              }
 29              IF (left_sum && right_sum == == AVG AVG)
 30              { // if and about equal to the average, out of the loop 
31 is                  in Flag =to true ;
 32                  BREAK ;
 33 is              }
 34 is          }
 35          return In Flag && right - left> . 1 ; // when the left and right and left intermediate pointer equal a minimum of space, the array can be represented trisection 
36      }
 37 [  };
 38 is  int main ( void ) {
 39      Solution * = S new new Solution ();
 40      Vector < int > V = { . 3 , . 3 , . 6 , . 5 , - 2 , 2 , . 5 , . 1 , -. 9 , . 4 };
 41 is      BOOL Result = S-> canThreePartsEqualSum (V);
 42 is      COUT << " the array " << (Result? " Can " : " not " ) << " is trisected " << endl;
 43 }

Well, the above is the content of the essay, the source code can be downloaded from (https://github.com/maoqifan1/leetcode.git) my github, see you next time.

Guess you like

Origin www.cnblogs.com/maoqifansBlog/p/12481864.html