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 ea6eb62

Browse files
authored
add js solutions to leetcode problems: no.54 and no.189 (doocs#370)
* add js solution to leetcode problem: no.189 * add js solution to leetcode problem: no.54 * add some discriptions
1 parent 6db6506 commit ea6eb62

File tree

6 files changed

+128
-0
lines changed

6 files changed

+128
-0
lines changed

‎solution/0000-0099/0054.Spiral Matrix/README.md‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,37 @@ class Solution {
114114
}
115115
```
116116

117+
### **JavaScript**
118+
119+
```js
120+
/**
121+
* @param {number[][]} matrix
122+
* @return {number[]}
123+
*/
124+
var spiralOrder = function (matrix) {
125+
let m = matrix.length;
126+
if (m === 0) return [];
127+
let res = [];
128+
let top = 0, bottom = m - 1, left = 0, right = matrix[0].length - 1;
129+
while (left < right && bottom > top) {
130+
for (let i = left; i < right; i++) res.push(matrix[top][i]);
131+
for (let i = top; i < bottom; i++) res.push(matrix[i][right]);
132+
for (let i = right; i > left; i--) res.push(matrix[bottom][i]);
133+
for (let i = bottom; i > top; i--) res.push(matrix[i][left]);
134+
top++;
135+
bottom--;
136+
left++;
137+
right--;
138+
}
139+
if (left === right) {
140+
for (i = top; i <= bottom; i++) res.push(matrix[i][left]);
141+
} else if (top === bottom) {
142+
for (i = left; i <= right; i++) res.push(matrix[top][i]);
143+
}
144+
return res;
145+
};
146+
```
147+
117148
### **...**
118149

119150
```

‎solution/0000-0099/0054.Spiral Matrix/README_EN.md‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,37 @@ class Solution {
103103
}
104104
```
105105

106+
### **JavaScript**
107+
108+
```js
109+
/**
110+
* @param {number[][]} matrix
111+
* @return {number[]}
112+
*/
113+
var spiralOrder = function (matrix) {
114+
let m = matrix.length;
115+
if (m === 0) return [];
116+
let res = [];
117+
let top = 0, bottom = m - 1, left = 0, right = matrix[0].length - 1;
118+
while (left < right && bottom > top) {
119+
for (let i = left; i < right; i++) res.push(matrix[top][i]);
120+
for (let i = top; i < bottom; i++) res.push(matrix[i][right]);
121+
for (let i = right; i > left; i--) res.push(matrix[bottom][i]);
122+
for (let i = bottom; i > top; i--) res.push(matrix[i][left]);
123+
top++;
124+
bottom--;
125+
left++;
126+
right--;
127+
}
128+
if (left === right) {
129+
for (i = top; i <= bottom; i++) res.push(matrix[i][left]);
130+
} else if (top === bottom) {
131+
for (i = left; i <= right; i++) res.push(matrix[top][i]);
132+
}
133+
return res;
134+
};
135+
```
136+
106137
### **...**
107138

108139
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {number[]}
4+
*/
5+
var spiralOrder = function (matrix) {
6+
let m = matrix.length;
7+
if (m === 0) return [];
8+
let res = [];
9+
let top = 0, bottom = m - 1, left = 0, right = matrix[0].length - 1;
10+
while (left < right && bottom > top) {
11+
for (let i = left; i < right; i++) res.push(matrix[top][i]);
12+
for (let i = top; i < bottom; i++) res.push(matrix[i][right]);
13+
for (let i = right; i > left; i--) res.push(matrix[bottom][i]);
14+
for (let i = bottom; i > top; i--) res.push(matrix[i][left]);
15+
top++;
16+
bottom--;
17+
left++;
18+
right--;
19+
}
20+
if (left === right) {
21+
for (i = top; i <= bottom; i++) res.push(matrix[i][left]);
22+
} else if (top === bottom) {
23+
for (i = left; i <= right; i++) res.push(matrix[top][i]);
24+
}
25+
return res;
26+
};

‎solution/0100-0199/0189.Rotate Array/README.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,23 @@ class Solution {
119119
}
120120
```
121121

122+
### **JavaScript**
123+
124+
<!-- 这里可写当前语言的特殊实现逻辑 -->
125+
使用原生 API 将数组的 `k~n-1` 范围内的元素插入到前面
126+
127+
```js
128+
/**
129+
* @param {number[]} nums
130+
* @param {number} k
131+
* @return {void} Do not return anything, modify nums in-place instead.
132+
*/
133+
var rotate = function (nums, k) {
134+
k %= nums.length;
135+
nums.splice(0, 0, ...nums.splice(-k, k))
136+
};
137+
```
138+
122139
### **...**
123140

124141
```

‎solution/0100-0199/0189.Rotate Array/README_EN.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ class Solution {
9898
}
9999
```
100100

101+
### **JavaScript**
102+
103+
```js
104+
/**
105+
* @param {number[]} nums
106+
* @param {number} k
107+
* @return {void} Do not return anything, modify nums in-place instead.
108+
*/
109+
var rotate = function (nums, k) {
110+
k %= nums.length;
111+
nums.splice(0, 0, ...nums.splice(-k, k))
112+
};
113+
```
114+
101115
### **...**
102116

103117
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {void} Do not return anything, modify nums in-place instead.
5+
*/
6+
var rotate = function (nums, k) {
7+
k %= nums.length;
8+
nums.splice(0, 0, ...nums.splice(-k, k))
9+
};

0 commit comments

Comments
(0)

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