力扣90. 子集 II
1、C
/** * Return an array of arrays of size *returnSize. * The sizes of the arrays are returned as *returnColumnSizes array. * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free(). */ void backtracking(int *nums,int numsSize,int start,int *returnSize,int **returnColumnSizes,int *path,int *pathSize,int *visited,int **result){ result[*returnSize] = (int*)malloc(sizeof(int)*numsSize); memcpy(result[*returnSize],path,sizeof(int)*numsSize); (*returnColumnSizes)[*returnSize] = *pathSize; (*returnSize)++; for(int i=start;i<numsSize;i++){ if(i>0&&nums[i-1]==nums[i]&&visited[i-1]==0){ continue; } path[*pathSize] = nums[i]; visited[i] = 1; (*pathSize)++; backtracking(nums,numsSize,i+1,returnSize,returnColumnSizes,path,pathSize,visited,result); (*pathSize)--; visited[i] = 0; } } void QuickSort1(int* a, int left, int right) { if (left >= right) { return; } int begin = left, end = right; int pivot = begin; int key = a[begin]; while (begin < end) { //右边找小的,如果不是小于key,继续 while (begin < end && a[end] >= key) { end--; } //找到比key小的,把它放在坑里,换新坑 a[pivot] = a[end]; pivot = end; //左边找大的,如果不是大于key,继续 while (begin < end && a[begin] <= key) { begin++; } //找到比key大的,把它放在坑里,换新坑 a[pivot] = a[begin]; pivot = begin; } a[pivot] = key;//bengin 与 end 相遇,相遇的位置一定是一个坑 QuickSort1(a, left, pivot - 1); QuickSort1(a, pivot + 1, right); } int** subsetsWithDup(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){ QuickSort1(nums,0,numsSize-1); *returnSize = 0; int *path = (int *)malloc(sizeof(int)*numsSize); int **result = (int**)malloc(sizeof(int*)*10001); *returnColumnSizes = (int*)malloc(sizeof(int)*10001); int *pathSize = (int*)calloc(1,sizeof(int)); int *visited = (int*)calloc(numsSize,sizeof(int)); backtracking(nums,numsSize,0,returnSize,returnColumnSizes,path,pathSize,visited,result); return result; }
2、C++
class Solution { public: vector<int> path; vector<vector<int>> result; void backtracking(vector<int>& nums,int start,vector<bool> &visited){ result.push_back(path); for(int i=start;i<nums.size();i++){ if(i>0&&nums[i-1]==nums[i]&&visited[i-1]==false){ continue; } path.push_back(nums[i]); visited[i] = true; backtracking(nums,i+1,visited); path.pop_back(); visited[i] = false; } } vector<vector<int>> subsetsWithDup(vector<int>& nums) { vector<bool> visited = {false}; sort(nums.begin(),nums.end()); backtracking(nums,0,visited); return result; } };
3、JAVA
class Solution { LinkedList<Integer> path = new LinkedList<Integer>(); List<List<Integer>> result = new ArrayList<>(); void backtracking(int[] nums,int start,boolean[] visited){ result.add(new ArrayList<>(path)); for(int i=start;i<nums.length;i++){ if(i>0 && nums[i-1]==nums[i] && visited[i-1] == false){ continue; } path.addLast(nums[i]); visited[i] = true; backtracking(nums,i+1,visited); path.removeLast(); visited[i] = false; } return; } public List<List<Integer>> subsetsWithDup(int[] nums) { boolean[] visited = new boolean[nums.length]; Arrays.sort(nums); Arrays.fill(visited,false); backtracking(nums,0,visited); return result; } }
4、Python
class Solution(object): def __init__(self): self.path = [] self.result = [] def backtracing(self,nums,start,visited): self.result.append(self.path[:]) for i in range(start,len(nums)): if i>0 and nums[i-1]==nums[i] and visited[i-1]==False: continue self.path.append(nums[i]) visited[i] = True self.backtracing(nums,i+1,visited) self.path.remove(nums[i]) visited[i] = False; def subsetsWithDup(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ visited = [False]*len(nums) nums.sort() self.backtracing(nums,0,visited) return self.result标签:nums,int,self,II,算法,子集,result,path,visited From: https://www.cnblogs.com/cmkbk/p/17530192.html