Exam 31. LeetCode OJ (18)

Source: Internet
Author: User

Exam 31. LeetCode OJ (18)

If it is easy to add two numbers, it is painful to add three numbers. If we want to add four numbers, then despair.

Yes, we were a little desperate when we got this question, but since the people who developed this question are so hard for us, we can't give up and don't let others down, so I bit my teeth and continue.

After experiencing the storm, we also learned to grow. This question is based on the previous question. The question requirements are strict. The four numbers must be equal to the target value, and the four numbers are not repeated and in ascending order.

Therefore, we have to test, that is, traverse, one by one. This should be a loop nested traversal, because we cannot let go of every situation, because it may be included in our requirements. The Code is as follows:

 

Class Solution {public: vector
 
  
> FourSum (vector
  
   
& Nums, int target) {vector
   
    
> Last; int len = nums. size (); if (len <4) {return last;} // 1. sort (nums. begin (), nums. end (); // traversal // special processing. If the sum of the minimum four numbers is greater than the target or the sum of the maximum four numbers is greater than the target, then we exit if (nums [len-1] + nums [len-2] + nums [len-3] + nums [len-4] <target) {return last;} if (nums [0] + nums [1] + nums [2] + nums [3]> target) {return last;} // looping, it cannot be repeated, so we have to try it one by one. Because of the sorting, the complexity of the situation reduces a lot of for (int I = 0; I <len-3; ++ I) {// Layer 1 while (I> 0 & nums [I] = nums [I-1]) {// process repeated numbers, however, it cannot be processed when the first traversal arrives. Otherwise, some solutions ++ I;} for (int j = I + 1; j may be missed.
    
     
I + 1 & nums [j] = nums [j-1]) {// process repeated numbers, but cannot be processed at the first traversal, otherwise, you may miss some solutions + + j;} int pos1 = j + 1; while (pos1 <len-1) {// Layer 3 while (pos1> j + 1 & nums [pos1] = nums [pos1-1]) {// process repeated numbers, however, it cannot be processed when the first traversal is reached. Otherwise, some solutions ++ pos1 may be missed;} int pos2 = pos1 + 1; while (pos2 <len & nums [I] + nums [j] + nums [pos1] + nums [pos2] <target) {++ pos2 ;} if (pos2 = len) {++ pos1; continue;} if (nums [I] + nums [j] + nums [pos1] + nums [pos2] = target) {vector
     
      
Tmp; tmp. push_back (nums [I]); tmp. push_back (nums [j]); tmp. push_back (nums [pos1]); tmp. push_back (nums [pos2]); last. push_back (tmp); ++ pos1; continue;} if (nums [I] + nums [j] + nums [pos1] + nums [pos2]> target) {// ++ pos1; continue ;}}} return last ;}};
     
    
   
  
 
I have summarized the practices of this type of questions. Don't be intimidated by the questions. You have to believe that there are many people in the past who have done the same thing, so we can do the same. My code is correct, the test result is as follows:

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.