1.缺失的第一个正数
class Solution(object):
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# 时间复杂度 o(n) 空间复杂度 o(1)
# 思路:原地哈希
# 把当前的数组当成哈希表 希望每一个数字放在对应的位置
# 题目提到的为正整数 因此是从 1 开始的,并且1放在位置 0
# 长度为4 的 nums[i] 最大出现的可能是4
# nums[i] 应该放在 nums[nums[i] - 1] (nums[i] - 1为下标)
size = len(nums)
for i in range(size):
# 1. 调整数组 把数组中的每个数转移到对应位置上
# 如果此时nums[i]的值符合范围 且 此时该数并不在位置上
while 1 <= nums[i] <= size and nums[i] != nums[nums[i] - 1]:
tmp = nums[i] - 1
nums[i] = nums[tmp]
nums[tmp] = tmp + 1
# 2. 遍历整个数组 如果出现不符合下标的数据就是缺少的值
for i in range(size):
if i + 1 != nums[i]:
return i + 1
# 如果所有下标都符合 那么最小的正整数就是 size + 1
return size + 1
2.矩阵置0
class Solution(object):
def setZeroes(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: None Do not return anything, modify matrix in-place instead.
"""
# 思路:先遍历整个矩阵,找到哪些行列需要进行置为0
# 时间 空间 o(m*n)
# 1.扫描这个矩阵 找出哪一行有0 哪一列有0
row = len(matrix) # 行数
col = len(matrix[0]) # 列数
row_set = set() # 需要置为0的行序号
col_set = set() # 需要置为0的列序号
for i in range(row):
for j in range(col):
if matrix[i][j] == 0:
row_set.add(i)
col_set.add(j) #注意集合用add
# 2.第二次扫描该矩阵 找到该置为0的行或列将该值置为0
for i in range(row):
for j in range(col):
if i in row_set or j in col_set:
matrix[i][j] = 0
return matrix
3.螺旋矩阵
class Solution(object):
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
# 时间 o(m*n) 空间 o(1)
# 思路 模拟整个过程
# 注意点1 行列要明确!!!
if not matrix: return []
l, r, t, b, res = 0, len(matrix[0]) - 1, 0, len(matrix) - 1, [] # 初始化边界
while True:
for i in range(l, r + 1):
res.append(matrix[t][i]) # 首先向右扫描
t += 1 # 缩小上边界
if t > b:
break # 注意退出的边界条件
for i in range(t, b + 1):
res.append(matrix[i][r]) # 向下扫描
r -= 1 # 缩小右边界
if r < l:
break
for i in range(r, l - 1, -1): # 向左扫描
res.append(matrix[b][i])
b -= 1 # 缩小下边界
if b < t:
break
for i in range(b, t - 1, -1): # 向上扫描
res.append(matrix[i][l])
l += 1
if l > r:
break
return res
4.旋转图像
class Solution(object):
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: None Do not return anything, modify matrix in-place instead.
"""
# 思路:选择90度相当于 先转置矩阵,再每行翻转矩阵
for i in range(0, len(matrix)):
for j in range(i, len(matrix[0])): # 注意起始点是从i开始,这样要可以转置否则
temp = matrix[i][j]
matrix[i][j] = matrix[j][i]
matrix[j][i] = temp
# print(matrix)
for i in range(0, len(matrix)):
left, right = 0, len(matrix[0]) - 1
while left < right:
tmp = matrix[i][left]
matrix[i][left] = matrix[i][right]
matrix[i][right] = tmp
left += 1
right -= 1
5.搜索二维矩阵II
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
# 思路:从矩阵左下角开始遍历,如果比当前的数小则索引上移,如果比当前的数大则索引右移。
i, j = len(matrix) - 1, 0 # 从左下角开始遍历
while i >= 0 and j <= len(matrix[0]) - 1 :
if target > matrix[i][j]:
j += 1
elif target < matrix[i][j]:
i -= 1
else:
return True
return None
6.相交链表
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
curA, curB = headA, headB
lenA, lenB = 0, 0
while curA:
curA = curA.next
lenA += 1
while curB:
curB = curB.next
lenB += 1
curA, curB = headA, headB #这里要重新赋值
if lenA < lenB:
curA, curB = curB, curA
lenA, lenB = lenB, lenA
for i in range(lenA - lenB):
curA = curA.next
while curA: # 现在A,B长度相同
if curA == curB: # 链表相交指的是指针相同
return curA
else:
curA = curA.next
curB = curB.next
return None
7.反转链表
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
# 思路注意的是指针之间的交换
cur = head
pre = None
while cur:
tmp = cur.next
cur.next = pre
pre = cur
cur = tmp
return pre
8.回文列表
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
# 思路:将链表存储为列表 进行比较 列表反转[::-1]
tmp = []
while head:
tmp.append(head.val)
head = head.next
return tmp[::-1] == tmp
9.环形链表
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
# 思路:如果有环 则快慢指针一定会相遇
slow = fast = head
while fast and fast.next: # 注意快慢指针判断的条件要有fast.next
slow = slow.next
fast = fast.next.next # 快指针每次走两步
if fast == slow:
return True
return False
10.环形链表II
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def detectCycle(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
# 跟上面的题目不一样的是 注意判断有环后要寻找环的起始点
# 需要一个指针从起始点出发,另一个指针从环相遇点出发,两个指针再次相遇的时候就是环的起点
fast = head
slow = head
while fast and fast.next:
# 报错的原因在fast.next.next存在与否
fast = fast.next.next #注意这里的while条件只要对fast限制
slow = slow.next
if(fast == slow): #相遇后开始寻找环的入口 说明有环
# 相遇后需要在意两个指针一个从相遇点出发,一个从起点出发
# 快指针从起点出发 慢指针从相遇点出发 再次相遇后的结点为环的起点
fast = head
while fast != slow:
fast = fast.next
slow = slow.next
return fast
return None