leetcode 35. 搜索插入位置 36. 有效的数独

leetcode 35. 搜索插入位置 36. 有效的数独

35. 搜索插入位置

难度简单1066收藏分享切换为英文接收动态反馈

给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。

请必须使用时间复杂度为 O(log n) 的算法。

示例 1:

输入: nums = [1,3,5,6], target = 5
输出: 2

示例 2:

输入: nums = [1,3,5,6], target = 2
输出: 1

示例 3:

输入: nums = [1,3,5,6], target = 7
输出: 4

示例 4:

输入: nums = [1,3,5,6], target = 0
输出: 0

示例 5:

输入: nums = [1], target = 0
输出: 0

提示:

  • 1 <= nums.length <= 104
  • -104 <= nums[i] <= 104
  • nums无重复元素升序排列数组
  • -104 <= target <= 104
from typing import List


class Solution:
    """
    简单
    """
    def searchInsert(self, nums: List[int], target: int) -> int:
        low = 0
        high = len(nums)
        ans = 0
        if high == 0 or target < nums[0]:
            return 0
        if target > nums[high - 1]:
            return high
        while low < high and nums[low] < target:
            low += 1
            ans = low
        return ans

    """
    二分
    """
    def searchInsert2(self, nums: List[int], target: int) -> int:
        low = 0
        high = len(nums)
        if high == 0 or target <= nums[0]:
            return 0
        if target > nums[high - 1]:
            return high
        while low < high:
            mid = (low + high) // 2
            guess = nums[mid]
            if guess == target:
                return mid
            if target > guess and target <= nums[mid + 1]:
                return mid + 1
            if target < guess and target > nums[mid - 1]:
                return mid
            if guess > target:
                high = mid - 1
            else:
                low = mid + 1
        return 0

if __name__ == '__main__':
    nums = [1,3,5,7,8,9,9,10]
    nums = [1]
    nums = [1,3]
    target = 9
    target = 2
    target = 1

    nums = [1, 4, 6, 7, 8, 9]
    target = 6
    print(Solution().searchInsert2(nums,target))
36. 有效的数独

难度中等581收藏分享切换为英文接收动态反馈

请你判断一个 9x9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。

  1. 数字 1-9 在每一行只能出现一次。
  2. 数字 1-9 在每一列只能出现一次。
  3. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)

数独部分空格内已填入了数字,空白格用 '.' 表示。

注意:

  • 一个有效的数独(部分已被填充)不一定是可解的。
  • 只需要根据以上规则,验证已经填入的数字是否有效即可。

示例 1:

img

输入:board = 
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
输出:true

示例 2:

输入:board = 
[["8","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
输出:false
解释:除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。

提示:

  • board.length == 9
  • board[i].length == 9
  • board[i][j] 是一位数字或者 '.'
from typing import List


class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        col = len(board)
        bevel = [[{}, {}, {}],[{}, {}, {}],[{}, {}, {}]]
        for i in range(col):
            col_dic = {}
            row_dic = {}
            c = i // 3
            for i2 in range(col):
                if board[i2][i] not in col_dic:
                    if board[i2][i] != ".":
                        col_dic[board[i2][i]] = ""
                else:
                    return False
                r = i2 // 3
                if board[i][i2] not in bevel[c][r]:
                    if board[i][i2] != ".":
                        bevel[c][r][board[i][i2]] = ""
                else:
                    return False
                if board[i][i2] not in row_dic:
                    if board[i][i2] != ".":
                        row_dic[board[i][i2]] = ""
                else:
                    return False
        return True



if __name__ == '__main__':
    board =     [["8","3",".",".","7",".",".",".","."]
                ,["6",".",".","1","9","5",".",".","."]
                ,[".","9","8",".",".",".",".","6","."]
                ,["8",".",".",".","6",".",".",".","3"]
                ,["4",".",".","8",".","3",".",".","1"]
                ,["7",".",".",".","2",".",".",".","6"]
                ,[".","6",".",".",".",".","2","8","."]
                ,[".",".",".","4","1","9",".",".","5"]
                ,[".",".",".",".","8",".",".","7","9"]]

    board =  [[".", ".", ".", ".", ".", ".", ".", ".", "."]
            , [".", ".", ".", ".", ".", ".", ".", ".", "."]
            , [".", "9", ".", ".", ".", ".", ".", ".", "1"]
            , ["8", ".", ".", ".", ".", ".", ".", ".", "."]
            , [".", "9", "9", "3", "5", "7", ".", ".", "."]
            , [".", ".", ".", ".", ".", ".", ".", "4", "."]
            , [".", ".", ".", "8", ".", ".", ".", ".", "."]
            , [".", "1", ".", ".", ".", ".", "4", ".", "9"]
            , [".", ".", ".", "5", ".", "4", ".", ".", "."]]

    board = [["5","3",".",".","7",".",".",".","."]
            ,["6",".",".","1","9","5",".",".","."]
            ,[".","9","8",".",".",".",".","6","."]
            ,["8",".",".",".","6",".",".",".","3"]
            ,["4",".",".","8",".","3",".",".","1"]
            ,["7",".",".",".","2",".",".",".","6"]
            ,[".","6",".",".",".",".","2","8","."]
            ,[".",".",".","4","1","9",".",".","5"]
            ,[".",".",".",".","8",".",".","7","9"]]

    board =  [[".", ".", ".", ".", ".", ".", ".", ".", "."]
            , ["2", ".", ".", ".", ".", ".", "3", ".", "."]
            , [".", "2", ".", "1", "8", ".", ".", ".", "."]
            , [".", ".", ".", "7", ".", ".", ".", ".", "."]
            , [".", ".", ".", ".", "1", ".", "9", "7", "."]
            , [".", ".", ".", ".", ".", ".", ".", ".", "."]
            , [".", ".", ".", "3", "6", ".", "1", ".", "."]
            , [".", ".", ".", ".", ".", ".", ".", ".", "."]
            , [".", ".", ".", ".", ".", ".", ".", "2", "."]]
    # print(1//3)
    print(Solution().isValidSudoku(board))
"."]
            , [".", ".", ".", "3", "6", ".", "1", ".", "."]
            , [".", ".", ".", ".", ".", ".", ".", ".", "."]
            , [".", ".", ".", ".", ".", ".", ".", "2", "."]]
    # print(1//3)
    print(Solution().isValidSudoku(board))

你可能感兴趣的:(leetcode,leetcode,算法)