首页 > 其他分享 >Weekly Contest 311

Weekly Contest 311

时间:2022-09-24 22:13:12浏览次数:86  
标签:val Contest int 311 len next ans self Weekly

Weekly Contest 311

Problem A

Smallest Even Multiple

思路

水题,判一下奇偶就行

代码

class Solution:
    def smallestEvenMultiple(self, n: int) -> int:
        if n%2==0:
            return n
        else:
            return 2*n

Problem B

Length of the Longest Alphabetical Continuous Substring

思路

滑动窗口,从左往右扫就行

代码

class Solution:
    def longestContinuousSubstring(self, s: str) -> int:
        l = 0
        r = 1
        ans = 1
        for i in range(1,len(s)):
            t = s[i]
            if ord(t)-ord(s[i-1])!=1:
                ans = max(ans,r-l)
                l = i
                r = i+1
            else:
                r+=1
        ans = max(ans,r-l)
        return ans

Problem C

Reverse Odd Levels of Binary Tree

思路

BFS,遇到奇数层先存储,到下一层之后再将上面的层反转重新赋值

代码

from queue import Queue
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
        q = Queue()
        q.put([root,0])
        val = []
        tree = []
        while q.empty() == False:
            r,d = q.get()
            if d%2==0:
                if len(val)!=0:
                    val = val[::-1]
                    for i in range(len(val)):
                        tree[i].val = val[i]
                    val = []
                    tree = []
            else:
                val.append(r.val)
                tree.append(r)
            if r.left is not None:
                q.put([r.left,d+1])
                q.put([r.right,d+1])
        if len(val)!=0:
            val = val[::-1]
            for i in range(len(val)):
                tree[i].val = val[i]
        return root

Problem D

Sum of Prefix Scores of Strings

思路

字典树,抄的板子,所以就只有C++的了

代码

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e6 + 10;
char S[maxn];

struct Trie{
    int next[26];
    int cnt;
    void init() {
        cnt = 0;
        memset(next, -1, sizeof next);
    }
}T[maxn];
int le;

void Insert(string s) {
    int p = 0;
    for (int i = 0; i < s.size(); i++) {
        int r = s[i] - 'a';
        if(T[p].next[r] == -1) {
            T[le].init();
            T[p].next[r] = le++;
        }
        p = T[p].next[r];
        T[p].cnt++;

    }
}

int query(string s) {
    int p = 0;
    int ans =0;
    for (int i = 0; i < s.size(); i++) {
        int r = s[i] - 'a';
        if(T[p].next[r] == -1) {
            // cout<<0<<endl;
            return 0;
        }
        ans+=T[p].cnt;
        p = T[p].next[r];
    }
    return ans+T[p].cnt;
}


class Solution {
public:
    vector<int> sumPrefixScores(vector<string>& words) {
        T[0].init();
        le = 1;
        int len = words.size();
        for(int i= 0;i<len;++i){
            Insert(words[i]);
        }
        vector<int> t;
        for(int i= 0;i<len;++i){
            t.push_back(query(words[i]));
        }
        return t;
    }
};

总结

有进步 能靠板子出4题了

标签:val,Contest,int,311,len,next,ans,self,Weekly
From: https://www.cnblogs.com/baihualiaoluan/p/16726787.html

相关文章

  • AtCoder Beginner Contest 268(D-E)
    D-UniqueUsername 题意:给出n个字符串,以任意顺序排列,然后在每两个字符串中间加最少一个"_",然后给出m个字符串,问是否能得出一个字符串,不在这m个字符串中,并且长度在3-16......
  • leetcode 311场周赛总结
    1、最小偶倍数(2413)题目:给你一个正整数n,返回2和n的最小公倍数(正整数)。签到题,奇数的话就*2,偶数直接返回。classSolution{public:intsmallestEvenMultip......
  • AtCoder Beginner Contest 043 题解
    欢迎来到我的算法小屋前言 TaskNameAChildrenandCandies(ABCEdit)BUnhappyHacking(ABCEdit)CBeTogetherDUnbalancedA1)题目描述2......
  • Codeforces Round #819 (Div. 1 + Div. 2) and Grimoire of Code Annual Contest 2022
    Preface明天的CF一定打啊(绝对不咕),今天白天补现代作业,英语作业到哭,而且还要准备四六级,每天逼着自己背单词A.MainakandArray不难发现换中间的数不会影响答案,因此操作......
  • AtCoder Beginner Contest 258
    AtCoderBeginnerContest258LinkA-When?模拟即可.点击查看代码#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;intmain(){intn;......
  • AtCoder Beginner Contest 269
    比赛链接A模拟即可。点击查看代码#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;inta,b,c,d;signedmain(){ cin>>a>>b>>c>>d; cout<<(......
  • [Leetcode Weekly Contest]311
    链接:LeetCode[Leetcode]2413.最小偶倍数给你一个正整数n,返回2和n的最小公倍数(正整数)。根据题意,当n为奇数时,答案为2n,当n为偶数时,答案为n。classSolution......
  • Weekly Contest 310
    WeeklyContest310ProblemAMostFrequentEvenElement思路水题,用map存一下每个偶数数量然后统计就行代码classSolution:defmostFrequentEven(self,nums:......
  • 2022 Jiangsu Collegiate Programming Contest
    A.PENTAKILL!把每个一个人的击杀序列分开,判断是否有连续五个不同的击杀就好#include<bits/stdc++.h>usingnamespacestd;map<string,vector<string>>st;int......
  • 【题解】CF1311E Construct the Binary Tree
    题目链接-->Problem-E-Codeforces题目大意给定\(n\)和\(d\),你需要构造一棵\(n\)个点的二叉树满足所有点的深度之和恰好为\(d\)。\(2≤n,d≤5000\)。分析......