中级农民
- 积分
- 278
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2014-5-21
- 最后登录
- 1970-1-1
|
1.- def uniqueSubsetSum(nums):
- nums = sorted(nums)
- L = len(nums)
- sums = {0}
- def helper(start, c_sum):
- if start >= L:
- return
- prev = float('inf')
- for i in range(start, L):
- if nums[i] == prev:
- continue
- prev = nums[i]
- c_sum += nums[i]
- sums.add(c_sum)
- helper(i + 1, c_sum)
- c_sum -= nums[i]
- helper(0,0)
- return list(sums)
- print uniqueSubsetSum( [1,2,2])
复制代码
2.
- # should ask boundry include or not
- # refer meeting room, [url]https://leetcode.com/problems/meeting-rooms-ii/solution/[/url]
- def kthUser(log, K):
- starts = map(lambda x: [x[1], x[0]], log)
- ends = map(lambda x: [x[2], x[0]], log)
- starts = sorted(starts)
- ends = sorted(ends)
- i = j = 0
- activate_meetings = 0
- in_room = set()
- result = set()
- escaped = 0
- while i < len(starts) and j < len(ends):
- # print 'in the room', in_room
- # print 'escaped', escaped
- # print 'activate', activate_meetings
- if starts[i][0] < ends[j][0]:
- activate_meetings += 1
- #print 'adding to room', starts[i][1]
- in_room.add(starts[i][1])
- if activate_meetings + escaped>= K :
- result |= in_room
- i += 1
- else:
- activate_meetings -= 1
- escaped += 1
- in_room.remove(ends[j][1])
- if escaped == K:
- break
- j += 1
- print 'i,j', i, j
- print result
复制代码
3.
- class TreeNode:
- def __init__(self, val, left, right, parent):
- self.val = val
- self.left = left
- self.right = right
- self.parent = parent
- def treeGame(position):
- counts = [0, 0, 0] # parent, left, right
- def count(node):
- if node is None:
- return 0
- c = count(node.left) + count(node.right) + 1
- if position.left and position.left == node:
- counts[1] = c
- elif position.right and position.right == node:
- counts[2] = c
- return c
- counts[0] = count(root)
- counts[0] = counts[0] - counts[1] - counts[2] - 1
- i = max(*[(counts[idx], idx) for idx in range(3)])[1]
- if i == 0:
- return position.parent.val
- elif i == 1:
- return position.left.val
- else:
- return position.right.val
- n1 = TreeNode(1, None, None, None)
- n2 = TreeNode(2, None, None, None)
- n3 = TreeNode(3, None, None, None)
- n4 = TreeNode(4, None, None, None)
- n5 = TreeNode(5, None, None, None)
- n6 = TreeNode(6, None, None, None)
- n1.left = n2
- n1.right = n3
- n2.left = n4
- n1.right = n3
- n3.left = n5
- n3.right = n6
- n4.parent = n2
- n2.parent = n1
- n3.parent = n1
- n5.parent = n3
- n6.parent = n3
- root = n1
- position = n2
- print treeGame(position)
复制代码
4.
- # lc 694
- def numDistinctIslands(grid):
- results = set()
- seen = set()
- def explore(i, j, r0, c0):
- if 0 <= i < len(grid) and 0 <= j < len(grid[0]) and grid[i][j] and (i, j) not in seen:
- seen.add((i, j))
- shape.add((i-r0,j-c0))
- explore(i+1, j, r0, c0)
- explore(i-1, j, r0, c0)
- explore(i, j + 1, r0, c0)
- explore(i, j - 1, r0, c0)
- for i in range(len(grid)):
- for j in range(len(grid[0])):
- shape = set()
- explore(i, j, i, j)
- if shape:
- results.add(frozenset(shape))
- return len(results)
- grid = [[1,1,0,0,0],
- [1,1,0,0,0],
- [0,0,0,1,1],
- [0,0,0,1,1],]
- print numDistinctIslands(grid)
- grid = [[1,1,0,1,1],
- [1,0,0,0,0],
- [0,0,0,0,1],
- [1,1,0,1,1]]
- print numDistinctIslands(grid)
- grid = [
- [1, 1, 1, 0, 0, 0],
- [1, 1, 0, 0, 0, 0],
- [0, 0, 0, 1, 0, 0],
- [1, 1, 1, 0, 0, 0],
- [1, 1, 0, 0, 1, 1]]
- print numDistinctIslands(grid)
复制代码
|
|