首页 > 其他分享 >第 321 场周赛

第 321 场周赛

时间:2023-01-07 00:00:38浏览次数:56  
标签:周赛 cnt ListNode nums int Solution next 321

1.找出中枢整数

找出中枢整数

Solution

class Solution {
public:
    int pivotInteger(int n) {
        int sum = (1 + n)* n / 2;
        int tmp = (int)sqrt(sum);
        return tmp * tmp == sum ? tmp : -1;
    }
};

2.追加字符以获得子序列

追加字符以获得子序列

Solution

class Solution {
public:
    int appendCharacters(string s, string t) {
        int i,j;
        for(i = 0, j = 0;i < s.size();i++){
            if(s[i] == t[j]){
                j++;
            }
        }
        return t.size() - j;
    }
};

3.从链表中移除节点

从链表中移除节点

Solution

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNodes(ListNode* head) {
        //将链表放入数组
        vector<int>a;
        for(ListNode *i = head; i != nullptr;i = i->next){
            a.push_back(i->val);
        }
        //判断哪些数可以放入
        int n = a.size();
        int maxnn = -1;
        vector<int>ans;
        for(int i = n-1; i >= 0;i--){
            if(maxnn <= a[i]) ans.push_back(a[i]);
            maxnn = max(maxnn, a[i]);
        }
        reverse(ans.begin(), ans.end());
        //建立链表
        ListNode *fake = new ListNode();
        ListNode *now = fake;
        for (int x : ans) {
            ListNode *node = new ListNode(x);
            now->next = node;
            now = node;
        }
        ListNode *ret = fake->next;
        delete fake;
        return ret;
    }
};

4.统计中位数为 K 的子数组

统计中位数为 K 的子数组

Solution

class Solution {
public:
    int countSubarrays(vector<int> &nums, int k) {
        int pos = find(nums.begin(), nums.end(), k) - nums.begin(), n = nums.size();
        unordered_map<int, int> cnt;
        cnt[0] = 1; // i=pos 的时候 c 是 0,直接记到 cnt 中,这样下面不是大于就是小于
        for (int i = pos + 1, c = 0; i < n; ++i) {
            c += nums[i] > k ? 1 : -1;
            ++cnt[c];
        }

        int ans = cnt[0] + cnt[1]; // i=pos 的时候 c 是 0,直接加到答案中,这样下面不是大于就是小于
        for (int i = pos - 1, c = 0; i >= 0; --i) {
            c += nums[i] < k ? 1 : -1;
            ans += cnt[c] + cnt[c + 1];
        }
        return ans;
    }
};

标签:周赛,cnt,ListNode,nums,int,Solution,next,321
From: https://www.cnblogs.com/TTS-TTS/p/17031965.html

相关文章

  • LeetCode第 322 场周赛
    1.回环句回环句SolutionclassSolution{public:boolisCircularSentence(stringsentence){intn=sentence.size(),i=0;if(......
  • 第 93 场双周赛
    1.数组中字符串的最大值数组中字符串的最大值SolutionclassSolution{public:intmaximumValue(vector<string>&strs){intans=-1;for(......
  • 【LeetCode周赛-312】子数组按位与最大值、并查集(图)
    周赛链接:​​​https://leetcode.cn/contest/weekly-contest-312/​​A.2418.按身高排序题目描述:给你一个字符串数组names,和一个由互不相同的正整数组成的数组heig......
  • 第 324 场周赛
    1.统计相似字符串对的数目统计相似字符串对的数目SolutionclassSolution{public:intsimilarPairs(vector<string>&words){unordered_map<int,int......
  • 第 326 场周赛
    1.统计能整除数字的位数统计能整除数字的位数SolutionclassSolution{public:intcountDigits(intnum){intans=0;intn=num;......
  • AcWing第84场周赛
    本蒟蒻第一次AK周赛第一题、最大数量本题使用桶排序代码#include<bits/stdc++.h>usingnamespacestd;intm[10005];intmain(){intn;cin>>n;f......
  • CCNUACM寒假培训第二周周赛部分题解(ACF)
    A题大意:给出n个数,每次可以选择任意一个数进行加一操作,可执行k次,求最大值可能的最大最小值考虑最大值最大,即所有操作都对初始n个数中的最大值进行,答案即max(a1,.....,an)+......
  • [第326场周赛]分解质因数,埃氏筛,欧拉筛
    leetcode新年福利,本次周赛没有Hard难度的题目,然后我就第一次AK了~总的来说不是很难,涉及到了三个算法,在此记录一下。分解质因数题目链接:​​6279.数组乘积中的不同质因数数......
  • ACWING 第 84 场周赛 ABC
    来水一篇博客:)https://www.acwing.com/activity/content/competition/problem_list/2742/难度偏低(三题都cf800的难度),就不写详解了4788.最大数量#include<bits/stdc++......
  • LeetCode第 94 场双周赛
    1.最多可以摧毁的敌人城堡数目题目最多可以摧毁的敌人城堡数目Solution可以第一重循环找到\(1\),然后从该位置分别向左和向又寻找\(-1\),寻找过程中遇到\(1\)则停止,不......