2017-3-7 leetcode 66 119 121
今天纠结了一整天
==============================================================
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的更多相关文章
- 2017/11/22 Leetcode 日记
2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...
- 2017/11/21 Leetcode 日记
2017/11/21 Leetcode 日记 496. Next Greater Element I You are given two arrays (without duplicates) num ...
- 2017/11/13 Leetcode 日记
2017/11/13 Leetcode 日记 463. Island Perimeter You are given a map in form of a two-dimensional intege ...
- 2017/11/20 Leetcode 日记
2017/11/14 Leetcode 日记 442. Find All Duplicates in an Array Given an array of integers, 1 ≤ a[i] ≤ n ...
- 2017/11/9 Leetcode 日记
2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'res ...
- 2017/11/7 Leetcode 日记
2017/11/7 Leetcode 日记 669. Trim a Binary Search Tree Given a binary search tree and the lowest and h ...
- 2017/11/6 Leetcode 日记
2017/11/6 Leetcode 日记 344. Reverse String Write a function that takes a string as input and returns ...
- 2017/11/5 Leetcode 日记
2017/11/5 Leetcode 日记 476. Number Complement Given a positive integer, output its complement number. ...
- 2017/11/3 Leetcode 日记
2017/11/3 Leetcode 日记 654. Maximum Binary Tree Given an integer array with no duplicates. A maximum ...
随机推荐
- WinForm进程 线程
进程主要调用另一程序,线程 分配工作. 一.进程: 进程是一个具有独立功能的程序关于某个数据集合的一次运行活动.它可以申请和拥有系统资源,是一个动态的概念,是一个活动的实体.Process 类,用来操 ...
- DropDownListFor
- JavaScript数组的操作
<html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...
- 3rd 逻辑运算符的基本用法
03.01_Java语言基础(逻辑运算符的基本用法)(掌握) A:逻辑运算符有哪些 &,|,^,! &&,|| B:案例演示 逻辑运算符的基本用法 注意事项: a:逻辑运算符一 ...
- DataFrame与数据库的相互转化
在Spark中,Dataframe简直可以称为内存中的文本文件. 就像在电脑上直接操作txt. csv. json文件一样简单. val sparkConf = new SparkConf().set ...
- (转)基于MVC4+EasyUI的Web开发框架经验总结(1)-利用jQuery Tags Input 插件显示选择记录
http://www.cnblogs.com/wuhuacong/p/3667703.html 最近花了不少时间在重构和进一步提炼我的Web开发框架上,力求在用户体验和界面设计方面,和Winform开 ...
- JeeSite 4.0 规划(二)
==== 点击放大查看 ==== ==== 点击放大查看 ====
- MongoDB_可视化工具Robo 3T
Robo 3T可以对MongoDB进行可视化操作. Robo 3T安装 官网下载地址:https://robomongo.org/ 进入官网,点击下载,Studio 3T功能更全面,基础功能是免费的, ...
- 路飞学城Python-Day59(第五模块思维导图)
- POJ 2115 C Looooops( 简单拓欧 + 快速幂 )
链接:传送门 题意:题目中给出一个循环 for (variable = A; variable != B; variable += C) ,这个东东还需要 mod 2^k 问至少多次能退出,如果进入死 ...