Skip to content

Commit caa0c46

Browse files
author
zhangzhengxian
committed
7.18
1 parent ccba289 commit caa0c46

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
|48|[Rotate Image](https://leetcode.com/problems/rotate-image/)| [JavaScript](./algorithms/RotateImage.js)|Medium|
4343
|49|[Group Anagrams](https://leetcode.com/problems/group-anagrams/)| [JavaScript](./algorithms/groupAnagrams.js)|Medium|
4444
|50|[Pow(x, n)](https://leetcode.com/problems/powx-n/)| [JavaScript](./algorithms/Pow(x%2C%20n).js)|Medium|
45+
|51|[N-Queens](https://leetcode.com/problems/n-queens/)| [JavaScript](./algorithms/N-Queens.js)|Hard|
46+
|53|[Maximum Subarray](https://leetcode.com/problems/maximum-subarray/)| [JavaScript](./algorithms/MaximumSubarray.js)|Easy|
47+
|54|[Spiral Matrix](https://leetcode.com/problems/spiral-matrix/)| [JavaScript](./algorithms/SpiralMatrix.js)|Medium|
4548
|69|[Sqrt(x)](https://leetcode.com/problems/sqrtx/)| [JavaScript](./algorithms/Sqrt(x).js)|Easy|
4649
|371|[Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/)| [JavaScript](./algorithms/Sum_of_Two_Integers.js)|Easy|
4750
|372|[Super Pow](https://leetcode.com/problems/super-pow/)| [JavaScript](./algorithms/SuperPow.js)|Medium|

algorithms/MaximumSubarray.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var maxSubArray = function(nums) {
6+
var temp = nums[0],
7+
max = nums[0];
8+
for (let i = 1; i < nums.length; i++) {
9+
if (temp < 0) {
10+
temp = nums[i];
11+
if (nums[i] > max) {
12+
max = nums[i];
13+
}
14+
} else {
15+
if (temp + nums[i] > max) {
16+
max = temp + nums[i];
17+
}
18+
temp = temp + nums[i];
19+
}
20+
}
21+
return max;
22+
};

algorithms/N-Queens.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @param {number} n
3+
* @return {string[][]}
4+
*/
5+
var solveNQueens = function(n) {
6+
let result = [],
7+
i = 1,
8+
temp = [],
9+
// 检测是否符合要求的工具函数
10+
check = function(arr, num) {
11+
for (let k = 0; k < num; k++) {
12+
if (arr[k] === arr[num] || Math.abs(arr[k] - arr[num]) === num - k) {
13+
return false;
14+
}
15+
}
16+
return true;
17+
},
18+
queens = '.'.repeat(n).split('');
19+
temp[0] = 0;
20+
// 回溯法求解
21+
while (i > 0) {
22+
temp[i - 1] += 1;
23+
while (temp[i - 1] <= n){
24+
if(check(temp, i - 1)) {
25+
if (i === n) {
26+
result.push(temp.slice(0, n));
27+
}
28+
temp[i] = 0;
29+
i += 2;
30+
break;
31+
} else {
32+
temp[i - 1] += 1;
33+
}
34+
}
35+
i--;
36+
}
37+
// 得到的实际上是数字解,比如[2,4,1,3]这种,所以需要转换一下
38+
return result.map((val) => {
39+
return val.map((item) => {
40+
let q = [...queens];
41+
q.splice(item - 1, 1, 'Q');
42+
return q.join('');
43+
})
44+
})
45+
};

algorithms/SpiralMatrix.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {number[]}
4+
*/
5+
var spiralOrder = function(matrix) {
6+
if (matrix.length === 0) {
7+
return matrix;
8+
}
9+
let m = matrix.length,
10+
n = matrix[0].length,
11+
result =[],
12+
res = [];
13+
if (m === 1) {
14+
return matrix[0];
15+
} else if (n === 1) {
16+
return matrix.map(val => {
17+
return val[0];
18+
})
19+
}
20+
let min = Math.min(m, n);
21+
let count = Math.ceil(min / 2)
22+
for (let i = 0; i < count; i++) {
23+
let top = matrix[i].slice(i, n - i),
24+
bottom = [],
25+
right = [],
26+
left = [];
27+
if(i !== m - i - 1) {
28+
bottom = matrix[m - i - 1].slice(i, n - i).reverse();
29+
}
30+
for (let j = i + 1; j < m - i - 1; j++) {
31+
right.push(matrix[j][n - i - 1]);
32+
if (i !== n - i - 1) {
33+
left.push(matrix[j][i]);
34+
}
35+
}
36+
result.push(...[...top, ...right, ...bottom, ...left.reverse()]);
37+
}
38+
return result;
39+
};

0 commit comments

Comments
 (0)