剑指offer
Frankfurta
低头要有勇气,抬头要有底气。要么尽情学习,要么放肆运动,要么勇敢去爱,混乱迷惘不是真正的你!
展开
-
剑指offer-二叉树中和为某一值的路径python
相当于深度搜索dfs class Solution: def dfs(self,root,s,li,listAll): li.append(root.val) if not root.left and not root.right and s==root.val: listAll.append(li) if root.l转载 2017-11-28 19:41:54 · 2066 阅读 · 3 评论 -
剑指offer-栈的压入、弹出序列
class Solution: def IsPopOrder(self, pushV, popV): # write code here l,j=[],0 if not pushV or not popV: return False for i in range(len(pushV)):转载 2017-11-27 21:00:40 · 225 阅读 · 0 评论 -
剑指offer-定义栈实现栈最小元素的min函数
转载源地址: https://www.nowcoder.com/questionTerminal/4c776177d2c04c2494f2555c9fcc1e49 来源:牛客网==解法1# -*- coding:utf-8 -*- class Solution: def __init__(self): self.stack = [] self.assist转载 2017-11-27 20:27:05 · 260 阅读 · 0 评论 -
顺时针打印矩阵
class Solution: def printMatrix(self, matrix): res = [] while matrix: #part1 res += matrix.pop(0) #part2 if matrix and matrix[0]:转载 2017-11-27 16:01:52 · 252 阅读 · 0 评论 -
剑指offer-判断二叉树B是不是包含二叉树A
# -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def HasSubtree(self, pRoot1, pRoo翻译 2017-11-27 14:57:12 · 248 阅读 · 0 评论 -
剑指offer-合并两个有序链表
# -*- coding:utf-8 -*- class Solution: def Merge(self, pHead1, pHead2): p = ListNode(90) p1,p2,Head=pHead1,pHead2,p# Head是用来存储新加的链表的头节点,p是新链表操作的指针 while p2 and p1:原创 2017-11-24 15:54:22 · 201 阅读 · 1 评论 -
剑指offer-链表中倒数第k个结点
正确代码class Solution: def FindKthToTail(self, head, k): l = [] while head!=None: l.append(head) head = head.next if k > len(l) or k < 1: r原创 2017-11-24 10:25:06 · 159 阅读 · 0 评论 -
剑指offer-二进制中1的个数,负数用补码表示
自己写的两个,一个是根据补码的特点,一个是使用python强大的库函数,哇,python是真的简单。# -*- coding:utf-8 -*- class Solution: def NumberOf1(self, n): count = 0 if n < 0: n = n + 2 ** 32 while n:原创 2017-11-24 09:32:20 · 664 阅读 · 0 评论 -
剑指offer-用两个栈实现队列
# -*- coding:utf-8 -*- class Solution: def __init__(self):#注意内置函数是双下划线 self.stack1=[] self.stack2=[] def push(self, node): # write code here self.stack1.append(n转载 2017-11-23 10:45:44 · 174 阅读 · 0 评论 -
剑指offer-从头到尾打印链表
class Solution: def printListFromTailToHead(self, listNode): # write code here l=[] head = listNode while head: l.insert(0,head.val) head = h原创 2017-11-21 12:21:08 · 248 阅读 · 0 评论 -
剑指offer-替换空格
题目描述 请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。class Solution: def replaceSpace(self, s): ans = s for i in range(len(s)): if s[i].iss原创 2017-11-21 11:40:12 · 145 阅读 · 0 评论 -
文章标题
从上往下打印出二叉树的每个节点,同层节点从左至右打印。class Solution: def PrintFromTopToBottom(self, root): # write code here l=[] if not root: return [] q=[root] while q:转载 2017-11-27 21:25:00 · 200 阅读 · 0 评论