LeetCode
文章平均质量分 70
鱼思故渊
这个作者很懒,什么都没留下…
展开
-
LeetCode 分类难度总结
1Two Sum25arraysort setTwo Pointers2Add Two Numbers34linked listTwo Pointers转载 2015-03-26 14:20:23 · 1470 阅读 · 0 评论 -
Covert sorted list to binary search tree--LeetCode
题目:把一个有序的链表转换为BST思路:和上面的一样,只不过这里需要注意查找链表中间点的方法程序中只有最后三个函数有用#include using namespace std;/*将一个有序的链表 转换为二叉平衡树 BST *//*思路:这个和上面将有序的数组装换为BST非常相似 都是采用同样的方法 只是这里查找链表的中间节点是一个很麻烦的事情 */typedef原创 2015-03-29 17:01:26 · 622 阅读 · 1 评论 -
Construct binary tree form postorder and inorder--LeetCode
和上面的题目相似 思路也是类似Tree* helpersecond(vector& inorder,int in_begin,int in_end,vector& post,int post_begin,int post_end){ Tree* root =NULL; int mid; int i; if(in_begin > in_end) { return NULL; }原创 2015-03-30 10:13:25 · 629 阅读 · 0 评论 -
Combination Sum--LeetCode
思路:求组合数#include #include #include using namespace std;/* 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.原创 2015-03-30 17:13:50 · 488 阅读 · 0 评论 -
Construct binary tree from preorder and inoder--LeetCode
题目:Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.思路:根据前序和中序的特点 递归调用创建二叉树#include #include u原创 2015-03-30 10:10:29 · 707 阅读 · 0 评论 -
Multiply Strings--LeetCode
题目: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.思路:大数相乘 使用字符串表示#include #includ原创 2015-03-30 21:16:06 · 552 阅读 · 0 评论 -
Combinations --LeetCode
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], [1,3], [1,4],]原创 2015-03-30 22:21:47 · 765 阅读 · 0 评论 -
Remove Duplicates from Sorted Array--LeetCode
题目: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 this in p原创 2015-03-31 09:58:53 · 764 阅读 · 0 评论 -
Linked List Cycle--LeetCode
题目:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?思路:上述题目中所说的不需要额外空间是指非常数的额外空间。当然,从最基本的考虑,可以使用hashset来存储一个链表的所有节点,如果发现节原创 2015-04-01 20:34:35 · 544 阅读 · 0 评论 -
Median of Two Sorted Array---LeetCode
这道题比较直接的想法就是用Merge Sorted Array这个题的方法把两个有序数组合并,当合并到第(m+n)/2个元素的时候返回那个数即可,而且不用把结果数组存起来。算法时间复杂度是O(m+n),空间复杂度是O(1)。因为代码比较简单,就不写出来了,跟Merge Sorted Array比较类似,大家可以参照这个题目的解法。接下来我们考虑有没有优化的算法。优化的思想来源于orde原创 2015-04-02 08:52:23 · 630 阅读 · 0 评论 -
Linked List Cycle II--LeetCode
这道题是Linked List Cycle的扩展,就是在确定是否有cycle之后还要返回cycle的起始点的位置。从Linked List Cycle中用的方法我们可以得知a=kc-b(不了解的朋友可以先看看Linked List Cycle)。现在假设有两个结点,一个从链表头出发,一个从b点出发,经过a步之后,第一个结点会到达cycle的出发点,而第二个结点会走过kc-b,加上原来的原创 2015-04-02 08:42:07 · 773 阅读 · 0 评论 -
Subsets--LeetCode
题目: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.原创 2015-04-03 15:39:46 · 737 阅读 · 0 评论 -
Sort Colors--LeetCode
题目: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原创 2015-04-03 14:44:30 · 739 阅读 · 0 评论 -
Subsets II--LeetCode
题目: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原创 2015-04-03 15:44:16 · 692 阅读 · 0 评论 -
Median of Two Sorted Arrays--LeetCode
题目:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).思路:这道题比较直接的想法就是用Merge Sort原创 2015-04-01 08:52:24 · 807 阅读 · 0 评论 -
Letter Combinations of a Phone Number--LeetCode
题目: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原创 2015-04-01 11:57:28 · 620 阅读 · 0 评论 -
Minimum Window Substring -- LeetCode
题目:Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum windo原创 2015-04-01 08:31:46 · 612 阅读 · 0 评论 -
Length of last word--LeetCode
题目: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:原创 2015-04-01 09:31:37 · 905 阅读 · 0 评论 -
Valid Palindrome--LeetCode
题目: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原创 2015-04-01 11:21:18 · 729 阅读 · 0 评论 -
Two Sum--LeetCode
题目: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 the targ原创 2015-04-01 15:59:50 · 569 阅读 · 0 评论 -
Permutation II--LeetCode
Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1].原创 2015-04-03 19:55:34 · 590 阅读 · 0 评论 -
Permutation--LeetCode
题目:Given 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], [2,3,1], [3,1,2], and [3,2,1].思路:对原创 2015-04-03 18:09:01 · 521 阅读 · 0 评论 -
Valid Parentheses--LeetCode
题目: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原创 2015-04-01 17:04:59 · 456 阅读 · 0 评论 -
Single Number--LeetCode
题目:Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without原创 2015-04-01 17:26:04 · 443 阅读 · 0 评论 -
Simplify Path--LeetCode
题目:Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"click to show corner cases.Corner Cases:原创 2015-04-03 11:35:44 · 841 阅读 · 0 评论 -
Convert Sorted List to Binary Search Tree--LeetCode
题目:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.思路:和上面的思想一样,只不过注意找到链表的中间节点的方法void Inorder(BinTree* root){ if(root == NULL)原创 2015-04-04 11:42:10 · 764 阅读 · 0 评论 -
Reverse Linked List II--LeetCode
题目:Reverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.Note:Given m,原创 2015-04-04 10:49:54 · 623 阅读 · 0 评论 -
Convert Sorted Array to Binary Search Tree--LeetCode
题目:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路:从一个数组中找到中间的元素作为BST的根,然后坐边的作为左子树,右边的作为右子树,递归调用#include #include #include #include原创 2015-04-04 11:41:17 · 832 阅读 · 0 评论 -
Binary Tree Level Order Traversal--LeetCode
题目: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原创 2015-04-05 10:57:01 · 767 阅读 · 0 评论 -
Plus One--LeetCode
题目:Given a number represented as an array of digits, plus one to the number.思路:数组中每个元素都是0~9.为这个数组加1vector PlusOne(vector& vec){ int carray =1; int i=0; int tmp; for(i=vec.size()-1;i>=0;原创 2015-04-05 14:50:21 · 515 阅读 · 0 评论 -
Minimum Depth of Binary Tree--LeetCode
题目: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 leaf node.思路:这里递归就需要注意一些东西了,对于非递归来说,只原创 2015-04-05 10:30:42 · 585 阅读 · 0 评论 -
Add Binary--LeetCode
题目:Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".思路:字符串的叠加,注意进位string AddBinary(string& first,string& second){ strin原创 2015-04-05 15:14:15 · 643 阅读 · 0 评论 -
Balanced Binary Tree--LeetCode
判断一个二叉树是否为平衡二叉树int Depth(BinTree* root){ if(root == NULL) return 0; return max(Depth(root->left),Depth(root->right))+1;}bool isBalancedBinTree(BinTree* root){ if(root ==NULL) return 1; i原创 2015-04-05 10:48:22 · 715 阅读 · 0 评论 -
Longest Palindromic Substring--LeetCode
题目: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.思路:第一种思路,就是以某原创 2015-04-01 09:21:45 · 665 阅读 · 0 评论 -
Maximum Depth of Binary Tree--LeetCode
题目: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 leaf node.使用递归的方法为:int MaxDep原创 2015-04-05 10:08:09 · 701 阅读 · 0 评论 -
Find Peak Element--LeetCode
A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multiple peaks, in that原创 2015-04-11 19:52:13 · 494 阅读 · 0 评论 -
Rotate Array--LeetCode
Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to[5,6,7,1,2,3,4]. 思路:三次翻转。#include #include using namespace std原创 2015-04-11 21:52:44 · 485 阅读 · 0 评论 -
Reverse Nodes in k-Group--LeetCode
题目:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remain原创 2015-03-31 17:24:06 · 713 阅读 · 0 评论 -
sort list--LeetCode
Sort a linked list in O(n log n) time using constant space complexity.看到O(n log n)的排序算法,适合单链表的首先想到的就是归并排序达到这个复杂度的排序算法,对于单链表来说,最合适的就是 #include #include using namespace std;/*对于链表的排序 使用归并排序原创 2015-03-24 19:28:11 · 790 阅读 · 0 评论 -
Binary Tree Maximum Path Sum--LeetCode
题目: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思路:刚开始思路原创 2015-04-09 11:44:23 · 841 阅读 · 0 评论