Login
Register
Problem list
Online status
哈基米叮咚鸡胖宝宝踩踩背
:
2025-05-17 15:44:05
最爱的leetcode#1 梦开始的地方 哈希表储存 实现O(n)时间复杂度 #include
using namespace std; void find(vector
nums, int x) { int n = nums.size(); map
m; for (int i = 0; i < n; i++){ if (!m.count(x - nums[i])) { m[nums[i]] = true; } else { cout << "yes" << endl; return; } } cout << "no" << endl; } int main() { int t; cin >> t; while (t--) { int n, x; cin >> n >> x; vector
nums(n); for (int i = 0; i < n; i++) cin >> nums[i]; find(nums, x); } return 0; }
JiaweiPeng
:
2024-01-03 00:05:09
def has_pair_with_sum(arr, x): seen = set() for num in arr: if x - num in seen: return "yes" seen.add(num) return "no" m = int(input()) for _ in range(m): n, x = map(int, input().split()) arr = list(map(int, input().split())) print(has_pair_with_sum(arr, x))
User01
:
2020-11-02 10:54:12
Syntactic requirements?
Post Your Comment