
leetcode
文章平均质量分 56
LchinaM
这个作者很懒,什么都没留下…
展开
-
binary tree traver
1. preorder travelclass Solution {public: vector preorderTraversal(TreeNode* root) { vector result; stack myStack; TreeNode * temp = root; while(!myStack.empty()原创 2017-09-12 15:51:55 · 389 阅读 · 1 评论 -
LeeCode 106. Construct Binary Tree from Inorder and Postorder 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.recursive answer:class Solution {public: TreeNode原创 2017-02-23 15:13:32 · 360 阅读 · 1 评论 -
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.recursive answer:递归方法:在preorder找到root节点,接着在inorder里找到ro原创 2017-02-23 14:33:33 · 408 阅读 · 0 评论 -
LeetCode 103. Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary原创 2017-02-23 13:44:11 · 299 阅读 · 1 评论 -
LeetCode 102. 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,null,null,15,7], 3 / \ 9 2原创 2017-02-23 13:12:56 · 259 阅读 · 1 评论 -
LeetCode 100. 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.answer:原创 2017-02-23 11:22:32 · 274 阅读 · 0 评论 -
LeetCode 98. 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 node's key.Th原创 2017-02-23 10:58:24 · 276 阅读 · 0 评论 -
LeetCode 5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example:Input: "babad"Output: "bab"Note: "aba" is also a valid answer.原创 2017-02-14 19:29:08 · 270 阅读 · 1 评论 -
LeetCode 3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is "原创 2017-02-14 16:32:23 · 256 阅读 · 2 评论 -
LeetCode 2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return i原创 2017-02-14 15:21:06 · 248 阅读 · 1 评论 -
LeetCode 96. Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \原创 2017-02-22 21:40:41 · 305 阅读 · 1 评论 -
LeetCode 94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree [1,null,2,3], 1 \ 2 / 3return [1,3,2].recursive solution:原创 2017-02-22 20:29:28 · 254 阅读 · 1 评论 -
LeetCode 93. Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given "25525511135",return ["255.255.11.135", "255.255.111.35"]. (Order原创 2017-02-22 19:45:36 · 235 阅读 · 1 评论 -
LeetCode 92. Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.Note:Given m, n satisfy the原创 2017-02-22 18:41:16 · 376 阅读 · 1 评论 -
LeetCode 91. Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total nu原创 2017-02-22 16:56:16 · 328 阅读 · 0 评论 -
LeetCode 1. two sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the sam原创 2017-02-13 22:22:50 · 239 阅读 · 0 评论 -
LeetCode 86. Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of原创 2017-02-21 21:48:51 · 304 阅读 · 1 评论 -
LeetCode 6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I原创 2017-02-15 12:22:29 · 326 阅读 · 1 评论 -
LeetCode 8. String to Integer (atoi)
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input ca原创 2017-02-15 14:16:20 · 236 阅读 · 1 评论 -
LeetCode 108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.answer:class Solution {public: TreeNode* sortedArrayToBST(vector& nums) { return myATB原创 2017-02-23 20:46:17 · 381 阅读 · 0 评论 -
139. Word Break
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may as原创 2017-09-11 21:16:19 · 362 阅读 · 1 评论 -
137. Single Number II
Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.Note:Your algorithm should have a linear runtime complexity. Coul原创 2017-09-11 18:53:57 · 321 阅读 · 1 评论 -
135. Candy
There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least on原创 2017-09-11 16:31:01 · 305 阅读 · 1 评论 -
134. Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to原创 2017-09-11 15:30:33 · 301 阅读 · 1 评论 -
131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = "aab",Return[ ["aa","b"], [原创 2017-09-10 16:29:25 · 306 阅读 · 1 评论 -
162. Find Peak Element
A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multiple peaks, in原创 2017-09-13 12:44:36 · 349 阅读 · 0 评论 -
154. Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Suppose an array sorted in ascending order is rotated原创 2017-09-13 12:22:32 · 396 阅读 · 1 评论 -
153. Find Minimum in Rotated Sorted Array
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.You may assume no原创 2017-09-13 11:24:18 · 416 阅读 · 1 评论 -
152. Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the larges原创 2017-09-13 11:08:19 · 418 阅读 · 1 评论 -
127. Word Ladder
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:Only one letter can be changed at a原创 2017-06-27 11:06:56 · 358 阅读 · 1 评论 -
LeetCode 120. 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], [原创 2017-02-24 20:18:30 · 266 阅读 · 1 评论 -
LeetCode 15. 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: The solution set must not contain原创 2017-02-15 20:18:28 · 187 阅读 · 1 评论 -
LeetCode 12. Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.answer:class Solution {public: string intToRoman(int num) { string th原创 2017-02-15 18:58:37 · 260 阅读 · 1 评论 -
LeetCode 11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Fin原创 2017-02-15 16:39:15 · 276 阅读 · 1 评论 -
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.For example:Given the below binary tree and sum = 22, 5 / \原创 2017-02-23 22:12:03 · 299 阅读 · 1 评论 -
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.answer:class Solution {public: TreeNode* sortedListToBST(ListNode* head) {原创 2017-02-23 21:22:39 · 440 阅读 · 1 评论 -
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.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->原创 2017-02-21 20:52:01 · 277 阅读 · 1 评论 -
LeetCode 81. Search in Rotated Sorted Array II
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Write a function to determine if a given target原创 2017-02-21 20:18:35 · 223 阅读 · 1 评论 -
LeetCode 78. Subsets
Given a set of distinct integers, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.For example,If nums = [1,2,3], a solution is:[ [3], [1],原创 2017-02-21 14:04:07 · 407 阅读 · 1 评论 -
LeetCode 39. Combination Sum
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen原创 2017-02-18 15:14:33 · 226 阅读 · 1 评论