第 361 场 LeetCode 周赛题解

A 统计对称整数的数目

在这里插入图片描述

枚举 x x x

class Solution {
public:int countSymmetricIntegers(int low, int high) {int res = 0;for (int i = low; i <= high; i++) {string s = to_string(i);if (s.size() & 1)continue;int s1 = 0, s2 = 0;for (int k = 0; k < s.size(); k++)if (k < s.size() / 2)s1 += s[k] - '0';elses2 += s[k] - '0';if (s1 == s2)res++;}return res;}
};

B 生成特殊数字的最少操作

在这里插入图片描述

双指针:则若字符串操作完后为 0 0 0 ,设字符串长为 n n n ,则需要 n n n n − 1 n-1 n1 (字符串中含 0)操作使得字符串变为 0 0 0 , 若字符串操作完后至少有两位数字,则其最后两位只能是 { 25 , 50 , 75 , 00 } \{25, 50, 75, 00\} {25,50,75,00} 其中之一,枚举可能的后两位,用双指针计算要得到当前枚举值的最少操作数

class Solution {
public:int minimumOperations(string num) {vector<string> tar{"25", "50", "75", "00"};int n = num.size();int res = num.find('0') == num.npos ? n : n - 1;for (auto &s: tar) {int i = s.size() - 1;int j = n - 1;int cur = 0;//得到当前枚举值的最少操作数for (; i >= 0 && j >= 0;) {if (s[i] == num[j]) {i--;j--;} else {j--;cur++;}}if (i < 0)res = min(res, cur);}return res;}
};

C 统计趣味子数组的数目

在这里插入图片描述
在这里插入图片描述

前缀和:设数组 l i li li 有: l i i = { 1 , n u m s [ i ] % m o d = k 0 , n u m s [ i ] % m o d ≠ k li_i=\left\{\begin{matrix} 1 & , nums[i]\%mod=k \\ 0 & , nums[i]\%mod\ne k \end{matrix}\right. lii={10,nums[i]%mod=k,nums[i]%mod=k,设 l i li li 上的前缀和为 p s i = ( ∑ j = 0 j < i l i i ) % m o d ps_i=(\sum_{j=0}^{j<i} li_i)\%mod psi=(j=0j<ilii)%mod ,设子数组 n u m s [ l , r ] nums[l,r] nums[l,r] 为趣味子数组,则有: ( p s r + 1 − p s l ) % m o d = k (ps_{r+1}-ps_{l})\%mod=k (psr+1psl)%mod=k,即有 p s l = ( ( p s r + 1 − k ) % m o d + m o d ) % m o d ps_l=((ps_{r+1}-k)\%mod+mod)\%mod psl=((psr+1k)%mod+mod)%mod

class Solution {
public:using ll = long long;long long countInterestingSubarrays(vector<int> &nums, int modulo, int k) {unordered_map<int, ll> cnt;//cnt[val]: 前缀和val出现的次数cnt[0] = 1;//前缀为空int s = 0;//当前前缀和ll res = 0;for (int i = 0; i < nums.size(); i++) {if (nums[i] % modulo == k)s = (s + 1) % modulo;int s_l = ((s - k) % modulo + modulo) % modulo;res += cnt[s_l];cnt[s]++;}return res;}
};

D 边权重均等查询

在这里插入图片描述
在这里插入图片描述

倍增+枚举:1)预处理:设 0 0 0 为树的根节点,枚举边的权重 w _ i d w\_id w_id,从树根开始 d f s dfs dfs ,计算各节点 u u u 到树根的路径上的边数 l e v e l [ u ] level[u] level[u],以及节点 u u u 到树根的路径上边权重为 w _ i d w\_id w_id 的边的数目 s [ u ] [ w _ i d ] s[u][w\_id] s[u][w_id],求倍增数组 p p p p [ u ] [ j ] p[u][j] p[u][j]为与 u u u 距离为 2 j 2^j 2j的祖先节点。2)对一个查询 ( a , b ) (a,b) (a,b),用倍增的方式求 a a a b b b 的最近公共祖先 c c c ,然后枚举 w _ i d w\_id w_id ,将 a a a b b b 间路径上的边的边权统一为 w _ i d w\_id w_id 的操作数为: ( l e v e l [ a ] − l e v e l [ c ] − ( s [ a ] [ w _ i d ] − s [ c ] [ w _ i d ] ) ) + ( l e v e l [ b ] − l e v e l [ c ] − ( s [ b ] [ w _ i d ] − s [ c ] [ w _ i d ] ) ) \left ( level[a] - level[c] - (s[a][w\_id] - s[c][w\_id]) \right ) + \left ( level[b] - level[c] - (s[b][w\_id] - s[c][w\_id]) \right ) (level[a]level[c](s[a][w_id]s[c][w_id]))+(level[b]level[c](s[b][w_id]s[c][w_id]))

class Solution {
public:vector<int> minOperationsQueries(int n, vector<vector<int>> &edges, vector<vector<int>> &queries) {vector<pair<int, int>> e[n];//邻接表int mx_w = 0, mn_w = INT32_MAX;//最大权重、最小权重for (auto &ei: edges) {e[ei[0]].emplace_back(ei[1], ei[2]);e[ei[1]].emplace_back(ei[0], ei[2]);mx_w = max(mx_w, ei[2]);mn_w = min(mn_w, ei[2]);}int level[n], s[n][27];int p[n][15];function<void(int, int, int, int, int)> dfs = [&](int cur, int par, int lev, int sum, int w_id) {if (w_id == mn_w)//倍增数组一轮dfs即可计算for (int i = 0; i < 15; i++)p[cur][i] = i != 0 ? p[p[cur][i - 1]][i - 1] : par;level[cur] = lev;s[cur][w_id] = sum;for (auto &[j, w]: e[cur])if (j != par)dfs(j, cur, lev + 1, w == w_id ? sum + 1 : sum, w_id);};for (int i = mn_w; i <= mx_w; i++)//枚举w_iddfs(0, 0, 0, 0, i);vector<int> res;res.reserve(queries.size());for (auto &qi: queries) {int a = qi[0], b = qi[1];if (a == b) {res.push_back(0);continue;}if (level[a] < level[b])swap(a, b);int c = a;//c最终为a和b的最近公共祖先for (int step = level[a] - level[b], ind = 0; step >= (1 << ind); ind++)if (step >> ind & 1)c = p[c][ind];if (c != b) {int b_ = b;for (int ind = 14; ind >= 0; ind--) {if (p[c][ind] != p[b_][ind]) {c = p[c][ind];b_ = p[b_][ind];}}c = p[c][0];}int res_i = INT32_MAX;for (int w_id = mn_w; w_id <= mx_w; w_id++) {//枚举w_idint t1 = level[a] - level[c] - (s[a][w_id] - s[c][w_id]);int t2 = level[b] - level[c] - (s[b][w_id] - s[c][w_id]);res_i = min(res_i, t1 + t2);}res.push_back(res_i);}return res;}
};

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/97009.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

openGauss学习笔记-59 openGauss 数据库管理-相关概念介绍

文章目录 openGauss学习笔记-59 openGauss 数据库管理-相关概念介绍59.1 数据库59.2 表空间59.3 模式59.4 用户和角色59.5 事务管理 openGauss学习笔记-59 openGauss 数据库管理-相关概念介绍 59.1 数据库 数据库用于管理各类数据对象&#xff0c;与其他数据库隔离。创建数据…

OPENCV实现计算描述子

1、计算描述子 kp,des = sift.computer(img,kp) 2、其作用是进行特征匹配 3、同时计算关键点和描述 3.1、kp,des = sift.detectAnd Computer(img,...)

Spark-Core核心算子

文章目录 一、数据源获取1、从集合中获取2、从外部存储系统创建3、从其它RDD中创建4、分区规则—load数据时 二、转换算子(Transformation)1、Value类型1.1 map()_1.2 mapPartitions()1.3 mapPartitionsWithIndex(不常用)1.4 filterMap()_扁平化&#xff08;合并流&#xff09;…

Vue框架--Vue中的事件

1.事件处理 事件的基本使用: (1).使用v-on:xxx 或 @xxx 绑定事件,其中xxx是事件名; (2).事件的回调需要配置在methods对象中,最终会在vm上; (3).methods中配置的函数,不要用箭头函数!否则this就不是vm了; (4).methods中配置的函数,都是被Vue所管理的函数,this的…

javaweb入门版学生信息管理系统-增删改查+JSP+Jstl+El

dao public class StudentDao {QueryRunner queryRunner QueryRunnerUtils.getQueryRunner();//查询全部学生信息public List<Student> selectStudent(){String sql "select * from tb_student";List<Student> students null;try {students queryRunn…

江苏移动基于OceanBase稳步创新推进核心数据库分布式升级

*本文首发自《中国电信业》 数字经济时代&#xff0c;数据库作为企业核心数据存储、处理、挖潜等方面的关键载体&#xff0c;重要性日益凸显。对于运营商而言&#xff0c;数据库具有行业用户数量多、访问数量多、业务复杂度高、数据安全性高、响应要求性高以及需要 7*24 小时服…

Android基础之Activity生命周期

Activity是Android四大组件之一、称为之首也恰如其分。 Activity直接翻译为中文叫活动。在Android系统中Activity就是我看到的一个完整的界面。 界面中看到的TextView(文字&#xff09;、Button(按钮)、ImageView&#xff08;图片&#xff09;都是需要Activity来承载的。 总…

肖sir __linux__面试题和考核05

面试题 1、查看linux中第11行到第20行的数据&#xff08;比如文档a 有30行&#xff09; 方法1&#xff1a;tail -n 11 mm |head -n10 n 表示从第10行开始&#xff0c;取前10行 方法2&#xff1a;head -n -10 mm| tail -n 10 表示从末尾第10行开始&#xff0c;最后10行 方法3&am…

联合教育部高等学校科学研究发展中心,阿依瓦科技创新教育专项正式发布!

7 月 24 日&#xff0c;教育部科技发展中心官网发布了《中国高校产学研创新基金&#xff0d;阿依瓦科技创新教育专项申请指南》。 针对高校在人工智能、智能制造、智慧校园、大数据等领域科研和教研的创新研究&#xff0c;教育部高等学校科学研究发展中心与阿依瓦(北京)技术有…

如何实现自定义数据库账号密码加解密

背景 在实际的项目开发中我们有时候基于安全考虑需要在项目配置文件中对数据库账号密码做加密处理&#xff0c;这个时候我们就可以使用jasypt这个组件来实现。如果有些项目安全等级要求比较高&#xff0c;可能加密的算法需要自定义或者使用SM4国密算法来实现加解密&#xff0c…

《Web安全基础》04. 文件上传漏洞

web 1&#xff1a;文件上传漏洞2&#xff1a;WAF 绕过2.1&#xff1a;数据溢出2.2&#xff1a;符号变异2.3&#xff1a;数据截断2.4&#xff1a;重复数据 本系列侧重方法论&#xff0c;各工具只是实现目标的载体。 命令与工具只做简单介绍&#xff0c;其使用另见《安全工具录》…

给视频添加背景图片,让它们更具魅力!

想要让你的视频更加出彩吗&#xff1f;给它们添加背景图片是不错的选择&#xff01;但是&#xff0c;如何做到呢&#xff1f;不用担心&#xff0c;我们的视频剪辑高手可以帮助你轻松实现&#xff01;我们提供多种背景图片选择&#xff0c;你可以根据自己的喜好和需求进行选择。…