Replies: 34 comments 10 replies
-
请教各位大佬我没看懂最后的优化,有愿意帮忙解释的吗?谢谢! |
Beta Was this translation helpful? Give feedback.
-
自己用草稿纸演算,a相当一个桶数组 |
Beta Was this translation helpful? Give feedback.
-
思想是贪心的思想叭 |
Beta Was this translation helpful? Give feedback.
-
解法一和解法二并不完全相同比如数组中有零这个元素是有出入,更正应该很简单,不说了 |
Beta Was this translation helpful? Give feedback.
-
bool数组,0代表false, 1代表true 确实在纸上画画就好懂些 |
Beta Was this translation helpful? Give feedback.
-
最后那个优化,用 C++11 的 set 写,是这种风格(更容易懂,但是复杂度从 O(1) 变成了 O(log(n))) #include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> a = {1, 2, 3, 4, -1, -2, -4, 3}; // a 也可以写成数组
set<int> sol;
int ans = 0;
for (auto &&i : a)
{
if (sol.count(i)) // 1
{
ans++;
}
sol.insert(-i); // 2
}
cout << ans;
} 1 和 2 处的 |
Beta Was this translation helpful? Give feedback.
This comment was marked as spam.
This comment was marked as spam.
-
哈希数组,使用数组索引表示数字,索引位置的值表示是否遍历过该值。在这里只要确定与当前遍历位置的值的相反数存在不存在就可以统计一次了。 |
Beta Was this translation helpful? Give feedback.
-
@bored17 您看了吗? |
Beta Was this translation helpful? Give feedback.
-
哈希法 |
Beta Was this translation helpful? Give feedback.
-
总感觉那个越界了,met少了一位。而且那个MAXN也没有注释取的是绝对值后的最大值。 |
Beta Was this translation helpful? Give feedback.
-
"一个数组中的数互不相同,求其中和0为的数对的个数。" |
Beta Was this translation helpful? Give feedback.
-
数有正有负,开2倍空间才能将负数,正数都表示出来,数num在空间的位置为num+MAXN。判断num能不能配对,要看-num是否在空间里面。 |
Beta Was this translation helpful? Give feedback.
-
请问++a和a++为何偏向前者 |
Beta Was this translation helpful? Give feedback.
-
我也感觉 MAXN应该是指数组a每个元素取绝对值后的最大值 但是met长度应该是2*MAXN+1 因为除了0左右两侧的元素 还有元素0 所以要单独加1 |
Beta Was this translation helpful? Give feedback.
-
数组有0元素不能乘2吧 |
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
maxn如果等于3,met长度为6,下标0到5。如果a[i]=3,那么met[a[i] + MAXN]=met[6]。根本不存在啊? |
Beta Was this translation helpful? Give feedback.
-
熄灯问题java AC:
|
Beta Was this translation helpful? Give feedback.
-
萌新讲一下我对最后优化的简单理解 |
Beta Was this translation helpful? Give feedback.
-
https://oi-wiki.cf/basic/enumerate/
Beta Was this translation helpful? Give feedback.
All reactions