GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/algorithms/cpp/twoSum/twoSum.III.cpp at master · weimo/leetcode

Expand file tree

/

twoSum.III.cpp

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
43 lines (39 loc) · 1.21 KB
/

twoSum.III.cpp

Copy path

File metadata and controls

43 lines (39 loc) · 1.21 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Source : https://oj.leetcode.com/problems/two-sum-iii-data-structure-design/
// Author : Hao Chen
// Date : 2014-12-29
/**********************************************************************************
*
* Design and implement a TwoSum class. It should support the following operations: add and find.
*
* add - Add the number to an internal data structure.
* find - Find if there exists any pair of numbers which sum is equal to the value.
*
* For example,
*
* add(1); add(3); add(5);
* find(4) -> true
* find(7) -> false
*
**********************************************************************************/
class TwoSum {
private:
unordered_map<int, int> nums;
public:
//O(1) add
void add(int number) {
nums[number]++;
}
//O(n) find
bool find(int value) {
int one, two;
for(auto it = nums.begin(); it != nums.end(); it++){
one = it->first;
two = value - one;
if ( (one == two && it->second > 1) ||
(one != two && nums.find(two) != nums.end() ) ){
return true;
}
}
return false;
}
};
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.