Skip to content

Commit adf05b7

Browse files
committed
4 problems solved.
1 parent 081a042 commit adf05b7

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

src/1309.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
string freqAlphabets(string s) {
2+
int len = s.size();
3+
int i = 0;
4+
string res;
5+
while (i < len)
6+
{
7+
int p = s.find('#', i);
8+
if (p != -1)
9+
{
10+
for (; i < p-2; ++i)
11+
res.push_back(s[i]-'1'+'a');
12+
int t = (s[i]-'0')*10+(s[i+1]-'0');
13+
res.push_back(t-10+'j');
14+
i = p+1;
15+
}
16+
else
17+
{
18+
for (; i < len; ++i)
19+
res.push_back(s[i]-'1'+'a');
20+
}
21+
}
22+
23+
return res;
24+
}

src/3.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
int lengthOfLongestSubstring(string s) {
2+
int len = s.size();
3+
if (len <= 1)
4+
return len;
5+
6+
map<char, int> pos;
7+
pos[s[0]] = 0;
8+
9+
int i = 1, tmp = 1;
10+
int maxlen = 1;
11+
int index = 0;
12+
while (i < len)
13+
{
14+
if (pos.find(s[i]) == pos.end() || pos[s[i]] == -1)
15+
{
16+
++tmp;
17+
pos[s[i]] = i;
18+
++i;
19+
}
20+
else
21+
{
22+
if (tmp > maxlen)
23+
maxlen = tmp;
24+
int t = pos[s[i]];
25+
for (int j = index; j <=t; ++j)
26+
pos[s[j]] = -1;
27+
tmp = i-t;
28+
pos[s[i]] = i;
29+
index = t+1;
30+
++i;
31+
}
32+
}
33+
34+
if (tmp > maxlen)
35+
maxlen = tmp;
36+
37+
return maxlen;
38+
}

src/78.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
vector<vector<int>> subsets(vector<int>& nums) {
2+
vector<vector<int>> res;
3+
res.push_back({});
4+
int len = nums.size();
5+
if (len == 0)
6+
return res;
7+
8+
vector<int> vec;
9+
search(nums, 0, len, vec, res);
10+
11+
return move(res);
12+
}
13+
14+
void search(vector<int>& nums, int index, int len, vector<int>& vec, vector<vector<int>>& res)
15+
{
16+
for (int i = index; i < len; ++i)
17+
{
18+
vector<int> tmp(vec);
19+
tmp.push_back(nums[i]);
20+
res.push_back(tmp);
21+
search(nums, i+1, len, tmp, res);
22+
}
23+
}

src/79.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
bool exist(vector<vector<char>>& board, string word) {
2+
int row = board.size();
3+
if (row == 0)
4+
return word == "";
5+
int col = board[0].size();
6+
7+
if (col == 0)
8+
return word == "";
9+
int total = row*col;
10+
11+
int sz = word.size();
12+
for (int i = 0; i < row; ++i)
13+
{
14+
for (int j = 0; j < col; ++j)
15+
{
16+
if (board[i][j] == word[0])
17+
{
18+
vector<int> flag(total, 0);
19+
flag[i*col+j] = 1;
20+
bool res = search(board, row, col, i, j, 1, sz, word, flag);
21+
if (res)
22+
{
23+
return true;
24+
}
25+
}
26+
}
27+
}
28+
29+
return false;
30+
}
31+
32+
bool search(const vector<vector<char>>& board, int row, int col, int r, int c, int ct, int sz, const string& word, vector<int>& flag)
33+
{
34+
if (ct == sz)
35+
return true;
36+
bool res = false;
37+
if (r > 0 && flag[(r-1)*col+c] == 0 && board[r-1][c] == word[ct])
38+
{
39+
vector<int> flag1(flag);
40+
flag1[(r-1)*col+c] = 1;
41+
res = search(board, row, col, r-1, c, ct+1, sz, word, flag1);
42+
}
43+
if (!res && r < row-1 && flag[(r+1)*col+c] == 0 && board[r+1][c] == word[ct])
44+
{
45+
vector<int> flag1(flag);
46+
flag1[(r+1)*col+c] = 1;
47+
res = search(board, row, col, r+1, c, ct+1, sz, word, flag1);
48+
}
49+
if (!res && c > 0 && flag[r*col+c-1] == 0 && board[r][c-1] == word[ct])
50+
{
51+
vector<int> flag1(flag);
52+
flag1[r*col+c-1] = 1;
53+
res = search(board, row, col, r, c-1, ct+1, sz, word, flag1);
54+
}
55+
if (!res && c < col-1 && flag[r*col+c+1] == 0 && board[r][c+1] == word[ct])
56+
{
57+
vector<int> flag1(flag);
58+
flag1[r*col+c+1] = 1;
59+
res = search(board, row, col, r, c+1, ct+1, sz, word, flag1);
60+
}
61+
62+
return res;
63+
}

0 commit comments

Comments
 (0)