-
-
Notifications
You must be signed in to change notification settings - Fork 242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
旋转矩阵 #317
Comments
/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var rotate = function(matrix) {
const n = matrix.length;
const matrix_new = new Array(n).fill(0).map(() => new Array(n).fill(0)); //新矩阵
for(let i = 0; i < n; i++){
for(let j = 0; j < n; j++){
matrix_new[j][n - i - 1] = matrix[i][j];
//重点在这里,对于矩阵中第 i 行的第 j 个元素,在旋转后,它出现在倒数第 i 列的第 j 个位置。
//倒数第i列相当于正数n-i列,因为下标从0开始,所以这里还要-1,第j个位置其实就是第j行
}
}
for(let i = 0; i < n; i++){
for(let j = 0; j < n; j++){
matrix[i][j] = matrix_new[i][j];
}
}
}; |
情况一:顺时针转 90 度:先转置再左右镜像
|
function rotateMatrix(m: number[][]):void {
let m_length: number = m[0].length;
let temp: number;
for (let i = 0; i < m_length; i++) {
for (let j = 0; j < Math.floor(m_length>>1); j++) {
temp = m[i][j];
m[i][j] = m[i][m_length-1-j];
m[i][m_length-1-j]=temp;
}
}
for (let i = 0; i < m_length-1; i++) {
for (let j = 0; j < m_length-i-1; j++) {
temp = m[i][j];
m[i][j] = m[m_length-1-j][m_length-1-i];
m[m_length-1-j][m_length-1-i] = temp;
}
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: