http://www.1point3acres.com/bbs/thread-205789-1-1.html
给一个list的digit,给一个maximum number,要求返回所有由这些digit组成的且不超过max的数字,一个digit可以重复使用比如{3,7,8}, max=1000, 返回{3,33,333,37,38...7,738,...}
突然想到我明明问了她0不能放在第一位的,结果写的时候给忘了...orz
然后写完跑了几个test case,并且跑完她问我有没有可以优化的,我说可以对list排序一下,这样遇到大于max的可以直接返回,不用考虑后序结果
写的时候也忘记考虑有重复数字了。。。orz.鐣欏璁哄潧-涓€浜�-涓夊垎鍦�
最后又问了复杂度,一开始答O(n^2),后来觉得不对,改成了O(n!)。【每题必问复杂度。。。
public List<String> getNumbers(List<int> digits, int maxNumber) {
LinkedList<int> result = new LinkedList();. Waral 鍗氬鏈夋洿澶氭枃绔�,
recursiveGetNumbers(digits, 0, maxNumber, 0, result);
return result;
}
public void recursiveGetNumbers(List<int> digits, int level, int maxNumber, int curResult, List<int> result) {.1point3acres缃�
if (level == digits.length) {
if (curResult <= maxNumber) {
result.add(curResult);
}
return;
}
recursiveGetNumbers(digits, level+1, maxNumber, curResult, result);
curResult = curResult * 10 + digits[level];
recursiveGetNumbers(digits, level+1, maxNumber, curResult, result);. 鍥磋鎴戜滑@1point 3 acres
}
X. BFS
给一个list的digit,给一个maximum number,要求返回所有由这些digit组成的且不超过max的数字,一个digit可以重复使用比如{3,7,8}, max=1000, 返回{3,33,333,37,38...7,738,...}
突然想到我明明问了她0不能放在第一位的,结果写的时候给忘了...orz
然后写完跑了几个test case,并且跑完她问我有没有可以优化的,我说可以对list排序一下,这样遇到大于max的可以直接返回,不用考虑后序结果
写的时候也忘记考虑有重复数字了。。。orz.鐣欏璁哄潧-涓€浜�-涓夊垎鍦�
最后又问了复杂度,一开始答O(n^2),后来觉得不对,改成了O(n!)。【每题必问复杂度。。。
- void helper(vector<int>& digits, int tmp, int maxNumber, vector<int> &res) {
- if (tmp > maxNumber) {
- return;
- }
- if (tmp != 0) {.鐣欏璁哄潧-涓€浜�-涓夊垎鍦�
- res.push_back(tmp);. Waral 鍗氬鏈夋洿澶氭枃绔�,
- }
- for (int i = 0; i < digits.size(); i++) {. visit 1point3acres.com for more.
- helper(digits, tmp * 10 + digits[i], maxNumber, res);
- }
- }
- vector<int> getNumbers(vector<int>& digits, int maxNumber) {.鐣欏璁哄潧-涓€浜�-涓夊垎鍦�
- vector<int> res;
- sort(digits.begin(), digits.end());
- helper(digits,0, maxNumber, res);
- if (digits[0] == 0) {
- res.push_back(0);
- }
- return res;
- }
public List<String> getNumbers(List<int> digits, int maxNumber) {
LinkedList<int> result = new LinkedList();. Waral 鍗氬鏈夋洿澶氭枃绔�,
recursiveGetNumbers(digits, 0, maxNumber, 0, result);
return result;
}
public void recursiveGetNumbers(List<int> digits, int level, int maxNumber, int curResult, List<int> result) {.1point3acres缃�
if (level == digits.length) {
if (curResult <= maxNumber) {
result.add(curResult);
}
return;
}
recursiveGetNumbers(digits, level+1, maxNumber, curResult, result);
curResult = curResult * 10 + digits[level];
recursiveGetNumbers(digits, level+1, maxNumber, curResult, result);. 鍥磋鎴戜滑@1point 3 acres
}
X. BFS
- public List<Integer> getNumbers(int[] digits, int maxNumber) {
- List<Integer> result = new ArrayList<Integer>();
- if(digits.length == 0) return result;
- Arrays.sort(digits);
- Queue<Integer> queue = new LinkedList<Integer>();
- queue.add(0);
- while(!queue.isEmpty()){
- int size = queue.size();
- for(int i = 0; i < size; i++){.鐣欏璁哄潧-涓€浜�-涓夊垎鍦�
- int previous = queue.poll();
- for(int j = 0; j < digits.length; j++){
- int current = previous * 10 + digits[j];
- if(current <= maxNumber && current != 0){
- queue.add(current);
- result.add(current);
- }. from: 1point3acres.com/bbs
- }
- }
- }
- if(digits[0] == 0) result.add(0, 0); // corner case when there is 0 in digits array
- return result;
- }