
C++
文章平均质量分 66
Tingmei
这个作者很懒,什么都没留下…
展开
-
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 · 4511 阅读 · 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 · 1200 阅读 · 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 · 1131 阅读 · 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 · 1137 阅读 · 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 · 7149 阅读 · 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 · 3967 阅读 · 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 · 1539 阅读 · 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 · 1890 阅读 · 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 · 1010 阅读 · 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 · 703 阅读 · 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 · 1093 阅读 · 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 · 2718 阅读 · 1 评论 -
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 · 1151 阅读 · 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 · 759 阅读 · 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 · 910 阅读 · 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 sol原创 2012-09-30 11:13:48 · 2313 阅读 · 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 · 1201 阅读 · 0 评论 -
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 · 724 阅读 · 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 · 763 阅读 · 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 · 1692 阅读 · 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 · 1196 阅读 · 0 评论 -
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 · 2080 阅读 · 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 · 2399 阅读 · 1 评论 -
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 · 2004 阅读 · 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 · 1358 阅读 · 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 · 1927 阅读 · 0 评论 -
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 · 1005 阅读 · 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 · 4046 阅读 · 1 评论 -
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 · 4452 阅读 · 4 评论 -
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 · 1396 阅读 · 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 · 877 阅读 · 0 评论 -
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 · 1807 阅读 · 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 · 1225 阅读 · 0 评论 -
LeetCode: String to Integer (atoi)
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible inpu原创 2012-10-06 18:29:19 · 1370 阅读 · 0 评论 -
LeetCode: Sort Colors
Problem: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 u原创 2012-10-06 19:59:21 · 875 阅读 · 0 评论 -
LeetCode: Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i,原创 2012-10-06 23:39:37 · 978 阅读 · 0 评论 -
LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree./** * Definition for binary tree * struct TreeNode {原创 2012-10-07 10:15:46 · 1498 阅读 · 0 评论 -
LeetCode: Jump Game
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.Determine if yo原创 2012-10-07 15:39:13 · 983 阅读 · 0 评论 -
LeetCode: Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parentheses substring is "()",原创 2012-10-07 17:19:09 · 1544 阅读 · 0 评论 -
LeetCode: Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.时间复杂度为O(k*n),其中n为lists的最大长度,/** * Definition for singly-linked list. * struct ListNode { *原创 2012-10-07 21:08:04 · 2476 阅读 · 0 评论