Skip to content

Commit

Permalink
Added problem 594 and 217
Browse files Browse the repository at this point in the history
  • Loading branch information
YUXUANCHENG committed Aug 30, 2020
1 parent 7c46dd8 commit addd1d1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
22 changes: 22 additions & 0 deletions 217_duplicate/hash.cpp
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;
}
};
29 changes: 29 additions & 0 deletions 594_harmonious/hashing.cpp
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;
}
};
17 changes: 3 additions & 14 deletions 739_T/stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,11 @@ class Solution
std::vector<int> ans(size + 1, 0);
while (size >= 0)
{
if (s1.empty())
while (!s1.empty() && T.at(size) >= T.at(s1.top()))
{
ans.at(size) = 0;
}
else if (T.at(size) > T.at(s1.top()))
{
while (!s1.empty() && T.at(size) > T.at(s1.top()))
{
s1.pop();
}
ans.at(size) = !s1.empty() ? s1.top() - size : 0;
}
else
{
ans.at(size) = s1.top() - size;
s1.pop();
}
ans.at(size) = !s1.empty() ? s1.top() - size : 0;
s1.push(size);
size--;
}
Expand Down

0 comments on commit addd1d1

Please sign in to comment.