贪心法
tangyifei1991
这个作者很懒,什么都没留下…
展开
-
Jump Game 跳台阶问题
class Solution { public: bool canJump(int A[], int n) { int reach=0; for(int i=0;i { if(i { reach=max(reach,i+A[i]);原创 2014-01-21 10:43:25 · 387 阅读 · 0 评论 -
Jump Game II 最小到达终点的步数
class Solution { public: //贪心的规则就是在能够到达的范围内,选择一个能够到达最远距离的点,更新步数,更新最远到达的范围 int jump(int A[], int n) { int step=0; int reach=0; int tmp=0; for(int i=0;i {原创 2014-01-21 10:45:10 · 482 阅读 · 0 评论 -
Best Time to Buy and Sell Stock 买卖股票最好的时间
class Solution { public: int maxProfit(vector &prices) { ////贪心法,分别找到价格最低和最高的一天,低进高出。 int n=prices.size(); if(n return 0; int profit=0; int原创 2014-01-21 10:47:00 · 349 阅读 · 0 评论 -
Best Time to Buy and Sell Stock II 买卖股票最好的时间II
class Solution { public: //贪心法,低进高出,把所有正的价格差价相加起来。 int maxProfit(vector &prices) { int n=prices.size(); if(n return 0; int profit=0; int minbuy=prices[0]; for(int原创 2014-01-21 10:49:03 · 365 阅读 · 0 评论 -
Longest Substring Without Repeating Characters 不含重复字符的最长子串
class Solution { public: int lengthOfLongestSubstring(string s) { int ASCII_MAX=26; int last[ASCII_MAX]; fill(last,last+ASCII_MAX,-1); int len=0; int原创 2014-01-21 10:52:02 · 320 阅读 · 0 评论 -
Container With Most Water 盛最多的水
class Solution { public: int maxArea(vector &height) { int l=0; int r=height.size()-1; int ret=0; int area=0; while(l { area=mi原创 2014-01-21 10:56:37 · 385 阅读 · 0 评论