当前位置:网站首页>【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;
}边栏推荐
- Make the seckill Carnival more leisurely: the database behind the promotion (Part 2)
- The forked VM terminated without saying properly goodbye
- R语言ggplot2可视化密度图:按照分组可视化密度图、自定义配置geom_density函数中的alpha参数设置图像透明度(防止多条密度曲线互相遮挡)
- 软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】
- 【学习笔记】阶段测试1
- R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the coef function to obtain the log odds ratio corresponding to eac
- TDengine 社区问题双周精选 | 第三期
- Loop invariant
- LeetCode_ 67 (binary sum)
- 非技术部门,如何参与 DevOps?
猜你喜欢

The IPO of Ruineng industry was terminated: the annual revenue was 447million and it was planned to raise 376million
![Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]](/img/c2/a5f5fe17a6bd1f6f9df828ddd224d6.png)
Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]

Thymeleaf 使用后台自定义工具类处理文本

Make the seckill Carnival more leisurely: the database behind the promotion (Part 2)

ASP.NET大型外卖订餐系统源码 (PC版+手机版+商户版)

Tiflash compiler oriented automatic vectorization acceleration

Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million

Thymeleaf 模板的创建与使用

快消品行业SaaS多租户解决方案,构建全产业链数字化营销竞争力

LeetCode_ 2 (add two numbers)
随机推荐
tidb-dm报警DM_sync_process_exists_with_error排查
[learning notes] stage test 1
Introduction, installation, introduction and detailed introduction to postman!
C - Divisors of the Divisors of An Integer Gym - 102040C
周大福践行「百周年承诺」,真诚服务推动绿色环保
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the coef function to obtain the log odds ratio corresponding to eac
区间 - 左闭右开
Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management
【学习笔记】阶段测试1
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
Webrtc learning (II)
Use the word "new" to attract curious people
Hongmeng fourth training
Principle and performance analysis of lepton lossless compression
乌卡时代下,企业供应链管理体系的应对策略
What category does the Internet of things application technology major belong to
Catch all asynchronous artifact completable future
Judge whether the variable is an array
微服务项目部署后,无法访问静态资源,无法访问到上传到upload中的文件,解决办法
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)