78 Subsets(求子集Medium)
题目意思:求解一个数组的所有子集,子集内的元素增序排列
eg:[1,3,2] result:[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]
思路:这是一个递推的过程 [] []+[1] [2]+[1,2]+[]+[1]
第k项的子集为第k个数分别加到k-1项的子集,再加上k-1项的子集
程序过程:
-------------------
ans[0] []
-------------------
ans[1] [1]
-------------------
ans[2] [2]
ans[3] [1,2]
-------------------
ans[4] [3]
ans[5] [1,3]
ans[6] [2,3]
ans[7] [1,2,3]
-------------------
时间复杂度:
1+1+2+4+.....
为2的n次方级别
运行时间:
12ms 1 class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int> > ans;
vector<int> empty;
ans.push_back(empty); //首先给ans[0]为空
sort(nums.begin(),nums.end());
for(int i=;i<nums.size();++i){
int size=ans.size(); //不能在循环中赋值,因为ans在循环中长度增加
for(int j=;j<size;++j){
vector<int> temp;
temp=ans[j];
temp.push_back(nums[i]);
ans.push_back(temp);
}
}
return ans;
}
};
78 Subsets(求子集Medium)的更多相关文章
- 78. Subsets 求所有子集(有重复就continue)
[抄题]: Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The ...
- [leetcode]78. Subsets数组子集
Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...
- [Leetcode 78]求子集 Subset
[题目] Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The ...
- 刷题78. Subsets
一.题目说明 题目78. Subsets,给一列整数,求所有可能的子集.题目难度是Medium! 二.我的解答 这个题目,前面做过一个类似的,相当于求闭包: 刷题22. Generate Parent ...
- LeetCode(78):子集
Medium! 题目描述: 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3] 输出: [ [3 ...
- [LeetCode] 78. Subsets 子集合
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- SCU 4424(求子集排列数)
A - A Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Practice ...
- 基于visual Studio2013解决面试题之1309求子集
题目
- 78. Subsets(M) & 90. Subsets II(M) & 131. Palindrome Partitioning
78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution ...
随机推荐
- 编写一个单独的Web Service for Delphi7(步骤)
1新建一个SOAP Server Application,在提示输入接口时输入MyHello,把所有文件保存在一个叫Ser的目录下,其中一个包含TWebModule1的文件保存为main.pas.在M ...
- Course Schedule ——LeetCode
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- osg
智能指针使用: osg::Geode* geode=new osg::Geode;//新建Geode指针 osg::ref_ptr<osg::Geode>geodePtr=geode;// ...
- 通过web修改svn用户密码
使用方法: 将文件changePasswd.cgi和changePasswd.ini 放到apche安装目录下的cgi-bin下(cgi-bin的目录可以通过/etc/httpd/conf/httpd ...
- 一步一步学android之事件篇——单击事件
在使用软件的时候单击事件必不可少,比如我想确定.取消等都需要用户的单击,所有的单击事件都是由View.OnClickListener接口来进行处理的,接口定义如下: public static int ...
- Java调用R(二)_JRI
推荐使用.相比RServe更灵活,效率更高. 基本步骤 1. R中需要安装rJava包. 2. 系统变量Path加上 C:\Program Files\R\R-3.0.1\bin\i386;C:\ ...
- Java 二分查找
public int binarySearch(int[] nums, int target) { int low = 0; int high = nums.length; while (low &l ...
- Demo_玩家移动(主要注意动画的设置)
using UnityEngine; using System.Collections; public class NewPlayerMove : MonoBehaviour { private fl ...
- C++ 求阶乘 四种方法
来总结下求阶乘的各种方法哈. 写在最前:①各个代码仅仅是提供了求阶乘的思路,以便在实际须要时再来编码,代码并不健壮!②各个程序都在1到10内測试正确. 代码一: #include<iostrea ...
- 2015湖南省选集训DAY5——work(BZOJ4177)
Description Mike有一个农场,这个农场n个牲畜围栏,如今他想在每一个牲畜围栏中养一仅仅动物,每仅仅动物能够是牛或羊,并且每一个牲畜围栏中的饲养条件都不同,当中第i个牲畜围栏中的动物长大后 ...