sola
BAN USERdef generate(num):
if num < 1: return ['']
if num <= 10: return [str(chr(96 + num))]
first_two, mag = num, 1
while first_two > 99:
first_two /= 10
mag *= 10
res = []
if first_two <= 26:
for s in generate(num - ((first_two // 10) * mag * 10)):
res.append(chr(96 + first_two//10) + s)
for s in generate(num - (first_two * mag)):
res.append(chr(96 + first_two) + s)
return res
python solution
from collections import defaultdict
seen = defaultdict(int)
def powerset(arr):
if len(arr) == 0: return [[]]
val = arr[0]
sets = powerset(arr[1:])
new_sets = []
for s in sets:
count = len([x for x in s if x == val])
if count == seen[val]:
new = s[:]
new.append(val)
new_sets.append(new)
seen[val] += 1
sets.extend(new_sets)
return sets
public static int secondMinimum(int[] arr) {
int[] mins = new int[2];
mins[0] = Integer.MAX_VALUE;
mins[1] = Integer.MAX_VALUE;
for(int i = 0; i < arr.length; i++) {
if(arr[i] < mins[1]) {
if(arr[i] < mins[0]) {
mins[1] = mins[0];
mins[0] = arr[i];
} else {
mins[1] = arr[i];
}
}
}
return mins[1];
}
in order traversal where larger child comes first. take an iterative approach so that we can break early
public static V kthLargest(BinaryTree<V> root, int k) {
Stack<BinaryTree<V>> stack = new Stack<BinaryTree<V>>();
BinaryTree<V> node = root;
int i = 1;
V retval = root.key;
while(!stack.empty() || node != null) {
if(node != null) {
stack.push(node);
node = node.rightChild;
} else {
node = stack.pop();
retval = node.key;
if(i++ == k) break;
node = node.leftChild;
}
}
return retval;
}
Repalicegpopa, Apple Phone Number available 24/7 for our Customers at AMD
I am working as a U.S. marshal in Pro Property Maintenance. I want to write about I Want My ...
- sola September 30, 2013