LeetCode78 subconjuntos1

Descripción del título

Ahora hay un conjunto S de enteros sin elementos repetitivos. Busque todos los subconjuntos de S.
Nota:
Los elementos en el subconjunto que proporcione deben estar ordenados en orden no creciente.
No pueden aparecer elementos duplicados en el conjunto de soluciones.
Por ejemplo:
si S = [ 1,2,3], el conjunto de soluciones debe ser:
[↵ [3], ↵ [1], ↵ [2], ↵ [1,2,3], ↵ [1,3], ↵ [2 , 3], ↵ [1,2], ↵ [] ↵]

Análisis

  • Cada elemento debe elegir unirse a la oportunidad de no unirse al conjunto.
  • Por lo tanto, puede usar el método de retroceso, cada posición puede estar vacía, puede ser un elemento de la posición.

código java

import java.util.*;
public class Solution {
    public  ArrayList<ArrayList<Integer>> subsets(int[] S) {
       
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        if(S == null || S.length == 0){
            return res;
        }
        Arrays.sort(S);
        helperSub(S,0,new ArrayList<>(),res);
        
         Collections.sort(res, new Comparator<ArrayList<Integer>>() {
          @Override
            public int compare(ArrayList<Integer> o1, ArrayList<Integer> o2) {
                if (o1.size()!=o2.size()) {
                    return o1.size()-o2.size();
                }else {
                    for (int i = 0; i < o1.size(); i++) {
                        int comp=o1.get(i)-o2.get(i);
                        if (comp!=0) return comp;
                    }
                }
                return 0;
            }
        });
        return  res;
    }
    
    private void helperSub(int[] s, int curIndex, ArrayList<Integer> cur, ArrayList<ArrayList<Integer>> res) {
        if(curIndex == s.length){
            res.add(new ArrayList<>(cur));
        }else{
            //CurIndex处的 s元素不加入集合内
            helperSub(s,curIndex+1,cur,res);
            
            //curIndex 处 s元素加入集合内
            cur.add(s[curIndex]);
            helperSub(s,curIndex+1,cur,res);
            cur.remove(cur.size()-1);
        }
    }
}

Método dos

import java.util.*;
 
public class Solution {
    ArrayList<ArrayList<Integer>> listAll = new ArrayList<>();
 
    public ArrayList<ArrayList<Integer>> subsets(int[] num) {
        if (num == null || num.length <= 0)
            return listAll;
        ArrayList<Integer> list = new ArrayList<>();
        Arrays.sort(num);
 
        Findsubset(num, 0, list);
           //排序
        Collections.sort(listAll, new Comparator<ArrayList<Integer>>() {
          @Override
            public int compare(ArrayList<Integer> o1, ArrayList<Integer> o2) {
                if (o1.size()!=o2.size()) {
                    return o1.size()-o2.size();
                }else {
                    for (int i = 0; i < o1.size(); i++) {
                        int comp=o1.get(i)-o2.get(i);
                        if (comp!=0) return comp;
                    }
                }
                return 0;
            }
        });
        return listAll;
    }
 
    public void Findsubset(int[] set, int start, ArrayList<Integer> list) {
        listAll.add(new ArrayList<>(list));
 
        for (int i = start; i < set.length; i++) {
            list.add(set[i]);
            Findsubset(set, i + 1, list);
            list.remove(list.size() - 1);
        }
    }
}
Publicado 72 artículos originales · elogiado 0 · visitas 716

Supongo que te gusta

Origin blog.csdn.net/weixin_40300702/article/details/105554443
Recomendado
Clasificación