今天纠结了一整天
==============================================================

leetcode66 https://leetcode.com/problems/plus-one/?tab=Description

leetcode119 https://leetcode.com/problems/pascals-triangle-ii/?tab=Description

leetcode121 https://leetcode.com/problems/best-time-to-buy-and-sell-stock/?tab=Description

===============================================================

66说的是
给你一串十进制个位数,代表一个大整数,然后对他加一,输出结果

我的思路
一开始没看懂题目,WA了一发才明白digit是“一位数字”的意思。。。没啥说的,水题

 #include<bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int temp=,n=digits.size();
vector<int> ans(digits);
for(int i=n-;i>=&&temp;i--){
ans[i]+=temp;
if(ans[i]>=){
ans[i]-=;
temp=;
}else temp=;
}
if(temp==){
ans.resize(n+);
for(int i=;i<=n;i++){
ans[i]=ans[i-];
ans[]=;
}
}
return ans;
}
};

66

===============================================================

119说的是
输入k,输出杨辉三角的第k行,要求只使用O(k)的额外空间

我的思路
额。。。确定这不是数学题?其实就是让输出k次方的二项式系数

              
(BTW,k如果大于11,就gg,因为int不一定存的下了2333)

 class Solution {
public:
vector<int> getRow(int rowIndex) {
int &k=rowIndex;
vector<int> kj(k+),ans(k+);
kj[]=;
for(int i=;i<=k;i++){
kj[i]=kj[i-]*(i+);
}
for(int i=;i<=k;i++){
ans[i]=kj[k]/(kj[i]*kj[k-i]);
}
return ans;
}
};

WA

打脸了,有一组k=32.。。。这题时间不重要,空间很关键,因为就算k=32,O(n^2)的时间也是完全可以接受的。。。。用普通的求杨辉三角的方法来搞一下,只不过上一行信息不再保存了

 class Solution {
public:
vector<int> getRow(int rowIndex) {
int k=rowIndex+;
vector<int> ans(k,);
for(int i=;i<k;i++){
for(int j=i-;j>;j--){
ans[j]=ans[j]+ans[j-];
}
}
return ans;
}
};

119AC

================================================================

121说的是
给你n个数字,代表的是连续n天的股票价格,问你在只允许一次买入一次卖出的情况下,你最多赚多少钱?

我的思路
水题,线性扫一遍,记录到目前为止的最低价,和当天的价格做差,更新答案。

 class Solution {
public:
int maxProfit(vector<int>& prices) {
int n=prices.size();
if(n==)return ;
int temp=prices[],ans=;
for(int i=;i<n;i++){
temp=temp>prices[i]?prices[i]:temp;
ans=ans<(prices[i]-temp)?(prices[i]-temp):ans;
}
return ans;
}
};

121

RE一次,写的代码不强壮,没考虑边界,当数组为空的时候,temp试图访问不存在的地方。

2017-3-7 leetcode 66 119 121的更多相关文章

  1. 2017/11/22 Leetcode 日记

    2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...

  2. 2017/11/21 Leetcode 日记

    2017/11/21 Leetcode 日记 496. Next Greater Element I You are given two arrays (without duplicates) num ...

  3. 2017/11/13 Leetcode 日记

    2017/11/13 Leetcode 日记 463. Island Perimeter You are given a map in form of a two-dimensional intege ...

  4. 2017/11/20 Leetcode 日记

    2017/11/14 Leetcode 日记 442. Find All Duplicates in an Array Given an array of integers, 1 ≤ a[i] ≤ n ...

  5. 2017/11/9 Leetcode 日记

    2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'res ...

  6. 2017/11/7 Leetcode 日记

    2017/11/7 Leetcode 日记 669. Trim a Binary Search Tree Given a binary search tree and the lowest and h ...

  7. 2017/11/6 Leetcode 日记

    2017/11/6 Leetcode 日记 344. Reverse String Write a function that takes a string as input and returns ...

  8. 2017/11/5 Leetcode 日记

    2017/11/5 Leetcode 日记 476. Number Complement Given a positive integer, output its complement number. ...

  9. 2017/11/3 Leetcode 日记

    2017/11/3 Leetcode 日记 654. Maximum Binary Tree Given an integer array with no duplicates. A maximum ...

随机推荐

  1. Solr.NET快速入门(三)【高亮显示】

    此功能会"高亮显示"匹配查询的字词(通常使用标记),包括匹配字词周围的文字片段. 要启用高亮显示,请包括HighlightingParameters QueryOptions对象, ...

  2. P3808 【模版】AC自动机(简单版)

    题目背景 这是一道简单的AC自动机模版题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 题目描述 给定n个模式串和1个文本串,求有多少个模式串在文本 ...

  3. Java基础3一基础语句

    1.条件语句:所谓的条件语句就是指有选择的去执行部分代码. 包括:if条件语句和switch条件语句 if条件语句: 语法: (1)if(条件语句){ //条件成立时需要执行的代码   } (2)if ...

  4. Android ToolBar自定义图标,关联DrawerLayout

    Android5.0出现了一个可以代替ActionBar的控件ToolBar,使用更加灵活,一般我们使用ToolBar来和DrawerLayout配合使用,官方提供了一个开关类ActionBarDra ...

  5. spring IOC(DI)和AOP

    软件152谭智馗 IOC(Inversion of Control,控制倒转)Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制. DI—Dependency Injecti ...

  6. asp.net URL传递中文参数System.Web.HttpUtility.UrlEncode与Server.UrlEncode的区别

    asp.net URL传递中文参数System.Web.HttpUtility.UrlEncode与Server.UrlEncode的区别(一) HttpUtility.UrlEncode 方法: 对 ...

  7. VS 在代码中括号总是跟着类型后面

    if (OK.Text.Contains("运费")) {// 像这样子,这个大括号不是直接在IF下,而是跟在后面 工具-->选项-->文本编辑器-->C# -- ...

  8. 【Oracle】DBMS_STATS.GATHER_TABLE_STATS分析表

    表分析,简单的说,就是收集表和索引的信息,CBO根据这些信息决定SQL最佳的执行路径.通过对表的分析,可以产生一些统计信息,通过这些信息oracle的优化程序可以进行优化. 语法: DBMS_STAT ...

  9. 京东专业“卖”队友,魅族手环将亮相1206魅蓝note新品发布会

    京东一直是国内顶级的数码产品自营销售渠道,但是,正因为庞大的数据体系和平台特殊性,经常会帮我们发现一些“好玩的”保密性较高的东西,譬如价格.信息.谍照等.而在最新上线的京东超级品牌日活动页面上,专业“ ...

  10. JavaScript数组操作函数

    A: 购物车会有这样的情况,购物车是一个数组,每一个商品是一个对象,分别对应一个id,和一个num ,然后改变商品的时候需要和购物车对比,如果购物车中有这个商品的话,就只改变这个商品对应的id的num ...