力扣刷题记录9

力扣刷题记录9

今天比较划水,只看了个简单题和中等题。
500. 键盘行
这个没什么说的,直接写了个数组存储然后遍历。但比较开心的是结果很好
在这里插入图片描述
这里是代码


```cpp
class Solution {
public:
    vector<string> findWords(vector<string>& words) {
        int A[70]={1,2,2,1,0,1,1,1,0,1,1,1,2,2,0,0,0,0,1,0,0,2,0,2,0,2,
        -1,-1,-1,-1,-1,-1,
        1,2,2,1,0,1,1,1,0,1,1,1,2,2,0,0,0,0,1,0,0,2,0,2,0,2};
        vector<string> res;
        int flag=1;
        int temp;
        int last;
        for(string word : words)
        {
            flag=1;
            int n=word.size();
            last=word[0]-'A';
            for(int i=1;i<n;i++)
            {
                temp=word[i]-'A';
                if(A[temp]!=A[last])
                {
                    flag=0;
                    break;
                }
                last=temp;
            }
            if(flag)
            res.push_back(word);
        }
        return res;
    }
};
  1. 两个非重叠子数组的最大和
    有一说一,这个题目我的想法和题解类似的。。。O(n)用累加和来解决,但是边界条件出了点问题。。。
    下面给出力扣英文的题解。
    希望学习下这个代码,十分简洁,边界处理很好。
class Solution {
public:
        int maxSumTwoNoOverlap(vector<int>& A, int L, int M) {
        for (int i = 1; i < A.size(); ++i)
            A[i] += A[i - 1];
        int res = A[L + M - 1], Lmax = A[L - 1], Mmax = A[M - 1];
        for (int i = L + M; i < A.size(); ++i) {
            Lmax = max(Lmax, A[i - M] - A[i - L - M]);
            Mmax = max(Mmax, A[i - L] - A[i - L - M]);
            res = max(res, max(Lmax + A[i] - A[i - M], Mmax + A[i] - A[i - L]));
        }
        return res;
    }
    };
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值