mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-19 20:28:43 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -255,6 +255,7 @@ public:
|
||||
|
||||
|
||||
## Java
|
||||
**使用标记数组**
|
||||
```Java
|
||||
class Solution {
|
||||
List<List<Integer>> lists = new ArrayList<>();
|
||||
@ -292,6 +293,44 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
**不使用标记数组**
|
||||
```Java
|
||||
class Solution {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
LinkedList<Integer> path = new LinkedList<>();
|
||||
int sum = 0;
|
||||
|
||||
public List<List<Integer>> combinationSum2( int[] candidates, int target ) {
|
||||
//为了将重复的数字都放到一起,所以先进行排序
|
||||
Arrays.sort( candidates );
|
||||
backTracking( candidates, target, 0 );
|
||||
return res;
|
||||
}
|
||||
|
||||
private void backTracking( int[] candidates, int target, int start ) {
|
||||
if ( sum == target ) {
|
||||
res.add( new ArrayList<>( path ) );
|
||||
return;
|
||||
}
|
||||
for ( int i = start; i < candidates.length && sum + candidates[i] <= target; i++ ) {
|
||||
//正确剔除重复解的办法
|
||||
//跳过同一树层使用过的元素
|
||||
if ( i > start && candidates[i] == candidates[i - 1] ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
sum += candidates[i];
|
||||
path.add( candidates[i] );
|
||||
// i+1 代表当前组内元素只选取一次
|
||||
backTracking( candidates, target, i + 1 );
|
||||
|
||||
int temp = path.getLast();
|
||||
sum -= temp;
|
||||
path.removeLast();
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Python
|
||||
**回溯+巧妙去重(省去使用used**
|
||||
@ -384,6 +423,7 @@ class Solution:
|
||||
## Go
|
||||
主要在于如何在回溯中去重
|
||||
|
||||
**使用used数组**
|
||||
```go
|
||||
func combinationSum2(candidates []int, target int) [][]int {
|
||||
var trcak []int
|
||||
@ -423,7 +463,41 @@ func backtracking(startIndex,sum,target int,candidates,trcak []int,res *[][]int,
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**不使用used数组**
|
||||
```go
|
||||
func combinationSum2(candidates []int, target int) [][]int {
|
||||
var trcak []int
|
||||
var res [][]int
|
||||
sort.Ints(candidates)
|
||||
backtracking(0,0,target,candidates,trcak,&res)
|
||||
return res
|
||||
}
|
||||
func backtracking(startIndex,sum,target int,candidates,trcak []int,res *[][]int){
|
||||
//终止条件
|
||||
if sum==target{
|
||||
tmp:=make([]int,len(trcak))
|
||||
//拷贝
|
||||
copy(tmp,trcak)
|
||||
//放入结果集
|
||||
*res=append(*res,tmp)
|
||||
return
|
||||
}
|
||||
//回溯
|
||||
for i:=startIndex;i<len(candidates) && sum+candidates[i]<=target;i++{
|
||||
// 若当前树层有使用过相同的元素,则跳过
|
||||
if i>startIndex&&candidates[i]==candidates[i-1]{
|
||||
continue
|
||||
}
|
||||
//更新路径集合和sum
|
||||
trcak=append(trcak,candidates[i])
|
||||
sum+=candidates[i]
|
||||
backtracking(i+1,sum,target,candidates,trcak,res)
|
||||
//回溯
|
||||
trcak=trcak[:len(trcak)-1]
|
||||
sum-=candidates[i]
|
||||
}
|
||||
}
|
||||
```
|
||||
## javaScript
|
||||
|
||||
```js
|
||||
|
@ -166,7 +166,7 @@ if (i > startIndex && nums[i] == nums[i - 1] ) {
|
||||
|
||||
|
||||
### Java
|
||||
|
||||
使用used数组
|
||||
```java
|
||||
class Solution {
|
||||
List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合
|
||||
@ -202,6 +202,37 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
不使用used数组
|
||||
```java
|
||||
class Solution {
|
||||
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
LinkedList<Integer> path = new LinkedList<>();
|
||||
|
||||
public List<List<Integer>> subsetsWithDup( int[] nums ) {
|
||||
Arrays.sort( nums );
|
||||
subsetsWithDupHelper( nums, 0 );
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
private void subsetsWithDupHelper( int[] nums, int start ) {
|
||||
res.add( new ArrayList<>( path ) );
|
||||
|
||||
for ( int i = start; i < nums.length; i++ ) {
|
||||
// 跳过当前树层使用过的、相同的元素
|
||||
if ( i > start && nums[i - 1] == nums[i] ) {
|
||||
continue;
|
||||
}
|
||||
path.add( nums[i] );
|
||||
subsetsWithDupHelper( nums, i + 1 );
|
||||
path.removeLast();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
### Python
|
||||
```python
|
||||
class Solution:
|
||||
|
@ -80,7 +80,7 @@ return treeNum;
|
||||
class Solution {
|
||||
private:
|
||||
int getNodesNum(TreeNode* cur) {
|
||||
if (cur == 0) return 0;
|
||||
if (cur == NULL) return 0;
|
||||
int leftNum = getNodesNum(cur->left); // 左
|
||||
int rightNum = getNodesNum(cur->right); // 右
|
||||
int treeNum = leftNum + rightNum + 1; // 中
|
||||
|
@ -284,7 +284,6 @@ func search(nums []int, target int) int {
|
||||
* @param {number} target
|
||||
* @return {number}
|
||||
*/
|
||||
/**
|
||||
var search = function(nums, target) {
|
||||
let left = 0, right = nums.length - 1;
|
||||
// 使用左闭右闭区间
|
||||
@ -326,6 +325,46 @@ var search = function(nums, target) {
|
||||
};
|
||||
```
|
||||
|
||||
**TypeScript**
|
||||
|
||||
(版本一)左闭右闭区间
|
||||
|
||||
```typescript
|
||||
function search(nums: number[], target: number): number {
|
||||
let left: number = 0, right: number = nums.length - 1;
|
||||
while (left <= right) {
|
||||
let mid: number = left + Math.floor((right - left) / 2);
|
||||
if (nums[mid] > target) {
|
||||
right = mid - 1;
|
||||
} else if (nums[mid] < target) {
|
||||
left = mid + 1;
|
||||
} else {
|
||||
return mid;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
```
|
||||
|
||||
(版本二)左闭右开区间
|
||||
|
||||
```typescript
|
||||
function search(nums: number[], target: number): number {
|
||||
let left: number = 0, right: number = nums.length;
|
||||
while (left < right) {
|
||||
let mid: number = left + Math.floor((right - left) / 2);
|
||||
if (nums[mid] > target) {
|
||||
right = mid;
|
||||
} else if (nums[mid] < target) {
|
||||
left = mid + 1;
|
||||
} else {
|
||||
return mid;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
```
|
||||
|
||||
**Ruby:**
|
||||
|
||||
```ruby
|
||||
|
Reference in New Issue
Block a user