Leetcode刷题笔记-不知道怎么分类的题

731. My Calendar II

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

class MyCalendarTwo(object):
    # O(N)
    def __init__(self):
        self.overlap = []
        self.nums = []

    def book(self, start, end):
        for s, e in self.overlap:
            if start < e and end > s:
                return False
        for s, e in self.nums:
            if start < e and end > s:
                self.overlap.append([max(start, s), min(end, e)])
        self.nums.append([start, end])
        return True

 

36. Valid Sudoku

class Solution(object):
    def isValidSudoku(self, board):
        """
        :type board: List[List[str]]
        :rtype: bool
        """
        return self.isRowValid(board) and self.isColValid(board) and self.isBoxValid(board)
    
    def isValidUnit(self, unit):
        unit = [x for x in unit if x != '.']
        return len(unit) == len(set(unit))
    
    def isRowValid(self, board):
        for r in board:
            if not self.isValidUnit(r):
                return False
        return True
    
    def isColValid(self, board):
        for c in zip(*board):
            if not self.isValidUnit(c):
                return False
        return True
    
    def isBoxValid(self, board):
        for i in (0, 3, 6):
            for j in (0, 3, 6):
                box = [board[r][c] for r in xrange(i, i+3) for c in xrange(j, j+3)]
                if not self.isValidUnit(box):
                    return False
        return True

621. Task Scheduler

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

class Solution(object):
    def leastInterval(self, tasks, n):
        """
        :type tasks: List[str]
        :type n: int
        :rtype: int
        """
        counter = collections.Counter(tasks)
        task_list = [[value, key] for key, value in counter.items()]
        task_list.sort(reverse=True)
        most = task_list[0][0]
        sameLength = sum([1 for value, key in task_list if value == most])
        return max((most-1) * (n+1) + sameLength, len(tasks))
        

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

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

# The read4 API is already defined for you.
# @param buf, a list of characters
# @return an integer
# def read4(buf):

class Solution(object):
    def __init__(self):
        self.queue = []
        
    def read(self, buf, n):
        """
        :type buf: Destination buffer (List[str])
        :type n: Maximum number of characters to read (int)
        :rtype: The number of characters read (int)
        """
       
        num = 0
        
        while True:
            buf4 = [''] * 4
            size = read4(buf4)
             
            self.queue += buf4
            
            minSize = min(len(self.queue), n-num)
            
            if minSize == 0:
                break
            
            for i in xrange(minSize):
                buf[num] = self.queue.pop(0)
                num += 1
        return num
        
       

 

 

 

398. Random Pick Index

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

class Solution(object):
    def __init__(self, nums):
        self.nums  = nums       

    def pick(self, target)
        re = -1
        count = 0
        for i, n in enumerate(self.nums):
            if target == n:
                count += 1
                if random.randint(1, count) == 1:
                    re = i
        return re

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值