![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LEETCODE
文章平均质量分 50
笨笨De蜗牛
愿自己像一只蜗牛那样一点一点往上爬,一点一点的不断积累,终有一天能过上自己想要的生活。
展开
-
【Leetcode】99. Recover Binary Search Tree( Morris 遍历)
题目地址:Recover Binary Search Tree题意是一个二分查找树种的两个元素位置调换,在不改变结构的情况下,恢复这个树。解法1;根据平衡二叉树的性质,中跟遍历得到的应该是一个递增序列。class Solution { public void recoverTree(TreeNode root) { List<Integer> vals...原创 2020-04-13 21:13:52 · 335 阅读 · 0 评论 -
【Leetcode】98. Validate Binary Search Tree
题目地址:98. Validate Binary Search Tree题意:判断一个二叉树是否是平衡二叉树。平衡二叉树的性质就是中序遍历得到的是一个递增的数组。很容易就写出下面方法:class Solution { public boolean isValidBST(TreeNode root) { if(root==null)return true; ...原创 2020-04-12 23:54:48 · 300 阅读 · 0 评论 -
【Leetcode】889. Construct Binary Tree from Preorder and Postorder Traversal
题目链接:889. Construct Binary Tree from Preorder and Postorder Traversal根据前序和后序建立一个二叉树,注意这种情况是不能唯一确定一棵树的,所以返回任意一个即可。class Solution {public: TreeNode* constructFromPrePost(vector<int>& pr...原创 2020-03-30 09:36:20 · 312 阅读 · 0 评论 -
【Leetcode】105. Construct Binary Tree from Preorder and Inorder Traversal
题目地址:105. Construct Binary Tree from Preorder and Inorder Traversal题意:根据前跟和中跟建立二叉树。.递归方法:class Solution {public: TreeNode* buildTree(vector<int>& preorder, vector<int>& ino...原创 2020-03-30 09:29:44 · 159 阅读 · 0 评论 -
【Leetcode】124. Binary Tree Maximum Path Sum
Given a non-empty binary tree, find the maximum path sum.For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connection...原创 2020-03-13 19:15:47 · 105 阅读 · 0 评论 -
九度OJ #1437 To Fill or Not to Fil
题目描述:With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Dif转载 2014-05-02 11:20:46 · 769 阅读 · 0 评论 -
112. Path Sum
112. Path SumMy SubmissionsQuestionTotal Accepted: 94794 Total Submissions: 305442 Difficulty: EasyGiven a binary tree and a sum, determine if the tree has a root-to-leaf path原创 2016-03-15 23:56:30 · 255 阅读 · 0 评论 -
225. Implement Stack using Queues
mplement the following operations of a stack using queues.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Return wheth原创 2016-03-16 13:33:30 · 255 阅读 · 0 评论 -
219. Contains Duplicate II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and jis at most k.一原创 2016-03-16 23:57:02 · 210 阅读 · 0 评论 -
19. Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the原创 2016-03-17 15:53:13 · 195 阅读 · 0 评论 -
338. Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.Example:For num =原创 2016-03-30 00:23:55 · 287 阅读 · 0 评论 -
260. Single Number III
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.For example:Given原创 2016-03-30 12:13:27 · 198 阅读 · 0 评论 -
238. Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O原创 2016-03-30 13:30:27 · 266 阅读 · 0 评论 -
20. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all va原创 2016-03-18 19:48:06 · 226 阅读 · 0 评论 -
58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is原创 2016-03-18 20:48:50 · 232 阅读 · 0 评论 -
299. Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint t原创 2016-03-18 21:34:11 · 248 阅读 · 0 评论 -
319. Bulb Switcher
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off原创 2016-03-31 11:51:39 · 207 阅读 · 0 评论 -
234. Palindrome Linked List(链表模板)
Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?思路1:遍历一边链表,把它放到数组或VECTOR里,转化为字符串的回文判断。但是时间复杂度和空间复杂度都是O(n)。class Solution原创 2016-03-23 15:33:07 · 283 阅读 · 0 评论 -
205. Isomorphic Strings
Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with anot原创 2016-03-17 15:27:57 · 235 阅读 · 0 评论 -
290. Word Pattern
Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.原创 2016-03-19 16:37:24 · 240 阅读 · 0 评论 -
144. 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].递归实现:/** * Defini原创 2016-03-31 18:53:18 · 400 阅读 · 0 评论 -
38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as原创 2016-03-20 13:15:48 · 239 阅读 · 0 评论 -
94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].递归:class Solution {原创 2016-04-01 15:25:09 · 247 阅读 · 0 评论 -
226. Invert Binary Tree
nvert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1Trivia:This problem was inspired by this original tweet by Max Howel原创 2016-03-09 16:51:46 · 251 阅读 · 0 评论 -
217. Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is原创 2016-03-09 22:47:03 · 203 阅读 · 0 评论 -
206. Reverse Linked List
Reverse a singly linked list.click to show more hints.Subscribe to see which companies asked this question//递归public class Solution { public ListNode reverseList(ListNode head)原创 2016-03-09 23:26:34 · 234 阅读 · 0 评论 -
203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5Credits:Special than原创 2016-03-21 20:45:44 · 270 阅读 · 0 评论 -
257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]二叉树的遍历,下面原创 2016-03-21 21:56:12 · 299 阅读 · 0 评论 -
318. Maximum Product of Word Lengths
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case le原创 2016-04-03 22:45:15 · 196 阅读 · 0 评论 -
14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.Subscribe to see which companies asked this question求最长公共子串。思路:可以把它想象成一个二维的字符数组,我们一列一列的遍历。如果一列有一个数和第原创 2016-03-22 23:38:06 · 287 阅读 · 0 评论 -
328. Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do it in原创 2016-03-11 21:39:20 · 207 阅读 · 0 评论 -
263. Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly sinc原创 2016-03-11 23:07:08 · 236 阅读 · 0 评论 -
67. Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".用大数加法的思想,按位相加。class Solution {public: string addBinary(string a, string b原创 2016-03-23 16:02:06 · 327 阅读 · 0 评论 -
28. Implement strStr()
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.题目就是 字符串匹配。我觉得这个题目主要是学习回顾了一下经典的KMP算法吧,这个算法感觉一段时间不看,就又忘了。。原创 2016-03-23 17:26:11 · 191 阅读 · 0 评论 -
21. Merge Two Sorted Lists
21. Merge Two Sorted ListsMy SubmissionsQuestionTotal Accepted: 114471 Total Submissions: 327808 Difficulty: EasyMerge two sorted linked lists and return it as a new list. The原创 2016-03-12 22:17:58 · 220 阅读 · 0 评论 -
232. Implement Queue using Stacks
Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.empty(原创 2016-03-12 23:05:30 · 232 阅读 · 0 评论 -
110. Balanced Binary Tree
110. Balanced Binary TreeMy SubmissionsQuestionTotal Accepted: 101551 Total Submissions: 301899 Difficulty: EasyGiven a binary tree, determine if it is height-balanced.For th原创 2016-03-13 11:00:21 · 271 阅读 · 0 评论 -
303. Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example:Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1sumRange(2, 5) -> -1sumRan原创 2016-03-23 19:38:00 · 214 阅读 · 0 评论 -
228. Summary Ranges
iven a sorted integer array without duplicates, return the summary of its ranges.For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].题意就是连续的一段数字为一个区域,即 nums[j]+1==nums[j+1]class Sol原创 2016-03-23 21:34:40 · 203 阅读 · 0 评论 -
198. House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house原创 2016-03-13 12:37:49 · 240 阅读 · 0 评论