diff --git a/README.md b/README.md index 2bf93af3..c7a13e34 100644 --- a/README.md +++ b/README.md @@ -514,7 +514,7 @@ LeetCode |51|[N-Queens](https://leetcode.com/problems/n-queens/)| [C++](./algorithms/cpp/nQueens/nQueuens.cpp)|Hard| |50|["Pow(x, n)"](https://leetcode.com/problems/powx-n/)| [C++](./algorithms/cpp/pow/pow.cpp), [Java](./algorithms/java/src/powXn/PowXn.java)|Medium| |49|[Group Anagrams](https://leetcode.com/problems/anagrams/)| [C++](./algorithms/cpp/anagrams/GroupAnagrams.cpp)|Medium| -|48|[Rotate Image](https://leetcode.com/problems/rotate-image/)| [C++](./algorithms/cpp/rotateImage/rotateImage.cpp), [Java](./algorithms/java/src/RotateImage/rotateImage.java)|Medium| +|48|[Rotate Image](https://leetcode.com/problems/rotate-image/)| [C++](./algorithms/cpp/rotateImage/rotateImage.cpp), [Java](./algorithms/java/src/RotateImage/rotateImage.java), [Javascript](./algorithms/javascript/rotateImage/rotateImage.js)|Medium| |47|[Permutations II](https://leetcode.com/problems/permutations-ii/)| [C++](./algorithms/cpp/permutations/permutations.II.cpp)|Hard| |46|[Permutations](https://leetcode.com/problems/permutations/)| [C++](./algorithms/cpp/permutations/permutations.cpp)|Medium| |45|[Jump Game II](https://leetcode.com/problems/jump-game-ii/)| [C++](./algorithms/cpp/jumpGame/jumpGame.II.cpp)|Hard| diff --git a/algorithms/javascript/rotateImage/rotateImage.js b/algorithms/javascript/rotateImage/rotateImage.js new file mode 100644 index 00000000..7754e343 --- /dev/null +++ b/algorithms/javascript/rotateImage/rotateImage.js @@ -0,0 +1,21 @@ +/** + * @param {number[][]} matrix + * @return {void} Do not return anything, modify matrix in-place instead. + */ +var rotate = function(matrix) { + const n = matrix.length; + const depth = ~~(n / 2); + + for (let i = 0; i < depth; i++) { + const innerLimit = n - 1 - 2 * i; + const opp = n - i - 1; + for (let j = 0; j < innerLimit; j++) { + const temp = matrix[i][i + j]; + matrix[i][i + j] = matrix[opp - j][i]; + matrix[opp - j][i] = matrix[opp][opp - j]; + matrix[opp][opp - j] = matrix[i + j][opp]; + matrix[i + j][opp] = temp; + } + } + return matrix; +}; \ No newline at end of file