Leetcode Weekly Contest 418

楼主: oin1104 (是oin的说)   2024-10-06 13:03:50
不小心错两次 干
不然排名更前面
这次5000 名左右
不上不下
我的徽章...什么时后回来我身边...
q1
三个数字的二进制组合最大的
思路:
全部举出来
```cpp
class Solution {
public:
int btd(string bin) {
int dec = 0;
for (char bit : bin)
{
dec = dec * 2 + (bit - '0');
}
return dec;
}
string tob(int num) {
string binary = "";
while (num > 0) {
binary = (num % 2 == 0 ? "0" : "1") + binary;
num /= 2;
}
return binary;
}
int maxGoodNumber(vector<int>& nums)
{
string s1 ;
string s2 ;
string s3 ;
s1 = tob(nums[0]);
s2 = tob(nums[1]);
s3 = tob(nums[2]);
string t;
t=s1+s2+s3;
int a = btd(t);
t=(s2+s3+s1);
int b = btd(t);
t=(s3+s2+s1);
int c = btd(t);
t=(s1+s3+s2);
int d = btd(t);
t=(s2+s1+s3);
int e = btd(t);
t=(s3+s1+s2);
int f = btd(t);
return max(a ,max(b ,max(c ,max(d ,max(e ,f ) ) ) ) );
}
};
```
q2
给你n个点
还有一些单向的边
第k个点是有问题的
并且他可以指到的点都会跟着有问题
要把有问题的点头删掉
但是如果没问题的点 指向 有问题的点
那那些有问题的点就不删
回传剩下的点
思路:
先对第k点做bfs纪录
然后再从没问题的点bfs
只要遇到有问题的点
那就直接回传原本阵列
不然就遍历纪录 看那些可以
```cpp
class Solution {
public:
vector<int> remainingMethods(int n, int k, vector<vector<int>>& invocations)
{
vector<int> haha(n);
for(int i = 0 ; i < n ; i ++)
{
haha[i] = i;
}
unordered_map<int,vector<int>> path;
vector<int> indeg(n,0);
for(vector<int> kk : invocations)
{
path[kk[0]].push_back(kk[1]);
indeg[kk[1]] ++;
}
vector<int> passed(n,0);
vector<int> sus(n,0);
queue<int> qq;
qq.push(k);
while(!qq.empty())
{
int p = qq.front();
qq.pop();
sus[p] = 1;
passed[p] = 1;
for(int i : path[p])
{
indeg[i]
作者: sixB (6B)   2024-10-06 13:07:00
你好厉害
作者: wu10200512 (廷廷)   2024-10-06 13:24:00
好强
作者: sixB (6B)   2024-10-06 13:54:00
宝 max可以传arraymax({a,b,c,d,e,f})
楼主: oin1104 (是oin的说)   2024-10-06 14:03:00
阿 好像是 我写的好丑

Links booklink

Contact Us: admin [ a t ] ucptt.com