Skip to content

Commit c829479

Browse files
author
Tushar Borole
committed
48. Rotate Image
1 parent 096c39b commit c829479

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

.idea/workspace.xml

Lines changed: 13 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Iterative](single_number_II.js) | 64 ms | 37.6 MB | Medium | NlogN | | [:link:](https://www.youtube.com/watch?v=KXjgQWDvQ24) |
9090
| 11 | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [Ratcheting](container_with_most_water.js) | 52 ms | 35.4 MB | Medium | O(N) | O(1) | [:link:](https://www.youtube.com/watch?v=k5fbSqb9sCI&t=138s) |
9191
| 24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [Recursive](swap_nodes_in_pairs.js) | 68 ms | 34 MB | Medium | O(N) | | [:link:](https://www.youtube.com/watch?v=zOovxGmION4) |
92+
| 48 | [Rotate Image](https://leetcode.com/problems/rotate-image/) | [Iterative](rotate_image.js) | 56 ms | 33.7 MB | Medium | O(N) | O(1) | [:link:](https://leetcode.com/problems/rotate-image/solution/) |
9293
| 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | [Itterative](valid_sudoku.js) | 68 ms | 37.7 MB | Medium | O(1) | O(1) | [:link:](https://leetcode.com/problems/valid-sudoku/solution/) |
9394
| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [Sliding Window Approach](k_distinct_characters.js) | 68 ms | 37.1 MB | Hard | NlogN | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1936s) |
9495

rotate_image.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// https://leetcode.com/problems/rotate-image/
2+
3+
/**
4+
* @param {number[][]} matrix
5+
* @return {void} Do not return anything, modify matrix in-place instead.
6+
*/
7+
var rotate = function(matrix) {
8+
const n = matrix.length;
9+
10+
//tranpose the matric
11+
for (let i = 0; i < n; i++) {
12+
for (let j = i; j < n; j++) {
13+
const temp = matrix[j][i];
14+
matrix[j][i] = matrix[i][j];
15+
matrix[i][j] = temp;
16+
}
17+
}
18+
// reverse row
19+
for (let i = 0; i < n; i++) {
20+
for (let j = 0; j < Math.floor(n / 2); j++) {
21+
let tmp = matrix[i][j];
22+
matrix[i][j] = matrix[i][n - j - 1];
23+
matrix[i][n - j - 1] = tmp;
24+
}
25+
}
26+
};
27+
28+
rotate([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); //?

0 commit comments

Comments
 (0)