Skip to content

Commit d36e3a4

Browse files
authored
Merge pull request #70 from AlgorithmIsMyLife/20일차_Jongeun
Add day20
2 parents 600048e + cca1460 commit d36e3a4

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Solution
2+
{
3+
public:
4+
vector<vector<string>> partition(string s)
5+
{
6+
vector<vector<string>> res;
7+
vector<string> sub;
8+
_partition(res, sub, s);
9+
return res;
10+
}
11+
12+
void _partition(vector<vector<string>> &res, vector<string> &sub, string s)
13+
{
14+
if (isPalindrom(s))
15+
{
16+
sub.push_back(s);
17+
res.push_back(sub);
18+
sub.pop_back();
19+
}
20+
21+
for (int i = 0; i < s.size() - 1; i++)
22+
{
23+
string left = s.substr(0, i + 1);
24+
if (isPalindrom(left))
25+
{
26+
sub.push_back(left);
27+
_partition(res, sub, s.substr(i + 1));
28+
sub.pop_back();
29+
}
30+
}
31+
}
32+
33+
bool isPalindrom(string &s)
34+
{
35+
int l = 0;
36+
int r = s.size() - 1;
37+
while (l <= r)
38+
{
39+
if (s[l] == s[r])
40+
{
41+
l++;
42+
r--;
43+
}
44+
else
45+
{
46+
return false;
47+
}
48+
}
49+
50+
return true;
51+
}
52+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution
2+
{
3+
public:
4+
vector<string> letterCombinations(string digits)
5+
{
6+
7+
// construct the map
8+
unordered_map<char, string> m;
9+
m['2'] = "abc";
10+
m['3'] = "def";
11+
m['4'] = "ghi";
12+
m['5'] = "jkl";
13+
m['6'] = "mno";
14+
m['7'] = "pqrs";
15+
m['8'] = "tuv";
16+
m['9'] = "wxyz";
17+
18+
vector<string> res;
19+
if (digits == "")
20+
{
21+
return res;
22+
}
23+
24+
_letterCombinations(res, digits, m, "", 0);
25+
return res;
26+
}
27+
28+
void _letterCombinations(vector<string> &res, string &digits, unordered_map<char, string> &m, string comb, int start)
29+
{
30+
if (start == digits.size())
31+
{
32+
res.push_back(comb);
33+
return;
34+
}
35+
36+
for (auto c : m[digits[start]])
37+
{
38+
39+
_letterCombinations(res, digits, m, comb + c, start + 1);
40+
}
41+
}
42+
};

Jongeun/Day20/355_DesignTwitter.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class Twitter
2+
{
3+
public:
4+
Twitter()
5+
{
6+
}
7+
8+
void postTweet(int userId, int tweetId)
9+
{
10+
tweets.push_back({userId, tweetId});
11+
}
12+
13+
vector<int> getNewsFeed(int userId)
14+
{
15+
vector<int> res;
16+
int num = 10;
17+
unordered_set<int> temp = followMap[userId];
18+
for (int i = tweets.size() - 1; i >= 0; i--)
19+
{
20+
if (tweets[i].first == userId || temp.find(tweets[i].first) != temp.end())
21+
{
22+
res.push_back(tweets[i].second);
23+
num--;
24+
}
25+
if (num == 0)
26+
{
27+
break;
28+
}
29+
}
30+
31+
return res;
32+
}
33+
34+
void follow(int followerId, int followeeId)
35+
{
36+
followMap[followerId].insert(followeeId);
37+
}
38+
39+
void unfollow(int followerId, int followeeId)
40+
{
41+
followMap[followerId].erase(followeeId);
42+
}
43+
44+
private:
45+
unordered_map<int, unordered_set<int>> followMap;
46+
vector<pair<int, int>> tweets;
47+
};
48+
49+
/**
50+
* Your Twitter object will be instantiated and called as such:
51+
* Twitter* obj = new Twitter();
52+
* obj->postTweet(userId,tweetId);
53+
* vector<int> param_2 = obj->getNewsFeed(userId);
54+
* obj->follow(followerId,followeeId);
55+
* obj->unfollow(followerId,followeeId);
56+
*/

0 commit comments

Comments
 (0)