当前位置:网站首页>Leetcode hot topic Hot 100 day 33: "subset"
Leetcode hot topic Hot 100 day 33: "subset"
2022-07-05 04:24:00 【Ultimate brocade】
Continue to brush LeetCode Hot topic HOT 100 The subject of , And update my blog solutions. stay csdn In my blog, I will try to explain clearly in words , relevant Java Code, you can go to my Personal blog jinhuaiyu.com View in .
subject : A subset of
Give you an array of integers nums , Elements in an array Different from each other . Returns all possible subsets of the array ( Power set ).
Solution set You can't Contains a subset of repetitions . You can press In any order Returns the solution set .
Example 1:
Input :nums = [1,2,3]
Output :[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Example 2:
Input :nums = [0]
Output :[[],[0]]
Tips :
1 <= nums.length <= 10
-10 <= nums[i] <= 10
nums All elements in Different from each other
solution 1: Dynamic programming
We need to find the former i The set of all subsets of digits and the first i+1 The relationship between sets of all subsets of digits . Suppose the set of the former is f(i), Add numbers to each subset nums[i+1] Form a set add, be f(i+1) Namely f(i)+add. You can try this set rule by yourself .
In limine , We need to put an empty set in the set ( An empty set is a subset of all sets ), And then from 0 To traverse the nums Array , Start calculating layer by layer f(i). In each layer, the set obtained from the previous layer is copied and added with numbers nums[i], Then put back the collection .
solution 2: Binary corresponding subset
For all subsets , All individually correspond to one nums Array every take and don't take , Assume that 1 Do not take as 0, about nums by {5,2,9}, Subset empty set corresponds to binary number 000, A subset of {5,2} Corresponding to binary number 110. Altogether 2^n A subset of , It corresponds to 2^n Binary numbers (n position ,n by nums length ). And these binary numbers are continuous , Its decimal size ranges from 0 To 2^n - 1.
We can enumerate decimal values from 0 To 2^n - 1 Binary number of (n position , Front complement 0) To get all the corresponding subsets . For one of the binary numbers , If the first i Position as 1, be nums[i] Join the current subset .
To judge a decimal number m After converting to binary, the i Whether bit is bit 1, We can use it and 2^i Find the position and &. because 2^i Binary number of , Except for i Is it 1 The others are 0, If m Of the i It's also 0, Then the two are bitwise and must be 0, Otherwise 1.
solution 3: to flash back ( Depth-first search )
Depth first search , Each path corresponds to a different subset , Depth is nums.length. There are taking and not taking on each floor nums[i] Two options , Reach the leaf node ( The first n layer ) Record the subset corresponding to the current path . A lot of backtracking problems have been done before , This problem is simpler , There are only two options on each floor , You can put it first. nums[i] Join in , Then drill down , Go back and take out this number , And search down without taking the number .
Finally, The code with detailed comments is placed in my Personal blog http://jinhuaiyu.com/leetcode-subsets/
边栏推荐
- 【虚幻引擎UE】打包报错出现!FindPin错误的解决办法
- 【虚幻引擎UE】实现UE5像素流部署仅需六步操作少走弯路!(4.26和4.27原理类似)
- [untitled]
- About the prompt loading after appscan is opened: guilogic, it keeps loading and gets stuck. My personal solution. (it may be the first solution available in the whole network at present)
- 直播预告 | 容器服务 ACK 弹性预测最佳实践
- How to solve the problem that easycvr changes the recording storage path and does not generate recording files?
- Alibaba cloud ECS uses cloudfs4oss to mount OSS
- 技术教程:如何利用EasyDSS将直播流推到七牛云?
- 可观测|时序数据降采样在Prometheus实践复盘
- Convert Boolean to integer value PHP - Convert Boolean to integer value PHP
猜你喜欢
【虚幻引擎UE】实现UE5像素流部署仅需六步操作少走弯路!(4.26和4.27原理类似)
American 5g open ran suffered another major setback, and its attempt to counter China's 5g technology has failed
Laravel8 export excel file
解密函数计算异步任务能力之「任务的状态及生命周期管理」
The development of mobile IM based on TCP still needs to keep the heartbeat alive
kubernetes集群之调度系统
函數(易錯)
美国5G Open RAN再遭重大挫败,抗衡中国5G技术的图谋已告失败
Looking back on 2021, looking forward to 2022 | a year between CSDN and me
Rome chain analysis
随机推荐
[phantom engine UE] package error appears! Solutions to findpin errors
Threejs realizes rain, snow, overcast, sunny, flame
[phantom engine UE] realize the animation production of mapping tripod deployment
Ffmepg usage guide
A solution to the problem that variables cannot change dynamically when debugging in keil5
Kwai, Tiktok, video number, battle content payment
File upload bypass summary (upload labs 21 customs clearance tutorial attached)
Number of possible stack order types of stack order with length n
Function (error prone)
MacBook installation postgresql+postgis
自动语音识别(ASR)研究综述
【虚幻引擎UE】打包报错出现!FindPin错误的解决办法
快手、抖音、视频号交战内容付费
Threejs Internet of things, 3D visualization of farm (III) model display, track controller setting, model moving along the route, model adding frame, custom style display label, click the model to obt
概率论与数理统计考试重点复习路线
C language course setting: cinema ticket selling management system
小程序中实现文章的关注功能
Moco is not suitable for target detection? MsrA proposes object level comparative learning target detection pre training method SOCO! Performance SOTA! (NeurIPS 2021)...
Web开发人员应该养成的10个编程习惯
Serpentine matrix