Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 292

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report