算法
文章平均质量分 66
Tingmei
这个作者很懒,什么都没留下…
展开
-
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 sol原创 2012-09-30 11:13:48 · 2296 阅读 · 0 评论 -
LeetCode: Binary Tree Level Order Traversal
Problem: 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 /原创 2012-09-30 12:59:48 · 4495 阅读 · 0 评论 -
LeetCode: Maximum Subarray
Problem:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray原创 2012-09-23 10:48:56 · 1046 阅读 · 0 评论 -
LeetCode: 4Sum
Problem: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.原创 2012-09-30 15:02:19 · 7118 阅读 · 0 评论 -
LeetCode: Generate Parentheses
Problem:Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())原创 2012-09-30 15:30:35 · 3938 阅读 · 1 评论 -
LeetCode: Two Sum
Problem:Given 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 two numbers such that they add up to t原创 2012-09-30 15:41:01 · 1523 阅读 · 0 评论 -
LeetCode: Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.两个有序单链表合并。/** * Definition for singly-linked list. *原创 2012-09-30 16:01:07 · 1871 阅读 · 0 评论 -
LeetCode: Merge Sorted Array
Problem:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.两个有序数组合并。class Solution {public: void原创 2012-09-30 15:48:17 · 997 阅读 · 0 评论 -
LeetCode: Remove Nth Node From End of List
Problem: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 f原创 2012-09-30 16:43:08 · 688 阅读 · 0 评论 -
LeetCode: Remove Duplicates from Sorted List
Problem: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->3./**原创 2012-09-30 16:49:31 · 1076 阅读 · 0 评论 -
LeetCode: Climbing Stairs
Question: 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?递归求解。f(1) =原创 2012-09-30 09:55:46 · 1131 阅读 · 0 评论 -
LeetCode: Add Binary
ProblemGiven two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".即模拟加法运算。class Solution {public: string addBinary(string a, str原创 2012-09-30 10:43:26 · 896 阅读 · 0 评论 -
LeetCode: Add Two Numbers
ProblemYou are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return原创 2012-09-30 11:00:22 · 747 阅读 · 0 评论 -
LeetCode: Binary Tree Zigzag Level Order Traversal
Problem:Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:原创 2012-09-30 13:37:13 · 1182 阅读 · 0 评论 -
LeetCode: 3Sum
ProblemGiven 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原创 2012-09-30 14:13:03 · 1108 阅读 · 0 评论 -
LeetCode: 3Sum Closest
Problem: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原创 2012-09-30 14:26:16 · 1120 阅读 · 0 评论 -
LeetCode: Remove Element
Given 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. It doesn't matter what you leave beyond the new length.c原创 2012-09-30 16:14:14 · 1186 阅读 · 0 评论 -
LeetCode: Remove Duplicates from Sorted Array
Problem:Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do th原创 2012-09-30 17:09:08 · 2700 阅读 · 1 评论 -
LeetCode: Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1原创 2012-10-04 16:22:35 · 700 阅读 · 0 评论 -
LeetCode: Depth of Binary Tree
Problem:Given a binary tree, find its depth (maximum height).递归求解。/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; *原创 2012-10-04 16:40:16 · 745 阅读 · 0 评论 -
LeetCode: Longest Common Prefix
Problem:Write a function to find the longest common prefix string amongst an array of strings.求所有字符串的最长公共前缀。class Solution {public: string longestCommonPrefix(vector &strs) { // S原创 2012-09-30 18:35:24 · 1683 阅读 · 0 评论 -
LeetCode: Anagrams
Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.求数组中所有的anagramsclass Solution {public: vector anagrams(vector &strs) {原创 2012-10-04 17:22:21 · 1183 阅读 · 0 评论 -
LeetCode: Balanced Binary Tree
Problem:Given a binary tree, determine if it is height-balanced.An example of a height-balanced tree. A height-balanced tree is a tree whose subtrees differ in height by no more than one原创 2012-10-04 18:47:20 · 4031 阅读 · 1 评论 -
LeetCode: Binary Tree Level Order Traversal II
Problem: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,原创 2012-10-04 18:58:29 · 2066 阅读 · 0 评论 -
LeetCode: Combinations
Problem:Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2原创 2012-10-04 19:14:50 · 2387 阅读 · 1 评论 -
LeetCode: Sqrt(x)
Problem:Implement int sqrt(int x).Compute and return the square root of x.牛顿迭代法。class Solution {public: int sqrt(int x) { // Start typing your C/C++ solution below原创 2012-10-04 19:29:37 · 1790 阅读 · 0 评论 -
LeetCode: Word Search
Problem: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 "adjacent" cells are those horizontally o原创 2012-10-04 21:01:24 · 4429 阅读 · 4 评论 -
LeetCode: Length of Last Word
Problem: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.原创 2012-10-04 21:19:46 · 993 阅读 · 0 评论 -
LeetCode: Rotate List
Problem:Given 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./** * Definition原创 2012-10-04 22:12:33 · 863 阅读 · 0 评论 -
LeetCode: Pow(x, n)
Problem: Implement pow(x, n).注意负数转化为整数时,可能会有溢出。class Solution {public: double pow(double x, int n) { // Start typing your C/C++ solution below // DO NOT write int main() funct原创 2012-10-04 19:46:52 · 1993 阅读 · 0 评论 -
LeetCode: First Missing Positive
Problem:Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time a原创 2012-10-04 21:42:41 · 1380 阅读 · 0 评论 -
LeetCode: Plus One
Problem:Given a number represented as an array of digits, plus one to the number.class Solution {public: vector plusOne(vector &digits) { // Start typing your C/C++ solution below原创 2012-10-04 19:57:58 · 1344 阅读 · 0 评论 -
LeetCode: Convert Sorted Array to Binary Search Tree
Problem:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.For example,Given array [9,12,14,17,19,23,50,54,67,72,76],the below BST is one po原创 2012-10-04 20:18:13 · 1213 阅读 · 0 评论 -
LeetCode: Implement strStr()
Problem:Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.KMP字符串匹配算法。class Solution {public: char *strS原创 2012-10-04 20:32:34 · 1908 阅读 · 0 评论 -
LeetCode: Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimite原创 2012-10-06 21:36:28 · 2634 阅读 · 0 评论 -
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 sequence o原创 2012-10-07 13:14:24 · 5404 阅读 · 1 评论 -
LeetCode: Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string原创 2012-10-07 16:17:13 · 1033 阅读 · 0 评论 -
LeetCode: 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.O(n^3)方法,348 milli secs class Solution {public: int maximalRectangle(v原创 2012-10-07 19:35:31 · 3216 阅读 · 1 评论 -
LeetCode: Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.class Solution {public: string m原创 2012-10-07 21:27:41 · 2121 阅读 · 0 评论 -
LeetCode: Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321Have you thought about this?Here are some good questions to ask before coding. Bonus points原创 2012-10-08 11:44:48 · 1002 阅读 · 0 评论