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 ...
随机推荐
- NFS 开机自动挂载共享目录
开机自动挂载: 如果服务端或客户端的服务器重启之后需要手动挂载,我们可以加入到开机自动挂载 在服务端/客户端的/etc/fstab里添加 192.168.22.204:/opt/filestore ...
- Nginx介绍及知识点(摘抄)
正向代理是把自己的网络环境切换成代理的网络 反向代理是代理机器返回给我要我的资源 本文借鉴参考于http://tengine.taobao.org/book/chapter_02.html. 属于纯干 ...
- todo reading
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind https ...
- C++序列化使用
error C2248 无法访问私有成员 :原因 ifstream 作为参数必须传引用! (1):C++使用STL序列化:原文链接:http://blog.csdn.net/pandaxcl/arti ...
- 创建一个dynamics CRM workflow (五) - Deploy Custom Workflows
我们打开plugin registeration tool. 注册一个新的assembly. custom workflow 和 plugin注册的方法还有些不同. 这一步custom workflo ...
- jQuery添加新的元素
append() - 在被选元素的结尾插入内容 prepend() - 在被选元素的开头插入内容 after() - 在被选元素之后插入内容 before() - 在被选元素之前插入内容 $(&quo ...
- Java中成员变量和局部变量区别
在类中的位置不同 重点 成员变量:类中,方法外 局部变量:方法中或者方法声明上(形式参数) 作用范围不一样 重点 成员变量:类中 局部变量:方法中 初始化值的不同 重点 成员变量:有默认值 局部变量: ...
- utf8_general_ci、utf8_unicode_ci和utf8_bin的区别(转载)
例如: CREATE DATABASE IF NOT EXISTS redmine DEFAULT CHARSET utf8 COLLATE utf8_general_ci; CREATE DATAB ...
- 如何检查Windows网络通信端口占用
最近本地测试jsp程序发现tomcat启动失败,无法监听8080端口,也没记得别的什么程序占用了8080端口,干脆就改成了8090端口先用着.今天找了找Windows上查看网络通信端口占用的方法. 先 ...
- /proc/vmstat 详解
参考 #cat /proc/vmstat nr_free_pages 20223354 nr_alloc_batch 899 nr_inactive_anon 393025 nr_active_ano ...