JavaScript-given a value, such as 100, given an array, select n elements from the array, and the n elements add up to 100, to get a result.

Source: Internet
Author: User
Array:

var arr = [99.1, 92.2, 60, 50,           49.5, 45.7, 25.1, 20,            17.4, 13, 10, 7, 2.1, 2, 1];

The array elements that are found and are 100:

[60,20,10,7,2,1]

Reply content:

Array:

var arr = [99.1, 92.2, 60, 50,           49.5, 45.7, 25.1, 20,            17.4, 13, 10, 7, 2.1, 2, 1];

The array elements that are found and are 100:

[60,20,10,7,2,1]

function f($n, $arr) { //$n是目标数字,$arr是数字组成的数组    if (empty($arr)) return false; //如果数组没有元素,返回    if (in_array($n, $arr)) return [$n];//如果期望的值已经存在,直接返回这个值    foreach($arr as $k => $v) { //遍历数组        if ($v > $n) continue; //比指定数还大,过        $copy = $arr; //复制数组        unset($copy[$k]); //去掉复制数组中已经被选中的数字        $next = f($n - $v, $copy); //递归计算        if(! empty($next)) return array_merge([$v], $next); //合并结果集    }    

To the Python version:

def subsetsum(elements, target):    if target==0:        return True, []    elif not elements or target < 0:        return False, None    result, subset = subsetsum(elements[:-1], target-elements[-1])    return (True, subset + [elements[-1]]) if result else subsetsum(elements[:-1], target)

The idea is simple, and when I ask elements if it can be added target , there are only two possibilities:

    1. I'm going to use it to add-- element[-1] target I'm going to be able to use elements[:-1] the add-out target-elements[-1] .

    2. I don't need to use element[-1] it to add-I'm target going to be able to use elements[:-1] the add-out target .

Boundary condition is:

    1. target 0 That's when I'm not going to be able to add anything on my behalf, soreturn True, []

    2. When it elements 's empty or target negative, the rep never comes out, soreturn False, None

Tests :

elements = [99.1, 92.2, 60, 50, 49.5, 45.7, 25.1, 20, 17.4, 13, 10, 7, 2.1, 2, 1]target = 100result, subset = subsetsum(elements, target)print(result, subset)

Results :

True [60, 20, 10, 7, 2, 1]

The problem is that it is more interesting to see the problem, if you have to think about the speed of the solution, and so on.

Having done this research, I present a variation of the problem that you can consider to look at:

We are going to set an integer (which represents the negative) for the Multiset (Multiset is a set, but allow elements to be repeated), called, in order to set elements another integral multi-set called targets , to ask whether there are several sub-sets, the elements of each child multiset and exactly one in the .

The definition does not understand is not bad, I take an example:

elements = (1,4,6,4,1)targets = (5,10,1)

There are solutions to this example:

(1,4) -> 5(4,6) -> 10(1) -> 1

Note that each element in the in elements can only be used once!

Combinatorial issues. Take a look at Leetcode's original question:

function t100(array){  var tempArray = array.concat([]).sort(function(a,b){    return a-b;  });  var temp = 0;  var result = [];  for(var i=0;i
  
   
    
   100){      tempArray.length = i;    }  }  for(var i=0;i
   
    < code="">
   
  

递归内套一个循环

  • 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.