to do
- cc150 array&string (6-11)/11
- leetcode review day 1
1.6] Rotate N*N matrix by 90 degrees
- note how to use
start
andend
to keep track easily
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
for (int layer=0; layer<matrix.size()/2; ++layer) {
int start = layer, end = start+(n-1);
for (int i=start; i<end; ++i) {
int tmp = matrix[start][i];
matrix[start][i] = matrix[end-(i-start)][start];
matrix[end-(i-start)][start] = matrix[end][end-(i-start)];
matrix[end][end-(i-start)] = matrix[i][end];
matrix[i][end] = tmp;
}
n-=2;
}
}
1.7] Given M*N matrix, clear row and col where 0 occurs
Set Matrix Zeroes
check~ note the last for loop, to avoid repeatly setting zeros
void setZeroes(vector<vector<int>>& matrix) {
if (matrix.empty() || matrix[0].empty()) return;
int m = matrix.size(), n = matrix[0].size();
vector<bool> shouldSetRow(m, false);
vector<bool> shouldSetCol(n, false);
for (int i=0; i<m; ++i) {
for (int j=0; j<n; ++j) {
if (!matrix[i][j]) {
shouldSetRow[i] = true;
shouldSetCol[j] = true;
}
}
}
for (int i=0; i<m; ++i) {
for (int j=0; j<n; ++j) {
if (shouldSetRow[i] || shouldSetCol[j])
matrix[i][j] = 0;
}
}
}
1.8] 假设提供了isSubstring,可以检查strA是否为strB的子串。给定两个字符串s1和s2,判断是否s2由s1旋转而来,只可用isSubstring一次。
恩不剧透了,想不出看P116