Topic describes

Given a two-dimensional integer array matrix, return the transpose of matrix. The transpose of a matrix is to reverse the main diagonal of the matrix and swap the row index and column index of the matrix.Copy the code

Example 1:

Input: matrix = [[1, 2, 3], [4 and 6], [7,8,9]] output: [,4,7 [1], [2,5,8], [3,6,9]]Copy the code

Example 2:

Input: matrix = [[1, 2, 3], [4 and 6]] output: [[1, 4], [2, 5], [3]]Copy the code

Solution idea and code

1. Switch numbers
/** * @param {number} x * @return {boolean} */ var isPalindrome = function(x) { if(x<0) return false x = x.toString() let len = x.length let halflen = len % 2 == 0 ? len : len - 1 for(let i = 0; i<halflen; i++){ if(x[i] ! == x[len-i-1]) return false } return true };Copy the code
2. Compare strings from both ends
/** * @param {number} x * @return {boolean} */ var isPalindrome = function (x) { if (x < 0 || (x % 10 === 0 && x ! = 0)) return false; let revertedNumber = 0; while (x > revertedNumber) { revertedNumber = Math.floor(revertedNumber * 10 + x % 10); x = Math.floor(x/10); } return x === revertedNumber || x === Math.floor(revertedNumber / 10) };Copy the code

This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign