![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
剑指offer
酸乳酸乳
这个作者很懒,什么都没留下…
展开
-
剑指OFFER
二维数组查找python实现: # -*- coding:utf-8 -*-class Solution: # array 二维列表 def Find(self, target, array): # write code here x = len(array) y = len(array[0]) for i ...原创 2018-11-07 09:09:38 · 86 阅读 · 0 评论 -
二叉树的深度 python
# -*- coding:utf-8 -*-# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def TreeDepth(self, pRoot): ...原创 2018-11-18 19:08:52 · 241 阅读 · 0 评论 -
平衡二叉树 剑指offer python
第一次写是从上向下遍历,但是这会导致很多不必要的判断,代码如下:# -*- coding:utf-8 -*-# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: ...原创 2018-11-19 08:35:21 · 210 阅读 · 0 评论 -
数组中只出现一次的数 python
看了网上的解法,对于两两出现的数,我应该想到使用异或,我们先计算整个数组的异或值,那么最后结果其实就是两个不一样的数的异或的结果(因为其他的都两两消除了),然后我们根据这个值的最后为1的位,来来讲这个数组进行分类,得到的两个数组,在自己进行异或就分别得到了两个不一样的值,代码如下: # -*- coding:utf-8 -*-class Solution: # 返回[a,b] 其...原创 2018-11-19 09:22:13 · 146 阅读 · 0 评论 -
求1+2+3+...+n python
递归# -*- coding:utf-8 -*-class Solution: def Sum_Solution(self, n): # write code here return n and n + self.Sum_Solution(n-1)reduce()函数# -*- coding:utf-8 -*-class Solution...原创 2018-11-19 16:10:01 · 1539 阅读 · 0 评论 -
剑指offer 二叉树中和为某一值的路径
里面总是反映我添加的是空列表,后来使用了list函数,但是不知道是什么原因,就通过了# -*- coding:utf-8 -*-# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass S...原创 2018-11-17 12:11:42 · 79 阅读 · 0 评论 -
二叉搜索树与双向链表 python
使用链表# -*- coding:utf-8 -*-# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def Convert(self, pRootOf...原创 2018-11-18 11:17:36 · 102 阅读 · 0 评论 -
把二叉树打印成多行 python
class Solution: # 返回二维列表[[1,2],[4,5]] def Print(self, pRoot): # write code here if not pRoot: return [] tree = [pRoot] result = [] while tr...原创 2018-11-20 11:06:23 · 126 阅读 · 1 评论 -
按之字形打印二叉树
# -*- coding:utf-8 -*-# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: # 返回二维列表[[1,2],[4,5]] def P...原创 2018-11-20 14:25:15 · 145 阅读 · 0 评论 -
数字在排序数列中出现的次数python
使用二分# -*- coding:utf-8 -*-class Solution: def GetNumberOfK(self, data, k): # write code here count = 0 if len(data)==0: return 0 index = self.binary...原创 2018-11-18 19:01:34 · 1086 阅读 · 0 评论 -
二进制中1的个数
参考了其他人的代码# -*- coding:utf-8 -*-class Solution: def NumberOf1(self, n): # write code here count = 0 for i in range(32): if n&1: count += ...转载 2018-11-07 15:31:59 · 61 阅读 · 0 评论 -
剑指offer 替换空格
python实现使用正则表达式import re# -*- coding:utf-8 -*-class Solution: # s 源字符串 def replaceSpace(self, s): # write code here regex = re.compile(r'\s') return regex.sub('%20...原创 2018-11-07 09:25:49 · 64 阅读 · 0 评论 -
从尾到头打印链表
python实现之间一直错的地方是if head==None:head = head.next# -*- coding:utf-8 -*-# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: # 返...原创 2018-11-07 09:48:15 · 65 阅读 · 0 评论 -
根据前序和中序构建二叉树
进行递归# -*- coding:utf-8 -*-# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: # 返回构造的TreeNode根节点 de...原创 2018-11-07 10:22:45 · 142 阅读 · 0 评论 -
用两个栈实现队列python
# -*- coding:utf-8 -*-class Solution: def __init__(self): self.stack1=[] self.stack2=[] def push(self, node): # write code here self.stack1.append(node) d...原创 2018-11-07 10:40:16 · 153 阅读 · 0 评论 -
斐波那契
# -*- coding:utf-8 -*-class Solution: def Fibonacci(self, n): # write code her if n <= 1: return n if n == 2: return 1 a = 1 b ...原创 2018-11-07 14:12:23 · 267 阅读 · 0 评论 -
跳台阶
python# -*- coding:utf-8 -*-class Solution: def jumpFloor(self, number): # write code here a = 0 b = 1 sum = 0 if number <= 1: return num...原创 2018-11-07 14:19:41 · 65 阅读 · 0 评论 -
变态跳台阶
# -*- coding:utf-8 -*-class Solution: def jumpFloorII(self, number): # write code here if number <= 2: return number a = 1 b = 2 sum = 0 ...原创 2018-11-07 14:49:49 · 75 阅读 · 0 评论 -
矩形覆盖
# -*- coding:utf-8 -*-class Solution: def rectCover(self, number): # write code here if number<=2: return number a = 1 b = 2 sum = 0 ...原创 2018-11-07 14:58:40 · 67 阅读 · 0 评论 -
剑指offer c++
3.从你头到尾打印链表/*** struct ListNode {* int val;* struct ListNode *next;* ListNode(int x) :* val(x), next(NULL) {* }* };*/class Solution {public: ...原创 2018-12-22 19:10:29 · 294 阅读 · 0 评论