周 赛 补 题

竞赛 - 力扣 (LeetCode)

1.力扣​​​​​​

谢谢这题给我往下做的信心🌹

思路:看代码👍

class Solution {
public:
    int smallestEvenMultiple(int n) {
        if(n%2==0)return n;
        else return n*2;
    }
};

2.力扣​​​​​

遍历字符串,记录每个符合条件的子字符串,再比较大小即可

class Solution {
public:
    int longestContinuousSubstring(string s) {
        int ans=0,res=0;
        int n=s.size();
        for(int i=0;i<n-1;i++){
            if(s[i]==s[i+1]-1)res++;
            else res=0;
            ans=max(ans,res);
        }
        return ans+1;
    }
};

3.力扣

树左右同时进行dfs,遍历点具有对称性
左子树的左子树节点和右子树的右子节点对称,同时遍历的时候进行交换

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    // 遍历对称的二叉树,交换值
    void dfs(TreeNode*  &left,TreeNode* &right,int k)
    {
        if(!left)
        return;
        if(k%2==1)
        {
            int l=left->val;
            left->val=right->val;
            right->val=l;
        }
        dfs(left->left,right->right,k+1);
        dfs(left->right,right->left,k+1);
    }
    TreeNode* reverseOddLevels(TreeNode* root) {
        dfs(root->left,root->right,1);
        return root;
    }
};

因为前两题写出来膨胀了,看到这题想都没想就去做,然后就做不出来了

二叉树这块还得再看看

登录 - AcWing

1.4615. 相遇问题 - AcWing题库

求出x和y的距离,再求甲乙每秒移动的速度,若可整除,输出c/(a+b),否则输出-1

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

signed main()
{
    int n;
    cin>>n;
    while(n--){
        int x,y,a,b;
        cin>>x>>y>>a>>b;
        int c=abs(y-x);
        if(c%(a+b)==0){
            cout<<c/(a+b)<<endl;
        }
        else puts("-1");
    }
}

2.4616. 击中战舰 - AcWing题库

现在有a艘,至少击沉1搜====最多可以摆放(a-1)艘,每个区间,构造时,每一段攻击第b个位置。

#include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;

int main(){
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, a, b, k;
    cin >> n >> a >> b >> k;
    string s;
    cin >> s;

    int cnt = 0;
    int winLen = 0;
    for (int i = 0; i < n; i ++){
        if (s[i] == '1'){
            winLen = 0;
        }else{
            winLen ++;
            if (winLen == b){
                cnt ++;
                winLen = 0;
            }
        }
    }

    int need = cnt - (a - 1);
    cout << need << endl;

    winLen = 0;
    for (int i = 0; i < n; i ++){
        if (s[i] == '1'){
            winLen = 0;
        }else{
            winLen ++;
            if (winLen == b){
                cout << i + 1 << ' ';
                winLen = 0;
                need --;
                if (need == 0){
                    break;
                }
            }
        }
    }
    cout << endl;

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值