leetCode 78.Subsets (子集) 解题思路和方法
Given a set of distinct integers, nums, 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 nums = [1,2,3],
a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
思路:这题和上题的组合差点儿相同。仅仅是k的数字是变动的。从0-n。
所以对照上一题,也就是对K加个循环。
代码例如以下:
public class Solution {
boolean[] f;
List<List<Integer>> list;
public List<List<Integer>> subsets(int[] nums) {
list = new ArrayList<List<Integer>>();
if(nums.length == 0){//必须是有效k值
return list;
}
f = new boolean[nums.length];
for(int i = 0; i <= nums.length;i++){
count(nums,"",i,0);//调用函数
}
return list;
}
/**
* 实现对k个数字的排练组合
* @param a 数组
* @param s 排练组合得到的结果
* @param nn 排练组合的数字个数
*/
private void count(int[] a,String s,int nn,int j){
if(nn == 0){//处理结果
String[] sa = s.split(",");//切割数组
List<Integer> al = new ArrayList<Integer>();
for(int i = 0;i < sa.length; i++){
if(sa[i].length() > 0)//仅仅有当不为空的时候才加入
al.add(Integer.parseInt(sa[i]));//加入
}
Collections.sort(al);//排序,非降序
list.add(al);
return;
}
//遍历,从i=j開始。仅仅要i开头的与i大的值
for(int i = j; i < a.length; i++){
if(!f[i]){
f[i] = true;
count(a,s + "," + a[i],nn-1,i+1);//调用下一个为false的数字
f[i] = false;
}
}
}
}
leetCode 78.Subsets (子集) 解题思路和方法的更多相关文章
- [Leetcode 78]求子集 Subset
[题目] Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The ...
- [LeetCode] Subsets I (78) & II (90) 解题思路,即全组合算法
78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a ...
- leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- leetCode 103.Binary Tree Zigzag Level Order Traversal (二叉树Z字形水平序) 解题思路和方法
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [LeetCode] Longest Valid Parentheses 解题思路
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Leetcode 78题-子集
LeetCode 78 网上已经又很多解这题的博客了,在这只是我自己的解题思路和自己的代码: 先贴上原题: 我的思路: 我做题的喜欢在本子或别处做写几个示例,以此来总结规律:下图就是我从空数组到数组长 ...
- [LeetCode] 134. Gas Station 解题思路
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- [LeetCode] 16. 3Sum Closest 解题思路
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- leetcode 78. Subsets 、90. Subsets II
第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...
随机推荐
- atoi (String to Integer) leetcode
将字符串转化为数字,其注意事项有: Requirements for atoi: The function first discards as many whitespace characters a ...
- 在CNN网络中roi从原图映射到feature map中的计算方法
在使用fast rcnn以及faster rcnn做检测任务的时候,涉及到从图像的roi区域到feature map中roi的映射,然后再进行roi_pooling之类的操作.比如图像的大小是(600 ...
- CPP-基础:模板
// template.cpp : 定义控制台应用程序的入口点. #include "stdafx.h" #include <iostream> #include &l ...
- springmvc请求方法那些事
@RequestMapping 用法详解之地址映射 (2013-08-11 16:06:58) 转载▼ 标签: it 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式 ...
- [LUOGU] P2920 [USACO08NOV]时间管理Time Management
见7.3测试 #include<iostream> #include<algorithm> #include<cstdio> using namespace std ...
- selenium 浏览器基础操作(Python)
想要开始测试,首先要清楚测试什么浏览器.如何为浏览器安装驱动,前面已经聊过. 其次要清楚如何打开浏览器,这一点,在前面的代码中,也体现过,但是并未深究.下面就来聊一聊对浏览器操作的那些事儿. from ...
- Centos7 使用firewall管理防火墙
一.Centos7使用firewall的管理防火墙 1.firewalld基本使用 启动:systemctl start firewalld 关闭:systemctl stop firewalld 状 ...
- python3.x Day6 IO多路复用
IO多路复用import asyncio 这个是异步IO模块 这个还不知道怎么用 select poll epoll 都是IO多路复用 windows 仅支持select linux2.6以后 支持e ...
- 【集合遍历-Java】
遍历List集合的三种方法 1.增强for循环 for(String str : list) {//其内部实质上还是调用了迭代器遍历方式,这种循环方式还有其他限制,不建议使用. System.out. ...
- 性能优化 java 24 次阅读 · 读完需要 15 分钟 0
摘要: 技术传播的价值,不仅仅体现在通过商业化产品和开源项目来缩短我们构建应用的路径,加速业务的上线速率,也会体现在优秀程序员在工作效率提升.产品性能优化和用户体验改善等小技巧方面的分享,以提高我们的 ...