-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c46dd8
commit addd1d1
Showing
3 changed files
with
54 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <vector> | ||
#include <unordered_set> | ||
|
||
class Solution { | ||
public: | ||
bool containsDuplicate(std::vector<int>& nums) { | ||
bool result = false; | ||
std::unordered_set<int> set; | ||
for (int num : nums) | ||
{ | ||
auto find_result = set.find(num); | ||
if (find_result == set.end()) | ||
set.insert(num); | ||
else | ||
{ | ||
result = true; | ||
break; | ||
} | ||
} | ||
return result; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <vector> | ||
#include <unordered_map> | ||
#include <math.h> | ||
|
||
class Solution { | ||
public: | ||
int findLHS(std::vector<int>& nums) { | ||
int result = 0; | ||
std::unordered_map<int, int> map; | ||
for (int num : nums) | ||
{ | ||
auto find_result = map.find(num); | ||
if (find_result == map.end()) | ||
map.emplace(num,1); | ||
else | ||
find_result->second ++; | ||
} | ||
for (auto iterat = map.begin(); iterat != map.end(); iterat ++) | ||
{ | ||
auto find_result = map.find(iterat->first + 1); | ||
if (find_result != map.end()) | ||
{ | ||
int temp = iterat -> second + find_result -> second; | ||
result = std::max(result, temp); | ||
} | ||
} | ||
return result; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters