程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> (ÿÈÕËã·¨)LeetCode --- Subsets(×Ó¼¯ºÏ)

(ÿÈÕËã·¨)LeetCode --- Subsets(×Ó¼¯ºÏ)

編輯:C++入門知識

(ÿÈÕËã·¨)LeetCode --- Subsets(×Ó¼¯ºÏ)


Given a set of distinct integers, S, return all possible subsets.

Note:

  • Elements in a subset must be in non-descending order.
  • The solution set must not contain duplicate subsets.

    For example,
    If S = [1,2,3], a solution is:

    [
      [3],
      [1],
      [2],
      [1,2,3],
      [1,3],
      [2,3],
      [1,2],
      []
    ]
    

    Show Tags



    ×Ó¼¯ºÏ£¬»¹ÊÇͨ¹ý»ØËݵķ½·¨À´Çó½â¡£¸ú֮ǰk¸öÔªËصÄ×Ô¼º²»Ò»ÑùµÄÊÇÕâÀïÈÎÒâ¸ö×ÓÔªËض¼¿ÉÒÔ×÷Ϊ×Ó¼¯ºÏ¡£Òò´ËÓÐ×Ó¼¯ºÏµÄʱºòÎÒÃǶ¼Ï£ÍûÄܹ»Ìí¼Óµ½solutionÖУ¬¿ÉÒÔͨ¹ý¿Õµü´úµÄ·½Ê½Ìí¼Ó¡£»¹ÓоÍÊÇÏÈÌí¼ÓÒ»¸öÔªËØ£¬À´µ÷ÓÃsubº¯Êý£¬È»ºó»ØËÝ£¬¾ÍÊǰѸÕÌí¼ÓµÄÔªËØÈ¥³ý¡£ÒòΪÔÚµ÷ÓÃsubµÄ¹ý³ÌÖл¹ÓзÖÖ§µÄ¹ý³Ì£¬ÕâÊÇ×ã¹»µÄ¡£

    \

    <†·Ÿ"http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;">class Solution { public: void sub(vector& s, int index, vector& path, vector>& solution) { if(s.size() == index) { solution.push_back(path); return; } sub(s, index + 1, path, solution); path.push_back(s[index]); sub(s, index + 1, path, solution); path.pop_back(); } vector > subsets(vector &S) { vector> solution; vector path; sort(S.begin(), S.end()); sub(S, 0, path, solution); return solution; } };

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved