
algorithm
文章平均质量分 71
dyllanzhou
这个作者很懒,什么都没留下…
展开
-
[Leetcode]Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "BANC".原创 2015-09-02 22:34:30 · 264 阅读 · 0 评论 -
[Leetcode]Encode and Decode Strings
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings.Machine 1 (sender) has the functio原创 2015-09-11 13:49:16 · 730 阅读 · 0 评论 -
[Leetcode]Closest Binary Search Tree Value II
Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target.Note:Given target value is a floating point.You may assume k is always valid, t原创 2015-09-12 10:27:59 · 452 阅读 · 0 评论 -
[Leetcode] Course Schedule II
There are a total of n courses you have to take, labeled from 0 ton - 1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a p原创 2015-09-06 15:44:11 · 167 阅读 · 0 评论 -
[Leetcode]Basic Calculator
Implement a basic calculator to evaluate a simple expression string.The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty原创 2015-08-21 23:24:10 · 304 阅读 · 0 评论 -
[Leetcode]Majority Element II
Given an integer array of size n, find all elements that appear more than⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.class Solution {public: /*alorithm: hash solu原创 2015-08-21 10:58:16 · 168 阅读 · 0 评论 -
[Leetcode]Closest Binary Search Tree Value
Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.Note:Given target value is a floating point.You are guaranteed to have only one原创 2015-09-12 10:31:57 · 414 阅读 · 0 评论 -
[Leetcode]Missing Ranges
Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its missing ranges.For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "原创 2015-09-07 16:20:18 · 263 阅读 · 0 评论 -
[Leetcode]Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next() and原创 2015-03-15 17:24:40 · 246 阅读 · 0 评论 -
[Leetcode]Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.Write原创 2015-03-15 11:38:45 · 279 阅读 · 0 评论 -
[Leetcode]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"]algorithm: dfs /*原创 2015-08-17 12:38:45 · 357 阅读 · 0 评论 -
[Leetcode]Course Schedule
There are a total of n courses you have to take, labeled from 0 ton - 1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a p原创 2015-09-06 14:46:28 · 311 阅读 · 0 评论 -
Topological sorting
algorithmKahn algorithmL ← Empty list that will contain the sorted elementsS ← Set of all nodes with no incoming edgeswhile S is non-empty do remove a node n from S add n to tail of L转载 2015-09-06 13:00:49 · 374 阅读 · 0 评论 -
[Leetocde]Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betwee原创 2015-08-06 15:37:13 · 305 阅读 · 0 评论 -
[Leetcode]Implement Queue using Stacks
Implement Queue using StacksImplement 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()原创 2015-08-07 12:30:35 · 249 阅读 · 0 评论 -
[Leetcode]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?/** * Definition for singly-linked list. * struct ListNode { * int va原创 2015-08-06 16:56:22 · 249 阅读 · 0 评论 -
[Leetcode]Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead.For example, given the array [2,3,1原创 2015-09-01 15:59:02 · 340 阅读 · 0 评论 -
[Leetcode]Summary Ranges
Given 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"].class Solution {public: string itos(int num){原创 2015-08-07 13:12:03 · 405 阅读 · 0 评论 -
[Leetcode]Rotate Array
Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to[5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you ca原创 2015-08-08 20:59:40 · 228 阅读 · 0 评论 -
[Leetcode] Product of Array Except Self
Given an array of n integers where n > 1, nums, return an arrayoutput such that output[i] is equal to the product of all the elements ofnums except nums[i].Solve it without division and in O(n).原创 2015-08-19 11:34:22 · 235 阅读 · 0 评论 -
[Leetcode]Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.If the fractional part is repeating, enclose the repeating part in parentheses.For原创 2015-09-10 11:08:30 · 289 阅读 · 0 评论 -
[Leetcode]Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes原创 2015-08-20 12:54:58 · 249 阅读 · 0 评论 -
[Leetcode]Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right whichminimizes the sum of all numbers along its path.Note: You can only move either down or right at原创 2015-09-25 17:21:19 · 239 阅读 · 0 评论 -
[Leetcode]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 that原创 2015-10-01 22:04:38 · 260 阅读 · 0 评论 -
[Leetcode]Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume原创 2015-09-01 12:38:40 · 292 阅读 · 0 评论 -
[Leetcode]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 a binary tree node. * struct TreeNode { * int val; * TreeNode *left;原创 2015-09-22 19:18:30 · 370 阅读 · 0 评论 -
[Leetcode] 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?class Solution {public: /*algorithm: hash solution time O(n) space O(n)原创 2015-09-16 11:09:06 · 290 阅读 · 0 评论 -
[Leetcode]Paint House II
There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two ad原创 2015-09-15 17:28:51 · 539 阅读 · 0 评论 -
[Leetcode]Paint House
There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the ho原创 2015-09-14 12:36:51 · 633 阅读 · 0 评论 -
[Leetcode] Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If the原创 2015-09-22 12:51:54 · 267 阅读 · 0 评论 -
[Leetcode]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].Note: Recursive solution is trivi原创 2015-09-22 11:44:33 · 305 阅读 · 0 评论 -
[Leetcode]Find Minimum in Rotated Sorted Array
Suppose a sorted array 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 duplicate exists in the arra原创 2015-09-23 16:36:50 · 301 阅读 · 0 评论 -
[Leetcode]Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is原创 2016-03-09 22:40:57 · 342 阅读 · 0 评论 -
[Leetcode]Power of Four
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.Example:Given num = 16, return true.Given num = 5, return false.Follow up: Could you solve it without loop原创 2016-06-24 15:31:12 · 303 阅读 · 0 评论 -
[Leetcode]Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return[2].Note:Each element in the result must be unique.The result c原创 2016-06-20 18:37:36 · 261 阅读 · 0 评论 -
[Leetcode] 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: Recursive solution is triv原创 2015-09-14 11:14:32 · 245 阅读 · 0 评论 -
[Leetcode]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 \原创 2015-09-16 17:46:40 · 293 阅读 · 0 评论 -
[Leetcode]Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find thekth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Follow up:What if the BST原创 2015-08-20 22:20:59 · 288 阅读 · 0 评论 -
[Leetcode]Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity./** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next原创 2015-09-18 13:27:28 · 313 阅读 · 0 评论 -
[Leetcode]Verify Preorder Sequence in Binary Search Tree
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree.You may assume each number in the sequence is unique.Follow up:Could you do it原创 2015-09-18 16:53:13 · 1502 阅读 · 0 评论