zoukankan      html  css  js  c++  java
  • leetcode[77]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:
    void combination(vector<vector<int>> &res,vector<int> &temp, int from, int to, int k)
    {
        if (k==0)
        {
            res.push_back(temp);
            return;
        }
        else
        {
            if(from>to)return;
            else
            {
                temp.push_back(from);
                combination(res,temp,from+1,to,k-1);
                temp.pop_back();
                combination(res,temp,from+1,to,k);
            }
        }
        return;
    }
    vector<vector<int> > combine(int n, int k) 
    {
        vector<vector<int>> res;
        res.clear();
        vector<int> temp;
        temp.clear();
        combination(res,temp,1,n,k);
        return res;
    }
    };
  • 相关阅读:
    vim 去掉自动注释和自动回车
    性别回归
    表情识别
    python list按字典的key值排序
    pytorch学习率策略
    python将list元素转为数字
    php面向对象
    mysql
    mysql
    mysql
  • 原文地址:https://www.cnblogs.com/Vae1990Silence/p/4281459.html
Copyright © 2011-2022 走看看