• 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是“一位数字”的意思。。。没啥说的,水题

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 class Solution {
     4 public:
     5     vector<int> plusOne(vector<int>& digits) {
     6         int temp=1,n=digits.size();
     7         vector<int> ans(digits);
     8         for(int i=n-1;i>=0&&temp;i--){
     9             ans[i]+=temp;
    10             if(ans[i]>=10){
    11                 ans[i]-=10;
    12                 temp=1;
    13             }else temp=0;
    14         }
    15         if(temp==1){
    16             ans.resize(n+1);
    17             for(int i=1;i<=n;i++){
    18                 ans[i]=ans[i-1];
    19                 ans[0]=1;
    20             }
    21         }
    22         return ans;
    23     }
    24 };
    66

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

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

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

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

     1 class Solution {
     2 public:
     3     vector<int> getRow(int rowIndex) {
     4         int &k=rowIndex;
     5         vector<int> kj(k+1),ans(k+1);
     6         kj[0]=1;
     7         for(int i=1;i<=k;i++){
     8             kj[i]=kj[i-1]*(i+1);
     9         }
    10         for(int i=0;i<=k;i++){
    11             ans[i]=kj[k]/(kj[i]*kj[k-i]);
    12         }
    13         return ans;
    14     }
    15 };
    WA

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

     1 class Solution {
     2 public:
     3     vector<int> getRow(int rowIndex) {
     4         int k=rowIndex+1;
     5         vector<int> ans(k,1);
     6         for(int i=2;i<k;i++){
     7             for(int j=i-1;j>0;j--){
     8                 ans[j]=ans[j]+ans[j-1];
     9             }
    10         }
    11         return ans;
    12     }
    13 };
    119AC

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

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

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

     1 class Solution {
     2 public:
     3     int maxProfit(vector<int>& prices) {
     4         int n=prices.size();
     5         if(n==0)return 0;
     6         int temp=prices[0],ans=0;
     7         for(int i=1;i<n;i++){
     8             temp=temp>prices[i]?prices[i]:temp;
     9             ans=ans<(prices[i]-temp)?(prices[i]-temp):ans;
    10         }
    11         return ans;
    12     }
    13 };
    121

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

  • 相关阅读:
    1 < 2 < 3为true, 3 > 2 > 1为false
    我的第五代选择器Icarus
    浮动不换行
    教你游泳,不会游的看了包你学会!!! 分享
    node.js 一个简单的页面输出
    天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为,所以动心忍性,增益其所不能
    setTimeout和setInterval的使用
    JS window.open()属性
    车牌识别及验证码识别的一般思路
    苹果开发者账号注册流程
  • 原文地址:https://www.cnblogs.com/xuwangzihao/p/6517175.html
Copyright © 2020-2023  润新知