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 ...
随机推荐
- Java基础7一面向对象
1.构造方法: (1)定义:方法名称必须和类名相同,没有返回值,也没有void (2)语法: [访问修饰符] 类名(){ } (3)作用:创建对象.初始化成员变量. (4)构造方法的分类: A.无参数 ...
- 第八课: - 从Microsoft SQL数据库读取
第 8 课 如何从Microsoft SQL数据库中提取数据 In [1]: # Import libraries import pandas as pd import sys from sqlalc ...
- 参数转对象 类似 ?camera=1&travel=0&faceScore=1
parseQueryString(url) { var obj = {}; var keyvalue = []; var key = "", value = "" ...
- python版本及ML库
一:关于Python版本的选择问题 关于Python的选择问题:要看学术界能不能把科学库迁移到Python3. 1:多个版本共用: 最近发现SciPy的最高版本是3.2,只能是退而求其次,不使用最新版 ...
- otool -l 可执行文件结构
otool -l /Users/zzf073/Desktop/FqlMerchantX /Users/zzf073/Desktop/FqlMerchantX: Mach header magic cp ...
- JDK源码中的英文注释翻译(Enum<E extends Enum<E>>)
public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializab ...
- pycharm安装以及简单使用教程
https://www.cnblogs.com/jin-xin/articles/9811379.html 以windows版本举例: 1.首先去Pycharm官网,或者直接输入网址:http:/ ...
- Java将数据以Excel文件形式导出后台代码实现
下面代码实现所需jar包: tomcat-embed-core-8.5.11.jar: commons-lang3-3.0.1.jar: commons-io-2.5.jar: poi-3.9.jar ...
- centos7编译安装mysql5.6
先安装如下依赖包: $ yum -y install make gcc-c++ cmake bison-devel ncurses-devel 下载MySQL5.6.14安装包,https://pa ...
- Django:URL,Views,Template,Models
准备工作:熟悉Django命令行工具 django-admin.py 是Django的一个用于管理任务的命令行工具,常用的命令整理如下: <1> 创建一个django工程 : django ...