o******0 发帖数: 105 | 1 1.Give a set of prime numbers, output a set of all possible products.
2.add two unsigned numbers without using arithmetic operations
3.hamming distance sum for all pairs in an array | e***i 发帖数: 231 | 2 抛个砖
1. 可以用递归或者循环
#!/bin/python
def _rec_all_prod(primes):
if len(primes) == 1:
return primes
sub_prod = _rec_all_prod(primes[1:])
new_prod = sub_prod[:]
new_prod.append(primes[0])
for prod in sub_prod:
new_prod.append(prod*primes[0])
return new_prod
def get_prod_one(primes):
return set(_rec_all_prod(primes))
def _loop_all_prod(primes):
new_prod = []
for x in xrange(2**len(primes)):
mask = "{0:b}".format(x)[::-1]
total = 1
for b, v in zip(mask, primes):
if (b == '1'):
total *= v
if (total != 1):
new_prod.append(total)
return new_prod
def get_prod_two(primes):
return set(_loop_all_prod(primes))
if __name__ == "__main__":
primes1 = [2,3,5]
primes2 = [2,3,5,7,11]
print get_prod_one(primes1)
print get_prod_two(primes1)
print get_prod_one(primes2)
print get_prod_two(primes2) | k******a 发帖数: 44 | 3 第二块砖
1. Lintcode的subset问题?
2. 按位(xor,然后进位)
3. 汉明距离的和。
感觉应该1列1列地看每列不同字符的个数,然后根据不同字符个数和总的字符串个数做
一个数学运算。gut feeling。 | r*******g 发帖数: 1335 | | f*******r 发帖数: 976 | 5 mark
【在 o******0 的大作中提到】 : 1.Give a set of prime numbers, output a set of all possible products. : 2.add two unsigned numbers without using arithmetic operations : 3.hamming distance sum for all pairs in an array
|
|