mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
组合新增java版本未剪枝优化版本
This commit is contained in:
@ -351,7 +351,30 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
### Java:
|
### Java:
|
||||||
|
未剪枝优化
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
List<List<Integer>> result= new ArrayList<>();
|
||||||
|
LinkedList<Integer> path = new LinkedList<>();
|
||||||
|
public List<List<Integer>> combine(int n, int k) {
|
||||||
|
backtracking(n,k,1);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void backtracking(int n,int k,int startIndex){
|
||||||
|
if (path.size() == k){
|
||||||
|
result.add(new ArrayList<>(path));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (int i =startIndex;i<=n;i++){
|
||||||
|
path.add(i);
|
||||||
|
backtracking(n,k,i+1);
|
||||||
|
path.removeLast();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
剪枝优化:
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
List<List<Integer>> result = new ArrayList<>();
|
List<List<Integer>> result = new ArrayList<>();
|
||||||
|
Reference in New Issue
Block a user