周 赛 补 题

 AcWing 58场周赛

1.

4488. 寻找1 - AcWing题库高质量的算法题库https://www.acwing.com/problem/content/4491/

简单的遍历查找

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int a[100000];
    int n;
    cin >> n;
    for(int i=0;i<n;i++)
    {
        cin >> a[i];
        if(a[i]==1)
        {
            cout << "YES";
            break;
        }
        if(i==n-1)
        {
            cout << "NO";
            break;
        }
    }
    return 0;
}

也可以不用数组

读入n个数,然后判断只要等于1,输出YES,然后退出
如果全部输入完还没有,就输出NO

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n,i,c;
    cin>>n;
    for(i=1;i<=n;i++){
        cin>>c;
        if(c==1){
            cout<<"YES";
            return 0;
        }
    } 
    cout<<"NO";
}

2.

4489. 最长子序列 - AcWing题库高质量的算法题库https://www.acwing.com/problem/content/4492/用双指针,求出最大子序列长度即可

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main(){
    int N;
    cin >> N;
    int a[N + 1];
    for (int i = 0; i < N; i ++ ) cin >> a[i];
    //双指针
    int cnt = 1;
    for (int i = 0; i < N; i ++ ){
        int j = i + 1;
        while(j < N && a[j] <= a[j - 1] * 2) j ++;
        cnt = max(cnt, j - i);
        i = j - 1;  
    }

    cout << cnt;

    return 0;

}

力扣第300场周赛

1.力扣https://leetcode.cn/problems/decode-the-message/使用hash表,首先遍历一次key,获得hash表,再遍历一遍message去获得答案

class Solution {
public:
    string decodeMessage(string key, string message) {
        string res;
        unordered_map<char,char>m;
        string s="abcdefghijklmnopqrstuvwxyz"; //字母表
        int n=0;
        for(auto& c:key)
        {
            //此字母没映射过,且不是空格
            if(m.count(c)==0 && c!=' ')
            {
                m[c]=s[n];
                n++;
            }
        }
        for(auto& b:message)
        {
            if(b==' ') res+=' ';
            else
                res+=m[b];
        }
        return res;
    }
};

2.

力扣https://leetcode.cn/problems/spiral-matrix-iv/

按顺时针控制上下左右4个方向来往矩阵中填入数据

class Solution {
public:
    vector<vector<int>> spiralMatrix(int m, int n, ListNode* head) {
        vector<vector<int>> ret(m, vector<int>(n , -1)) ;
        ListNode* tmp = head;
        int xs = -1, xe = n, ys = -1, ye = m, i = 0, j = 0, col = 0;
        while (tmp){
            
            if ((col % 4) == 0){
                ret[i][j] = tmp->val;
                j ++;
                if (j == xe){
                    col ++;
                    j --;
                    i ++;
                    ys ++;
                }
            } else if ((col % 4) == 1){
                ret[i][j] = tmp->val;
                i ++;
                if (i == ye){
                    xe --;                 
                    col ++;
                    i --;
                    j -- ;
                }
            } else if ((col % 4) == 2){
                ret[i][j] = tmp->val;
                j --;
                if (j == xs){
                    ye --;
                    col ++;
                    j ++ ;
                    i --;  
                }
            }else {
                ret[i][j] = tmp->val;
                i --;
                if (i == ys){
                    xs ++;
                    col ++;
                    i ++ ;
                    j ++;
                }
            }
            
            
            tmp = tmp->next;
            
        }
        return ret ;
        
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值