36. 有效的数独
判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。
- 数字
1-9
在每一行只能出现一次。 - 数字
1-9
在每一列只能出现一次。 - 数字
1-9
在每一个以粗实线分隔的3x3
宫内只能出现一次。
上图是一个部分填充的有效的数独。
数独部分空格内已填入了数字,空白格用 '.'
表示。
示例 1:
输入: [ ["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:
输入: [ ["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 存在, 因此这个数独是无效的。
说明:
- 一个有效的数独(部分已被填充)不一定是可解的。
- 只需要根据以上规则,验证已经填入的数字是否有效即可。
- 给定数独序列只包含数字
1-9
和字符'.'
。 - 给定数独永远是
9x9
形式的。
代码思路:
先用一个函数判断是不是有效行(列),同时判断这一行(列)有没有重复的元素。
class Solution:
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
def isValidList(xs):
xs = list(filter(lambda x: x !="." ,xs))
return len(set(xs)) == len(xs)
for i in range(9):
if not isValidList([board[i][j] for j in range(9)]) or not isValidList([board[j][i] for j in range(9)]):
return False
for i in range(3):
for j in range(3):
if not isValidList([board[m][n] for n in range(3 * j, 3 * j + 3) for m in range(3 * i, 3*i+3)]):
return False
return True
367. 有效的完全平方数
给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。
注意:不要使用任何内置的库函数,如 sqrt
。
示例 1:
输入: 16 输出: True
示例 2:
输入: 14 输出: False
代码:前两种方法超时了,第三种方法改进
class Solution:
def isPerfectSquare(self, num):
"""
:type num: int
:rtype: bool
"""
#### 超出时间限制
# if num == 1:
# return True
# m = num //2
# for i in range(m+1):
# if i**2 == num:
# return True
# return False
#### 通过列举所有的完全平方数,1,4,9,16,25,36,49,64,81,100…等等,
#### 发现完全平方数的差都为奇数,即1,3,5,7,9,11,13,15…
#### 等等~所以可以判断完全平方数应该是N个奇数的和。
#### 超出时间限制
# if num == 1:
# return True
# for i in range(1, num, 2):
# num -= i
# if num == 0:
# return True
# else:
# continue
# return False
left, right = 0, num
while left < right:
mid = left + (right - left) // 2
if mid ** 2 < num:
left = mid + 1
else:
right = mid
if left < 1:
sqrt_num = left -1
else:
sqrt_num = left
return sqrt_num ** 2 == num
48. 旋转图像
给定一个 n × n 的二维矩阵表示一个图像。
将图像顺时针旋转 90 度。
说明:
你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。
示例 1:
给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2], [9,6,3] ]
示例 2:
给定 matrix = [ [ 5, 1, 9,11], [ 2, 4, 8,10], [13, 3, 6, 7], [15,14,12,16] ], 原地旋转输入矩阵,使其变为: [ [15,13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7,10,11] ]
代码:
class Solution:
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
##### the first method
# n = len(matrix)
# m = n // 2
# for i in range(n):
# for j in range(n-i-1):
# temp = matrix[i][j]
# matrix[i][j] = matrix[n-j-1][n-i-1]
# matrix[n-j-1][n-i-1] = temp
# for i in range(m):
# for j in range(n):
# temp = matrix[i][j]
# matrix[i][j] = matrix[n-i-1][j]
# matrix[n-i-1][j] = temp
##### the second method
n = len(matrix)
for i in range(n):
for j in range(i+1, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
print(matrix)
for i in range(n):
matrix[i].reverse()
print(matrix)