Skip to content

add js solutions to leetcode problems: no.54 and no.189 #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions solution/0000-0099/0054.Spiral Matrix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,37 @@ class Solution {
}
```

### **JavaScript**

```js
/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function (matrix) {
let m = matrix.length;
if (m === 0) return [];
let res = [];
let top = 0, bottom = m - 1, left = 0, right = matrix[0].length - 1;
while (left < right && bottom > top) {
for (let i = left; i < right; i++) res.push(matrix[top][i]);
for (let i = top; i < bottom; i++) res.push(matrix[i][right]);
for (let i = right; i > left; i--) res.push(matrix[bottom][i]);
for (let i = bottom; i > top; i--) res.push(matrix[i][left]);
top++;
bottom--;
left++;
right--;
}
if (left === right) {
for (i = top; i <= bottom; i++) res.push(matrix[i][left]);
} else if (top === bottom) {
for (i = left; i <= right; i++) res.push(matrix[top][i]);
}
return res;
};
```

### **...**

```
Expand Down
31 changes: 31 additions & 0 deletions solution/0000-0099/0054.Spiral Matrix/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,37 @@ class Solution {
}
```

### **JavaScript**

```js
/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function (matrix) {
let m = matrix.length;
if (m === 0) return [];
let res = [];
let top = 0, bottom = m - 1, left = 0, right = matrix[0].length - 1;
while (left < right && bottom > top) {
for (let i = left; i < right; i++) res.push(matrix[top][i]);
for (let i = top; i < bottom; i++) res.push(matrix[i][right]);
for (let i = right; i > left; i--) res.push(matrix[bottom][i]);
for (let i = bottom; i > top; i--) res.push(matrix[i][left]);
top++;
bottom--;
left++;
right--;
}
if (left === right) {
for (i = top; i <= bottom; i++) res.push(matrix[i][left]);
} else if (top === bottom) {
for (i = left; i <= right; i++) res.push(matrix[top][i]);
}
return res;
};
```

### **...**

```
Expand Down
26 changes: 26 additions & 0 deletions solution/0000-0099/0054.Spiral Matrix/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function (matrix) {
let m = matrix.length;
if (m === 0) return [];
let res = [];
let top = 0, bottom = m - 1, left = 0, right = matrix[0].length - 1;
while (left < right && bottom > top) {
for (let i = left; i < right; i++) res.push(matrix[top][i]);
for (let i = top; i < bottom; i++) res.push(matrix[i][right]);
for (let i = right; i > left; i--) res.push(matrix[bottom][i]);
for (let i = bottom; i > top; i--) res.push(matrix[i][left]);
top++;
bottom--;
left++;
right--;
}
if (left === right) {
for (i = top; i <= bottom; i++) res.push(matrix[i][left]);
} else if (top === bottom) {
for (i = left; i <= right; i++) res.push(matrix[top][i]);
}
return res;
};
17 changes: 17 additions & 0 deletions solution/0100-0199/0189.Rotate Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,23 @@ class Solution {
}
```

### **JavaScript**

<!-- 这里可写当前语言的特殊实现逻辑 -->
使用原生 API 将数组的 `k~n-1` 范围内的元素插入到前面

```js
/**
* @param {number[]} nums
* @param {number} k
* @return {void} Do not return anything, modify nums in-place instead.
*/
var rotate = function (nums, k) {
k %= nums.length;
nums.splice(0, 0, ...nums.splice(-k, k))
};
```

### **...**

```
Expand Down
14 changes: 14 additions & 0 deletions solution/0100-0199/0189.Rotate Array/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ class Solution {
}
```

### **JavaScript**

```js
/**
* @param {number[]} nums
* @param {number} k
* @return {void} Do not return anything, modify nums in-place instead.
*/
var rotate = function (nums, k) {
k %= nums.length;
nums.splice(0, 0, ...nums.splice(-k, k))
};
```

### **...**

```
Expand Down
9 changes: 9 additions & 0 deletions solution/0100-0199/0189.Rotate Array/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @param {number[]} nums
* @param {number} k
* @return {void} Do not return anything, modify nums in-place instead.
*/
var rotate = function (nums, k) {
k %= nums.length;
nums.splice(0, 0, ...nums.splice(-k, k))
};