leetcode
MissZhou要努力
不晓日月,不辩兰艾,终日碌碌,安与燕雀相随乎
展开
-
leetcode 127. Word Ladder[bfs] lintcode 120. 单词接龙
https://leetcode.com/problems/word-ladder/Given two words (beginWordandendWord), and a dictionary's word list, find the length of shortest transformation sequence frombeginWordtoendWord, such t...原创 2019-07-11 00:10:59 · 177 阅读 · 0 评论 -
leetcode322. Coin Change
辣鸡如我,居然现在完全背包还要想这么久https://leetcode.com/problems/coin-change/You are given coins of different denominations and a total amount of moneyamount. Write a function to compute the fewest number of coi...原创 2019-07-06 19:32:08 · 173 阅读 · 0 评论 -
leetcode 264. Ugly Number II
经典的找第几个丑数是啥大一小孩都会https://leetcode.com/problems/ugly-number-ii/Write a program to find then-th ugly number.Ugly numbers arepositive numberswhose prime factors only include2, 3, 5.Example...原创 2019-07-06 19:41:04 · 189 阅读 · 0 评论 -
377. Combination Sum IV
辣鸡题目也没说取模啊Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.Example:nums = [1, 2, 3]targe...原创 2019-07-06 22:35:18 · 150 阅读 · 0 评论 -
leetcode 82. Remove Duplicates from Sorted List II【删除链表重复字符】
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.Example 1:Input: 1->2->3->3->4->4->5Output: 1->...原创 2018-11-05 10:23:08 · 175 阅读 · 0 评论 -
leetcode 129. Sum Root to Leaf Numbers
https://leetcode.com/problems/sum-root-to-leaf-numbers/Medium71225FavoriteShareGiven a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example i...原创 2019-07-11 23:56:09 · 124 阅读 · 0 评论 -
leetcode 34. Find First and Last Position of Element in Sorted Array【裸二分】
https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/Given an array of integersnumssorted in ascending order, find the starting and ending position of a giventarg...原创 2019-07-16 23:09:15 · 139 阅读 · 0 评论 -
leetcode 307. Range Sum Query - Mutable【线段树,指针】
Given an integer arraynums, find the sum of the elements between indicesiandj(i≤j), inclusive.Theupdate(i, val)function modifiesnumsby updating the element at indexitoval.Example:...原创 2019-07-21 15:53:38 · 219 阅读 · 0 评论 -
leetcode 130. Surrounded Regions【并查集】
https://leetcode.com/problems/surrounded-regions/Given a 2D board containing'X'and'O'(the letter O), capture all regions surrounded by'X'.A region is captured by flipping all'O's into'X's i...原创 2019-07-21 19:06:24 · 244 阅读 · 0 评论 -
leetcode 47. Permutations II[排列]
https://leetcode.com/problems/permutations-ii/Given a collection of numbers that might contain duplicates, return all possible unique permutations.Example:Input: [1,1,2]Output:[ [1,1,2], ...原创 2019-07-14 12:30:43 · 164 阅读 · 0 评论 -
leetcode 904. Fruit Into Baskets[map维护滑动窗口]
https://leetcode.com/problems/fruit-into-baskets/In a row of trees, thei-th treeproducesfruit with typetree[i].Youstart at any treeof your choice, then repeatedly perform the following steps...原创 2019-07-14 11:02:55 · 179 阅读 · 0 评论 -
leetcode 97. Interleaving String
https://leetcode.com/problems/interleaving-string/Givens1,s2,s3, find whethers3is formed by the interleaving ofs1ands2.Example 1:Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"Outp...原创 2019-07-03 00:44:58 · 141 阅读 · 0 评论 -
leetcode 94. Binary Tree Inorder Traversal中序遍历
我特么要菜哭了这玩意怎么能卡这么久Example:Input: [1,null,2,3] 1 \ 2 / 3Output: [1,3,2]Follow up: Recursive solution is trivial, could you do it iteratively?/** * Definition for a bina...原创 2018-12-17 09:38:29 · 218 阅读 · 0 评论 -
leetcode 96. Unique Binary Search Trees【DP数二叉搜索树】
https://leetcode.com/problems/unique-binary-search-trees/Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n?Example:Input: 3Output: 5Explanation:Give...原创 2018-12-17 11:42:45 · 237 阅读 · 0 评论 -
leetcode 95. Unique Binary Search Trees II【求二叉搜索树的各种具体情况】
https://leetcode.com/problems/unique-binary-search-trees-ii/Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n.Example:Input: 3Output:[...原创 2018-12-18 12:37:51 · 193 阅读 · 2 评论 -
leetcode103. Binary Tree Zigzag Level Order Traversal【打印二叉树】
Given a binary tree, return thezigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary tre...原创 2019-03-07 00:34:05 · 162 阅读 · 0 评论 -
leetcode 109. Convert Sorted List to Binary Search Tree【给定有序数组构建二叉树】
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.For this problem, a height-balanced binary tree is defined as a binary tree in which the ...原创 2019-03-11 23:13:14 · 142 阅读 · 0 评论 -
93. Restore IP Addresses
https://leetcode.com/problems/restore-ip-addresses/貌似这个题是去年年底刷了,半天没写出来搁浅了?WA了一次 没考虑0000TLE了一次应该事先判断长度Given a string containing only digits, restore it by returning all possible valid IP addres...原创 2019-06-08 14:35:16 · 162 阅读 · 0 评论 -
leetcode 92. Reverse Linked List II
https://leetcode.com/problems/reverse-linked-list-ii/难度还好,和之前的反转链表没本质区别,把头尾拆出来即可关于stack存储,和汪童鞋中午吃饭讨论说起来 存listnode肯定更好一些Reverse a linked list from positionmton. Do it in one-pass.Note:1 ≤m...原创 2019-06-09 16:58:45 · 111 阅读 · 0 评论 -
leetcode 113. Path Sum II【回溯】
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.Note:A leaf is a node with no children.Example:Given the below binary tree andsum = 22,...原创 2019-06-10 23:52:01 · 226 阅读 · 0 评论 -
leetcode300. Longest Increasing Subsequence
https://leetcode.com/problems/longest-increasing-subsequence/Given an unsorted array of integers, find the length of longest increasing subsequence.Example:Input: [10,9,2,5,3,7,101,18]Output: ...原创 2019-07-07 21:38:16 · 142 阅读 · 0 评论 -
lintcode 178. 图是否是树【并查集】
昨晚迷迷糊糊写的忘记了树的条件是不成环,今天捡起来debug半天才发现最后比较的不应该是父节点相同 而应该是根结点相同其实最明智的做法是判断给出的边数,如果之前没成环,边数=n-1,那么就是树!class Solution {public: /** * @param n: An integer * @param edges: a list o...原创 2019-08-08 20:38:45 · 360 阅读 · 0 评论 -
leetcode 11. Container With Most Water& lintcode 363. 接雨水
愚蠢如我这两个其实还不太一样的和这个http://acm.hdu.edu.cn/showproblem.php?pid=1506更不一样(后者是单调栈)https://www.lintcode.com/problem/trapping-rain-water/description由于图是酱婶的,所以需要一位一位的算class Solution {public: ...原创 2019-08-18 12:58:08 · 199 阅读 · 0 评论 -
lintcode 88LCA
https://lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description描述中文English给定一棵二叉树,找到两个节点的最近公共父节点(LCA)。最近公共祖先是两个节点的公共的祖先节点且具有最大深度。假设给出的两个节点都在树中存在您在真实的面试中是否遇到过这个题?是题目...原创 2019-09-20 00:44:14 · 243 阅读 · 0 评论 -
lintcode 95. 验证二叉查找树【剑指offer】
描述中文English给定一个二叉树,判断它是否是合法的二叉查找树(BST)一棵BST定义为:节点的左子树中的值要严格小于该节点的值。 节点的右子树中的值要严格大于该节点的值。 左右子树也必须是二叉查找树。 一个节点的树也是二叉查找树。您在真实的面试中是否遇到过这个题?是题目纠错样例样例 1:输入:{-1}输出:true解释:二叉树如下(仅有一个...原创 2019-09-21 01:16:22 · 210 阅读 · 0 评论 -
lintcode86. 二叉查找树迭代器
https://www.lintcode.com/problem/binary-search-tree-iterator/description看着这个题无比眼熟之前亚麻暑假实习manager问过我然而 那会是有父指针的这个题思路是什么呢?就变成了纯正的中序遍历非递归版的而且写法也有要求不能是while循环在外面的是要while current的然后就出了...原创 2019-09-21 18:03:41 · 179 阅读 · 0 评论 -
lintcode 448. 二叉查找树的中序后继
本来以为 既然都是找后继 都是一样的结果 都!不!一!样!查找的时候怎么用val比较呢?考虑如果当前root的值比查找的 小于或等于:淘汰掉左子树就好要是需要找到左子树了,那么递归下去同样的方法,如果找不到,那么说明当前这个根节点是要找的class Solution {public: /* * @param root: The root of the B...原创 2019-09-21 20:59:15 · 245 阅读 · 0 评论 -
lintcode 11. 二叉查找树中搜索区间
中序遍历一遍就好啦/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this-&g...原创 2019-09-21 21:49:40 · 209 阅读 · 0 评论 -
lintcode 85. 在二叉查找树中插入节点
描述中文English给定一棵二叉查找树和一个新的树节点,将节点插入到树中。你需要保证该树仍然是一棵二叉查找树。保证不会出现重复的值您在真实的面试中是否遇到过这个题?是题目纠错样例样例 1: 输入: tree = {}, node= 1 输出: {1} 样例解释: 在空树中插入一个点,应该插入为根节点。 样例 2: 输入: tree...原创 2019-09-22 00:27:15 · 252 阅读 · 0 评论 -
lintcode87. 删除二叉查找树的节点 leetcode 450. Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.Basically, the deletion can be divided int...原创 2019-09-22 14:49:16 · 197 阅读 · 0 评论 -
lintcode 124. 最长连续序列[unordered_set]
https://www.lintcode.com/problem/longest-consecutive-sequence/description描述中文English给定一个未排序的整数数组,找出最长连续序列的长度。您在真实的面试中是否遇到过这个题?是题目纠错说明要求你的算法复杂度为O(n)样例样例 1输入 : [100, 4, 200, 1,...原创 2019-09-22 18:12:42 · 263 阅读 · 0 评论 -
lintcode DP 总结
107.Word BreakFollowhttps://www.lintcode.com/problem/word-break/descriptionDescription中文EnglishGiven a string s and a dictionary of words dict, determine if s can be break into a space-separa...原创 2019-09-28 17:57:29 · 243 阅读 · 0 评论 -
leetcode 116. Populating Next Right Pointers in Each Node & 117. Populating Next Right Pointers in E
You are given aperfect binary treewhereall leaves are on the same level, and every parent has two children. The binary tree has the following definition:struct Node { int val; Node *left; ...原创 2019-09-09 22:54:12 · 190 阅读 · 0 评论 -
leetcode 146. LRU Cache
https://leetcode.com/problems/lru-cache/submissions/Design and implement a data structure forLeast Recently Used (LRU) cache. It should support the following operations:getandput.get(key)- Ge...原创 2019-09-08 23:38:53 · 170 阅读 · 0 评论 -
leetcode 买卖股票【六题集合】
121.Best Time to Buy and Sell StockSay you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (i.e., buy...原创 2019-08-25 00:53:30 · 412 阅读 · 0 评论 -
lintcode 382. 三角形计数【双指针】
https://www.lintcode.com/problem/triangle-count/description描述中文English给定一个整数数组,在该数组中,寻找三个数,分别代表三角形三条边的长度,问,可以寻找到多少组这样的三个数来组成三角形?您在真实的面试中是否遇到过这个题?是题目纠错样例样例 1:输入: [3, 4, 6, 7]输出: 3解...原创 2019-08-25 19:59:16 · 294 阅读 · 0 评论 -
lintcode532. 逆序对/剑指offer【归并排序】
https://www.lintcode.com/problem/reverse-pairs/descriptionhttps://www.nowcoder.com/practice/96bd6684e04a44eb80e6a68efc0ec6c5?tpId=13&tqId=11188&tPage=1&rp=1&ru=/ta/coding-interviews&...原创 2019-08-25 23:44:07 · 226 阅读 · 0 评论 -
lintcode 81. 数据流中位数[heap]
描述中文English数字是不断进入数组的,在每次添加一个新的数进入数组的同时返回当前新数组的中位数。您在真实的面试中是否遇到过这个题?是题目纠错说明中位数的定义:这里的中位数不等同于数学定义里的中位数。 中位数是排序后数组的中间值,如果有数组中有n个数,则中位数为A[(n-1)/2]A[(n−1)/2]。 比如:数组A=[1,2,3]的中位数是2,数组A=[1...原创 2019-08-21 22:20:13 · 255 阅读 · 0 评论 -
lintcode 89. K数之和【背包】
89.K数之和中文English给定n个不同的正整数,整数k(k<=n)以及一个目标数字target。 在这n个数里面找出k个数,使得这k个数的和等于目标数字,求问有多少种方案?样例样例1输入:List = [1,2,3,4]k = 2target = 5输出: 2说明: 1 + 4 = 2 + 3 = 5样例2输入:...原创 2019-09-01 12:09:57 · 362 阅读 · 0 评论 -
leetcode138. Copy List with Random Pointer【剑指offer】
https://leetcode.com/problems/copy-list-with-random-pointer/A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Retur...原创 2019-09-03 00:09:40 · 241 阅读 · 0 评论