Get the App
SLTechnology News&Howtos  ›  Servers  › 

What is the method for Java to find out the combination of numbers?

Shulou Source: shulou.com Published: 2022-05-31 17:55:38 10月03日 Update

This article mainly introduces "what is the method of Java to find out the combination of numbers". In the daily operation, I believe that many people have doubts about the method of Java to find out the method of digital combination. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for everyone to answer the doubt of "Java to find out what the method of digital combination is". Next, please follow the editor to study!

Give a set of candidate numbers (C) and target numbers (T), find all the combinations in C, so that the sum of the found numbers is T. The numbers in C can be selected indefinitely. For example, the solution given by the candidate array [2Magne 3je 6je 7] and the target number 7 is: [7] and [2je 2je 3] give an array and find a set of numbers from which the sum is equal to target. Arrays are unordered, but they are all positive integers. Unlike the 40 questions I and II, there are no repeated numbers in the I array, but a number can be used multiple times; there are repeats in the II array, and a number can only be used once. Both I and II require that there are no duplicate solutions in the results, and that the numbers in each solution are arranged in a non-decreasing order. Package com.lifeibigdata.algorithms.leetcode;import java.util.ArrayList;import java.util.Arrays;import java.util.List;/** * Created by lifei on 16-7-4. * / public class CombinationSum {public static void main (String [] args) {int [] can = new int [] {5 CombinationSum cs = new CombinationSum (); cs.combinationSum (can,6) For (List list:ans) {for (int i:list) {System.out.print (I + ",");} System.out.println ();}} / / static List result;// List solu;// public List combinationSum (int [] candidates, int target) {/ / result = new ArrayList () / / solu = new ArrayList (); / / Arrays.sort (candidates); / / getCombination (candidates, target, 0,0); / / return result;//} / / public void getCombination (int [] candidates, int target, int sum, int level) {/ / if (sum > target) return;// if (sum==target) {/ / result.add (new ArrayList (solu); / / return / /} / / for (int itemized leveling

Tags: Numbers methods combinations arrays learning number more goals help different practical next from global variables sought integers articles unlimited conditions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MySQL NVidia Xiaomi Redmi