JS-FCC algorithm-pairwise

Source: Internet
Author: User

Tag:i++   class    Object     no    return     Confession    ace    Design    wrap   

Find your other half.

It is said that good programmers are good at object-oriented programming, but they often can't find the other half. Because you are always limiting yourself to being a programmer and not opening your mind.

It's a community time, and here you should find the other half that has the same values but complements you.

For example: You have a strong programming ability, the valuation of 11 points, if the 20 is divided into the best couple to calculate, you should find a strong design ability, the value of 9 points of the girl.

Then when you meet a design ability for 9 of girls, do not hesitate, bold to confess. Don't think the melon behind is sweeter than the front.

For example: There is an ability array [7,9,11,13,15] , calculated according to the best combined value for 20来, with only 7+13 and 9+11 of two combinations. The index of the array is 0,13 at the index of the array to 3,9 in the array, and the index of 7 on the array is 1,11 at 2.

So we say the function: pairwise([7,9,11,13,15],20) The return value should be 0+3+1+2 and, that is, 6.

My Code:

functionpairwise (arr, arg) {if(arr.length===0){    return0; }    varIdxarr=[]; Outerloop: for(vari=0;i<arr.length-1;i++){    if(typeofarr[i]== "Number"){       for(varj=i+1;j<arr.length;j++){        if(arr[i]+arr[j]===Arg)          {Idxarr.push (i);          Idxarr.push (j); Arr.splice (J,1, undefined); ContinueOuterloop; }      }    }  }  varSum=idxarr.reduce (function(A, B) {returnA +b;}); returnsum;} Pairwise ([1, 1, 1], 2);

The back of the advanced FCC algorithm is a little bit simpler, more than the previous few good long, the comments are not necessary to add

JS-FCC algorithm-pairwise

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.