Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions Jongeun/Day16/211_DesignAddandSearchWordsDataStructure.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

struct TrieNode
{
TrieNode *ChildNode[26];
bool isEnd;
TrieNode()
{
for (int i = 0; i < 26; i++)
{
ChildNode[i] = nullptr;
}
isEnd = false;
}
};

class WordDictionary
{
public:
WordDictionary()
{
root = new TrieNode();
}

void addWord(string word)
{
TrieNode *cur = root;
for (auto c : word)
{
if (cur->ChildNode[c - 'a'] == nullptr)
{
TrieNode *temp = new TrieNode();
cur->ChildNode[c - 'a'] = temp;
cur = temp;
}
else
{
cur = cur->ChildNode[c - 'a'];
}
}

cur->isEnd = true;
}

bool search(string word)
{
TrieNode *cur = root;
return _search(root, word);
}

bool _search(TrieNode *node, string word)
{
if (word.empty())
{
return node->isEnd == true;
}

TrieNode *cur = node;
for (int i = 0; i < word.size(); i++)
{
if (word[i] == '.')
{
bool searched = false;
for (int j = 0; j < 26; j++)
{
if (cur->ChildNode[j] != nullptr)
{
searched = _search(cur->ChildNode[j], word.substr(i + 1));
}
if (searched == true)
{
return true;
}
}

return false;
}
else if (cur->ChildNode[word[i] - 'a'] == nullptr)
{
return false;
}
else
{
cur = cur->ChildNode[word[i] - 'a'];
}
}

return cur->isEnd == true;
}

private:
TrieNode *root;
};

/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary* obj = new WordDictionary();
* obj->addWord(word);
* bool param_2 = obj->search(word);
*/
37 changes: 37 additions & 0 deletions Jongeun/Day16/46_Permutations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Solution
{
public:
vector<vector<int>> permute(vector<int> &nums)
{
vector<vector<int>> res;
vector<int> ans;
unordered_map<int, int> m;
for (auto i : nums)
{
m[i] = 1;
}
_permute(res, ans, m);
return res;
}

void _permute(vector<vector<int>> &res, vector<int> &ans, unordered_map<int, int> &m)
{
if (ans.size() == m.size())
{
res.push_back(ans);
return;
}

for (auto &it : m)
{
if (it.second == 1)
{
it.second--;
ans.push_back(it.first);
_permute(res, ans, m);
ans.pop_back();
it.second++;
}
}
}
};
24 changes: 24 additions & 0 deletions Jongeun/Day16/973_KClosestPointstoOrigin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution
{
public:
vector<vector<int>> kClosest(vector<vector<int>> &points, int k)
{
priority_queue<pair<double, vector<int>>, vector<pair<double, vector<int>>>, std::greater<pair<double, vector<int>>>> pq; // min heap
vector<vector<int>> result;
for (auto &v : points)
{
double origin = sqrt(pow(v[0], 2) + pow(v[1], 2));
pq.push({origin, v});
}

while (k > 0)
{
auto p = pq.top();
pq.pop();
result.push_back(p.second);
k--;
}

return result;
}
};