20180506
1.Keyboard Row(500)
for与if混合使用
return [word for word in words if len(set([i for i,row in enumerate(['qwertyuiop','asdfghjkl','zxcvbnm'] for c in word.lower() if c in row)])) == 1]
2.Fizz Buzz(412)
replace()
替换字符用,r = 'a a a a b c d' r.replace(a,b,3) 输出:r = 'b b b a b c d'
map()
return [x for x in map(lambda i:'Fizz' * (not i % 3) + 'Buzz' * (not i % 5), range(1,n+1))]
20180509
3.Island Perimeter(463)
矩阵长和宽
x1 = [x][y] 长:x = len(x1) 宽:y = len(x1[0])
4.Toeplitz Matrix(766)
取列数
matrix[x][:-1] 到最后一列,但是不取,matrix[x][1:],取第一列以后,第一列不取。
5.Reshape the Matrix
切片
一个2*3矩阵变成一行:items = [y for x in nums for y in x] 然后再对items切片。
iter()
迭代器,不会一次性将元素加入内存,而是需要的时候才返回结果。
生成器是一个特殊的迭代器,返回值用yeild产生,而不是return
20180512
6.Next Greater Element I(496)
字典中的get()方法:
dict.get(key,default=None),得到相应的键值。
20180513
7.Goat Latin(824)
join()方法
将‘ ’加入列表' 'a','b','c'' 'a b c'
8.Single Number(136)
按位异或
可以通过按位异或^判断一个数组中只有单个的数字。