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
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> ans(n, vector<int>(n, 0));
int i = 1, x1 = 0, y1 = 0, x2 = n -1, y2 = n - 1;
while (n > 1) {
int x = x1, y = y1;
while (y < y2)
ans[x][y++] = i++;
while (x < x2)
ans[x++][y] = i++;
while (y > y1)
ans[x][y--] = i++;
while (x > x1)
ans[x--][y] = i++;
++x1;
++y1;
--x2;
--y2;
n -= 2;
}
if (n & 1)
ans[x1][y1] = i;
return ans;
}
};

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
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int left = 0;
int right = matrix[0].size() - 1;
int top = 0;
int bottom = matrix.size() - 1;
vector<int> ans;
while (true) {
for (int i = left; i <= right; ++i)
ans.push_back(matrix[top][i]);
if (++top > bottom) break;
for (int i = top; i <= bottom; ++i)
ans.push_back(matrix[i][right]);
if (--right < left) break;
for (int i = right; i >= left; --i)
ans.push_back(matrix[bottom][i]);
if (--bottom < top) break;
for (int i = bottom; i >= top; --i)
ans.push_back(matrix[i][left]);
if (++left > right) break;
}
return ans;
}
};

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
class Solution {
public:
int minOperations(vector<int>& nums1, vector<int>& nums2) {
const int l1 = nums1.size(), l2 = nums2.size();
if (min(l1, l2) * 6 < max(l1, l2)) return -1;
int s1 = accumulate(begin(nums1), end(nums1), 0), s2 = accumulate(begin(nums2), end(nums2), 0);
if (s1 > s2) return minOperations(nums2, nums1);
if (s1 == s2) return 0;
sort(begin(nums1), end(nums1));
sort(rbegin(nums2), rend(nums2));
int ans = 0;
for (int i = 0, j = 0; s1 < s2; ++ans) {
int d1 = i == l1 ? 0 : 6 - nums1[i];
int d2 = j == l2 ? 0 : nums2[j] - 1;
if (d1 >= d2) {
s1 += d1;
++i;
} else {
s2 -= d2;
++j;
}
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int minOperations(vector<int>& nums1, vector<int>& nums2) {
const int l1 = nums1.size(), l2 = nums2.size();
if (l1 * 6 < l2 || l2 * 6 < l1) return -1;
int s1 = accumulate(begin(nums1), end(nums1), 0), s2 = accumulate(begin(nums2), end(nums2), 0);
if (s1 > s2) return minOperations(nums2, nums1);
int cnt[6] = {0}, i = 5, ans = 0;
for (int num : nums1) ++cnt[6 - num];
for (int num : nums2) ++cnt[num - 1];
while (s1 < s2) {
while (cnt[i] == 0) --i;
s1 += i;
--cnt[i];
++ans;
}
return ans;
}
};

Reference: [Java/Python 3] 2 Greedy codes: sort and count w/ brief explanation and analysis. - LeetCode Discuss

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
class MyHashMap {
private:
vector<list<pair<int, int>>> data;
static const int base = 769;
static int hash(int key) {
return key % base;
}
public:
MyHashMap(): data(base) {
}
void put(int key, int value) {
int h = hash(key);
for (auto it = data[h].begin(); it != data[h].end(); ++it)
if ((*it).first == key) {
(*it).second = value;
return;
}
data[h].emplace_back(key, value);
}
int get(int key) {
int h = hash(key);
for (auto it = data[h].begin(); it != data[h].end(); ++it)
if ((*it).first == key)
return (*it).second;
return -1;
}
void remove(int key) {
int h = hash(key);
for (auto it = data[h].begin(); it != data[h].end(); ++it)
if ((*it).first == key) {
data[h].erase(it);
return;
}
}
};

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
class MyHashSet {
private:
vector<list<int>> data;
static const int base = 769;
static int hash(int key) {
return key % base;
}
public:
MyHashSet(): data(base) {
}
void add(int key) {
int h = hash(key);
for (auto it = data[h].begin(); it != data[h].end(); ++it)
if ((*it) == key)
return;
data[h].push_back(key);
}
void remove(int key) {
int h = hash(key);
for (auto it = data[h].begin(); it != data[h].end(); ++it)
if ((*it) == key) {
data[h].erase(it);
return;
}
}
bool contains(int key) {
int h = hash(key);
for (auto it = data[h].begin(); it != data[h].end(); ++it)
if ((*it) == key)
return true;
return false;
}
};

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
class Solution {
public:
bool isValidSerialization(string preorder) {
int n = preorder.size(), i = 0;
stack<int> stk;
stk.push(1);
while (i < n) {
if (stk.empty()) return false;
if (preorder[i] == ',') {
++i;
} else if (preorder[i] == '#') {
if (--stk.top() == 0)
stk.pop();
++i;
} else {
while (i < n && preorder[i] != ',')
++i;
if (--stk.top() == 0)
stk.pop();
stk.push(2);
}
}
return stk.empty();
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
bool isValidSerialization(string preorder) {
int n = preorder.size(), i = 0, slots = 1;
while (i < n) {
if (slots == 0) return false;
if (preorder[i] == ',') {
++i;
} else if (preorder[i] == '#') {
--slots;
++i;
} else {
while (i < n && preorder[i] != ',')
++i;
++slots;
}
}
return slots == 0;
}
};

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
class Solution {
public:
int calculate(string s) {
vector<int> stk;
char preSign = '+';
int n = s.length(), num = 0;
for (int i = 0; i < n; ++i) {
if (isdigit(s[i]))
num = num * 10 + (s[i] - '0');
if ((!isdigit(s[i]) && s[i] != ' ') || i == n - 1) {
switch (preSign) {
case '+':
stk.push_back(num);
break;
case '-':
stk.push_back(-num);
break;
case '*':
stk.back() *= num;
break;
case '/':
stk.back() /= num;
}
preSign = s[i];
num = 0;
}
}
return accumulate(stk.begin(), stk.end(), 0);
}
};

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
class Solution {
public:
int calculate(string s) {
stack<int> stk;
int cur = 0, sign = 1, ans = 0;
for (char c : s) {
if (c == ' ') continue;
else if (c == '+') {
ans += sign * cur;
cur = 0;
sign = 1;
} else if (c == '-') {
ans += sign * cur;
cur = 0;
sign = -1;
} else if (c == '(') {
stk.push(ans);
stk.push(sign);
ans = 0;
sign = 1;
} else if (c == ')') {
ans += sign * cur;
cur = 0;
ans *= stk.top(); stk.pop();
ans += stk.top(); stk.pop();
} else { // digit
cur = cur * 10 + (c - '0');
}
}
if (cur != 0) ans += sign * cur;
return ans;
}
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
int beautySum(string s) {
const int n = s.size();
int ans = 0;
vector<int> dp(26);
map<int, int> m;
for (int i = 0; i < n; ++i) {
fill(dp.begin(), dp.end(), 0);
m.clear();
for (int j = i; j < n; ++j) {
const int c = ++dp[s[j] - 'a'];
++m[c];
if (c > 1) {
auto it = m.find(c - 1);
if (--it->second == 0)
m.erase(it);
}
ans += m.rbegin()->first - m.begin()->first;
}
}
return ans;
}
};
0%