78 subset(product)

2017-10-23  本文已影响0人  Fei_JOB
import java.io.*;
import java.util.*;

class myCode
{
     public List<Integer> subset( int[] nums ){
         List<Integer> ans = new ArrayList<Integer>();
         helper(nums, 0, ans, 1);
         return ans;
     }
    
    public void helper(int[] nums, int idx, List<Integer> ans, int product){
        if(idx == nums.length) {
            return;
        }
        ans.add(nums[idx]*product);
        
        helper(nums, idx+1, ans, product);
      
        helper(nums, idx+1, ans, nums[idx]*product);
        return;
    }
  
    public static void main (String[] args) throws java.lang.Exception
    {
        myCode test = new myCode();
        int[] nums = {2,3, 4};
        List<Integer> ans = test.subset(nums);
        for(int n : ans){
            System.out.println(n );
        }
        
    }
}
`
上一篇下一篇

猜你喜欢

热点阅读