Dynamic programming


  • 0
    C
    class Solution {
        private static Map<String,List<Integer>> childrenListMap=new HashMap<>();
        public static void main(String[]args){
            int[] num=new int[]{4,3,2,1};
            findSubsequences(num);
        }
    
        public static List<List<Integer>> findSubsequences(int[] nums) {
            List<Integer> list=new ArrayList<>();
            for (int num : nums) {
                list.add(num);
            }
            childrenListMap.put(list.toString(),list);
            getChildrenList(list);
            List<List<Integer>> resultList=new ArrayList<>();
            for (Map.Entry<String, List<Integer>> stringListEntry : childrenListMap.entrySet()) {
                resultList.add(stringListEntry.getValue());
            }
            return resultList;
        }
        public static void getChildrenList(List<Integer> list){
            for (int i = 0; i < list.size(); i++) {
                int value=list.get(i);
                list.remove(i);
                List<Integer> childrenList=new ArrayList<>();
                if (list.size()>=2) {
                    childrenList.addAll(list);
                    if(!childrenListMap.containsKey(childrenList.toString())){
                        childrenListMap.put(childrenList.toString(),childrenList);
                    }
                }
                if (childrenList.size()>0) {
                    getChildrenList(list);
                }
                list.add(i,value);
            }
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.