- 博客(74)
- 资源 (1)
- 收藏
- 关注
原创 算法复习笔记 | 排序算法比较
最近正好复习复习算法,于是从排序算法开始做一个总结。以下的代码均为原创,如果有任何问题,欢迎指正。简单来讲,排序算法的实质是将长度为n的数组中的数字按照从小到大或者从大到小的顺利排列。简而言之,在不考虑算法的情况下,我们可以把排序抽象为如下的一个函数:array表示T类型的一个数组,num表示数组的长度。本文假设我们实现的排序算法都是按照从小到大的顺序排列;从大到小的排列类似。
2013-09-14 00:32:16 4084 1
原创 LeetCode | Gray Code
题目:The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the seq
2013-09-29 10:56:11 3538 1
原创 LeetCode | Sort Colors
题目:Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use th
2013-09-22 20:14:56 3028 2
原创 LeetCode | Regular Expression Matching
题目:Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire
2013-09-22 19:50:13 1341
原创 LeetCode | Wildcard Matching
题目:Implement wildcard pattern matching with support for '?' and '*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching shoul
2013-09-22 17:49:56 1874 3
原创 LeetCode | Palindrome Number
题目:Determine whether an integer is a palindrome. Do this without extra space.思路:翻转数字并与原数字比较。代码:class Solution {public: bool isPalindrome(int x) { // Start typin
2013-09-22 16:20:14 957
原创 LeetCode | 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.思路:类似http://blog.csdn.net/lanxu_yy/article/details/11898523,不过需要利用链表的方式找
2013-09-22 16:00:57 1233
原创 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 binary tree * struct TreeNode {
2013-09-22 15:07:29 1388
原创 LeetCode | 4Sum
题目:Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Not
2013-09-22 14:28:49 1000
原创 LeetCode | 3Sum Closest
题目:Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would
2013-09-22 14:23:37 1052
原创 LeetCode | 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:Elements in a
2013-09-22 14:10:03 1742
原创 LeetCode | Sqrt(x)
题目:Implement int sqrt(int x).Compute and return the square root of x.思路:利用二分法查找:第一种可能性是直接找到能够麻烦要求的数;第二种可能性是找到相邻的两个数,可以比较两个数哪一个离target更近,不过题目当中希望找的是更小的那个数。代码:class Solut
2013-09-22 11:24:35 3345
原创 LeetCode | Subsets
题目:Given 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 not contain duplicate subsets.
2013-09-22 01:24:15 5219 1
原创 LeetCode | 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.The solution set must not
2013-09-22 01:21:20 3500
原创 LeetCode | 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 tox.You should preserve the original relative order of the nodes
2013-09-22 00:59:17 977
原创 LeetCode | 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
2013-09-22 00:57:58 1442
原创 LeetCode | 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 pretty straight forward. Could
2013-09-21 23:51:37 1268
原创 LeetCode | Balanced Binary Tree
题目:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofevery node nev
2013-09-21 23:36:55 4935 2
原创 LeetCode | 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 still work?Note:You may only us
2013-09-21 23:16:07 1080
原创 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 rig
2013-09-21 22:52:34 1149
原创 LeetCode | Pascal's Triangle II
题目:Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?
2013-09-21 22:32:16 1008
原创 LeetCode | Pascal's Triangle
题目:Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]思路:就是一个正常
2013-09-21 22:17:52 1602
原创 LeetCode | 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
2013-09-21 22:08:56 1129
原创 LeetCode | Best Time to Buy and Sell Stock III
题目:Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most two transactions.Note
2013-09-21 21:51:57 1966 6
原创 LeetCode | Best Time to Buy and Sell Stock II
题目:Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like
2013-09-21 20:32:14 1497
原创 LeetCode | Best Time to Buy and Sell Stock
题目:Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of
2013-09-21 20:27:36 1317 1
原创 LeetCode | 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 / \ 2 3思路:
2013-09-21 20:17:00 3470
原创 LeetCode | 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 consecutive elements sequence
2013-09-21 19:25:44 1657
原创 LeetCode | Length of Last Word
题目:Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note:
2013-09-19 23:44:42 1010
原创 LeetCode | Climbing Stairs
题目:You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?思路:利用DP的方法,
2013-09-19 23:35:32 9099 8
原创 LeetCode | Longest Common Prefix
题目:Write a function to find the longest common prefix string amongst an array of strings.思路:首先找出所有字符串的最小长度。然后依次比较前几位在各个字符串中是否相同,若相同,则作为输出结果;否则,停止循环。代码:class Solution {publi
2013-09-19 19:23:46 1241
原创 LeetCode | 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 tree should look like:
2013-09-19 18:49:37 2009 2
原创 LeetCode | 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: Panama" is a palindrome."race a c
2013-09-19 18:39:29 997
原创 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: Recursiv
2013-09-18 20:48:05 1154
原创 LeetCode | 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,#,#,15,7}, 3 / \ 9
2013-09-18 20:30:16 1242
原创 LeetCode | Binary Tree Level Order Traversal II
题目:Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,
2013-09-18 20:27:34 1973
原创 LeetCode | 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 no
2013-09-18 19:54:13 3184 2
原创 LeetCode | 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, and there exists one unique longest palindromic substring.思路:
2013-09-18 19:37:20 1558
原创 LeetCode | Valid Parentheses
题目:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are
2013-09-17 22:20:55 1141
原创 LeetCode | 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
2013-09-17 22:15:10 1612
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人