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

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. js两个页面之间URL传递参数中文乱码

  2. function at line ### more than 60 upvalues

    lua中函数的upvalues是有上限的,在luaconf.h中定义: /*@@ LUAI_MAXUPVALUES is the maximum number of upvalues per func ...

  3. Android学习——Button填充颜色及实现圆角

    在drawable下新建文件夹bt_shape.xml,如下: <?xml version="1.0" encoding="utf-8"?> < ...

  4. ML:流形学习

    很多原理性的东西需要有基础性的理解,还是篇幅过少,所以讲解的不是特别的清晰. 原文链接:http://blog.sciencenet.cn/blog-722391-583413.html 流形(man ...

  5. jQuery 父级,祖先,兄弟,等选择性操作

    jQuery.parent(expr) 找父亲节点,可以传入expr进行过滤,比如$("span").parent()或者$("span").parent(&q ...

  6. Linux搭建oracle数据库

    1.安装前准备   软件硬件要求 操作系统:CentOS 6.4(32bit)Oracle数据库版本:Oracle 10g(10201_database_linux32.zip)最小内存:1G(检查命 ...

  7. [luogu2047 NOI2007] 社交网络 (floyed最短路)

    传送门 输入输出样例 输入样例#1: 4 4 1 2 1 2 3 1 3 4 1 4 1 1 输出样例#1: 1.000 1.000 1.000 1.000 题解 在进行floyed的过程中,顺便更新 ...

  8. 【Computer Vision】 复现分割网络(1)——SegNet

    目录 Tags: ComputerVision 编译 数据处理 训练结果 Reference Tags: ComputerVision 编译 src/caffe/layers/contrastive_ ...

  9. Spring环境搭建及简单demo

    1. Spring框架简介(以下这段话可用于面试求职) Spring为JavaEE开发提供了一个轻量级的解决方案,主要表现为, IOC(或者叫做DI)的核心机制,提供了bean工厂(Spring容器) ...

  10. some untracked working tree files问题解决

    我使用的是idea,情境是在使用git同步代码的时候,出现的错误. 我这里报错是在右上角的显示信息,其中有一个show view的可点击连接 我点击之后将上面展示的文件删除之后重新同步代码,成功.