ARTS挑战-第三周

2019-04-06  本文已影响0人  陈_振

Algorithm

给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组。如果不存在符合条件的连续子数组,返回 0。

示例:

输入: s = 7, nums = [2,3,1,2,4,3]

输出: 2

解释: 子数组 [4,3] 是该条件下的长度最小的连续子数组。

class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {

        int l = 0 , r = -1;
        int sum = 0;
        int res = nums.size() + 1;

        while(l < nums.size()) {

            if(r + 1 < nums.size() && sum < s) {
                sum += nums[++r];
            }
            else {
                sum -= nums[l++];
            }

            if(sum >= s) {
                res = min(res, r - l + 1);
            }
        }

        if(res == nums.size() + 1) {
            return 0;
        }
        
        return res;
    }
};

Review

Improving Immutable Object Initialization in Objective-C

本文的单词摘自Arek Holko的博客,记录自己看文档时遇到的生词。

  1. ** drawbacks**
    n. 缺点;退税(drawback的复数)
    美 ['drɔ,bæk]
    eg: In most cases this kind of an initializer is all we need. It's easy to notice its drawbacks, though。

  2. ** counterparts**
    n. 副本;配对物;极相似的人或物
    美 ['kaʊntɚpɑrt]
    eg: We move between mutable and immutable counterparts with -copy and -mutableCopy.

Tip

Ctrl+移动光标命令相关
Ctrl +
A移动到行首
E移动到行尾
K删除到行尾
U删除到行头
N移动到下一行
P移动到上一行
D向右删除一个字母
H向左删除一个字母
F向右移动一个字母
B向左移动一个字母

Share

If you have any doubts about whether an object is, or should be, mutable, go with immutable.

上一篇 下一篇

猜你喜欢

热点阅读