-
Notifications
You must be signed in to change notification settings - Fork 0
/
alien_task.cpp
91 lines (84 loc) · 2.68 KB
/
alien_task.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class Solution {
public:
bool isAlienSorted(vector<string>& words, string order) {
int m[26];
for (int i = 0; i < 26; i++)
m[order[i] - 'a'] = i;
for (auto &a:words) {
for (auto &c:a) {
c = m[c -'a'];
}
}
return is_sorted(words.begin(), words.end());
}
};
class Solution {
public:
vector<int> exclusiveTime(int n, vector<string>& logs) {
vector<int> res(n, 0);
stack<int> st;
int preTime = 0, idx = 0, time = 0;
char type[10];
for (string log : logs) {
sscanf(log.c_str(), "%d:%[^:]:%d", &idx, type, &time);
if (type[0] == 's') {
if (!st.empty()) {
res[st.top()] += time - preTime;
}
st.push(idx);
} else {
res[st.top()] += ++time - preTime;
st.pop();
}
preTime = time;
}
return res;
}
};
class Solution {
public:
bool validTicTacToe(vector<string>& board) {
bool xwin = false, owin = false;
vector<int> row(3), col(3);
int diag = 0, antidiag = 0, turns = 0;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (board[i][j] == 'X') {
++row[i]; ++col[j]; ++turns;
if (i == j) ++diag;
if (i + j == 2) ++antidiag;
} else if (board[i][j] == 'O') {
--row[i]; --col[j]; --turns;
if (i == j) --diag;
if (i + j == 2) --antidiag;
}
}
}
xwin = row[0] == 3 || row[1] == 3 || row[2] == 3 ||
col[0] == 3 || col[1] == 3 || col[2] == 3 ||
diag == 3 || antidiag == 3;
owin = row[0] == -3 || row[1] == -3 || row[2] == -3 ||
col[0] == -3 || col[1] == -3 || col[2] == -3 ||
diag == -3 || antidiag == -3;
if ((xwin && turns == 0) || (owin && turns == 1)) return false;
return (turns == 0 || turns == 1) && (!xwin || !owin);
}
};
class Solution {
public:
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
vector<TreeNode*> res;
unordered_map<string, int> m;
helper(root, m, res);
return res;
}
string helper(TreeNode* node, unordered_map<string, int>& m, vector<TreeNode*>& res) {
if (!node)
return "#";
string str = to_string(node->val) + "," + helper(node->left,m, res) + "," + helper(node->right, m, res);
if (m[start] == 1)
res.push_back(node);
++m[str];
return str;
}
};