1. 判断链表中是否有环
题目如下
使用双指针解题,快指针走一步,慢指针走两步。快的追上慢的了就是有环
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
#
# @param head ListNode类
# @return bool布尔型
#
class Solution:
def hasCycle(self , head: ListNode) -> bool:
# 快慢指针都先指向head
fast = head
low = head
# 快慢指针:
# 快指针每次走两步
# 慢指针每次走一步
while True:
# 看一下列表是否为空
if fast == None or fast.next == None:
return False
# 快指针走两步,慢指针走一步
fast = fast.next.next
low = low.next
# 快的追上慢的了
if low == fast:
return True
return False
2. 将升序数组转化为平衡二叉搜索树
因为输入是确定的升序数组,所以不需要考虑先排序的问题
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param nums int整型一维数组
# @return TreeNode类
#
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
# 如果数组为空,返回 None
if not nums:
return None
# 找到中间的元素作为根节点
idx = len(nums) // 2
node = TreeNode(nums[idx])
# 递归构建左子树和右子树
node.left = self.sortedArrayToBST(nums[:idx])
node.right = self.sortedArrayToBST(nums[idx+1:])
return node
3. 二叉树的最大深度
题目如下
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param root TreeNode类
# @return int整型
#
class Solution:
def maxDepth(self , root ):
# write code here
if not root: return 0
# +1 是为了加上 root
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
4. 删除有序链表中重复的元素
双指针解法
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param head ListNode类
# @return ListNode类
#
class Solution:
def deleteDuplicates(self , head ):
# 如果链表为空,返回没有
if not head: return None
# 设置双指针,一个指向当前,一个指向下一个
slow,fast = head,head.next
# 当 fast指向有效元素时
while fast:
# 如果 fast 和 slow、不相等,slow 向前移动到fast,fast移动到下一个
if fast.val != slow.val:
slow.next = fast
slow = slow.next
fast = fast.next
# 检查完之后,slow移动开
slow.next = None
return head
单指针解法
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param head ListNode类
# @return ListNode类
#
class Solution:
def deleteDuplicates(self, head):
# 如果链表为空,返回 None
if not head:
return None
current = head # 初始化指针
while current.next:
if current.val == current.next.val:
# 跳过重复的节点
current.next = current.next.next
else:
# 移动指针到下一个节点
current = current.next
return head
5. 第一个只出现一次的字符
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
# @param str string字符串
# @return int整型
#
class Solution:
def FirstNotRepeatingChar(self, s):
for i in range(len(s)):
if not s[i] in s[:i] + s[i+1:]:
return i
return -1
6. 求平方根
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param x int整型
# @return int整型
#
class Solution:
def sqrt(self, x: int) -> int:
if x < 2:
return x # 对于 x = 0 和 x = 1,直接返回 x
left, right = 0, x
while left <= right:
mid = (left + right) // 2
if mid * mid == x:
return mid
elif mid * mid < x:
left = mid + 1
else:
right = mid - 1
return right # 向下取整的平方根
7. 合并两个排序的链表
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param pHead1 ListNode类
# @param pHead2 ListNode类
# @return ListNode类
#
class Solution:
def Merge(self , pHead1: ListNode, pHead2: ListNode) -> ListNode:
# 如果链表1 和 链表2 都是空的
if not pHead1 and not pHead2:
return None
# 链表1空
if not pHead1 and pHead2:
return pHead2
# 链表2空
if pHead1 and not pHead2:
return pHead1
#初始化 head 和 previous
head = None
previous = None
# 如果 链表1 的第一个值小于链表2的第一个值
# Head = 链表1 第一个,previous 和 head 挂钩,链表一指针向后移动
if pHead1.val < pHead2.val:
head = pHead1
previous = head
pHead1 = pHead1.next
# 链表2 第一个数值小的话,就是链表2 作为第一个
else:
head = pHead2
previous = head
pHead2 = pHead2.next
# 当链表1和链表2 不为空的情况下。双指针遍历
while pHead1 and pHead2:
if pHead1.val < pHead2.val:
previous.next = pHead1
previous = pHead1
pHead1 = pHead1.next
else:
previous.next = pHead2
previous = pHead2
pHead2 = pHead2.next
# 最后将剩下的那个加入链表
if not pHead1:
previous.next = pHead2
if not pHead2:
previous.next = pHead1
return head
8. 不同路径的数目(一)
class Solution:
def uniquePaths(self , m , n ):
# 初始化一张地图,m行,n列。全部填充为0
dp = [[0]*n for _ in range(m)]
#
for i in range(m):
for j in range(n):
# 如果是在第一行或第一列,路径数为 1。因为只能走右边和下面这两个方向
if i == 0 or j == 0:
dp[i][j] = 1
else:
# 之后的每一个格子都是上面一格的数量加上左边一格的数量,直到最后一格
dp[i][j] = dp[i-1][j] + dp[i][j-1]
return dp[-1][-1]
9. 有效括号序列
#
#
# @param s string字符串
# @return bool布尔型
#
class Solution:
def isValid(self , s ):
# write code here
#长度为奇数 一定不对
if len(s)%2 == 1:
return False
#只有3种括号在s中,才进行替换
while '{}' in s or '[]' in s or '()' in s:
s = s.replace('[]','').replace('{}','').replace('()','')
return True if s=='' else False
10. 最长公共前缀
class Solution:
def longestCommonPrefix(self , strs ):
# 如果字符串为空
if not strs:
return ""
result = ""
# 先找到最短的字符串
min_len = min([len(s)for s in strs])
# 按照最短的字符的长度遍历
for i in range(min_len):
# 取第一个字符的第一位,检查其余字符的相同位置是否相同
now = strs[0][i]
if all(s[i]==now for s in strs):
result += now
else:
return result
return result