Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0d0e187

Browse files
add: Rotate Image and Rotate Array
1 parent 29ffc77 commit 0d0e187

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Progress: 20/
1313
|19|[Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) | [JavaScript](./src/remove-nth-node-from-end-of-list/res.js)|Medium|
1414
|22|[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) | [JavaScript](./src/generate-parentheses/res.js)|Medium|
1515
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [JavaScript](./src/remove-duplicates-from-sorted-array/res.js)|Easy|
16+
|48|[Rotate Image](https://leetcode.com/problems/rotate-image/) | [JavaScript](./src/rotate-image/res.js)|Medium|
1617
|66|[Plus One](https://leetcode.com/problems/plus-one/) | [JavaScript](./src/plus-one/res.js)|Easy|
1718
|69|[Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [JavaScript](./src/sqrtx/res.js)|Easy|
1819
|175|[Combine Two Tables](https://leetcode.com/problems/combine-two-tables/)| [SQL](./src/combine-two-tables/res.txt)|Easy|
@@ -22,6 +23,7 @@ Progress: 20/
2223
|182|[Duplicate Emails](https://leetcode.com/problems/duplicate-emails/) | [SQL](./src/duplicate-emails/res.txt)|Easy|
2324
|183|[Customers Who Never Order](https://leetcode.com/problems/customers-who-never-order/) | [SQL](./src/customers-who-never-order/res.txt)|Easy|
2425
|184|[Department Highest Salary](https://leetcode.com/problems/department-highest-salary/) | [SQL](./src/department-highest-salary/res.txt)|Medium|
26+
|189|[Rotate Array](https://leetcode.com/problems/rotate-array/) | [JavaScript](./src/rotate-array/res.js)|Easy|
2527
|190|[Reverse Bits](https://leetcode.com/problems/reverse-bits/) | [JavaScript](./src/reverse-bits/res.js)|Easy|
2628
|196|[Delete Duplicate Emails](https://leetcode.com/problems/delete-duplicate-emails/) | [SQL](./src/delete-duplicate-emails/res.txt)|Easy|
2729
|197|[Rising Temperature](https://leetcode.com/problems/rising-temperature/) | [SQL](./src/rising-temperature/res.txt)|Easy|

‎src/rotate-array/res.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Rotate an array of n elements to the right by k steps.
2+
3+
// For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
4+
5+
// Note:
6+
// Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
7+
8+
/**
9+
* res.js
10+
* @authors Joe Jiang (hijiangtao@gmail.com)
11+
* @date 2017年02月25日 22:21:51
12+
* @version $Id$
13+
*/
14+
15+
/**
16+
* @param {number[]} nums
17+
* @param {number} k
18+
* @return {void} Do not return anything, modify nums in-place instead.
19+
*/
20+
var rotate = function(nums, k) {
21+
let numslen = nums.length;
22+
for (let i=0; i<k; i++) {
23+
let lastnode = nums.pop();
24+
nums.unshift(lastnode);
25+
}
26+
};

‎src/rotate-image/res.js‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// You are given an n x n 2D matrix representing an image.
2+
3+
// Rotate the image by 90 degrees (clockwise).
4+
5+
// Follow up:
6+
// Could you do this in-place?
7+
8+
/**
9+
* res.js
10+
* @authors Joe Jiang (hijiangtao@gmail.com)
11+
* @date 2017年02月25日 22:57:12
12+
* @version $Id$
13+
*/
14+
15+
/**
16+
* @param {number[][]} matrix
17+
* @return {void} Do not return anything, modify matrix in-place instead.
18+
*/
19+
let rotate = function(matrix) {
20+
let len = matrix.length;
21+
for (let i=0; i<Number.parseInt(len/2); i++) {
22+
for (let j=0; j<len; j++) {
23+
let temp = matrix[i][j];
24+
matrix[i][j] = matrix[len-1-i][j];
25+
matrix[len-1-i][j] = temp;
26+
}
27+
}
28+
29+
for (let i=0; i<len; i++) {
30+
for (let j=0; j<i; j++) {
31+
let temp = matrix[i][j];
32+
matrix[i][j] = matrix[j][i];
33+
matrix[j][i] = temp;
34+
}
35+
}
36+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /