2014年12月3日

Longest Common Prefix

摘要: class Solution {public: string longestCommonPrefix(vector &strs) { int ind, tail, len, tmp; len = strs.size(); if(len == 0) re... 阅读全文

posted @ 2014-12-03 22:40 code#swan 阅读(133) 评论(0) 推荐(0)

3Sum

摘要: Given an arraySofnintegers, are there elementsa,b,cinSsuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.Note:Elemen... 阅读全文

posted @ 2014-12-03 22:40 code#swan 阅读(157) 评论(0) 推荐(0)

Longest Palindromic Substring[leetcode]

摘要: Given a stringS, find the longest palindromic substring inS. You may assume that the maximum length ofSis 1000, and there exists one unique longest pa... 阅读全文

posted @ 2014-12-03 22:35 code#swan 阅读(107) 评论(0) 推荐(0)

String to Integer (atoi)

摘要: Implementatoito convert a string to an integer.Hint:Carefully consider all possible input cases. If you want a challenge, please do not see below and ... 阅读全文

posted @ 2014-12-03 21:18 code#swan 阅读(99) 评论(0) 推荐(0)

Regular Expression Matching

摘要: Implement regular expression matching with support for'.'and'*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The... 阅读全文

posted @ 2014-12-03 21:17 code#swan 阅读(106) 评论(0) 推荐(0)

Reverse Nodes in k-Group

摘要: Given a linked list, reverse the nodes of a linked listkat a time and return its modified list.If the number of nodes is not a multiple ofkthen left-o... 阅读全文

posted @ 2014-12-03 21:13 code#swan 阅读(122) 评论(0) 推荐(0)

Combination Sum II

摘要: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Each number ... 阅读全文

posted @ 2014-12-03 21:03 code#swan 阅读(97) 评论(0) 推荐(0)

Trapping Rain Water

摘要: Givennnon-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.Fo... 阅读全文

posted @ 2014-12-03 21:02 code#swan 阅读(97) 评论(0) 推荐(0)

Edit Distance

摘要: Given two wordsword1andword2, find the minimum number of steps required to convertword1toword2. (each operation is counted as 1 step.)You have the fol... 阅读全文

posted @ 2014-12-03 20:52 code#swan 阅读(83) 评论(0) 推荐(0)

Word Search

摘要: Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjace... 阅读全文

posted @ 2014-12-03 20:50 code#swan 阅读(134) 评论(0) 推荐(0)

Search in Rotated Sorted Array II

摘要: Follow up for "Search in Rotated Sorted Array":What ifduplicatesare allowed?Would this affect the run-time complexity? How and why?Write a function to... 阅读全文

posted @ 2014-12-03 20:49 code#swan 阅读(112) 评论(0) 推荐(0)

Maximal Rectangle

摘要: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.public class Solution { public... 阅读全文

posted @ 2014-12-03 20:47 code#swan 阅读(168) 评论(0) 推荐(0)

Subsets II

摘要: Given a collection of integers that might contain duplicates,S, return all possible subsets.Note:Elements in a subset must be in non-descending order.... 阅读全文

posted @ 2014-12-03 20:47 code#swan 阅读(116) 评论(0) 推荐(0)

Largest Rectangle in Histogram

摘要: Givennnon-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histog... 阅读全文

posted @ 2014-12-03 20:46 code#swan 阅读(171) 评论(0) 推荐(0)

Partition List

摘要: Given a linked list and a valuex, partition it such that all nodes less thanxcome before nodes greater than or equal tox.You should preserve the origi... 阅读全文

posted @ 2014-12-03 20:45 code#swan 阅读(112) 评论(0) 推荐(0)

Reverse Linked List II

摘要: Reverse a linked list from positionmton. Do it in-place and in one-pass.For example:Given1->2->3->4->5->NULL,m= 2 andn= 4,return1->4->3->2->5->NULL.No... 阅读全文

posted @ 2014-12-03 20:43 code#swan 阅读(96) 评论(0) 推荐(0)

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.2... 阅读全文

posted @ 2014-12-03 20:42 code#swan 阅读(119) 评论(0) 推荐(0)

Unique Binary Search Trees II

摘要: Givenn, generate all structurally uniqueBST's(binary search trees) that store values 1...n.For example,Givenn= 3, your program should return all 5 uni... 阅读全文

posted @ 2014-12-03 20:41 code#swan 阅读(142) 评论(0) 推荐(0)

Interleaving String

摘要: Givens1,s2,s3, find whethers3is formed by the interleaving ofs1ands2.For example,Given:s1="aabcc",s2="dbbca",Whens3="aadbbcbcac", return true.Whens3="... 阅读全文

posted @ 2014-12-03 20:39 code#swan 阅读(126) 评论(0) 推荐(0)

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 ... 阅读全文

posted @ 2014-12-03 20:38 code#swan 阅读(123) 评论(0) 推荐(0)

Binary Tree Level Order Traversal

摘要: Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree{3,9,2... 阅读全文

posted @ 2014-12-03 20:37 code#swan 阅读(129) 评论(0) 推荐(0)

Binary Tree Level Order Traversal II

摘要: Given a binary tree, return thebottom-up level ordertraversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For exa... 阅读全文

posted @ 2014-12-03 20:35 code#swan 阅读(118) 评论(0) 推荐(0)

Minimum Depth of Binary Tree

摘要: 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 le... 阅读全文

posted @ 2014-12-03 20:34 code#swan 阅读(110) 评论(0) 推荐(0)

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.Fo... 阅读全文

posted @ 2014-12-03 20:33 code#swan 阅读(81) 评论(0) 推荐(0)

Flatten Binary Tree to Linked List

摘要: Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened t... 阅读全文

posted @ 2014-12-03 20:32 code#swan 阅读(107) 评论(0) 推荐(0)

Populating Next Right Pointers in Each Node

摘要: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointe... 阅读全文

posted @ 2014-12-03 20:30 code#swan 阅读(115) 评论(0) 推荐(0)

Populating Next Right Pointers in Each Node II

摘要: 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 stil... 阅读全文

posted @ 2014-12-03 20:28 code#swan 阅读(128) 评论(0) 推荐(0)

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 =... 阅读全文

posted @ 2014-12-03 20:14 code#swan 阅读(93) 评论(0) 推荐(0)

Distinct Subsequences

摘要: Given a stringSand a stringT, count the number of distinct subsequences ofTinS.A subsequence of a string is a new string which is formed from the orig... 阅读全文

posted @ 2014-12-03 20:13 code#swan 阅读(103) 评论(0) 推荐(0)

Pascal's Triangle II

摘要: Given an indexk, return thekthrow of the Pascal's triangle.For example, givenk= 3,Return[1,3,3,1].Note:Could you optimize your algorithm to use onlyO(... 阅读全文

posted @ 2014-12-03 20:10 code#swan 阅读(116) 评论(0) 推荐(0)

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 fol... 阅读全文

posted @ 2014-12-03 20:09 code#swan 阅读(87) 评论(0) 推荐(0)

Best Time to Buy and Sell Stock

摘要: Say you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie... 阅读全文

posted @ 2014-12-03 20:04 code#swan 阅读(85) 评论(0) 推荐(0)

Best Time to Buy and Sell Stock II

摘要: Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete a... 阅读全文

posted @ 2014-12-03 20:03 code#swan 阅读(79) 评论(0) 推荐(0)

Best Time to Buy and Sell Stock III

摘要: Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete a... 阅读全文

posted @ 2014-12-03 20:02 code#swan 阅读(104) 评论(0) 推荐(0)

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 ... 阅读全文

posted @ 2014-12-03 20:00 code#swan 阅读(101) 评论(0) 推荐(0)

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: Pana... 阅读全文

posted @ 2014-12-03 17:50 code#swan 阅读(99) 评论(0) 推荐(0)

Word Ladder

摘要: Given two words (startandend), and a dictionary, find the length of shortest transformation sequence fromstarttoend, such that:Only one letter can be ... 阅读全文

posted @ 2014-12-03 17:49 code#swan 阅读(133) 评论(0) 推荐(0)

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 ... 阅读全文

posted @ 2014-12-03 17:47 code#swan 阅读(98) 评论(0) 推荐(0)

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 rep... 阅读全文

posted @ 2014-12-03 17:46 code#swan 阅读(111) 评论(0) 推荐(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... 阅读全文

posted @ 2014-12-03 12:14 code#swan 阅读(102) 评论(0) 推荐(0)

Palindrome Partitioning

摘要: Given a strings, partitionssuch that every substring of the partition is a palindrome.Return all possible palindrome partitioning ofs.For example, giv... 阅读全文

posted @ 2014-12-03 12:13 code#swan 阅读(94) 评论(0) 推荐(0)

Palindrome Partitioning II

摘要: Given a strings, partitionssuch that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning ofs... 阅读全文

posted @ 2014-12-03 12:12 code#swan 阅读(113) 评论(0) 推荐(0)

Clone Graph

摘要: Clone an undirected graph. Each node in the graph contains alabeland a list of itsneighbors.OJ's undirected graph serialization:Nodes are labeled uniq... 阅读全文

posted @ 2014-12-03 12:10 code#swan 阅读(82) 评论(0) 推荐(0)

Gas Station

摘要: There areNgas stations along a circular route, where the amount of gas at stationiisgas[i].You have a car with an unlimited gas tank and it costscost[... 阅读全文

posted @ 2014-12-03 12:07 code#swan 阅读(113) 评论(0) 推荐(0)

Candy

摘要: There areNchildren standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requi... 阅读全文

posted @ 2014-12-03 12:06 code#swan 阅读(86) 评论(0) 推荐(0)

Single Number

摘要: Given an array of integers, every element appearstwiceexcept for one. Find that single one.Note:Your algorithm should have a linear runtime complexity... 阅读全文

posted @ 2014-12-03 12:04 code#swan 阅读(65) 评论(0) 推荐(0)

Single Number II

摘要: Given an array of integers, every element appearsthreetimes except for one. Find that single one.Note:Your algorithm should have a linear runtime comp... 阅读全文

posted @ 2014-12-03 12:02 code#swan 阅读(92) 评论(0) 推荐(0)

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 ... 阅读全文

posted @ 2014-12-03 12:00 code#swan 阅读(117) 评论(0) 推荐(0)

Word Break

摘要: Given a stringsand a dictionary of wordsdict, determine ifscan be segmented into a space-separated sequence of one or more dictionary words.For exampl... 阅读全文

posted @ 2014-12-03 11:59 code#swan 阅读(104) 评论(0) 推荐(0)

Word Break II

摘要: Given a stringsand a dictionary of wordsdict, add spaces insto construct a sentence where each word is a valid dictionary word.Return all such possibl... 阅读全文

posted @ 2014-12-03 11:57 code#swan 阅读(104) 评论(0) 推荐(0)

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-linked list. *... 阅读全文

posted @ 2014-12-03 11:56 code#swan 阅读(89) 评论(0) 推荐(0)

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?/**... 阅读全文

posted @ 2014-12-03 11:55 code#swan 阅读(90) 评论(0) 推荐(0)

Reorder List

摘要: Given a singly linked listL: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 exam... 阅读全文

posted @ 2014-12-03 11:46 code#swan 阅读(83) 评论(0) 推荐(0)

Binary Tree Preorder Traversal

摘要: Given a binary tree, return thepreordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,2,3].Not... 阅读全文

posted @ 2014-12-03 11:43 code#swan 阅读(82) 评论(0) 推荐(0)

Binary Tree Postorder Traversal

摘要: Given a binary tree, return thepostordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[3,2,1].No... 阅读全文

posted @ 2014-12-03 11:42 code#swan 阅读(92) 评论(0) 推荐(0)

LRU Cache

摘要: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset.get(key)- Get the valu... 阅读全文

posted @ 2014-12-03 11:37 code#swan 阅读(100) 评论(0) 推荐(0)

Insertion Sort List

摘要: Sort a linked list using insertion sort./** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNo... 阅读全文

posted @ 2014-12-03 11:36 code#swan 阅读(100) 评论(0) 推荐(0)

Sort List

摘要: Sort a linked list inO(nlogn) time using constant space complexity./** * Definition for singly-linked list. * struct ListNode { * int val; * L... 阅读全文

posted @ 2014-12-03 11:34 code#swan 阅读(123) 评论(0) 推荐(0)

Evaluate Reverse Polish Notation

摘要: Evaluate the value of an arithmetic expression inReverse Polish Notation.Valid operators are+,-,*,/. Each operand may be an integer or another express... 阅读全文

posted @ 2014-12-03 11:33 code#swan 阅读(116) 评论(0) 推荐(0)

Reverse Words in a String

摘要: Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".class Solution: # @param s,... 阅读全文

posted @ 2014-12-03 11:26 code#swan 阅读(76) 评论(0) 推荐(0)

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... 阅读全文

posted @ 2014-12-03 11:23 code#swan 阅读(74) 评论(0) 推荐(0)

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 7might become4 5 6 7 0 1 2).Find the minimum element.You m... 阅读全文

posted @ 2014-12-03 11:22 code#swan 阅读(79) 评论(0) 推荐(0)

Find Minimum in Rotated Sorted Array II

摘要: Follow upfor "Find Minimum in Rotated Sorted Array":What ifduplicatesare allowed?Would this affect the run-time complexity? How and why?Suppose a sort... 阅读全文

posted @ 2014-12-03 11:21 code#swan 阅读(82) 评论(0) 推荐(0)

Min Stack

摘要: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack.pop() -- Removes... 阅读全文

posted @ 2014-12-03 11:20 code#swan 阅读(155) 评论(0) 推荐(0)

Intersection of Two Linked Lists

摘要: 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: a... 阅读全文

posted @ 2014-12-03 11:18 code#swan 阅读(178) 评论(0) 推荐(0)

Word Ladder II

摘要: Given two words (startandend), and a dictionary, find all shortest transformation sequence(s) fromstarttoend, such that:Only one letter can be changed... 阅读全文

posted @ 2014-12-03 11:17 code#swan 阅读(114) 评论(0) 推荐(0)

Max Points on a Line

摘要: Givennpoints on a 2D plane, find the maximum number of points that lie on the same straight line./** * Definition for a point. * struct Point { * ... 阅读全文

posted @ 2014-12-03 11:15 code#swan 阅读(112) 评论(0) 推荐(0)

Valid Number

摘要: Validate if a given string is numeric.Some examples:"0"=>true" 0.1 "=>true"abc"=>false"1 a"=>false"2e10"=>trueNote:It is intended for the problem stat... 阅读全文

posted @ 2014-12-03 11:04 code#swan 阅读(92) 评论(0) 推荐(0)

导航