代码随想录刷题第28天

代码随想录刷题第28天

递增子序列


/*

* @lc app=leetcode.cn id=491 lang=cpp

*

* [491] 递增子序列

*

* https://leetcode.cn/problems/increasing-subsequences/description/

*

* algorithms

* Medium (52.65%)

 * Likes:    549

* Dislikes: 0

 * Total Accepted:    108.9K

* Total Submissions: 207K

 * Testcase Example:  '[4,6,7,7]'

*

* 给你一个整数数组 nums ,找出并返回所有该数组中不同的递增子序列,递增子序列中 至少有两个元素 。你可以按 任意顺序 返回答案。

* 

* 数组中可能含有重复元素,如出现两个整数相等,也可以视作递增序列的一种特殊情况。

* 

* 

* 

* 示例 1:

* 

* 

* 输入:nums = [4,6,7,7]

* 输出:[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]

* 

* 

* 示例 2:

* 

* 

* 输入:nums = [4,4,3,2,1]

* 输出:[[4,4]]

* 

* 

* 

* 

* 提示:

* 

* 

* 1 <= nums.length <= 15

* -100 <= nums[i] <= 100

* 

* 

*/

 

// @lc code=start

#include<vector>

#include<algorithm>

#include<unordered_set>

using namespace std;

class Solution {

public:

 

    vector<vector<int>> result;

    vector<int> path;

 

    void backtracking(vector<int>& nums,int index){

        if (path.size() > 1)

        {

            result.push_back(path);

        }

        unordered_set<int> used;

        for (int i = index; i < nums.size(); i++)

        {

            if (path.size() != 0 && nums[i] < path.back() || used.find(nums[i])!= used.end())

            {

                continue;

 

            }

            path.push_back(nums[i]);

            used.insert(nums[i]);

            backtracking(nums,i+1);

            path.pop_back();

        }

 

    }

    vector<vector<int>> findSubsequences(vector<int>& nums) {

        backtracking(nums,0);

        return result;

    }

};

// @lc code=end

 

 

全排列


/*

* @lc app=leetcode.cn id=46 lang=cpp

*

* [46] 全排列

*

* https://leetcode.cn/problems/permutations/description/

*

* algorithms

* Medium (78.78%)

 * Likes:    2314

* Dislikes: 0

 * Total Accepted:    763.4K

* Total Submissions: 968.9K

 * Testcase Example:  '[1,2,3]'

*

* 给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。

* 

* 

* 

* 示例 1:

* 

* 

* 输入:nums = [1,2,3]

* 输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

* 

* 

* 示例 2:

* 

* 

* 输入:nums = [0,1]

* 输出:[[0,1],[1,0]]

* 

* 

* 示例 3:

* 

* 

* 输入:nums = [1]

* 输出:[[1]]

* 

* 

* 

* 

* 提示:

* 

* 

* 1 <= nums.length <= 6

* -10 <= nums[i] <= 10

* nums 中的所有整数 互不相同

* 

* 

*/

 

// @lc code=start

#include<vector>

#include<algorithm>

#include<unordered_set>

using namespace std;

class Solution {

public:

     vector<vector<int>>  result;

     vector<int> path;

 

 

    void backtracking(vector<int>& nums,vector<bool> &used){

        if (path.size() == nums.size())

        {

            result.push_back(path);

            return;

        }

        for (int i = 0; i < nums.size(); i++)

        {

            if (used[i] == true )

            {

                continue;

            }

 

            path.push_back(nums[i]);

            used[i] = true;

            backtracking(nums,used);

            used[i] = false;

            path.pop_back();

        }

 

 

 

    }

 

    vector<vector<int>> permute(vector<int>& nums) {

        //使用数组比使用哈希表占用的资源少

        vector<bool> used(nums.size(),false);

        backtracking(nums,used);

        return result;

    }

};

// @lc code=end

 

 

全排列 II


/*

* @lc app=leetcode.cn id=47 lang=cpp

*

* [47] 全排列 II

*

* https://leetcode.cn/problems/permutations-ii/description/

*

* algorithms

* Medium (65.29%)

 * Likes:    1241

* Dislikes: 0

 * Total Accepted:    400.3K

* Total Submissions: 613K

 * Testcase Example:  '[1,1,2]'

*

* 给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。

* 

* 

* 

* 示例 1:

* 

* 

* 输入:nums = [1,1,2]

* 输出:

* [[1,1,2],

* ⁠[1,2,1],

* ⁠[2,1,1]]

* 

* 

* 示例 2:

* 

* 

* 输入:nums = [1,2,3]

* 输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

* 

* 

* 

* 

* 提示:

* 

* 

* 1 <= nums.length <= 8

* -10 <= nums[i] <= 10

* 

* 

*/

 

// @lc code=start

#include<vector>

#include<algorithm>

using namespace std;

 

class Solution {

public:

    vector<vector<int>>  result;

    vector<int> path;

 

    void backtracking(vector<int>& nums,vector<bool> &used){

 

        if (path.size() == nums.size())

        {

            result.push_back(path);

            return ;

        }

 

        for (int i = 0; i < nums.size(); i++)

        {

            if (i > 0 && nums[i - 1] == nums[i] && used[i - 1] == false)

            {

                continue;

            }

 

        if (used[i] == false)

        {

           path.push_back(nums[i]);

            used[i] = true;

            backtracking(nums,used);

            path.pop_back();

            used[i] = false;

        }

 

 

        }

 

 

 

    }

 

 

    vector<vector<int>> permuteUnique(vector<int>& nums) {

 

 

        sort(nums.begin(),nums.end());

        vector<bool> used(nums.size(),false);

        backtracking(nums,used);

        return result;

 

    }

};

// @lc code=end

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值