![](https://img-blog.csdnimg.cn/20201014180756926.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
剑指offer
996冲冲冲
这个作者很懒,什么都没留下…
展开
-
JZ81 调整数组顺序使奇数位于偶数前面(二)
前后双指针:class Solution(object): def exchange(self, nums): """ :type nums: List[int] :rtype: List[int] """ left,right =0, len(nums)-1 while left<right: while left < right and nums[left] &a原创 2022-02-25 20:12:06 · 247 阅读 · 0 评论 -
JZ71 跳台阶扩展问题
递归法:# -*- coding:utf-8 -*-class Solution: def jumpFloorII(self, number): # write code here if number <= 2: return number else: return 2*self.jumpFloorII(number-1)搜索法:# -*- coding:utf-8 -*-class S原创 2021-05-16 17:37:15 · 44 阅读 · 0 评论 -
JZ70 矩形覆盖
递归:class Solution: def rectCover(self, number): # write code here if number < 3: return number else: return self.rectCover(number-1)+self.rectCover(number-2)搜索:# -*- coding:utf-8 -*-class Solution原创 2021-05-16 18:02:23 · 63 阅读 · 0 评论 -
JZ69 跳台阶
递归法:# -*- coding:utf-8 -*-class Solution: def jumpFloor(self, number): # write code here if number < 3: return number else: return self.jumpFloor(number-1) + self.jumpFloor(number-2)搜索法:# -*- codi原创 2021-05-16 17:23:08 · 267 阅读 · 0 评论 -
JZ53 数字在升序数组中出现的次数
class Solution: def GetNumberOfK(self, data, k): l = 0 r = len(data)-1 while l <= r: m = l + (r - l)//2 if data[m] > k: r = m - 1 elif data[m] < k: l =原创 2021-10-15 21:48:57 · 59 阅读 · 0 评论 -
JZ38. 字符串的排列
排序法class Solution(object): def permutation(self, s): """ :type s: str :rtype: List[str] """ s = sorted(s) res = [] sol = [] vsd = [0]*len(s) def backtrack(s, res, sol, vsd):原创 2021-11-10 19:07:30 · 82 阅读 · 0 评论 -
JZ30 包含min函数的栈
class MinStack(object): def __init__(self): """ initialize your data structure here. """ self.stack=[] self.MinStack=[] def push(self, x): """ :type x: int :rtype: None """原创 2021-06-07 16:49:54 · 54 阅读 · 0 评论 -
JZ29 顺时针打印矩阵
在Python的列表里面,对操作符=和+=做了重载;+表示连接,+=表示追加并拓展列表;列表+=操作符右边的操作数必须是一个iterable对象。class Solution: # matrix类型为二维列表,需要返回列表 def printMatrix(self, matrix): """ :type root: TreeNode :rtype: bool """ res = [] whil原创 2021-06-05 18:07:48 · 92 阅读 · 0 评论 -
JZ28 对称的二叉树
递归class Solution(object): def isSymmetric(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True def recure(left,right): if not left and not right:原创 2021-06-04 17:01:56 · 50 阅读 · 0 评论 -
JZ27 二叉树的镜像
递归class Solution(object): def mirrorTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ if not root: return root.left, root.right = root.right, root.left if root.left: sel原创 2021-06-04 16:59:29 · 42 阅读 · 0 评论 -
JZ26 树的子结构
递归:class Solution(object): def isSubStructure(self, A, B): """ :type A: TreeNode :type B: TreeNode :rtype: bool """ def dfs(A): if not A or not B: return False if原创 2021-06-04 16:05:21 · 45 阅读 · 0 评论 -
JZ25 合并两个排序的链表
正常遍历比较法class Solution:# 返回合并后列表def Merge(self, pHead1, pHead2): # write code here node = ListNode(0) x = node while pHead1 and pHead2: if pHead1.val > pHead2.val: x.next = pHead2 pHead2 = pHead2.next原创 2021-05-26 16:47:57 · 68 阅读 · 0 评论 -
LC206,JZ24 反转链表
借用栈class Solution: # 返回ListNode def ReverseList(self, pHead): # write code here array = [] while pHead: array.append(pHead.val) pHead=pHead.next node = ListNode(0) x = node wh原创 2021-05-26 16:07:08 · 83 阅读 · 0 评论 -
JZ22 链表中倒数最后k个结点
双指针class Solution: def FindKthToTail(self , pHead , k ): # write code here slow = pHead fast = pHead if k<0 or pHead == None: return None for i in range(k): if fast==None:原创 2021-05-24 15:13:20 · 106 阅读 · 1 评论 -
JZ21 调整数组顺序使奇数位于偶数前面(一)
冒泡法class Solution: def reOrderArray(self , array ): # write code here ji = -1 for i in range(len(array)): if array[i]%2==1: ji +=1 x = array[i] for k in range(i,ji-1,-原创 2021-05-24 14:48:43 · 51 阅读 · 0 评论 -
JZ16 数值的整数次方
递归class Solution: def Power(self, x, n): if n==0: return 1 if n < 0: x=1/x n=-n if n&1: return x*self.Power(x*x, n//2) else: return self.Power(x*x, n//原创 2021-05-24 14:33:51 · 84 阅读 · 0 评论 -
JZ15 二进制中1的个数
# -*- coding:utf-8 -*-# -*- coding:utf-8 -*-class Solution: def NumberOf1(self, n): # write code here count = 0 if n < 0: n = n & 0xffffffff while n: count += 1 n = (n原创 2021-05-24 14:26:19 · 59 阅读 · 0 评论 -
JZ11 旋转数组的最小数字
注意左右边界和povclass Solution: def minNumberInRotateArray(self, rotateArray): # write code here if not rotateArray: return 0 left = 0 right = len(rotateArray)-1 while left < right: pov = le原创 2021-05-15 16:41:55 · 44 阅读 · 0 评论 -
JZ10 斐波那契数列
记忆搜索法:# -*- coding:utf-8 -*-class Solution: def Fibonacci(self, n): # write code here array = [0,1] for i in range (2,n+1): a = array[i-1] + array[i-2] array.append(a) return array[n]递归法:# -*原创 2021-05-15 18:01:43 · 49 阅读 · 0 评论 -
JZ9 用两个栈实现队列
注意栈先进后出列表先进先出class Solution: def __init__(self): self.push_stack=[] self.pop_stack=[] def push(self, node): # write code here return self.push_stack.append(node) def pop(self): # return xx if sel原创 2021-05-14 17:07:38 · 64 阅读 · 0 评论 -
JZ7 重建二叉树
简单易懂法(递归)# -*- coding:utf-8 -*-# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: # 返回构造的TreeNode根节点 def reConstructBinaryTree(self, pre, tin): # w原创 2021-05-14 17:05:06 · 75 阅读 · 0 评论 -
JZ6 从尾到头打印链表
记住要熟悉python这种无指针的语言的数据结构# -*- coding:utf-8 -*-# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: # 返回从尾部到头部的列表值序列,例如[1,2,3] def printListFromTailToHead(self, listNode): # writ原创 2021-05-11 16:10:47 · 51 阅读 · 0 评论 -
JZ5 替换空格
使用常规方法遍历替换,需要申请一个额外的空间class Solution: # s 源字符串 def replaceSpace(self, s): # write code here new_s = '' for i in s: if i == ' ': new_s += '%20' else: new_s += i原创 2021-05-11 15:34:21 · 49 阅读 · 0 评论 -
JZ4 二维数组中的查找
剑指offer1 二维数组查找python求解及变形题求解刷题记录:切勿眼高手低暴力法:# -*- coding:utf-8 -*-class Solution: # array 二维列表 def Find(self, target, array): # write code here for arr in array: if target in arr: return True原创 2021-05-11 10:51:56 · 61 阅读 · 0 评论 -
JZ3数组中的重复数字
哈希表中 查找时间复杂度为常数级别class Solution(object): def findRepeatNumber(self, nums): """ :type nums: List[int] :rtype: int """ dic = set() for i in nums: if i in dic: return i原创 2022-03-04 18:17:22 · 47 阅读 · 0 评论