牛客网(leetcode)
文章平均质量分 62
tangwei930927
希望跟大牛们学习知识
展开
-
leetcode第36题(distinct-subsequences)
题目:Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (ca原创 2018-02-08 16:18:04 · 144 阅读 · 0 评论 -
leetcode第46题(binary-tree-zigzag-level-order-traversal)
题目:Given a binary tree, return the zigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Give原创 2018-02-09 16:00:15 · 149 阅读 · 0 评论 -
leetcode第45题(maximum-depth-of-binary-tree)
题目:Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 思路:递归的计算左子树和右子树的深度,然后返回原创 2018-02-09 15:42:09 · 267 阅读 · 0 评论 -
leetcode第44题(construct-binary-tree-from-preorder-and-inorder-……)
题目:Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree.思路:前序遍历是根左右,中序遍历是左根右,第一个节点是跟,中序中根的左边为左子树,根原创 2018-02-09 15:29:08 · 182 阅读 · 0 评论 -
leetcode第51题(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 contains only nodes with keys less than the原创 2018-02-09 21:01:49 · 287 阅读 · 0 评论 -
leetcode第50题(recover-binary-search-tree)
题目:Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n ) space is pretty straight forward. Coul原创 2018-02-09 20:46:59 · 264 阅读 · 0 评论 -
leetcode第43题(binary-tree-level-order-traversal)
题目:Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 思路:中序遍历的顺序:左根右,后序遍历的顺序:左右根。因而我们可以找到最后一原创 2018-02-09 14:15:13 · 160 阅读 · 0 评论 -
leetcode第42题(binary-tree-level-order-traversal-ii)
题目:Given a binary tree, return the bottom-up level ordertraversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree{3,9,20,#,#原创 2018-02-09 14:11:37 · 156 阅读 · 0 评论 -
leetcode第41题(convert-sorted-array-to-binary-search-tree)
题目:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路:跟上题的思路一样,但这题更简单,因为数组可以随便取到中间的值,不需要通过快慢指针。代码:/** * Definition for binary tree * pu原创 2018-02-08 21:04:49 · 152 阅读 · 0 评论 -
leetcode第40题(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.思路:链接:https://www.nowcoder.net/questionTerminal/86343165c18a4069ab0ab30c32b1afd0原创 2018-02-08 20:49:30 · 226 阅读 · 0 评论 -
leetcode第47题(binary-tree-level-order-traversal)
题目:Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \原创 2018-02-09 16:15:00 · 246 阅读 · 0 评论 -
leetcode第48题(symmetric-tree)
题目:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3原创 2018-02-09 16:33:37 · 197 阅读 · 0 评论 -
leetcode第34.35题(populating-next-right-pointers-in-each-node)
题目:Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note:You may only原创 2018-02-08 15:37:22 · 158 阅读 · 0 评论 -
leetcode第33题(pascals-triangle)
题目:Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,思路:下一行数据是上一行两个相邻数相加。代码:til.ArrayList;public class Solution { public Arra原创 2018-02-07 21:16:41 · 211 阅读 · 0 评论 -
leetcode第32题(pascals-triangle-ii)
题目:Given an index k, return the k th row of the Pascal's triangle. For example, given k = 3,Return[1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space?原创 2018-02-07 21:13:40 · 176 阅读 · 0 评论 -
leetcode第31题(triangle)
题目:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4原创 2018-02-07 20:18:02 · 188 阅读 · 0 评论 -
leetcode第30题(best-time-to-buy-and-sell-stock)
题目:Say you have an array for which the i th element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of原创 2018-02-06 16:55:32 · 181 阅读 · 0 评论 -
leetcode第29题(best-time-to-buy-and-sell-stock-ii)
题目:Say you have an array for which the i th element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i原创 2018-02-06 16:52:59 · 257 阅读 · 0 评论 -
leetcode第28题(best-time-to-buy-and-sell-stock-iii)
题目:Say you have an array for which the i th element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note:原创 2018-02-06 16:50:29 · 345 阅读 · 0 评论 -
leetcode第27题(binary-tree-maximum-path-sum)
题目:Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1 / \ 2 3原创 2018-02-05 21:13:41 · 113 阅读 · 0 评论 -
leetcode第26题(valid-palindrome)
题目:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama"is a palindrome."race a car"is原创 2018-02-05 15:34:48 · 170 阅读 · 0 评论 -
leetcode第49题(same-tree)
题目:Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value.原创 2018-02-09 16:41:17 · 177 阅读 · 0 评论 -
leetcode第39题(balanced-binary-tree)
题目:Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node n原创 2018-02-08 20:19:18 · 158 阅读 · 0 评论 -
leetcode第38题(path-sum)
题目:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree原创 2018-02-08 19:46:23 · 201 阅读 · 0 评论 -
leetcode第12题(word-break)
题目:Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens ="leetcode",dict =[原创 2018-01-14 18:52:37 · 175 阅读 · 0 评论 -
leetcode第11题(word-break-ii)
题目:Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, giv原创 2018-01-14 18:24:41 · 209 阅读 · 0 评论 -
leetcode第10题(linked-list-cycle)
题目:Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space?思路:跟上面题一样,只是不用求相遇点,第一次相遇就可以判断有环。代码:/** * Definition for singly-原创 2018-01-14 14:56:47 · 149 阅读 · 0 评论 -
leetcode第9题(linked-list-cycle-ii)
题目:Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follow up:Can you solve it without using extra space?思路:跟之前剑指offer的题一样。二次相遇的点就是环的入口代原创 2018-01-14 14:50:33 · 129 阅读 · 0 评论 -
leetcode第8题(reorder-list)
题目:Given a singly linked list L: L 0→L 1→…→L n-1→L n,reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→… You must do this in-place without altering the nodes' values. For example,Given{1,2,3原创 2018-01-14 14:27:08 · 283 阅读 · 0 评论 -
leetcode第7题(binary-tree-preorder-traversal)
题目:Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,2,3]. Note: Recursiv原创 2018-01-12 20:40:56 · 132 阅读 · 0 评论 -
leetcode第6题(binary-tree-postorder-traversal)
题目:Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[3,2,1]. Note: Recursi原创 2018-01-12 20:27:03 · 214 阅读 · 0 评论 -
leetcode第4题(sort-list)
题目:Sort a linked list in O(n log n) time using constant space complexity. 思路:题目要求复杂度为O(nlogn),故可以考虑归并排序的思想。 归并排序的一般步骤为: 1)将待排序数组(链表)取中点并一分为二; 2)递归地对左半部分进行归并排序; 3)递归地对右半部分进行原创 2018-01-12 18:38:07 · 288 阅读 · 0 评论 -
leetcode第3题(max-points-on-a-line)
题目:Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.思路:需要两重循坏。第一重是循环遍历初始结点,第二重是循环遍历剩余结点。对于每个结点啊,如果b重合a,则以a为初始的节点的所有直线的点数加1,如果两个节点不重合,则确定一条直线,则a与b确原创 2018-01-12 17:43:54 · 181 阅读 · 0 评论 -
leetcode第2题(evaluate-reverse-polish-notation)
題目:Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are+,-,*,/. Each operand may be an integer or another expression. Some examples: ["2", "1"原创 2018-01-12 15:39:22 · 167 阅读 · 0 评论 -
leetcode第13题(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. Return a deep copy of the list. 思路:跟剑指offer25道题一样代码:原创 2018-01-14 19:34:54 · 111 阅读 · 0 评论 -
leetcode第14题(single-number-ii)
题目:Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it wi原创 2018-01-14 20:21:17 · 202 阅读 · 0 评论 -
leetcode第37题(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. For example:Given the below binary tree andsum = 22, 5原创 2018-02-08 19:44:13 · 172 阅读 · 0 评论 -
leetcode第23题(longest-consecutive-sequence)
题目:Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given[100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is原创 2018-01-19 16:59:15 · 207 阅读 · 0 评论 -
leetcode第22题(sum-root-to-leaf-numbers)
题目:Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. An example is the root-to-leaf path1->2->3which represents the number123. Find the原创 2018-01-19 15:27:36 · 173 阅读 · 0 评论 -
surrounded-regions
题目:Given a 2D board containing'X'and'O', capture all regions surrounded by'X'. A region is captured by flipping all'O's into'X's in that surrounded region . For example, X X X XX O O原创 2018-01-19 15:03:17 · 218 阅读 · 0 评论