Hướng dẫn transpose array javascript
This one, is not only a super efficient one, but a pretty short solution. Algorithm Time Complexity:
The example above will do only 6 iterations. The principle is simple, you on only iterate through the upper diagonal half of the matrix, because the diagonal line never changes and the bottom diagonal half being is switched together with the upper one, so there is no reason to iterate through it as well. This way, you save a lot of running time, especially in a large matrix. View Discussion Improve Article Save Article View Discussion Improve Article Save Article Given a 2D array (matrix) and the task is to get the transpose of the matrix using JavaScript. Approach 1:
Example: This example uses array.map() method to get the transpose of the array. HTML
Output:
Approach 2:
Example: This example creates a function which replaces every element by its mirror image to get the transpose of the array. HTML
Output:
|