摘要:Climbing StairsYou are climbing a stair case. It takesnsteps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct way...
阅读全文
摘要:Longest Substring Without Repeating CharactersGiven a string, find the length of the longest substring without repeating characters. For example, the ...
阅读全文
摘要:Longest Common PrefixWrite a function to find the longest common prefix string amongst an array of strings.算法思路:思路:貌似木有什么捷径,逐个比较,遇到不同即断开。代码如下: 1 publi...
阅读全文
摘要:Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any ...
阅读全文
摘要:Populating Next Right Pointers in Each NodeGiven a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLi...
阅读全文
摘要:Path Sum IIGiven 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 tr...
阅读全文
摘要:Path SumGiven 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 give...
阅读全文
摘要:Balanced Binary TreeGiven a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tre...
阅读全文
摘要:Convert Sorted Array to Binary Search TreeGiven an array where elements are sorted in ascending order, convert it to a height balanced BST.算法:根据有序数组,生...
阅读全文
摘要:Construct Binary Tree from Inorder and Postorder TraversalGiven inorder and postorder traversal of a tree, construct the binary tree.Note:You may assu...
阅读全文
摘要:Construct Binary Tree from Preorder and Inorder TraversalGiven preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume...
阅读全文
摘要:Minimum Depth of Binary TreeGiven a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root...
阅读全文
摘要:Maximum Depth of Binary TreeGiven a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root ...
阅读全文
摘要:Same TreeGiven two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally ide...
阅读全文
摘要:Symmetric TreeGiven a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric...
阅读全文
摘要:Recover Binary Search TreeTwo elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A sol...
阅读全文
摘要:Validate Binary Search TreeGiven a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtre...
阅读全文
摘要:Pow(x, n)Implement pow(x,n).算法思路:二分法,没什么好说的,小心点就行;这个题时间比较苛刻。return pow(x,n >> 1) * pow(x,n >> 1) 是过不去的,因此把pow(x,n / 2)求出来先。其实时间复杂度而言,是一样的。【注意】:n的取值范围;...
阅读全文
摘要:Subsets IIGiven a collection of integers that might contain duplicates,S, return all possible subsets.Note:Elements in a subset must be in non-descend...
阅读全文
摘要:SubsetsGiven a set of distinct integers,S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must...
阅读全文
摘要:Insert IntervalGiven a set ofnon-overlappingintervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals...
阅读全文
摘要:Merge IntervalsGiven a collection of intervals, merge all overlapping intervals.For example,Given[1,3],[2,6],[8,10],[15,18],return[1,6],[8,10],[15,18]...
阅读全文
摘要:Valid PalindromeGiven a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a pla...
阅读全文
摘要:Merge Sorted ArrayGiven two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that i...
阅读全文
摘要:Length of Last WordGiven a stringsconsists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string.I...
阅读全文
摘要:Simplify PathGiven an absolute path for a file (Unix-style), simplify it.For example,path="/home/", =>"/home"path="/a/./b/../../c/", =>"/c"click to sh...
阅读全文
摘要:Add BinaryGiven two binary strings, return their sum (also a binary string).For example,a ="11"b ="1"Return"100".算法思路:模拟二进制加法,跟十进制木有区别,将a,b转置(不转置的话,倒着...
阅读全文
摘要:Substring with Concatenation of All WordsYou are given a string,S, and a list of words,L, that are all of the same length. Find all starting indices o...
阅读全文
摘要:Valid ParenthesesGiven a string containing just the characters'(',')','{','}','['and']', determine if the input string is valid.The brackets must clos...
阅读全文
摘要:Reverse Words in a StringGiven an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".click...
阅读全文
摘要:ZigZag ConversionThe string"PAYPALISHIRING"is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern i...
阅读全文
摘要:Gray CodeThe gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integernrepresenting the tot...
阅读全文
摘要:Permutation SequenceThe set[1,2,3,…,n]contains a total ofn! unique permutations.By listing and labeling all of the permutations in order,We get the fo...
阅读全文
摘要:Next PermutationImplement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangemen...
阅读全文
摘要:Permutations IIGiven a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2]have the follow...
阅读全文
摘要:Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes co...
阅读全文
摘要:PermutationsGiven a collection of numbers, return all possible permutations.For example,[1,2,3]have the following permutations:[1,2,3],[1,3,2],[2,1,3]...
阅读全文
摘要:CombinationsGiven two integersnandk, return all possible combinations ofknumbers out of 1 ...n.For example,Ifn= 4 andk= 2, a solution is:[ [2,4], [3...
阅读全文
摘要:TriangleGiven 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...
阅读全文
摘要:Pascal's Triangle IIGiven an indexk, return thekthrow of the Pascal's triangle.For example, givenk= 3,Return[1,3,3,1].Note:Could you optimize your alg...
阅读全文
摘要:Pascal's TriangleGivennumRows, generate the firstnumRowsof Pascal's triangle.For example, givennumRows= 5,Return[ [1], [1,1], [1,2,1], [1,3,...
阅读全文
摘要:Remove Duplicates from Sorted Array IIFollow up for "Remove Duplicates":What if duplicates are allowed at mosttwice?For example,Given sorted array A =...
阅读全文
摘要:Remove Duplicates from Sorted ArrayGiven a sorted array, remove the duplicates in place such that each element appear onlyonceand return the new lengt...
阅读全文
摘要:Remove ElementGiven an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. ...
阅读全文
摘要:Sort ListSort a linked list inO(nlogn) time using constant space complexity.算法思想:时间复杂度为O(nlogn)的排序算法,有快排、归并、堆排序,快排需要往前遍历,因此不适合单链表,堆排序可以,但是需要O(n)的空间,因此...
阅读全文
摘要:Reverse Linked List IIReverse 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,return...
阅读全文
摘要:Reverse Nodes in k-GroupGiven 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...
阅读全文
摘要:Reorder ListGiven 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' val...
阅读全文
摘要:Insertion Sort ListSort a linked list using insertion sort.算法思路:最基本的插入排序,时间复杂度O(n*n),空间复杂度O(1)代码: 1 public class Solution { 2 public ListNode inse...
阅读全文
摘要:Partition ListGiven a linked list and a valuex, partition it such that all nodes less thanxcome before nodes greater than or equal tox.You should pres...
阅读全文
摘要:Remove Duplicates from Sorted List IIGiven a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinctnumbers from the or...
阅读全文
摘要:Remove Duplicates from Sorted ListGiven a sorted linked list, delete all duplicates such that each element appear onlyonce.For example,Given1->1->2, r...
阅读全文
摘要:Merge k Sorted ListsMergeksorted linked lists and return it as one sorted list. Analyze and describe its complexity.这道题,第一次刷是过了,第二次同样的代码超时了,看来case是在不断...
阅读全文
摘要:Merge Two Sorted ListsMerge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the fir...
阅读全文
摘要:Rotate ListGiven a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2->3->4->5->NULLandk=2,return4->5->1->2->3-...
阅读全文
摘要:Swap Nodes in PairsGiven a linked list, swap every two adjacent nodes and return its head.For example,Given1->2->3->4, you should return the list as2-...
阅读全文
摘要:Remove Nth Node From End of ListGiven a linked list, remove thenthnode from the end of list and return its head.For example, Given linked list: 1->2...
阅读全文
摘要:Linked List Cycle IIGiven a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without u...
阅读全文
摘要:Linked List CycleGiven a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?算法思路1:快慢指针,当两个指针相遇,则表示有环,...
阅读全文
摘要:Palindrome NumberDetermine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers b...
阅读全文
摘要:这篇博文较长,且理论为主,因为我准备换种风格,以保证大家看的时候不会觉得无聊,看完如果对dfs还是一头雾水,那我也不会请你吃麻辣烫开场:刷了一遍leetcode,发现DFS是个特别强大,且出题率特别高的一个基础算法,第一次接触DFS是树的一个搜索算法,相对而言的是BFS。根据难度与考察频率参考表的建...
阅读全文
摘要:Reverse IntegerReverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321click to show spoilers.Have you thought about th...
阅读全文
摘要:Plus OneGiven a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant dig...
阅读全文
摘要:Set Matrix ZeroesGiven amxnmatrix, if an element is 0, set its entire row and column to 0. Do it in place.Follow up:Did you use extra space?A straight...
阅读全文
摘要:Validate Binary Search TreeRecover Binary Search TreeSymmetric TreeSame TreeMaximum Depth of Binary TreeConstruct Binary Tree from Preorder and Inorde...
阅读全文
摘要:Count and SayThe count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1is read off as"one 1"or11.11is r...
阅读全文
摘要:Palindrome PartitioningGiven a strings, partitionssuch that every substring of the partition is a palindrome.Return all possible palindrome partitioni...
阅读全文
摘要:Letter Combinations of a Phone NumberGiven a digit string, return all possible letter combinations that the number could represent.A mapping of digit ...
阅读全文
摘要:Restore IP AddressesGiven a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given"255255...
阅读全文
摘要:Generate ParenthesesGivennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a sol...
阅读全文
摘要:Word SearchGiven 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, wh...
阅读全文
摘要:N-Queens IIFollow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.算法思路:与[leetcode]...
阅读全文
摘要:N-QueensThen-queens puzzle is the problem of placingnqueens on ann×nchessboard such that no two queens attack each other.Given an integern, return all...
阅读全文
摘要:Combination SumGiven a set of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Thes...
阅读全文
摘要:Combination Sum IIGiven a collection of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sum...
阅读全文
摘要:本文载自【k sum problem】以及【NSum】有位同僚,对该问题做出了代码的小结,想看吗?戳我戳我。问题陈述:在一个数组,从中找出k个数(每个数不能重复取。数组中同一个值有多个,可以取多个),使得和为零。找出所有这样的组合,要求没有重复项(只要值不同即可,不要求在原数组中的index不同)解...
阅读全文
摘要:4SumGiven an arraySofnintegers, are there elementsa,b,c, anddinSsuch thata+b+c+d= target? Find all unique quadruplets in the array which gives the sum...
阅读全文
摘要:3Sum ClosestGiven an arraySofnintegers, find three integers inSsuch that the sum is closest to a given number, target. Return the sum of the three int...
阅读全文
摘要:3SumGiven 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:El...
阅读全文
摘要:Two SumGiven an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the...
阅读全文