
数据结构
笛在月明
我们采集的只是石头,却必须时刻展望未来的大教堂。
展开
-
Python实现二叉树的递归和非递归遍历
本文定义的二叉树结构如下:class BinNode(): def __init__( self, val ): self.lchild = None self.rchild = None self.val = val先序遍历 递归写法def preOrder(self, root): if root == None:原创 2016-09-26 21:52:55 · 3116 阅读 · 0 评论 -
Linked List Cycle
Given a linked list, determine if it has a cycle in it.解法一: 先建一个节点,然后遍历这个链表,每遍历一个节点,就让这个节点指向刚刚新建的那个节点,这样如果有循环,那么它回来之后的next就会指向那个节点,那么就代表有循环。代码如下:# Definition for singly-linked list.# class ListNode:原创 2017-01-17 22:30:35 · 450 阅读 · 0 评论 -
Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘原创 2017-01-18 22:33:10 · 492 阅读 · 0 评论 -
查找单词是否存在于单词表格中
问题: 给定一个2维字母组成的矩阵,水平或垂直的字母可以连成一个单词,给定一个单词,判断其是否在给定的矩阵中。每个字母只能被使用一次。 举例: [ [‘A’,’B’,’C’,’E’], [‘S’,’F’,’C’,’S’], [‘A’,’D’,’E’,’E’] ] 则根据规则:”ABCCED”, -> returns true, “S原创 2017-01-24 22:46:56 · 1572 阅读 · 0 评论 -
leetcode 23. Merge k Sorted Lists 几种解法
1.取值排序法 首先我们最先想到的可能是将所有链表的值放进一个数组中,然后进行排序,最后将排序后的元素依次构建新的链表。代码如下: class Solution(object): def mergeKLists(self, lists): """ :type lists: List[ListNode] :rtype: ListNo...原创 2018-05-03 22:40:51 · 556 阅读 · 0 评论 -
单调栈
单调栈是一种特殊的栈,栈内元素保持递增或者递减。 单调栈有两个性质: 1.满足从栈顶到栈底的元素具有严格的单调性 2.满足栈的后进先出特性越靠近栈底的元素越早进栈 利用单调栈,可以找到从左/右遍历第一个比它小/大的元素的位置,在某些问题中可以将时间复杂度从O(N^2)降低为O(N).如leetcode 84. Largest Rectangle in Histogram. 这道题的描述...原创 2018-05-11 15:44:16 · 801 阅读 · 0 评论 -
leetcode 树相关题目小结
leetcode 98. Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node conta...原创 2018-05-13 15:07:49 · 1669 阅读 · 0 评论