File tree Expand file tree Collapse file tree 2 files changed +45
-0
lines changed
Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ bool containsDuplicate (vector<int >& nums) {
4+ set<int > s;
5+
6+ for (const auto & n : nums) {
7+ auto iter = s.find (n);
8+ if (iter != s.end ()){
9+ return true ;
10+ }
11+ s.insert (n);
12+ }
13+ return false ;
14+ }
15+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ vector<int > twoSum (vector<int >& nums, int target) {
4+
5+ for (int i = 0 ; i < nums.size () - 1 ; i++){
6+ for (int j = i + 1 ; j < nums.size (); j++){
7+ if (nums[i] + nums[j] == target){
8+ return std::vector<int >{i, j};
9+ }
10+ }
11+ }
12+ return {};
13+ }
14+ };
15+
16+ class Solution {
17+ public:
18+ vector<int > twoSum (vector<int >& nums, int target) {
19+ // value, index;
20+ unordered_map<int , int > um;
21+ for (int i = 0 ; i < nums.size (); i++){
22+ int gap = target - nums[i];
23+ if (um.count (gap)){
24+ return {um[gap], i};
25+ }
26+ um[nums[i]] = i;
27+ }
28+ return {};
29+ }
30+ };
You can’t perform that action at this time.
0 commit comments