当前位置:   article > 正文

Leetcode刷题笔记-不知道怎么分类的题_leetcode题目如何分类

leetcode题目如何分类

731. My Calendar II

overlap的部分放重复的,只要检查overlap。 

  1. class MyCalendarTwo(object):
  2. # O(N)
  3. def __init__(self):
  4. self.overlap = []
  5. self.nums = []
  6. def book(self, start, end):
  7. for s, e in self.overlap:
  8. if start < e and end > s:
  9. return False
  10. for s, e in self.nums:
  11. if start < e and end > s:
  12. self.overlap.append([max(start, s), min(end, e)])
  13. self.nums.append([start, end])
  14. return True

 

36. Valid Sudoku

  1. class Solution(object):
  2. def isValidSudoku(self, board):
  3. """
  4. :type board: List[List[str]]
  5. :rtype: bool
  6. """
  7. return self.isRowValid(board) and self.isColValid(board) and self.isBoxValid(board)
  8. def isValidUnit(self, unit):
  9. unit = [x for x in unit if x != '.']
  10. return len(unit) == len(set(unit))
  11. def isRowValid(self, board):
  12. for r in board:
  13. if not self.isValidUnit(r):
  14. return False
  15. return True
  16. def isColValid(self, board):
  17. for c in zip(*board):
  18. if not self.isValidUnit(c):
  19. return False
  20. return True
  21. def isBoxValid(self, board):
  22. for i in (0, 3, 6):
  23. for j in (0, 3, 6):
  24. box = [board[r][c] for r in xrange(i, i+3) for c in xrange(j, j+3)]
  25. if not self.isValidUnit(box):
  26. return False
  27. return True

621. Task Scheduler

思路:https://leetcode.com/problems/task-scheduler/discuss/104496/concise-Java-Solution-O(N)-time-O(26)-space 

  1. class Solution(object):
  2. def leastInterval(self, tasks, n):
  3. """
  4. :type tasks: List[str]
  5. :type n: int
  6. :rtype: int
  7. """
  8. counter = collections.Counter(tasks)
  9. task_list = [[value, key] for key, value in counter.items()]
  10. task_list.sort(reverse=True)
  11. most = task_list[0][0]
  12. sameLength = sum([1 for value, key in task_list if value == most])
  13. return max((most-1) * (n+1) + sameLength, len(tasks))

158. Read N Characters Given Read4 II - Call multiple times

什么神经病题目,理解不了

  1. # The read4 API is already defined for you.
  2. # @param buf, a list of characters
  3. # @return an integer
  4. # def read4(buf):
  5. class Solution(object):
  6. def __init__(self):
  7. self.queue = []
  8. def read(self, buf, n):
  9. """
  10. :type buf: Destination buffer (List[str])
  11. :type n: Maximum number of characters to read (int)
  12. :rtype: The number of characters read (int)
  13. """
  14. num = 0
  15. while True:
  16. buf4 = [''] * 4
  17. size = read4(buf4)
  18. self.queue += buf4
  19. minSize = min(len(self.queue), n-num)
  20. if minSize == 0:
  21. break
  22. for i in xrange(minSize):
  23. buf[num] = self.queue.pop(0)
  24. num += 1
  25. return num

 

 

 

398. Random Pick Index

不需要sorted的,后面每次遇到的3,可以替换第一个3的概率是 1/count,如果正好random是这个概率 ,就替换3.

  1. class Solution(object):
  2. def __init__(self, nums):
  3. self.nums = nums
  4. def pick(self, target)
  5. re = -1
  6. count = 0
  7. for i, n in enumerate(self.nums):
  8. if target == n:
  9. count += 1
  10. if random.randint(1, count) == 1:
  11. re = i
  12. return re

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/615554
推荐阅读
相关标签
  

闽ICP备14008679号