07 2017 档案
摘要:这道题让我们求二叉树的坡度,某个结点的坡度的定义为该结点的左子树之和与右子树之和的差的绝对值,这道题让我们求所有结点的坡度之和. 这道题最好的解法应该是用后序遍历来做,因为后序遍历的顺序是左-右-根,那么就会从叶结点开始处理,这样我们就能很方便的计算结点的累加和,同时也可以很容易的根据子树和来计算t
阅读全文
摘要:用的list.addFist(); 达到左右根的目的. 节点值在加的时候, 先判断是否为空. listNode 也是哦
阅读全文
摘要:算法: 根右左, 尾递归 容器: list 尾递归 corner case: 右没有怎么办, 加一个输入值表示深度, list.size() 也表示深度 The core idea of this algorithm: 1.Each depth of the tree only select one
阅读全文
摘要:Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using ext...
阅读全文
摘要:二有序数组常用二分法, 要判断数组是否越界, 而数组的题常用递归, 擦半的方法, 比快排的方法, 少一个partition 遍历, 但是还是分成logn 层, Lintcode: Nuts & Bolts Problem 关键在于找到合适的比较位置, 准备擦出哪个数组的一半, 防止数组越界 The
阅读全文
摘要:sorted 数组, 常用二指针遍历, 会倒叙, 会前序 逆序遍历的好处, 1只遍历后面的 2此题在于空间上可以不干扰.
阅读全文
摘要:有序数组的题, 二分法, 根据mid的位置再确定start 和end的位置, 画图分情况 三种情况 The idea is that when rotating the array, there must be one half of the array that is still in sorte
阅读全文
摘要:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume NO duplicates in the array. ...
阅读全文
摘要:while-loop 要非递归写很简单,就用循环, 如果很复杂,就要用递归. but while 容易死循环 看不出就画图 二分法的模板-
阅读全文
摘要:两个数组两个指针 两个数组的题常sort, 再通过两指针遍历 常常与0 的差的正负来判断, 来判断指针的走位, 此处直接判断大小更方便
阅读全文
摘要:其实是一种特别顺序的排序, 所以就用quicksort-partition, 不过比较的元素和设计互相patition, partition 的比较因为是有equals的, 所以得用三个指针遍历 考点: partition 的设计, pivot 的选择 , 另一方的排序通过nuts排好后的数组再排,
阅读全文
摘要:数组常sort
阅读全文
摘要:HashMap实现原理及源码分析 哈希表(hash table)也叫散列表,是一种非常重要的数据结构,应用场景及其丰富,许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希表,而HashMap的实现原理也常常出现在各类的面试题中,重要性可见一斑。本文会对java集合框架中的对
阅读全文
摘要:一维数组解题的三种方法: 双指针\ 队列 栈 堆\ 动归\二分法\hashmap Partition 题—以quicksort 为模板--O(n) 出题点: 如何Patition ? 二分递归的部分 前向型指针 – O(n) 窗口类指针常用到的数据结构是 int sum, hashmap, hash
阅读全文
摘要:0. Trie数据结构,也就是前缀树。然后dfs时,如果当前形成的单词不在Trie里,就没必要继续dfs下去了。如果当前字符串在trie里,就说明board可以形成这个word。 这道题很好的体现了Trie的优势:不用Trie, 我们就得把String[] words里面的word一个一个去boar
阅读全文
摘要:键值对想到hashmap, 增删节点考虑链表, + 头尾节点, 因此要构造相应的类 双向链表需要头尾节点 The problem can be solved with a hashtable that keeps track of the keys and its values in the dou
阅读全文
摘要:为啥用栈, 数组相对顺序不能变, 要找第一个比当前元素小的元素, 或大的元素, 同84. Largest Rectangle in Histogram 同84. Largest Rectangle in Histogram 在数组后面加"0", 这些操作都是为了遍历所有的元素, 有的不需要遍历所有的
阅读全文
摘要:遍历, 没用到bst的性质: 递归
阅读全文
摘要:Iterative做法 分治法: Recursion做法:
阅读全文
摘要:用bst的性质遍历就行了: Just walk down from the whole tree's root as long as both p and q are in the same subtree (meaning their values are both smaller or both
阅读全文
摘要:分治法, 自己画个简图(三个, 两个节点), 遍历一下 会写树的高度, 在树的高度上加了个判断而已.别忘了左子树和右子树也是要查的 分治法主要在如何设计返回值, 和题意与返回值的转化, 递归出口1(判空), 最后的节点的出口2(或许加判断等), 分, 合: 将分好的左右节点作为单个节点, 进行题意的
阅读全文
摘要:分治法的基本思想:将一个规模为n的问题分解为k个规模较小的子问题,这些子问题互相独立且与原问题相同。递归地解这些问题,然后将各个子问题的解合并成原问题的解。 分治法所能解决的问题一般具有以下几个特征: 该问题的规模缩小到一定的程度就可以容易地解决;因为问题的计算复杂性一般是随着问题规模的增加而增加,
阅读全文
摘要:public class MyNode { TreeNode node; int start; int end; public MyNode(TreeNode node, int start, int end) { this.node = node; this.start = start; this
阅读全文
摘要:递归出口1, 出口2(操作, 遍历(dfs), 操作), 操作, 遍历(dfs), 操作
阅读全文
摘要:从任意一点dfs, 和dfs return 当前, 左节点, 右节点的模板 The basic idea is to subtract the value of current node from sum until the subtraction equals 0, then we know th
阅读全文
摘要:感悟: 1. 什么时候用排列组合问题? 看看结果有ArrayList? 所求的容器有多个结果值-且各个结果值之间只是顺序,或长度不同,但是来源(存在有显性隐性输入值)都是一样的-- 正是排列组合的根本. 数组, 字符串 2. 关键点: 递归函数里面加入容器, 结果值--操作的元素---不断地操作结果中的值, 输入值, (判断访问过没有—看是否要求重复、位置计数器)。 3. ...
阅读全文
摘要:容器此处还可以用 hashMap, 其中set既可以表示里面的边又可以表示入度数, 但是此处只需要知道入度数就可以了. 之前还有用 int [] ArrayList[] 来表示的同set, 因为坐标可以表示map中的键, 容器内元素可以表示值.
阅读全文
摘要:copy 的题多用hashmap, 难点在于如何让遍历, 如何构建新的节点间的关系. An intuitive solution is to keep a hash table for each node in the list, via which we just need to iterate
阅读全文
摘要:矩阵的bfs, 套路一致,从外向内, 很easy 构造类, 遍历矩阵建立图 bfs要点在于如何建图, 是否建类, 建比较器, 建方向容器, 建走过的路的存储器(数组, 或者set, list) 如何遍历(堆不空?), 遍历到内部的点时判断是否符合题意(边界, 走过), 再考虑题意, 判断当前的点是否
阅读全文
摘要:感悟: 遍历图上的点先想到宽度优先搜索, 有很多模板 5.9: 图、节点 adj.get(edge[0]).add(edge[1]); adj.get(edge[1]).add(edge[0]); }), set的size==1当作叶节点的判断标准.加入节点存储器list中, 用while-loop
阅读全文
摘要:思路 先修课程是拓扑排序的经典应用, 这里相当于找有向图是否有环, 如果有环的话拓扑排序能遍历到的节点将少于图的节点. 这里我们建立一个图, 用一个数组记录每个节点的入度. 对图进行拓扑排序 复杂度 时间O(V+E) 空间 O(V) 有向图: 入度和边, 用什么容器, 怎么生成图, 根据什么入队,
阅读全文
摘要:难度:87,这道题跟 Rotate Image 很相似,都是需要把矩阵分割成层来处理,每一层都是按:1. 正上方;2. 正右方;3. 正下方;4. 正左方这种顺序添加元素到结果集合。实现中要注意细节,when I traverse left or up I have to check whether
阅读全文
摘要:这道题是一道典型的位操作Bit Manipulation的题目,我开始以为异或值最大的两个数一定包括数组的最大值,但是OJ给了另一个例子{10,23,20,18,28},这个数组的异或最大值是10和20异或,得到30。那么只能另辟蹊径,正确的做法是按位遍历,题目中给定了数字的返回不会超过231,那么
阅读全文
摘要:这道题让我们判断一个数是否为2的次方数,而且要求时间和空间复杂度都为常数,那么对于这种玩数字的题,我们应该首先考虑位操作 Bit Operation。在LeetCode中,位操作的题有很多,比如比如Repeated DNA Sequences 求重复的DNA序列, Single Number 单独的
阅读全文
摘要:Map.Entry<> entry : map.entrySet()
阅读全文
摘要:Let's say nums is [10,11,...,19]. Then after nth_element and ordinary partitioning, we might have this (15 is my median): I rewire it so that the firs
阅读全文
摘要:存在正负情况,处理方式是按正数处理,符号最后在判断,那么我们需要把除数和被除数取绝对值,那么问题就来了:由于整型数INT的取值范围是-2147483648~2147483647,而对-2147483648取绝对值就会超出范围,所以我们需要先转为long long型再取绝对值。那么怎么样找循环呢,肯定
阅读全文
摘要:Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to
阅读全文
摘要:道题给定我们一个有序数组,让我们总结区间,具体来说就是让我们找出连续的序列,然后首尾两个数字之间用个“->"来连接,那么我只需遍历一遍数组即可,每次检查下一个数是不是递增的,如果是,则继续往下遍历,如果不是了,我们还要判断此时是一个数还是一个序列,一个数直接存入结果,序列的话要存入首尾数字和箭头“-
阅读全文
摘要:Graph, DFS (1) Build the map, the key is dividend, the value is also a map whose key is divisor and value is its parameter. For example, a / b = 2.0,
阅读全文
摘要:这道题给我们一个二维数组,让我们求矩阵中最长的递增路径,规定我们只能上下左右行走,不能走斜线或者是超过了边界。那么这道题的解法要用递归和DP来解,用DP的原因是为了提高效率,避免重复运算。我们需要维护一个二维动态数组dp,其中dp[i][j]表示数组中以(i,j)为起点的最长递增路径的长度,初始将d
阅读全文
摘要:carry sum 处理字符串的运算问题, 在转化成数值: (int) (num1.charAt(i) - '0') 转换成数值(同 Character.getNumericValue(char a) ), 非ASCII 码. 再 转化成字符. 也常转化成AScII 码, 在转换成字符串, 所以看看
阅读全文
摘要:Example: 矩阵涉及到计算的问题常常根据题意, 目的找规律
阅读全文
摘要:这道题还是蛮有创意的一道题,是说自然数序列看成一个长字符串,问我们第N位上的数字是什么。那么这道题的关键就是要找出第N位所在的数字,然后可以把数字转为字符串,这样直接可以访问任何一位。那么我们首先来分析自然数序列和其位数的关系,前九个数都是1位的,然后10到99总共90个数字都是两位的,100到99
阅读全文
摘要:A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59). Each LED represents
阅读全文
摘要:Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover of a rectangular region. Each rectangle is represented
阅读全文
摘要:refer to: https://discuss.leetcode.com/topic/60394/easy-concept-with-python-c-java-solution E.g.input: [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]subar
阅读全文
摘要:编解码法 复杂度 时间 O(NN) 空间 O(1) 思路 最简单的方法是再建一个矩阵保存,不过当inplace解时,如果我们直接根据每个点周围的存活数量来修改当前值,由于矩阵是顺序遍历的,这样会影响到下一个点的计算。如何在修改值的同时又保证下一个点的计算不会被影响呢?实际上我们只要将值稍作编码就行了
阅读全文
摘要:我的做法, hashMap, O(n) space, O(n) time: 用ascii 码表, 时间, 空间都是O(1) 学会转化: (int) s.charAt(i)
阅读全文
摘要:316. Remove Duplicate Letters321. Create Maximum Number402. Remove K Digits这三道题都用到了 stack 来求原序列中不打乱相对次序的最小子序列的技巧:
阅读全文
摘要:http://blog.csdn.net/u014688145/article/details/72859739
阅读全文
摘要:Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The
阅读全文
摘要:You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1'
阅读全文
摘要:Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm th
阅读全文
摘要:Given a nested list of integers represented as a string, implement a parser to deserialize it. Each element is either an integer, or a list -- whose e
阅读全文
摘要:Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. to
阅读全文
摘要:https://leetcode.com/problems/evaluate-reverse-polish-notation/#/description RPN中文名字叫做逆波兰表示法,它的好处维基百科说了,就是不需要括号来表示运算的先后,直接根据式子本身就可以求解。解题思路就是维护一个栈,遇到数字
阅读全文
摘要:这两题有一个 trick 和 Minimum Window Substring 非常像,就是维护一个 "curCount" 代表目前 (i,j) 之间 match 上的数量,而通过 hash[] 的正负充当计数器的作用 参照 k characters: http://www.cnblogs.com/
阅读全文
摘要:https://leetcode.com/problems/linked-list-cycle-ii/#/description
阅读全文
摘要:ou are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concat
阅读全文
摘要:Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here ak-diff pair is defined as an integ
阅读全文
摘要:https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/#/solutions Given a string and a string dictionary, find the longest string
阅读全文
摘要:https://leetcode.com/problems/reverse-vowels-of-a-string/#/description http://www.cnblogs.com/EdwardLiu/p/6096319.html
阅读全文
摘要:https://leetcode.com/problems/trapping-rain-water/#/solutions Fb: O(1)space 这种方法是基于动态规划的,基本思路就是维护一个长度为n的数组,进行两次扫描,一次从左往右,一次从右往左。第一次扫描的时候维护对于每一个bar左边最大
阅读全文
摘要:https://leetcode.com/problems/partition-list/#/description http://www.cnblogs.com/EdwardLiu/p/3807137.html Given a linked list and a value x, partitio
阅读全文
摘要:https://leetcode.com/problems/remove-element/#/description Given an array and a value, remove all instances of that value in place and return the new
阅读全文
摘要:https://leetcode.com/problemset/all/?search=19 涉及链表删除操作的时候,稳妥起见都用 dummynode,省去很多麻烦。因为不一定什么时候 head 就被删了。 快慢指针
阅读全文
摘要:https://leetcode.com/problems/4sum/#/solutions Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target
阅读全文
摘要:Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers.
阅读全文
摘要:为何不像76. Minimum Window Substring用数组来匹配, 是因为双指针是根据字符串的顺序遍历的, 而数组的话没有顺序.如 For example,S = "ADOBECODEBANC"T = "ABC" Minimum window is "BANC". 并不是非得ABC的顺序
阅读全文

浙公网安备 33010602011771号