oh man this is so concise the brilliant! thanks for sharing!
P
psnr
@psnr
28
Reputation
38
Posts
259
Profile views
0
Followers
0
Following
Posts made by psnr

RE: Python beats 98%

RE: Nums = nums[k:] + nums[:k]
Thanks for the advice but the case my code fails at
# [1, 2], 1 > [2, 1] 
RE: Memoization: 3150ms > 130ms > 44ms (Python)
brilliant! what are the complexity of each solution, though?

RE: Python DFS, BFS, Union Find solutions
I was thinking for bfs maybe we could add one line after
for i in xrange(n)
to break out of the loop earlierfor i in xrange(n): if not g: break ......
Otherwise your code is fantastic. Thanks for sharing!

Two simple python solution
# divideby2 class Solution(object): def isPowerOfTwo(self, n): if n <= 0: return False while n % 2 == 0: n /= 2 return n == 1 # power of 2 should only have 1 bit that is on class Solution(object): def isPowerOfTwo(self, n): return False if n <= 0 else bin(n).count('1') == 1

Nums = nums[k:] + nums[:k]
def rotate(nums, k): nums = nums[k:] + nums[:k] # [1, 2], 1 > [2, 1] # [1, 2, 3, 4, 5, 6, 7], 3 > [5, 6, 7, 1, 2, 3, 4]
Did I get something wrong? Why wouldn't this work?

RE: Python 3 lines solution
also old style but cleaner
class Solution(object): def getHint(self, secret, guess): bull, cow = 0, 0 s = {} # secret hashtable g = {} # guess hashtable for i in xrange(len(secret)): if secret[i] == guess[i]: bull += 1 else: s[secret[i]] = s.get(secret[i], 0) + 1 g[guess[i]] = g.get(guess[i], 0) + 1 for k in s: if k in g: cow += min(s[k], g[k]) return '{0}A{1}B'.format(bull, cow)

RE: Simple O(N) time, O(1), space Java solution.
what if the linked list has odd number of elements? won't that make us 'lose' the last element if we use
while even and even.next? 
RE: Three ways to solve this problem in Python (buildin sort, merge and priority queue)
Thanks for sharing. Based on your sort() version, I implemented version that returns linked list rather than array.
class Solution(object): def mergeKLists(self, lists): res = [] for l in lists: while l: res.append((l.val, l)) l = l.next res.sort(key=lambda tup: tup[0]) dummy = ListNode(None) cur = dummy for l in res: cur.next = l[1] cur = cur.next return dummy.next