设为首页 加入收藏

TOP

leetcode - Combinations
2015-07-20 17:29:41 来源: 作者: 【 】 浏览:2
Tags:leetcode Combinations

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.

For example,
If n = 4 and k = 2, a solution is:

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

?

class Solution {
public:
    std::vector
  
    > combine(int n, int k) {
		std::vector
   
    > result; std::vector
    
      vec; dfs(n,k,1,0,result,vec); return result; } private: void dfs(int n, int k, int bgn, int end,std::vector
     
      > &result,std::vector
      
        &vec) { if(end == k) { result.push_back(vec); } for (int i = bgn; i <= n; i++) { vec.push_back(i); dfs(n,k,i+1,end+1,result,vec); vec.pop_back(); } } };
      
     
    
   
  


?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇nyoj 949哈利波特(细节题) 下一篇zoj 3827 Information Entropy(2..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Bash 脚本教程——Li (2025-12-26 07:53:35)
·实战篇!Linux shell (2025-12-26 07:53:32)
·整理了250个shell脚 (2025-12-26 07:53:29)
·HyperText Transfer (2025-12-26 07:20:48)
·半小时搞懂 HTTP、HT (2025-12-26 07:20:42)