Skip to content

Commit 5fac3f7

Browse files
committed
12 problems solved.
1 parent 2e109d8 commit 5fac3f7

File tree

12 files changed

+572
-0
lines changed

12 files changed

+572
-0
lines changed

src/138.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
RandomListNode *copyRandomList(RandomListNode *head) {
2+
if (head == nullptr)
3+
return nullptr;
4+
RandomListNode* h = head;
5+
map<RandomListNode*, int> mp;
6+
map<int, RandomListNode*> mp_cp;
7+
8+
int i = 0, j = 0;
9+
RandomListNode* head_cp = new RandomListNode(h->label);
10+
RandomListNode* node = head_cp;
11+
mp[h] = i++;
12+
mp_cp[j++] = head_cp;
13+
h = h->next;
14+
15+
while (h != nullptr)
16+
{
17+
node->next = new RandomListNode(h->label);
18+
node = node->next;
19+
mp[h] = i++;
20+
mp_cp[j++] = node;
21+
h = h->next;
22+
}
23+
for (auto m : mp)
24+
{
25+
if (m.first->random != nullptr)
26+
mp_cp[m.second]->random = mp_cp[mp[m.first->random]];
27+
}
28+
return head_cp;
29+
}

src/240.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
bool searchMatrix(vector<vector<int>>& matrix, int target) {
2+
int row = matrix.size();
3+
if (row == 0)
4+
return false;
5+
int col = matrix[0].size();
6+
int i = 0, j = col - 1;
7+
while (i < row && j >= 0)
8+
{
9+
if (matrix[i][j] == target)
10+
return true;
11+
else if (matrix[i][j] < target)
12+
++i;
13+
else
14+
--j;
15+
}
16+
return false;
17+
}

src/529.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click)
2+
{
3+
int posR = click[0], posC = click[1];
4+
if (board[posR][posC] == 'M')
5+
{
6+
board[posR][posC] = 'X';
7+
return board;
8+
}
9+
else
10+
{
11+
int row = board.size(), col = board[0].size();
12+
updateAdjacentSquare(board, posR, posC, row, col);
13+
return board;
14+
}
15+
}
16+
17+
void updateAdjacentSquare(vector<vector<char>>& board, int r, int c, int row, int col)
18+
{
19+
if (board[r][c] == 'E')
20+
{
21+
int num = 0;
22+
if (checkAdjacent(board, r, c, row, col, num))
23+
{
24+
board[r][c] = num + '0';
25+
return;
26+
}
27+
else
28+
{
29+
board[r][c] = 'B';
30+
if (r > 0)
31+
updateAdjacentSquare(board, r-1, c, row, col);
32+
if (r < row - 1)
33+
updateAdjacentSquare(board, r+1, c, row, col);
34+
if (c > 0)
35+
updateAdjacentSquare(board, r, c-1, row, col);
36+
if (c < col - 1)
37+
updateAdjacentSquare(board, r, c+1, row, col);
38+
if (r > 0 && c > 0)
39+
updateAdjacentSquare(board, r-1, c-1, row, col);
40+
if (r > 0 && c < col - 1)
41+
updateAdjacentSquare(board, r-1, c+1, row, col);
42+
if (r < row - 1 && c > 0)
43+
updateAdjacentSquare(board, r+1, c-1, row, col);
44+
if (r < row - 1 && c < col - 1)
45+
updateAdjacentSquare(board, r+1, c+1, row, col);
46+
}
47+
}
48+
}
49+
50+
bool checkAdjacent(vector<vector<char>>& board, int r, int c, int row, int col, int& num)
51+
{
52+
if (r > 0 && board[r - 1][c] == 'M')
53+
num++;
54+
if (r < row - 1 && board[r + 1][c] == 'M')
55+
num++;
56+
if (c > 0 && board[r][c - 1] == 'M')
57+
num++;
58+
if (c < col - 1 && board[r][c + 1] == 'M')
59+
num++;
60+
if (r > 0 && c > 0 && board[r-1][c-1] == 'M')
61+
num++;
62+
if (r > 0 && c < col - 1 && board[r-1][c+1] == 'M')
63+
num++;
64+
if (r < row - 1 && c > 0 && board[r+1][c-1] == 'M')
65+
num++;
66+
if (r < row - 1 && c < col - 1 && board[r+1][c+1] == 'M')
67+
num++;
68+
69+
return num != 0;
70+
}

src/565.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
int arrayNesting(vector<int>& nums) {
2+
int len = nums.size();
3+
int maxcount= 1;
4+
int i = 0;
5+
while (i < len)
6+
{
7+
int j = i;
8+
int ct = 1;
9+
while (nums[j] != j && nums[j] != -1)
10+
{
11+
int tmp = nums[j];
12+
nums[j] = -1;
13+
j = tmp;
14+
ct++;
15+
}
16+
if (ct - 1 > maxcount)
17+
maxcount = ct - 1;
18+
++i;
19+
}
20+
21+
return maxcount;
22+
}

src/61.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
ListNode* rotateRight(ListNode* head, int k) {
2+
if (head == nullptr || head->next == nullptr)
3+
return head;
4+
5+
ListNode* h = head;
6+
int len = 0;
7+
while (h != nullptr)
8+
{
9+
++len;
10+
h = h->next;
11+
}
12+
int n = k % len;
13+
if (n == 0)
14+
return head;
15+
h = head;
16+
int i = 0, limit = len - n;
17+
ListNode* node = nullptr;
18+
while (i < limit)
19+
{
20+
node = h;
21+
h = h->next;
22+
++i;
23+
}
24+
node->next = nullptr;
25+
node = h;
26+
while (node->next != nullptr)
27+
node = node->next;
28+
node->next = head;
29+
return h;
30+
}

src/655.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
vector<vector<string>> printTree(TreeNode* root) {
2+
vector<vector<string>> res;
3+
if (root == nullptr)
4+
return res;
5+
struct Elem
6+
{
7+
int val;
8+
int pos;
9+
Elem(int v, int p): val(v), pos(p){}
10+
};
11+
12+
vector<vector<Elem>> levelvec;
13+
deque<TreeNode*> dq;
14+
levelvec.push_back(vector<Elem>(1, Elem(root->val, 0)));
15+
dq.push_back(root);
16+
int sz = 0;
17+
int layer = 0;
18+
while ((sz = dq.size()) != 0)
19+
{
20+
vector<Elem> vec;
21+
for (int i = 0; i != sz; ++i)
22+
{
23+
TreeNode* node = dq[i];
24+
if (node->left != nullptr)
25+
{
26+
dq.push_back(node->left);
27+
vec.push_back(Elem(node->left->val, levelvec[layer][i].pos * 2));
28+
}
29+
if (node->right != nullptr)
30+
{
31+
dq.push_back(node->right);
32+
vec.push_back(Elem(node->right->val, levelvec[layer][i].pos * 2 + 1));
33+
}
34+
}
35+
dq.erase(dq.begin(), dq.begin() + sz);
36+
levelvec.push_back(vec);
37+
layer++;
38+
}
39+
40+
int num = pow(2, layer) - 1;
41+
for (int i = 0; i < layer; ++i)
42+
res.push_back(vector<string>(num, string("")));
43+
44+
for (int i = 0; i < layer; ++i)
45+
{
46+
int len = levelvec[i].size();
47+
int r = pow(2, layer - i);
48+
int base = (r - 1)/2;
49+
for (int j = 0; j < len; ++j)
50+
{
51+
res[i][base + r * levelvec[i][j].pos] = to_string(levelvec[i][j].val);
52+
}
53+
}
54+
55+
return res;
56+
}

src/678.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
bool checkValidString(string s) {
2+
int len = s.size();
3+
if (len == 0)
4+
return true;
5+
6+
int left = 0, star = 0;
7+
vector<int> pos1(len, 0);
8+
vector<int> pos2(len, 0);
9+
int i = 0, j = 0, k = 0;
10+
while (i < len)
11+
{
12+
if (s[i] == '(')
13+
{
14+
++left;
15+
pos1[j++] = i;
16+
}
17+
else if (s[i] == ')')
18+
{
19+
if (left > 0)
20+
{
21+
--left;
22+
pos1[--j] = -1;
23+
}
24+
else if (star > 0)
25+
{
26+
--star;
27+
pos2[--k] = -1;
28+
}
29+
else
30+
return false;
31+
}
32+
else
33+
{
34+
++star;
35+
pos2[k++] = i;
36+
}
37+
++i;
38+
}
39+
if (j > k)
40+
return false;
41+
42+
while (j > 0)
43+
{
44+
int m = pos1[--j];
45+
while (pos2[--k] < m);
46+
if (k < j)
47+
return false;
48+
}
49+
return true;
50+
}

src/721.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
2+
int num = accounts.size();
3+
if (num == 0 || num == 1)
4+
return accounts;
5+
6+
vector<vector<string>> res;
7+
map<string, vector<int>> mp;
8+
9+
for (int i = 0; i < num; ++i)
10+
{
11+
int len = accounts[i].size();
12+
for (int j = 1; j < len; ++j)
13+
mp[accounts[i][j]].push_back(i);
14+
}
15+
16+
vector<int> access(num, 0);
17+
for (int i = 0; i < num; ++i)
18+
{
19+
if (access[i] == 1)
20+
continue;
21+
access[i] = 1;
22+
set<string> account;
23+
merge(accounts, access, mp, i, account);
24+
25+
vector<string> ac(account.size()+1, "");
26+
ac[0] = accounts[i][0];
27+
copy(account.begin(), account.end(), ac.begin()+1);
28+
sort(ac.begin()+1, ac.end());
29+
res.push_back(ac);
30+
}
31+
return res;
32+
}
33+
void merge(const vector<vector<string>>& accounts, vector<int>& access, map<string, vector<int>>& mp, int index, set<string>& ac)
34+
{
35+
ac.insert(accounts[index].begin()+1, accounts[index].end());
36+
int len = accounts[index].size();
37+
for (int i = 1; i < len; ++i)
38+
{
39+
int n = mp[accounts[index][i]].size();
40+
for (int j = 0; j < n; ++j)
41+
{
42+
int m = mp[accounts[index][i]][j];
43+
if (access[m] == 0)
44+
{
45+
access[m] = 1;
46+
merge(accounts, access, mp, m, ac);
47+
}
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)