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