每日刷题计划Day[26,27]

13 篇文章 0 订阅
2 篇文章 0 订阅

题源:LeetCode

1706. 球会落何处

class Solution {
public:
    vector<int> findBall(vector<vector<int>>& grid) {
        //思想主要在循环上,真的妙
        int n = grid[0].size();
        vector<int> ans(n, -1);
        for(int j = 0; j < n; j++){
            int col = j;//球的初始列
            for(auto &row : grid){
                int dir = row[col];//挡板方向
                col += dir;//初始列左右移动
                if(col < 0 || col == n || row[col] != dir){
                    // row[col] != dir 移动后同一行格子的挡板方向与之前格子的挡板方向是否一致
                    col = -1;
                    break;
                }
            }
            if(col >= 0)//成功到达底部
                ans[j] = col;
        }
        return ans;
    }
};

2016. 增量元素之间的最大差值

思路:遍历一遍,边比较边记录前缀最小值和答案。

class Solution {
public:
    int maximumDifference(vector<int>& nums) {
        int n = nums.size();
        int ans = -1, premin = nums[0];
        for(int i = 1; i < n; i++){
            if(nums[i] > premin){
                ans = max(ans, nums[i] - premin);
            }else{
                //更新前缀最小值
                premin = nums[i];
            }
        }
        return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值