LeetCode
文章平均质量分 96
Leetcode题解法,C++
努力的小陶
这个作者很懒,什么都没留下…
展开
-
[Leetcode] 二叉树的遍历(#94,#144,#145 )
本篇主要总结下二叉树的前序,中序,后序三种遍历算法的递归和迭代C++实现。三种遍历的题目可见Leetcode#94,#144,#145 。1. 二叉树的结构和二叉树的建立(用于测试)#include#include#includeusing namespace std;struct TreeNode { int val; TreeNode *left; TreeNode *原创 2017-02-09 12:21:34 · 412 阅读 · 0 评论 -
[Leetcode] #101 Symmetric Tree
Discription:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \原创 2017-02-09 16:50:22 · 282 阅读 · 0 评论 -
[Leetcode] #104 Maximum Depth of Binary Tree
Discription: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.Solutions:1. DFS原创 2017-02-09 19:37:42 · 310 阅读 · 0 评论 -
[Leetcode] #102 Binary Tree Level Order Traversal
Discription: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],原创 2017-02-09 20:27:30 · 246 阅读 · 0 评论 -
[Leetcode] #108 Convert Sorted Array to Binary Search Tree
Discription:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.Solution:利用二分法将中间结点作为根节点,左半部分作为左子树,右半部分作为右子书,如此递归求解。TreeNode* sortedArrayToBST(ve原创 2017-02-09 21:40:20 · 226 阅读 · 0 评论 -
[Leetcode] #96 Unique Binary Search Trees
Discription: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原创 2017-02-10 10:32:58 · 214 阅读 · 0 评论 -
[Leetcode] #112#113 Path Sum I & II
Path Sum I: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 a...原创 2017-02-10 15:27:09 · 305 阅读 · 0 评论 -
[Leetcode] #111 Minimum Depth of Binary Tree
Discription:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.Solution:自己比较cuo的解法:原创 2017-02-10 17:56:45 · 234 阅读 · 0 评论 -
[Leetcode] #114 Flatten Binary Tree to Linked List
Discription:Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look l原创 2017-02-10 19:25:46 · 228 阅读 · 0 评论 -
[Leetcode] #109 Convert Sorted List to Binary Search Tree
Discription:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.Solution:我的解法:TreeNode* sortedListToBST(ListNode* head) { if (!head)原创 2017-02-10 20:45:54 · 231 阅读 · 0 评论 -
[Leetcode] #129 Sum Root to Leaf Numbers
Discription:Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find ...原创 2017-02-10 21:54:23 · 262 阅读 · 0 评论 -
[Leetcode] #160 Intersection of Two Linked Lists
Discription:Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2原创 2017-02-11 09:44:18 · 460 阅读 · 0 评论 -
[Leetcode] #203 Remove Linked List Elements
Discription: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 --> 5Solution原创 2017-02-11 10:24:54 · 242 阅读 · 0 评论 -
[Leetcode] #199 Binary Tree Right Side View
Discription:Given 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.For example:Given the following bi原创 2017-02-11 13:21:10 · 212 阅读 · 0 评论 -
[Leetcode] #147 Insertion Sort List
Discription:Sort a linked list using insertion sort.Solution:ListNode* insertionSortList(ListNode* head) { if (!head) return head; ListNode *head1 = new ListNode(-1), *head2 = head->next; h原创 2017-02-11 15:32:14 · 306 阅读 · 0 评论 -
[Leetcode] #153 Find Minimum in Rotated Sorted Array
DiscriptionSuppose 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 dup...原创 2017-02-11 16:23:37 · 252 阅读 · 0 评论 -
[Leetcode] #154 Find Minimum in Rotated Sorted Array II
DiscriptionFollow 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 rota...原创 2017-02-11 17:05:21 · 276 阅读 · 0 评论 -
[Leetcode] #105 Construct Binary Tree from Preorder and Inorder Traversal
Discription:Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.Solution:TreeNode* buildTree(vector&原创 2017-02-11 19:49:57 · 210 阅读 · 0 评论 -
[Leetcode] #141#142 Linked List Cycle I & II
Discription:Given a linked list, determine if it has a cycle in it.Solution:bool hasCycle(ListNode *head) { if (!head) return false; ListNode *pslow = head, *pfast = head; while (pfast && p...原创 2017-02-11 21:18:44 · 316 阅读 · 0 评论 -
[面试算法] 动态规划
LCS(最长公共子序列)int LCS(string s1, string s2){ int len1 = s1.size(), len2 = s2.size(); vector<vector<int>> lcs(len1 + 1, vector<int>(len2 + 1)); for (int i = 0; i <= len1; i++) l...原创 2017-04-12 15:29:32 · 343 阅读 · 0 评论 -
[面试算法] 01背包 & 完全背包
Discription小Ho现在手上有M张奖券,而奖品区有N件奖品,分别标号为1到N,其中第i件奖品需要need(i)张奖券进行兑换,同时也只能兑换一次,为了使得辛苦得到的奖券不白白浪费,小Ho给每件奖品都评了分,其中第i件奖品的评分值为value(i),表示他对这件奖品的喜好值。现在他想知道,凭借他手上的这些奖券,可以换到哪些奖品,使得这些奖品的喜好值之和能够最大。每组测试数据的第一行为两个正整...原创 2017-04-30 10:23:04 · 621 阅读 · 0 评论 -
[Leetcode] #61 Rotate List
DiscriptionGiven a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.Solu...原创 2017-02-12 08:56:17 · 250 阅读 · 0 评论 -
[Leetcode] #92#206 Reverse Linked List I & II
Discription:Reverse a singly linked list.Solution:ListNode* reverseList(ListNode* head) { ListNode *pre = NULL, *cur = head; while (cur){ ListNode *temp = cur->next; cur->next = pre; pre...原创 2017-02-12 09:05:08 · 245 阅读 · 0 评论 -
[Leetcode] #82#83 Remove Duplicates from Sorted List I & II
Discription:Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2-&g...原创 2017-02-12 09:12:23 · 250 阅读 · 0 评论 -
[Leetcode] #19 Remove Nth Node From End of List
Discription: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...原创 2017-02-12 09:19:49 · 215 阅读 · 0 评论 -
[Leetcode] #148 Sort List
Discription:Sort a linked list in O(n log n) time using constant space complexity.Solution:ListNode *merge(ListNode *head1, ListNode *head2) { ListNode *result = new ListNode(-1), *cur = result; ...原创 2017-02-12 09:23:42 · 228 阅读 · 0 评论 -
[Leetcode] #143 Reorder List
DiscriptionGiven a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it to {...原创 2017-02-12 10:39:54 · 295 阅读 · 0 评论 -
[Leetcode] #3 Longest Substring Without Repeating Characters
Discription: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 "b"...原创 2017-02-12 22:29:12 · 222 阅读 · 0 评论 -
[Leetcode] #5 Longest Palindromic Substring
DiscriptionGiven 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-13 18:34:51 · 184 阅读 · 0 评论 -
[Leetcode] #9 Palindrome Number
Discription:Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers be palindromes? (ie, -1)If you are thi原创 2017-02-14 09:12:38 · 208 阅读 · 0 评论 -
[Leetcode] #7 Reverse Integer
Discription:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Note:The input is assumed to be a 32-bit signed intege原创 2017-02-14 09:48:57 · 233 阅读 · 0 评论 -
[Leetcode] #11 Container With Most Water
Discription: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)原创 2017-02-14 10:29:52 · 205 阅读 · 0 评论 -
[Leetcode] #20 Valid Parentheses
Discription:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()原创 2017-02-14 12:05:06 · 194 阅读 · 0 评论 -
[Leetcode] #22 Generate Parentheses
DiscriptionGiven n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:[ "((()))", "(()())", "(())()", "(...原创 2017-02-14 13:04:55 · 243 阅读 · 0 评论 -
[Leetcode] #28 Implement strStr()
Discription:Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Solution:int strStr(string haystack, string needle原创 2017-02-14 15:06:06 · 231 阅读 · 0 评论 -
[Leetcode] #33 Search in Rotated Sorted Array
DiscriptionSuppose 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).You are given a target value to search. If fou...原创 2017-02-14 16:48:17 · 248 阅读 · 0 评论 -
[Leetcode] #34 Search for a Range
DiscriptionGiven an array of integers sorted in ascending order, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If t...原创 2017-02-14 18:41:05 · 289 阅读 · 0 评论 -
[Leetcode] #42 Trapping Rain Water
Discription:Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,原创 2017-02-14 19:48:14 · 318 阅读 · 0 评论 -
[Leetcode] #39#40#216 Combination Sum I & II & III
Discription: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 c...原创 2017-02-14 21:03:33 · 417 阅读 · 0 评论 -
[Leetcode] #78#90 Subsets I & II
Discription: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-14 21:38:21 · 274 阅读 · 0 评论