Given an array of size n, find all the possible sub set of the array of size k
2011-12-19 14:16
543 查看
Given an array of size n, find all the possible sub set of the array of size k(all the subsets must be of size k).
Q:
给一个大小为n的数组,输出其中k个数字的组合。
A:
for example:
Q:
给一个大小为n的数组,输出其中k个数字的组合。
A:
void subarray(int arr[], int t[], int n, int index, int k, int kIndex) { int i; if (n == 0) return; if (kIndex == k) { // 说明已经取到了k个数字,打印出来 for (i = 0; i < kIndex; i++) printf("%d ", t[i]); printf("\n"); return; } if (n - index < k - kIndex) return; t[kIndex] = arr[index]; subarray(arr, t, n, index + 1, k, kIndex + 1); // 已经取了当前的数字,再继续取; subarray(arr, t, n, index + 1, k, kIndex); // 因为k <= n,所以存在有无法取到的数字,该调用则是忽略前面的数字,从后面的数字开始取 }
for example:
int att1[5] = {1,2,3,4,5}; int att3[5] = {}; subarray(att1, att3, 5, 0, 4, 0);
相关文章推荐
- Data Structure Array: Given an array of of size n and a number k, find all elements that appear more than n/k times
- Given an array of size n, find the majority element. The majority element is the element that appear
- You’re given an array containing both positive and negative integers and required to find the sub-a
- given an array of integers Find the index of values that satisfy A+B = C + D
- [LinkedIn] Array of size n and an int k, find all elements that appear/occur more than n/k times
- Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ
- Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive
- Given an array of positive and negative integers find the first subarray with zero sum
- from given array of n elements find the maximum element for each consecutive sub-array of k elements.
- Given a sequence of numbers (or array).Find the maximum distance between all the same numbers.
- 数组空间Given a sequence of numbers (or array).Find the maximum distance between all the same numbers.
- Given an array of integers, return indices of the two numbers such that they add up to a specific ta
- Given an array of integers, every element appears twice except for one. Find that single one.
- Given an array of non-negative integers, you are initially positioned at the first index of the arra
- Given a set of n symbols a size k and a combination of length k of non repeating characters from the symbol set
- (MS)Design an algorithm to find the maximum subsquare such that all four borders are filled with bla
- Given an array of ages (integers) sorted lowest to highest, output the number of occurrences for eac
- CareerCup Find all the conflicting appointments from a given list of n appointments.
- [LeetCode] Find the Derangement of An Array 找数组的错排
- Given an array of integers, sort the array according to frequency of elements