LeetCode 77の組み合わせ(組成物)

トピックタグ:バックトラック

  それは解像度に戻るまで使用DFSは、再帰の確立は.... tempListサイズ= K 1.2.3.4に参加します。

  具体的にコードが表示されます。

 

Javaのソリューション: 

ランタイム:28ミリ秒、より速くより22.99パーセント 

メモリ使用率:42.6メガバイト未満が取れて6.52

完了日:2019年12月7日

キーポイント:DFS

クラスのソリューション{
    
    リスト <リスト<Integer型>> RES。
    
    公共リスト<リスト<Integer型>>コンバイン(int型 nは、int型のk){
        RES = 新しい ArrayListを<> ();
        一覧 <整数> tempList = 新しい ArrayListを<> ();
        
        DFS(tempList、N、K)。
        
        リターンのres;
    }
    
    
    プライベート ボイド DFS(一覧<整数> tempList、整数 nは、INTのK){
         場合(tempList.size()== k)は{
            res.add(新しい ArrayListを<> (tempList));
            リターン;
        }
        
        // 次の番号から開始する
        int型私はtempList.isEmpty()を=?1:tempList.get(tempList.size() - 1)+ 1 
        
        (; I <= N; I ++ ){
            tempList.add(I);
            DFS(tempList、N、K)。
            tempList.remove(tempList.size() - 1 )。
        }
    }
}

参考文献:N / A

LeetCodeタイトルリスト-  LeetCode質問一覧

トピック出典ます。https://leetcode.com/

おすすめ

転載: www.cnblogs.com/jimmycheng/p/12440849.html