作者oinishere (是oin捏)
看板Marginalman
標題Re: [閒聊] 每日leetcode
時間Thu May 2 13:52:17 2024
:
: https://reurl.cc/lQeDQj
:
: 2441. Largest Positive Integer That Exists With Its Negative
:
: 給定一不包含0的數列,尋找最大正整數,此正整數k的-k需存在於nums
:
: 回傳正整數k 如果無符合條件的正整數 回傳-1
:
: Example 1:
:
: Input: nums = [-1,2,-3,3]
: Output: 3
: Explanation: 3 is the only valid k we can find in the array.
婷婷:可不可以用unordered map
其實用set也可以捏
反正只是記錄而已
捏
```cpp
class Solution {
public:
int findMaxK(vector<int>& nums)
{
int res = -1;
int len = nums.size();
unordered_set<int> paper;
for(int k : nums)
{
if(paper.find(-k) != paper.end())res = max(res,abs(k));
paper.insert(k);
}
return res;
}
};
```
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.12.18.170 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1714629139.A.68F.html
→ oinishere: 再狠一點 也可以直接開2001大小的陣列 05/02 13:52
推 pysunsun: 你去看我的文 05/02 13:53
→ digua: 大師 05/02 13:56
推 wu10200512: 別卷了 05/02 13:56
推 JIWP: 別卷了 05/02 13:57
推 DJYOSHITAKA: 別捲了 05/02 13:58
推 jensheng09: 大帥 05/02 13:59
推 Che31128: 別卷了 05/02 14:01
→ SecondRun: 卷狗 05/02 14:16