
coding
文章平均质量分 58
HUANG Zichen
人生若只如初见
展开
-
LeetCode-二叉树总结(三)
BST二叉查找树(BST):根节点大于等于左子树所有节点,小于等于右子树所有节点。二叉查找树中序遍历有序。修剪二叉查找树669. Trim a Binary Search Tree (Easy)Input: 3 / \ 0 4 \ 2 / 1 L = 1 R = 3Output: 3 / 2 ...原创 2018-11-27 17:11:58 · 334 阅读 · 0 评论 -
LeetCode-Lowest Common Ancestor of a Binary Tree
一、Description题目描述:给定一个二叉树和两个结点p和q,找出p和q的最近公共祖先。二、Analyzation通过遍历分别找到从根节点到p和q的路径,放入一个栈中。如果两个栈的大小相同,则同时出栈直到两个栈出栈的结点值相同,则为最近公共祖先,如果两个栈的大小不同(p和q不在同一层),则大的那个栈出栈直到和另一个栈的大小相同,然后一起出栈直到结点值相同。三、Accepted...原创 2018-11-04 19:08:23 · 190 阅读 · 0 评论 -
LeetCode-Shortest Bridge
一、Description题目描述:给定一个二维数组,所有值为1的相邻元素(上下左右相邻)构成一个岛,即相互连通。在给出的数组中存在两个岛,求使得这两个岛连通需要最少将多少个数字0变为1。二、Analyzation首先通过深度优先搜索将一个岛的所有结点加入到一个队列中,并全部标记为已访问,然后依次从队列中取出Point,再次遍历(向上下左右四个方向探索),一旦遇到数字1,则返回此时的c...原创 2018-11-04 17:41:28 · 400 阅读 · 0 评论 -
LeetCode-Serialize And Deserialize BST
一、Description题目描述:实现一个二叉树的序列化与反序列化。二、Analyzation 1 / \2 3 \ 4 序列化:通过递归将一个二叉树的前序序列转换成形如 1, 2, #, 4, #, #, 3, #, # 的字符串,可以方便地存储在文件中。反序列化:将上述字符串转换成一个字符串数组,然...原创 2018-11-02 19:08:45 · 166 阅读 · 0 评论 -
LeetCode-Binary Tree Right Side View
一、DescriptionGiven a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.题目大意:给定一个二叉树,返回一个list,里面存放的是从右边的视角看这棵树得到的...原创 2018-11-02 11:12:11 · 172 阅读 · 0 评论 -
LeetCode-栈和队列总结
栈和队列栈和队列是很重要的数据结构,栈是先进后出,队列是先进先出,可以用两个栈实现队列,也可以用一个队列实现栈,这些基本操作都应该掌握熟练。数组中元素与下一个比它大的元素之间的距离739. Daily Temperatures (Medium)Input: [73, 74, 75, 71, 69, 72, 76, 73]Output: [1, 1, 4, 2, 1, 1, 0, 0]...原创 2018-11-09 18:48:32 · 220 阅读 · 0 评论 -
LeetCode-Different Ways to Add Parentheses
一、Description题目描述:给一个字符串,任意加括号,输出所有可能的取值。Example 1:Input: "2-1-1"Output: [0, 2]Explanation: ((2-1)-1) = 0 (2-(1-1)) = 2Example 2:Input: "2*3-4*5"Output: [-34, -14, -10, -10, 10]Expla...原创 2018-11-06 18:31:50 · 189 阅读 · 0 评论 -
LeetCode-Single Element in a Sorted Array
一、Description题目描述:在一个有序数组中找出只出现一次的数字,要求时间复杂度为O(log n) 。Example 1:Input: [1,1,2,3,3,4,4,8,8]Output: 2Example 2:Input: [3,3,7,7,10,11,11]Output: 10二、Analyzation方法一:如果没有时间复杂度的要求,可以考虑...原创 2018-11-06 17:21:26 · 211 阅读 · 0 评论 -
LeetCode-All Nodes Distance K in Binary Tree
一、Description题目描述:给定一个二叉树,一个target结点,一个K,找出所有离target结点的距离为K的所有结点,返回一个list。Example 1:Input: root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, K = 2Output: [7,4,1]二、Analyzation从target结点开始...原创 2018-11-03 20:39:50 · 196 阅读 · 0 评论 -
LeetCode-House Robber III
一、Description题目描述:从根节点出发,返回一个最大的结点和,要求结点不能相邻。Example 1:Input: [3,2,3,null,3,null,1] 3 / \ 2 3 \ \ 3 1Output: 7 Explanation: Maximum amount of money the thief can...原创 2018-11-03 19:50:59 · 160 阅读 · 0 评论 -
LeetCode-Redundant Connection
一、Description题目描述:给定一个二维数组,分别代表一个无向图的一条边,从数组中去掉一条边,使得该图可以成为一棵树(无回路),如果有多个答案,返回数组中靠后的那条边。Example 1:Input: [[1,2], [1,3], [2,3]]Output: [2,3]Explanation: The given undirected graph will be like...原创 2018-11-01 18:13:01 · 179 阅读 · 0 评论 -
LeetCode-Binary Tree Preorder Traversal
一、DescriptionGiven a binary tree, return the preorder traversal of its nodes' values.题目大意:输出一个二叉树的前序遍历序列。Example:Input: [1,null,2,3] 1 \ 2 / 3Output: [1,2,3]二、Analyzati...原创 2018-11-01 16:15:22 · 197 阅读 · 0 评论 -
LeetCode-Maximum Width of Binary Tree
一、Description题目描述:给定一个二叉树,返回它的最大宽度。最大宽度是这么定义的:即一层中最右结点与最左结点之间的长度。Example 1:Input: 1 / \ 3 2 / \ \ 5 3 9 Output: 4Explanation: The...原创 2018-11-03 09:13:56 · 165 阅读 · 0 评论 -
LeetCode-Smallest Subtree With All The Deepest Nodes
一、DescriptionReturn the node with the largest depth such that it contains all the deepest nodes in its subtree.题目大意:给定一个二叉树,返回具有包含所有最深子结点的最近祖先结点以及其所有子结点。Example 1:Input: [3,5,1,6,2,0,8,null,n...原创 2018-11-01 10:52:17 · 193 阅读 · 0 评论 -
LeetCode-位运算总结
位运算1. 基本原理0s 表示一串 0,1s 表示一串 1。x ^ 0s = x x & 0s = 0 x | 0s = xx ^ 1s = ~x x & 1s = x x | 1s = 1sx ^ x = 0 x & x = x x | x = x利用 x ^ 1s = ~x 的特点,可以将位级...转载 2018-11-07 19:16:50 · 739 阅读 · 0 评论 -
LeetCode-数组与矩阵总结
数组与矩阵把数组中的 0 移到末尾283. Move Zeroes (Easy)For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].class Solution { public void moveZeroes(int[] ...原创 2018-11-13 10:17:32 · 957 阅读 · 0 评论 -
实现LRU算法的两种方法
一、LinkedHashMapLinkedHashMap 内部维护了一个双向链表,用来维护插入顺序或者 LRU(最近最久未使用)顺序。其中有一个属性——accessOrder,当一个节点被访问时,如果 accessOrder 为 true,则会将该节点移到链表尾部。也就是说指定为 LRU 顺序之后,在每次访问一个节点时,会将这个节点移到链表尾部,保证链表尾部是最近访问的节点,那么链表首部就...原创 2018-11-29 22:16:01 · 1163 阅读 · 0 评论 -
LeetCode-数学专题总结
数学素数素数分解每一个数都可以分解成素数的乘积,例如 84 = 22 * 31 * 50 * 71 * 110 * 130 * 170 * …整除令 x = 2m0 * 3m1 * 5m2 * 7m3 * 11m4 * …令 y = 2n0 * 3n1 * 5n2 * 7n3 * 11n4 * …如果 x 整除 y(y mod x == 0),则对于所有 i,mi <= ni。...原创 2018-12-02 15:12:26 · 1263 阅读 · 0 评论 -
LeetCode-搜索专题总结
搜索深度优先搜索和广度优先搜索广泛运用于树和图中,但是它们的应用远远不止如此。BFS广度优先搜索一层一层地进行遍历,每层遍历都以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点。需要注意的是,遍历过的节点不能再次被遍历。第一层:0 -&amp;gt; {6,2,1,5}第二层:6 -&amp;gt; {4}2 -&amp;gt; {}1 -&amp;gt; {}5 -&am原创 2018-12-04 11:23:41 · 510 阅读 · 2 评论 -
LeetCode-二叉树总结(二)
层次遍历使用BFS进行层次遍历。不需要使用两个队列来分别存储当前层的节点和下一层的节点,因为在开始遍历一层的节点时,当前队列中的节点数就是当前层的节点数,只要控制遍历这么多节点数,就能保证这次遍历的都是当前层的节点。一棵树每层节点的平均数637. Average of Levels in Binary Tree (Easy)class Solution { public List&amp;amp;...原创 2018-11-25 17:44:52 · 1012 阅读 · 2 评论 -
LeetCode-二叉树总结(一)
二叉树递归一棵树要么是空树,要么有两个指针,每个指针指向一棵树。树是一种递归结构,很多树的问题可以使用递归来处理。树的高度104. Maximum Depth of Binary Tree (Easy)class Solution { public int maxDepth(TreeNode root) { if (null == root) { ...原创 2018-11-23 11:03:38 · 293 阅读 · 0 评论 -
LeetCode-二分查找的变种总结
二分查找二分查找作为一种基础算法,在面试和笔试中也是经常遇到,然而这一算法在不同的情形中也有不同的表现形式,下面是一些二分查找算法的变种总结。(以下代码均已实现)时间复杂度:二分查找也称为折半查找,每次都能将查找区间减半,这种折半特性的算法时间复杂度为 O(logN)。mid的计算:有两种计算中值 m 的方式:m = (l + h) / 2 m = l + (h - l)...原创 2018-11-17 19:05:01 · 518 阅读 · 0 评论 -
LeetCode-动态规划总结(四)
股票交易需要冷却期的股票交易121.Best Time to Buy and Sell Stock题目描述:只有一次交易机会,找出使得利润最大的价值。class Solution { public int maxProfit(int[] prices) { if (null == prices || 0 == prices.length) { ...原创 2018-11-19 19:23:45 · 337 阅读 · 0 评论 -
LeetCode-动态规划总结(三)
最长公共子序列对于两个子序列 S1 和 S2,找出它们最长的公共子序列。定义一个二维数组 dp 用来存储最长公共子序列的长度,其中 dp[i][j] 表示 S1 的前 i 个字符与 S2 的前 j 个字符最长公共子序列的长度。考虑 S1i 与 S2j 值是否相等,分为两种情况:当 S1i==S2j 时,那么就能在 S1 的前 i-1 个字符与 S2 的前 j-1 个字符最长公共子序列的基础...原创 2018-11-18 18:45:36 · 330 阅读 · 0 评论 -
LeetCode-动态规划总结(二)
最长递增子序列已知一个序列 {S1, S2,…,Sn},取出若干数组成新的序列 {Si1, Si2,…, Sim},其中 i1、i2 … im 保持递增,即新序列中各个数仍然保持原数列中的先后顺序,称新序列为原序列的一个 子序列 。如果在子序列中,当下标 ix > iy 时,Six > Siy,称子序列为原序列的一个 递增子序列 。定义一个数组 dp 存储最长递增子序列的长度,d...原创 2018-11-16 18:15:00 · 275 阅读 · 0 评论 -
LeetCode-Find the Duplicate Number
一、Description题目描述:给定n + 1个数字,这些数字的范围在1 - n之间,在这些数字中有一个出现不止一次的数字,找出这个数字。要求用O(1)的空间和少于O(n^2)的时间复杂度。二、Analyzation遍历这个数组,对于每个数字x,nums[x - 1]上的数字如果为正,则令其为相反数;如果nums[x - 1]上的数为负,则表示前面的数字中已经出现过当前数字...原创 2018-11-12 18:36:41 · 202 阅读 · 0 评论 -
LeetCode-动态规划总结(一)
动态规划递归和动态规划都是将原问题拆成多个子问题然后求解,他们之间最本质的区别是,动态规划保存了子问题的解,避免重复计算。斐波那契数列爬楼梯70. Climbing Stairs (Easy)题目描述:有 N 阶楼梯,每次可以上一阶或者两阶,求有多少种上楼梯的方法。定义一个数组 dp 存储上楼梯的方法数(为了方便讨论,数组下标从 1 开始),dp[i] 表示走到第 i 个楼梯的方法数目...原创 2018-11-15 20:52:28 · 543 阅读 · 0 评论 -
LeetCode-链表总结
链表链表在面试中手写代码的几率很大,也是十分重要的一种数据结构,本身并不复杂,但通过各种形式的题型可以看出一个人思路的严谨性。找出两个链表的交点160. Intersection of Two Linked Lists (Easy)A: a1 → a2 ↘ c1 → c2 → c3 ...原创 2018-11-08 19:10:52 · 227 阅读 · 0 评论 -
LeetCode-Minimum Area Rectangle
一、Description题目描述:给定一些点集,找出由这些组成的面积最小的矩形,矩形的长和宽分别平行于x、y轴。如果不存在这样的矩形,返回0。Example 1:Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]Output: 4Example 2:Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]...原创 2018-11-11 16:20:32 · 224 阅读 · 0 评论 -
LeetCode-Construct Binary Tree From Inorder And Postorder Traversal
一、DescriptionGiven inorder and postorder traversal of a tree, construct the binary tree.题目大意:给定一个二叉树的中序与后序遍历序列,构建这棵树并返回根结点。For example, giveninorder = [9,3,15,20,7]postorder = [9,15,7,20,3]...原创 2018-10-30 16:50:42 · 211 阅读 · 0 评论 -
LeetCode-Construct Binary Tree From Preorder And Postorder Traversal
一、DescriptionReturn any binary tree that matches the given preorder and postorder traversals.Values in the traversals pre and post are distinct positive integers.题目大意:给定二叉树的前序遍历序列和后序遍历序列,返回一个满足这...原创 2018-10-30 16:27:01 · 184 阅读 · 0 评论 -
LeetCode-Letter Combinations Of A Phone Number
一、Description给定一个字符串,包含从2 - 9位数,返回所有可能代表的字母组合。一个数字映射的字母的可能(就像在电话里按钮)如下所示。请注意,1不映射到任何字母。Example:Input: "23"Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce&原创 2018-10-20 09:14:59 · 216 阅读 · 0 评论 -
字典树-敏感词过滤
一、字典树在网站开发中,通常会对用户发布的问题、文章等内容进行文字过滤操作,倘若用传统的字符比较筛选或者KMP算法,显然时间效率上远远达不到要求,因此引入字典树(前缀树),即现将要屏蔽的敏感词通过字典树的形式存储起来,再通过对用户发布的内容进行搜索遍历打码等操作,将处理后的内容发布在网页上。字典树的结构如下:private class TireNode { private...原创 2018-10-09 22:04:38 · 1173 阅读 · 1 评论 -
LeetCode-Find All Duplicates in an Array
一、DescriptionGiven an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements that appear twice in this array.Could you do it...原创 2018-10-05 10:00:53 · 172 阅读 · 0 评论 -
LeetCode-Sqrt(x)
一、DescriptionImplement int sqrt(int x).Compute and return the square root of x, where x is guaranteed to be a non-negative integer.Since the return type is an integer, the decimal digits are tru...原创 2018-09-21 18:17:41 · 169 阅读 · 0 评论 -
LeetCode-Reverse Bits
一、DescriptionReverse bits of a given 32 bits unsigned integer.Example:Input:43261596Output:964176192Explanation:43261596 represented in binary as 00000010100101000001111010011100,return 9641...原创 2018-09-26 17:15:01 · 227 阅读 · 0 评论 -
LeetCode-Implement strStr()
一、DescriptionImplement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.题目大意:实现一个函数,找出一个字符串中第一次出现给定子串的位置,如果没有返回-1。Example:I...原创 2018-09-24 16:05:00 · 155 阅读 · 0 评论 -
LeetCode-Single Number
一、DescriptionGiven a non-empty 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 implemen...原创 2018-09-23 19:59:43 · 183 阅读 · 0 评论 -
LeetCode-Longest Palindromic Substring
一、DescriptionGiven a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.题目大意:给定一个字符串s,求其中最长的一个回文串。回文串即正着看反着看都是一样的字符串,比如"abcba"、"aaa"...二...原创 2018-09-23 14:15:37 · 161 阅读 · 0 评论 -
背包问题-HDU-3127 WHUgirls
WHUgirlsTime Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2557 Accepted Submission(s): 952Problem DescriptionThere are many pretty girl...原创 2016-01-23 08:56:09 · 390 阅读 · 0 评论