Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 45f4591

Browse files
author
kaidul
committed
650 problems solved
1 parent 025a546 commit 45f4591

12 files changed

+632
-443
lines changed

‎README.md

Lines changed: 394 additions & 391 deletions
Large diffs are not rendered by default.

‎source-code/Encode_and_Decode_TinyURL.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,45 @@
11
class Solution {
2+
public:
3+
unordered_map<string, string> url2Code;
4+
unordered_map<string, string> code2Url;
5+
6+
string alphaNums = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
7+
int ENCODE_LENGTH = 6;
8+
9+
string generateCode(string const& longUrl) {
10+
string code = "";
11+
for (int i = 0; i < ENCODE_LENGTH; i++) {
12+
code += alphaNums[rand() % alphaNums.length()];
13+
}
14+
15+
return code;
16+
}
17+
18+
// Encodes a URL to a shortened URL.
19+
string encode(string longUrl) {
20+
if (url2Code.find(longUrl) != url2Code.end()) {
21+
return "http://tinyurl.com/" + url2Code[longUrl];
22+
}
23+
string code;
24+
do {
25+
code = generateCode(longUrl);
26+
} while (code2Url.find(code) != code2Url.end());
27+
code2Url[code] = longUrl;
28+
url2Code[longUrl] = code;
29+
30+
return "http://tinyurl.com/" + code;
31+
}
32+
33+
// Decodes a shortened URL to its original URL.
34+
string decode(string shortUrl) {
35+
string code = shortUrl.substr(shortUrl.length() - ENCODE_LENGTH, ENCODE_LENGTH);
36+
return code2Url[code];
37+
}
38+
};
39+
40+
41+
// joke
42+
class Solution {
243
public:
344

445
// Encodes a URL to a shortened URL.

‎source-code/Jewels_and_Stones.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int numJewelsInStones(string J, string S) {
4+
vector<bool> isJewel(256, false);
5+
for (char jewel : J) {
6+
isJewel[jewel] = true;
7+
}
8+
int count = 0;
9+
for (char stone : S) {
10+
count += isJewel[stone];
11+
}
12+
13+
return count;
14+
}
15+
};

‎source-code/LRU_Cache.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ class LRUCache {
8484
}
8585
};
8686

87-
// using C++ list. got RE for memory corruption though :(
87+
88+
// using C++ std::list
8889
class LRUCache {
8990
public:
9091
LRUCache(int capacity) {
@@ -95,27 +96,24 @@ class LRUCache {
9596

9697
int get(int key) {
9798
if (table.find(key) != table.end()) {
98-
auto node = table[key];
99-
pair<int, int> entry = *node;
100-
entries.erase(node);
101-
entries.push_front(entry);
102-
return entry.second;
99+
auto entry = table[key];
100+
entries.splice(entries.begin(), entries, entry);
101+
return entry->second;
103102
}
104103
return -1;
105104
}
106105

107106
void put(int key, int value) {
108-
pair<int, int> newEntry = {key, value};
109107
if (table.find(key) != table.end()) {
110-
auto curr = table[key];
111-
entries.erase(curr);
108+
entries.erase(table[key]);
112109
} else {
113-
if(entries.size() >= capacity) {
110+
if(entries.size() >= capacity) {
114111
pair<int, int> tailEntry = entries.back();
115-
entries.pop_back();
116112
table.erase(tailEntry.first);
117-
}
113+
entries.pop_back();
114+
}
118115
}
116+
pair<int, int> newEntry = {key, value};
119117
entries.push_front(newEntry);
120118
table[key] = entries.begin();
121119
}

‎source-code/Number_of_Islands_II.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ class Solution {
2727
for(int i = 0; i < positions.size(); ++i) {
2828
int x = positions[i].first;
2929
int y = positions[i].second;
30-
if(find(x, y, parent) == WATER) {
31-
parent[x][y] = pair<int, int>(x, y);
32-
nSet++;
33-
}
30+
parent[x][y] = pair<int, int>(x, y);
31+
nSet++;
32+
3433
for(int k = 0; k < 4; ++k) {
3534
int newX = x + dx[k];
3635
int newY = y + dy[k];

‎source-code/Perfect_Squares.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
// top-down
2+
class Solution {
3+
int numSquares(int n, vector<int>& dp) {
4+
if (n == 0) {
5+
return 0;
6+
}
7+
if (dp[n] != INT_MAX) {
8+
return dp[n];
9+
}
10+
for (int i = 1; i * i <= n; i++) {
11+
int sqr = i * i;
12+
dp[n] = min(dp[n], 1 + numSquares(n - sqr, dp));
13+
}
14+
15+
return dp[n];
16+
}
17+
public:
18+
int numSquares(int n) {
19+
vector<int> dp(n + 1, INT_MAX);
20+
return numSquares(n, dp);
21+
}
22+
};
23+
124
// DP O(n sqrt(n))
225
class Solution {
326
public:

‎source-code/Remove_Duplicate_Letters.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@ class Solution {
1212
}
1313
for(int i = 0; i < n; ++i) {
1414
if(!taken[s[i] - 'a']) {
15-
while(!Stack.empty() and Stack.top() > s[i] and freq[Stack.top() - 'a'] > 1) {
15+
while(!Stack.empty() and Stack.top() > s[i] and freq[Stack.top() - 'a'] > 0) {
1616
char rmv = Stack.top();
1717
Stack.pop();
1818
taken[rmv - 'a'] = false;
19-
freq[rmv - 'a']--;
2019
}
2120
Stack.push(s[i]);
2221
taken[s[i] - 'a'] = true;
23-
} else {
24-
freq[s[i] - 'a']--;
2522
}
23+
freq[s[i] - 'a']--;
2624
}
2725
string result = "";
2826
result.reserve(Stack.size());

‎source-code/Robot_Return_to_Origin.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
bool judgeCircle(string moves) {
4+
int x = 0;
5+
int y = 0;
6+
for (char ch : moves) {
7+
y += (ch == 'U');
8+
y -= (ch == 'D');
9+
x += (ch == 'R');
10+
x -= (ch == 'L');
11+
}
12+
return (x == 0 and y == 0);
13+
}
14+
};

‎source-code/Robot_Room_Cleaner.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* // This is the robot's control interface.
3+
* // You should not implement it, or speculate about its implementation
4+
* class Robot {
5+
* public:
6+
* // Returns true if the cell in front is open and robot moves into the cell.
7+
* // Returns false if the cell in front is blocked and robot stays in the current cell.
8+
* bool move();
9+
*
10+
* // Robot will stay in the same cell after calling turnLeft/turnRight.
11+
* // Each turn will be 90 degrees.
12+
* void turnLeft();
13+
* void turnRight();
14+
*
15+
* // Clean the current cell.
16+
* void clean();
17+
* };
18+
*/
19+
20+
class Solution {
21+
unordered_map<int, unordered_map<int, int>> grid;
22+
int dx[4] = {-1, 0, 1, 0};
23+
int dy[4] = {0, 1, 0, -1};
24+
int x = 0, y = 0;
25+
int dir = 0;
26+
27+
void setback(Robot& robot) {
28+
robot.turnRight();
29+
robot.turnRight();
30+
robot.move();
31+
robot.turnRight();
32+
robot.turnRight();
33+
}
34+
public:
35+
void cleanRoom(Robot& robot) {
36+
if(grid[x][y] == 1) {
37+
return;
38+
}
39+
40+
robot.clean();
41+
grid[x][y] = 1;
42+
43+
for (int i = 0; i < sizeof(dx) / sizeof(dx[0]); i++) {
44+
if (robot.move()) {
45+
x += dx[dir];
46+
y += dy[dir];
47+
48+
cleanRoom(robot);
49+
50+
setback(robot);
51+
x -= dx[dir];
52+
y -= dy[dir];
53+
}
54+
robot.turnRight();
55+
dir = (dir + 1) % 4;
56+
}
57+
}
58+
};

‎source-code/Summary_Ranges.cpp

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,24 @@ class Solution {
22
public:
33
vector<string> summaryRanges(vector<int>& nums) {
44
vector<string> result;
5-
string elem = "";
6-
if(nums.size() > 0) {
7-
elem = to_string(nums[0]) + "->";
8-
}
9-
10-
for(int i = 1; i < nums.size(); ++i) {
11-
bool isRange = false;
12-
13-
while(i < nums.size() and (i > 0 and nums[i] == nums[i - 1] + 1)) {
14-
++i;
15-
isRange = true;
5+
int n = (int)nums.size();
6+
for (int left = 0, right = 0; right < n; left++, right++) {
7+
while (right + 1 < n and nums[right + 1] == nums[right] + 1) {
8+
right++;
169
}
17-
if(isRange) {
18-
elem += to_string(nums[i - 1]);
10+
int rangeLength = right - left + 1;
11+
string range;
12+
if (rangeLength == 1) {
13+
range = to_string(nums[left]);
1914
} else {
20-
elem = elem.substr(0, elem.size() - 2);
21-
// elem.erase(elem.size() - 3, 2);
22-
}
23-
result.push_back(elem);
24-
25-
if(i < nums.size()) {
26-
elem = to_string(nums[i]) + "->";
15+
range = to_string(nums[left]);
16+
range += "->";
17+
range += to_string(nums[right]);
2718
}
28-
29-
}
30-
31-
if(elem.size() > 0 and elem[elem.size() - 1] == '>') {
32-
result.push_back(elem.substr(0, elem.size() - 2));
19+
result.push_back(range);
20+
left = right;
3321
}
34-
22+
3523
return result;
3624
}
3725
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /