- 博客(11)
- 资源 (5)
- 收藏
- 关注
原创 Minimum Height Trees | LeetCode
For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called mini
2015-11-30 14:38:12 1162
原创 Best Time to Buy and Sell Stock| LeetCode
题目很简单,就是给定一个数值,每个值代表每天的股价,如果最多交易一次的话(买入一次,卖出一次),求最大收益。这个生活中都知道,低入高出,必然可以得到高收益。解题思路:设定初始化买入为第一天,记为最小,往后扫,记录如果卖出的话,最大收益为多少,并已之前的记录作比较。代码很简洁,如下: int maxProfit(vector& prices) { int res=0;
2015-11-25 22:00:41 458
原创 Partition List | LeetCode 8ms C++Solution
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of
2015-11-24 19:51:14 308
原创 3Sum | LeetCode O(N*N) Solution
解题思路:如果是两个数加和等于target,那么我们很容易就想到O(n)的算法,这里题意是三个数的和等于target,其实就可以拆分为2Sum的问题,只是target‘=target-i;这里的I是枚举的第一个数,所以有算法复杂度为O(N*N)注意:保证结果是排序的,所以可以在一开始就将原数组排好序。另外为了避免重复,有两种解决方案:1)在求出所有结果之后,使用unique函数结合er
2015-11-23 20:41:47 358
原创 H-index II | LeetCode 12ms Solution
相对于第一题,这里提供的输入数组时已经排序的。解题思路:最简单的做法就是遍历,依次判断每一个位置的元素是否符合H-index 的定义,而且题目中给出了当出现多个值得时候,选取最大的。比如输入为【0,1,2,5,6】根据定义是至多h篇文章至少被引用h次。所以这里出现2,5其实都是符合条件的,这时候我们选取5,这样返回值就是2.又假设输入时【1,0,1,1,2,1】这里1,2都是符合条件的,我
2015-11-23 10:55:34 311
原创 Binary Tree Path | LeetCode
solution:vector binaryTreePaths(TreeNode* root) { vector res; if(root==NULL) return res; string path=""; helper(path,res,root); return r
2015-11-22 23:36:26 363
原创 Basic Calculator II | LeetCode 48ms C++ Solution
Implement a basic calculator to evaluate a simple expression string.The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should
2015-11-22 22:33:43 349
原创 Maximum Product subarray | LeetCode
Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the larges
2015-11-22 19:40:57 307
原创 Container Water | LeetCode
思路:每次宽度上减少一格,只要保证高度上的增加就可以弥补,这样代码中的while循环解答:int maxArea1(vector& height) { int i=0,j=height.size()-1; int water=0; while(i int h=min(height[i],height[j
2015-11-19 21:48:03 236
原创 Longest increasing subsequence |LeetCode
int lengthOfLIS1(vector& nums) { if(nums.size()==0) return 0; vector res; for(int i=0;i auto it=lower_bound(res.begin(),res.end(),nums[i]);
2015-11-19 14:22:04 345
原创 Additive Number | Leetcode
Additive number is a positive integer whose digits can form additive sequence.A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent num
2015-11-18 19:43:35 686
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人