Skip to content

Commit eb4d75c

Browse files
committed
13 medium problems
1 parent bcca53c commit eb4d75c

13 files changed

+1009
-0
lines changed

continuous-subarray-sum_1_AC.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <unordered_map>
2+
using std::unordered_map;
3+
4+
class Solution {
5+
public:
6+
bool checkSubarraySum(vector<int>& nums, int k) {
7+
auto &a = nums;
8+
int n = a.size();
9+
int i;
10+
if (k == 0) {
11+
int cc;
12+
i = 0;
13+
while (i < n) {
14+
if (a[i] != 0) {
15+
++i;
16+
continue;
17+
}
18+
cc = 0;
19+
while (i < n && a[i] == 0) {
20+
++i;
21+
++cc;
22+
}
23+
if (cc >= 2) {
24+
return true;
25+
}
26+
}
27+
return false;
28+
}
29+
30+
unordered_map<int, int> um;
31+
um[0] = 0;
32+
int sum = 0;
33+
for (i = 0; i < n; ++i) {
34+
sum = (sum + a[i]) % k;
35+
if (um.find(sum) == um.end()) {
36+
um[sum] = i + 1;
37+
}
38+
if (i + 1 - um[sum] >= 2) {
39+
break;
40+
}
41+
}
42+
um.clear();
43+
44+
return i < n;
45+
}
46+
};

design-snake-game_1_AC.cpp

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Edge cases everywhere.
2+
#include <string>
3+
#include <unordered_map>
4+
#include <vector>
5+
using std::string;
6+
using std::unordered_map;
7+
using std::vector;
8+
9+
static const int offset[4][2] = {{-1, 0}, {+1, 0}, {0, -1}, {0, +1}};
10+
11+
typedef struct MyListNode {
12+
int x, y;
13+
struct MyListNode *next;
14+
15+
MyListNode(int _x = 0, int _y = 0): x(_x), y(_y), next(NULL) {}
16+
} MyListNode;
17+
18+
class SnakeGame {
19+
public:
20+
/** Initialize your data structure here.
21+
@param width - screen width
22+
@param height - screen height
23+
@param food - A list of food positions
24+
E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0]. */
25+
SnakeGame(int width, int height, vector<pair<int, int>> food) {
26+
static const string sd = "UDLR";
27+
int ls = sd.size();
28+
int i;
29+
for (i = 0; i < ls; ++i) {
30+
di[sd[i]] = i;
31+
}
32+
33+
n = height;
34+
m = width;
35+
36+
int sx = 0;
37+
int sy = 0;
38+
head = tail = new MyListNode(sx, sy);
39+
40+
f = food;
41+
fi = 0;
42+
if (fi < f.size()) {
43+
fx = f[fi].first;
44+
fy = f[fi].second;
45+
} else {
46+
fx = fy = -1;
47+
}
48+
49+
b.resize(n, vector<bool>(m, false));
50+
b[sx][sy] = true;
51+
52+
score = 0;
53+
}
54+
55+
/** Moves the snake.
56+
@param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down
57+
@return The game's score after the move. Return -1 if game over.
58+
Game over when snake crosses the screen boundary or bites its body. */
59+
int move(string direction) {
60+
int i = di[direction[0]];
61+
int x, y;
62+
63+
x = tail->x + offset[i][0];
64+
y = tail->y + offset[i][1];
65+
if (!inbound(x, y) || (b[x][y] && !(x == head->x && y == head->y))) {
66+
// Game over
67+
// An edge case here, watch out.
68+
return -1;
69+
}
70+
71+
if (x == fx && y == fy) {
72+
// Eat something
73+
tail->next = new MyListNode(x, y);
74+
tail = tail->next;
75+
b[x][y] = true;
76+
++score;
77+
78+
++fi;
79+
if (fi < f.size()) {
80+
fx = f[fi].first;
81+
fy = f[fi].second;
82+
} else {
83+
fx = fy = -1;
84+
}
85+
} else {
86+
b[head->x][head->y] = false;
87+
head->x = x;
88+
head->y = y;
89+
b[x][y] = true;
90+
if (head != tail) {
91+
MyListNode *p = head;
92+
93+
head = p->next;
94+
p->next = NULL;
95+
tail->next = p;
96+
tail = p;
97+
}
98+
}
99+
return score;
100+
}
101+
102+
~SnakeGame() {
103+
di.clear();
104+
b.clear();
105+
f.clear();
106+
107+
MyListNode *p = head;
108+
while (head != NULL) {
109+
head = head->next;
110+
delete p;
111+
p = head;
112+
}
113+
}
114+
private:
115+
unordered_map<char, int> di;
116+
117+
MyListNode *head, *tail;
118+
int n, m;
119+
int fx, fy;
120+
int fi;
121+
vector<vector<bool>> b;
122+
vector<pair<int, int>> f;
123+
124+
int score;
125+
126+
bool inbound(int x, int y) {
127+
return x >= 0 && x <= n - 1 && y >= 0 && y <= m - 1;
128+
}
129+
};
130+
131+
/**
132+
* Your SnakeGame object will be instantiated and called as such:
133+
* SnakeGame obj = new SnakeGame(width, height, food);
134+
* int param_1 = obj.move(direction);
135+
*/

encode-and-decode-strings_1_AC.cpp

+196
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
// Base64 is an option.
2+
#include <cctype>
3+
#include <string>
4+
#include <vector>
5+
using std::isdigit;
6+
using std::string;
7+
using std::to_string;
8+
using std::vector;
9+
10+
class Codec {
11+
public:
12+
Codec() {
13+
b64 = "";
14+
bi.resize(SIZE_DICT, -1);
15+
16+
int idx = 0;
17+
int i;
18+
for (i = 0; i < 26; ++i) {
19+
b64.push_back('A' + i);
20+
bi['A' + i] = idx++;
21+
}
22+
for (i = 0; i < 26; ++i) {
23+
b64.push_back('a' + i);
24+
bi['a' + i] = idx++;
25+
}
26+
for (i = 0; i < 10; ++i) {
27+
b64.push_back('0' + i);
28+
bi['0' + i] = idx++;
29+
}
30+
b64.push_back('+');
31+
bi['+'] = idx++;
32+
b64.push_back('/');
33+
bi['/'] = idx++;
34+
}
35+
36+
// Encodes a list of strings to a single string.
37+
string encode(vector<string>& strs) {
38+
auto &a = strs;
39+
int n = a.size();
40+
string res = "";
41+
42+
res.push_back(CHAR_SEQ_DEL);
43+
for (auto &s: a) {
44+
res += encodeSingle(s);
45+
}
46+
res.push_back(CHAR_SEQ_DEL);
47+
48+
return res;
49+
}
50+
51+
string encodeSingle(string &s) {
52+
string t = encodeBase64(s);
53+
int lt = t.size();
54+
55+
string res = "";
56+
57+
res.push_back(CHAR_WORD_DEL);
58+
res += to_string(lt);
59+
res.push_back(CHAR_WORD_DEL);
60+
res += t;
61+
res.push_back(CHAR_WORD_DEL);
62+
63+
return res;
64+
}
65+
66+
// Decodes a single string to a list of strings.
67+
vector<string> decode(string s) {
68+
vector<string> res;
69+
int ls = s.size();
70+
if (!(ls >= 2 && s[0] == CHAR_SEQ_DEL && s[ls - 1] == CHAR_SEQ_DEL)) {
71+
return res;
72+
}
73+
int i, j;
74+
string ss;
75+
76+
i = 1;
77+
while (i < ls - 1) {
78+
j = i + 1;
79+
while (j < ls - 1 && s[j] != CHAR_WORD_DEL) {
80+
++j;
81+
}
82+
++j;
83+
while (j < ls - 1 && s[j] != CHAR_WORD_DEL) {
84+
++j;
85+
}
86+
++j;
87+
ss = s.substr(i, j - i);
88+
res.push_back(decodeSingle(ss));
89+
i = j;
90+
}
91+
92+
return res;
93+
}
94+
95+
string decodeSingle(string &s) {
96+
string res = "";
97+
int ls = s.size();
98+
if (!(ls >= 2 && s[0] == CHAR_WORD_DEL && s[ls - 1] == CHAR_WORD_DEL)) {
99+
return res;
100+
}
101+
102+
int i = 1;
103+
int lt = 0;
104+
while (i < ls && isdigit(s[i])) {
105+
lt = lt * 10 + (s[i] - '0');
106+
++i;
107+
}
108+
++i;
109+
110+
string t = s.substr(i, ls - 1 - i);
111+
if (t.size() != lt) {
112+
return res;
113+
}
114+
115+
return decodeBase64(t);
116+
}
117+
118+
~Codec() {
119+
b64.clear();
120+
bi.clear();
121+
}
122+
private:
123+
static const int SIZE_DICT = 256;
124+
// Make sure the delimiter you choose is not among the charset of the encoding/decoding scheme.
125+
// Otherwise you'll have to use escaped charaters to avoid ambiguity.
126+
static const char CHAR_WORD_DEL = '\0';
127+
static const char CHAR_SEQ_DEL = '\1';
128+
129+
string b64;
130+
vector<int> bi;
131+
132+
string encodeBase64(string &s) {
133+
// Any implementation is OK, base64 is just one option.
134+
int ls = s.size();
135+
string t = "";
136+
int i;
137+
int idx;
138+
int val;
139+
int bc;
140+
141+
val = 0;
142+
bc = 0;
143+
i = 0;
144+
do {
145+
val = (val << 8) + s[i++];
146+
bc += 8;
147+
while (bc >= 6) {
148+
idx = (val >> bc - 6);
149+
val &= (1 << bc - 6) - 1;
150+
t.push_back(b64[idx]);
151+
152+
bc -= 6;
153+
}
154+
} while (i < ls);
155+
if (bc > 0) {
156+
val <<= (6 - bc);
157+
idx = val;
158+
t.push_back(b64[idx]);
159+
160+
bc = 0;
161+
}
162+
163+
// Padding with '=' is unnecessary if you got other delimiters in place.
164+
return t;
165+
}
166+
167+
string decodeBase64(string &s) {
168+
// Any implementation is OK, base64 is just one option.
169+
int ls = s.size();
170+
string t = "";
171+
int i;
172+
int idx;
173+
int val;
174+
int bc;
175+
176+
val = 0;
177+
bc = 0;
178+
i = 0;
179+
do {
180+
val = (val << 6) + bi[s[i++]];
181+
bc += 6;
182+
while (bc >= 8) {
183+
idx = (val >> (bc - 8));
184+
val &= (1 << (bc - 8)) - 1;
185+
t.push_back(idx);
186+
187+
bc -= 8;
188+
}
189+
} while (i < ls);
190+
return t;
191+
}
192+
};
193+
194+
// Your Codec object will be instantiated and called as such:
195+
// Codec codec;
196+
// codec.decode(codec.encode(strs));

0 commit comments

Comments
 (0)