Skip to content

Commit 0d1679c

Browse files
committed
update
1 parent ee9fe6b commit 0d1679c

File tree

39 files changed

+1073
-0
lines changed

39 files changed

+1073
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# AC:
2+
# from others' solution
3+
#
4+
class Solution(object):
5+
def distinctEchoSubstrings(self, S):
6+
N = len(S)
7+
P, MOD = 37, 344555666677777 # MOD is prime
8+
Pinv = pow(P, MOD - 2, MOD)
9+
10+
prefix = [0]
11+
pwr = 1
12+
ha = 0
13+
14+
for x in map(ord, S):
15+
ha = (ha + pwr * x) % MOD
16+
pwr = pwr * P % MOD
17+
prefix.append(ha)
18+
19+
seen = set()
20+
pwr = 1
21+
for length in range(1, N // 2 + 1):
22+
pwr = pwr * P % MOD # pwr = P^length
23+
for i in range(N - 2 * length + 1):
24+
left = (prefix[i + length] - prefix[i]) * pwr % MOD # hash of s[i:i+length] * P^length
25+
right = (prefix[i + 2 * length] - prefix[i + length]) % MOD # hash of s[i+length:i+2*length]
26+
if left == right:
27+
seen.add(left * pow(Pinv, i, MOD) % MOD) # left * P^-i is the canonical representation
28+
return len(seen)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* AC:
3+
* 思路:较易,略
4+
*/
5+
class Solution {
6+
public:
7+
vector<int> decompressRLElist(vector<int>& nums) {
8+
vector<int> ret;
9+
for(int i = 0; i < nums.size() / 2; i++)
10+
for(int j = 0; j < nums[2 * i]; j++)
11+
ret.push_back(nums[2 * i + 1]);
12+
13+
return ret;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* AC:
3+
* 思路:先按行预先计算出行的累加和,用一个 a[row + 2 * K][col + 2 * K] 的数组记录,
4+
* 然后在对 a 遍历,每个 [i][j] 位置累加 a[i][j] 到 a[i + 2 * K][j + 2 * K] 的K^2 个元素和
5+
* 这样总体复杂度是 O(m)(n)
6+
* T: O(m * n * K), 由于 K 是常数,所以接近 m * n 的复杂度
7+
*/
8+
class Solution {
9+
public:
10+
vector<vector<int>> matrixBlockSum(vector<vector<int>>& mat, int K) {
11+
int row = mat.size(), col = mat[0].size();
12+
vector<vector<int>> ret(row, vector<int>(col, 0));
13+
vector<vector<int>> record(row + 2 * K, vector<int>(row + 2 * K, 0));
14+
for(int i = 0; i < row; i++) {
15+
for(int j = 0; j < col; j++) {
16+
for(int k = -K; k <= K; k++) {
17+
if(j + k < 0 || j + k > col - 1) // 左右越界的部分不计算行的累加和
18+
continue;
19+
record[i + K][j + K] += mat[i][j + k];
20+
}
21+
}
22+
}
23+
24+
for(int i = 0; i < row; i++) {
25+
for(int j = 0; j < col; j++) {
26+
int temp = 0;
27+
for(int k = -K; k <= K; k++) {
28+
temp += record[i + K - k][j + K];
29+
}
30+
ret[i][j] = temp;
31+
}
32+
}
33+
34+
return ret;
35+
}
36+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* AC:
3+
* 思路:递归,定义 f(node), 对于每一个 node,判断到孙子节点,如果当前 node->val 为偶,
4+
* 则加进其孙子节点的值(如果存在),然后 再递归累加 ret += f(node->left) + f(node->right)
5+
* T:O(n), n 为二叉树的节点数
6+
*/
7+
/**
8+
* Definition for a binary tree node.
9+
* struct TreeNode {
10+
* int val;
11+
* TreeNode *left;
12+
* TreeNode *right;
13+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
14+
* };
15+
*/
16+
class Solution {
17+
public:
18+
int sumEvenGrandparent(TreeNode* root) {
19+
TreeNode* tmp = root;
20+
int ret = 0;
21+
if(root->val % 2 == 0) { // 偶数点
22+
if(root->left) {
23+
if(root->left->left)
24+
ret += root->left->left->val;
25+
if(root->left->right)
26+
ret += root->left->right->val;
27+
ret += sumEvenGrandparent(root->left); // 递归
28+
}
29+
if(root->right) {
30+
if(root->right->left)
31+
ret += root->right->left->val;
32+
if(root->right->right)
33+
ret += root->right->right->val;
34+
ret += sumEvenGrandparent(root->right);
35+
}
36+
} else {
37+
if(root->left) {
38+
ret += sumEvenGrandparent(root->left); // 递归
39+
}
40+
if(root->right) {
41+
ret += sumEvenGrandparent(root->right);
42+
}
43+
}
44+
45+
return ret;
46+
}
47+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int distinctEchoSubstrings(string text) {
4+
vector<string> ret;
5+
int textLength = text.length();
6+
for (int i = 0; i < textLength; i++) {
7+
for (int j = i + 1; j < textLength; j += 2) { // 偶数个字符的子串
8+
bool flag = true;
9+
for (int k = 0; k < (j - i + 1) / 2; k++) {
10+
if (text[i + k] != text[(j + i) / 2 + 1 + k]) {
11+
flag = false;
12+
break;
13+
}
14+
}
15+
if (flag) {
16+
string temp = text.substr(i, (j - i) + 1);
17+
ret.push_back(temp);
18+
}
19+
}
20+
}
21+
set<string>s(ret.begin(), ret.end()); // 去重
22+
23+
return s.size();
24+
}
25+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// AC:
2+
// Runtime: 5 ms, faster than 100.00% of Java online submissions for Sorting the Sentence.
3+
// Memory Usage: 38.5 MB, less than 100.00% of Java online submissions for Sorting the Sentence.
4+
// Using tree map to sort key.
5+
// T:O(n), S:O(n), n = len(s)
6+
//
7+
class Solution {
8+
public String sortSentence(String s) {
9+
TreeMap<Integer, String> record = new TreeMap<>();
10+
String[] sArr = s.split(" ");
11+
for (String item: sArr) {
12+
Integer index = Integer.parseInt(String.valueOf(item.charAt(item.length() - 1)));
13+
record.put(index, item.substring(0, item.length() - 1));
14+
}
15+
StringBuilder ret = new StringBuilder();
16+
for (Integer i: record.keySet()) {
17+
ret.append(record.get(i));
18+
ret.append(" ");
19+
}
20+
return ret.substring(0, ret.length() - 1);
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// AC:
2+
// Runtime: 8 ms, faster than 100.00% of Java online submissions for Incremental Memory Leak.
3+
// Memory Usage: 38.3 MB, less than 100.00% of Java online submissions for Incremental Memory Leak.
4+
// simulation... it seems that brute-force can pass. Any way I haven't thought out better solution...
5+
// complexity: T: O(sqrt(max(m1, m2))), S: O(1)
6+
//
7+
class Solution {
8+
public int[] memLeak(int memory1, int memory2) {
9+
int second = 1;
10+
while (memory1 >= second || memory2 >= second) {
11+
if (memory1 >= memory2) {
12+
if (memory1 >= second) {
13+
memory1 -= second;
14+
} else {
15+
break;
16+
}
17+
} else {
18+
if (memory2 >= second) {
19+
memory2 -= second;
20+
} else {
21+
break;
22+
}
23+
}
24+
second++;
25+
}
26+
int[] ret = new int[]{second, memory1, memory2};
27+
return ret;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// AC:
2+
// Runtime: 2056 ms, faster than 50.00% of Java online submissions for Rotating the Box.
3+
// Memory Usage: 64.7 MB, less than 100.00% of Java online submissions for Rotating the Box.
4+
// my thought: using string split(obstacle), and combine every part to make stone fall down.
5+
// complexity: T:O(m * n), S:O(m * n)
6+
//
7+
class Solution {
8+
public char[][] rotateTheBox(char[][] box) {
9+
int row = box.length, col = box[0].length;
10+
char[][] ret = new char[col][row];
11+
List<List<Character>> record = new LinkedList<>();
12+
for (int i = 0; i < row; i++) {
13+
// 逐行下落
14+
List<Character> temp = new LinkedList<>();
15+
StringBuilder tempStr = new StringBuilder();
16+
for (int j = 0; j <= col - 1; j++) {
17+
tempStr.append(box[i][j]);
18+
}
19+
tempStr.append("#");
20+
String[] tempArr = tempStr.toString().split("\\*");
21+
if (tempArr.length == 0) {
22+
for (int t = 0; t < tempStr.length(); t++) {
23+
temp.add('*');
24+
}
25+
} else {
26+
for (String part : tempArr) {
27+
if (part.isEmpty()) {
28+
temp.add('*');
29+
continue;
30+
}
31+
int countEmpty = 0, countStone = 0;
32+
for (char item : part.toCharArray()) {
33+
if (item == '#') {
34+
countStone++;
35+
}
36+
if (item == '.') {
37+
countEmpty++;
38+
}
39+
}
40+
for (int k = 0; k < countEmpty; k++) {
41+
temp.add('.');
42+
}
43+
for (int k = 0; k < countStone; k++) {
44+
temp.add('#');
45+
}
46+
47+
temp.add('*');
48+
}
49+
// 去除末尾 *
50+
if (temp.get(temp.size() - 1) == '*') {
51+
temp.remove(temp.size() - 1);
52+
}
53+
}
54+
temp.remove(temp.size() - 1);
55+
record.add(temp);
56+
}
57+
// rotate
58+
for (int i = 0; i < col; i++) {
59+
for (int j = 0; j < row; j++) {
60+
ret[i][j] = record.get(row - 1 - j).get(i);
61+
}
62+
}
63+
64+
return ret;
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
public int countPaths(int n, int[][] roads) {
3+
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
public int numberOfCombinations(String num) {
3+
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// AC: Runtime: 0 ms, faster than 100.00% of Java online submissions for Minimum Time to Type Word Using Special Typewriter.
2+
// Memory Usage: 36.7 MB, less than 100.00% of Java online submissions for Minimum Time to Type Word Using Special Typewriter.
3+
// .
4+
// T:O(n), S:(1)
5+
//
6+
class Solution {
7+
public int minTimeToType(String word) {
8+
int ret = 0, curPos = 0, size = word.length();
9+
for (int i = 0; i < size; i++) {
10+
int c = word.charAt(i) - 'a';
11+
int move = Math.abs(c - curPos);
12+
int minMove = Math.min(move, 26 - move);
13+
ret += minMove + 1;
14+
curPos = c;
15+
}
16+
17+
return ret;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// AC: Runtime: 6 ms, faster than 100.00% of Java online submissions for Maximum Matrix Sum.
2+
// Memory Usage: 48.1 MB, less than 100.00% of Java online submissions for Maximum Matrix Sum.
3+
// if have zero, then sum all absolute value. if not zero and negative numbers are odd, then sum all absolute and minus 2 * maxNegativeNumber.
4+
// T:O(m * n), S:O(1)
5+
//
6+
class Solution {
7+
public long maxMatrixSum(int[][] matrix) {
8+
int minAbs = Integer.MAX_VALUE, negaCount = 0;
9+
long absSum = 0;
10+
boolean hasZero = false;
11+
int row = matrix.length;
12+
for (int i = 0; i < row; i++) {
13+
for (int j = 0; j < row; j++) {
14+
if (!hasZero && matrix[i][j] == 0) {
15+
hasZero = true;
16+
}
17+
absSum += Math.abs(matrix[i][j]);
18+
if (!hasZero) {
19+
minAbs = Math.min(minAbs, Math.abs(matrix[i][j]));
20+
if (matrix[i][j] < 0) {
21+
negaCount++;
22+
}
23+
}
24+
}
25+
}
26+
27+
if (hasZero || (negaCount % 2 == 0)) {
28+
return absSum;
29+
} else {
30+
return absSum - 2L * minAbs;
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> groupThePeople(vector<int>& groupSizes) {
4+
5+
}
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
int smallestDivisor(vector<int>& nums, int threshold) {
4+
5+
}
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
int minFlips(vector<vector<int>>& mat) {
4+
5+
}
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* 给定一个正数,求各位之积减去各位之和。
3+
*
4+
* AC: Runtime: 0 ms | Memory Usage: 8.3 MB
5+
*/
6+
class Solution {
7+
public:
8+
int subtractProductAndSum(int n) {
9+
if(n == 0)
10+
return 0;
11+
int product = 1, sum = 0;
12+
while(n != 0) {
13+
product *= n % 10;
14+
sum += n % 10;
15+
n /= 10;
16+
}
17+
18+
return product - sum;
19+
}
20+
};

0 commit comments

Comments
 (0)