MCPcopy Create free account
hub / github.com/Ainevsia/Leetcode-Rust / twoSum

Method twoSum

1. Two Sum/Solution.cpp:14–21  ·  view source on GitHub ↗

O(n) using hashmap

Source from the content-addressed store, hash-verified

12public:
13 // O(n) using hashmap
14 vector<int> twoSum(vector<int>& nums, int target) {
15 unordered_map<int, int> map;
16 for (int i=0; i<nums.size(); i++) {
17 auto x = map.find(target - nums[i]);
18 if (x != map.end()) return {x->second, i};
19 map[nums[i]] = i;
20 }
21 }
22};
23
24int main() {

Callers

nothing calls this directly

Calls 1

findMethod · 0.45

Tested by

no test coverage detected