当前位置:网站首页>【leetcode周赛总结】LeetCode第 81 场双周赛(6.25)
【leetcode周赛总结】LeetCode第 81 场双周赛(6.25)
2022-07-05 14:21:00 【Let it beSun】
题目1 2315. 统计星号
思路:
简单题目直接遍历,用一个变量标识是否在竖线对之内
代码:
class Solution {
public int countAsterisks(String s) {
int count = 0;
int d = 0;
for(char c:s.toCharArray()){
if(d==1){
if(c=='|'){
d = 0;
}
}else{
if(c=='|'){
d = 1;
}
if(c=='*'){
count++;
}
}
}
return count;
}
}
题目2 统计无向图中无法互相到达点对数
思路:
并查集,并查集的参考模板见【算法笔记】重点总结并查集_Let it beSun的博客-CSDN博客
主要求每个连通分量的大小
一开始TLE(超时)的思路:求出每个连通分量的大小之后直接用乘法计算
例如:
此时几个联通分量的大小分别为{4,2,1}此时计算 ans = 4*2+4*1+2*1 = 14
但是时间复杂度O(n^2)超时;
计算方法一
所以考虑对于每一个节点,计算节点i不联通的点对数,因为每一个节点分别计算存在重复,所以答案就是求和再除以2。
for(int i=0;i<n;i++){
ans += (long)(n-sz[find(i)]);
}
return ans/2;
计算方法二
可以考虑对于所有的点对数 n*(n-1)/2 ,减去各个联通分量内部的节点对数 a*(a-1)/2
代码:
对应计算方法一:
class Solution {
int[] f = new int[100005];
int[] sz = new int[100005];
public int find(int x){
if(f[x]==x) return x;
return f[x] = find(f[x]);
}
public void unite(int x,int y){
int fx = find(x);
int fy = find(y);
if(fx!=fy){
f[fx] = fy;
sz[fy] += sz[fx];
sz[fx] = 0;
}
}
public long countPairs(int n, int[][] edges) {
for(int i=0;i<n;i++){
f[i] = i;
sz[i] = 1;
}
long ans = 0;
for(int i=0;i<edges.length;i++){
int a = edges[i][0];
int b = edges[i][1];
unite(a,b);
}
for(int i=0;i<n;i++){
ans += (long)(n-sz[find(i)]);
}
return ans/2;
}
}
对应计算方法二:
class Solution {
int[] f = new int[100005];
int[] sz = new int[100005];
public int find(int x){
if(f[x]==-1) return x;
return f[x] = find(f[x]);
}
public void unite(int x,int y){
int fx = find(x);
int fy = find(y);
if(fx!=fy){
f[fx] = fy;
sz[fy] += sz[fx];
sz[fx] = 0;
}
}
public long countPairs(int n, int[][] edges) {
for(int i=0;i<n;i++){
f[i] = -1;
sz[i] = 1;
}
long ans = 0;
for(int i=0;i<edges.length;i++){
int a = edges[i][0];
int b = edges[i][1];
unite(a,b);
}
for(int i=0;i<n;i++){
if(f[i]==-1){
ans += (long)sz[i]*(sz[i]-1)/2;
}
}
return (long)n*(n-1)/2-ans;
}
}
题目3 2317. 操作后的最大异或和
思路
就是统计这个数组中每一位是否出现1,对于所有二进制位,只要这一位在数组中出现过 1
,那么答案里这一位也是 1
。
因此答案就是所有数按位或的结果。复杂度 O(n)。
代码
方法一:直接统计
class Solution {
public int maximumXOR(int[] nums) {
int[] bit = new int[32];
for(int i=0;i<32;i++){
for(int num:nums){
if( ((num>>i)&1) == 1){
bit[i]++;
break;
}
}
}
int res = 0;
int t = 1;
for(int i=0;i<32;i++){
if(i!=0){
t = t*2;
}
res += bit[i]==0?0:t;
}
return res;
}
}
方法二:按位或
public int maximumXOR(int[] nums) {
int ans = 0;
for (int num : nums) ans |= num;
return ans;
}
边栏推荐
- Oneconnect listed in Hong Kong: with a market value of HK $6.3 billion, ye Wangchun said that he was honest and trustworthy, and long-term success
- Longest common subsequence dynamic programming
- 循环不变式
- C语言中限定符的作用
- Thymeleaf common functions
- R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用shadow_mark函数为动画添加静态散点图作为动画背景
- Niuke: intercepting missiles
- dynamic programming
- Shenziyu, the new chairman of Meizu: Mr. Huang Zhang, the founder, will serve as the strategic adviser of Meizu's scientific and technological products
- SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain
猜你喜欢
世界环境日 | 周大福用心服务推动减碳环保
How can non-technical departments participate in Devops?
无密码身份验证如何保障用户隐私安全?
freesurfer运行完recon-all怎么快速查看有没有报错?——核心命令tail重定向
Principle and performance analysis of lepton lossless compression
为什么我认识的机械工程师都抱怨工资低?
Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
分享 12 个最常用的正则表达式,能解决你大部分问题
How to deeply understand the design idea of "finite state machine"?
ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
随机推荐
快消品行业SaaS多租户解决方案,构建全产业链数字化营销竞争力
Thymeleaf common functions
Guofu hydrogen energy rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, and 360million yuan of accounts receivable exceed the revenue
Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting
3W原则[通俗易懂]
Thymeleaf 使用后台自定义工具类处理文本
R语言ggplot2可视化条形图:通过双色渐变配色颜色主题可视化条形图、为每个条形添加标签文本(geom_text函数)
Niuke: intercepting missiles
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
Total amount analysis accounting method and potential method - allocation analysis
无密码身份验证如何保障用户隐私安全?
SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain
Scenario based technology architecture process based on tidb - Theory
Linux下mysql数据库安装教程
[learning notes] connectivity and circuit of graph
Thymeleaf 模板的创建与使用
区间 - 左闭右开
After the microservice project is deployed, static resources and files uploaded to upload cannot be accessed. Solution
R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
CyCa children's physical etiquette Ningbo training results assessment came to a successful conclusion