0%

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function partition(s: string): string[] {
const res = [],
arr = [];

function isPalindrome(i: number, j: number) {
for (; i < j; ) {
if (s[i] !== s[j]) {
return false;
}
i++;
j--;
}
return true;
}

function loop(left: number, right: number) {
if (left === s.length) {
return res.push(arr.slice());
}
if (right > s.length) return;
if (isPalindrome(left, right - 1)) {
arr.push(s.slice(left, right));
loop(right, right + 1);
arr.pop();
}
loop(left, right + 1);
}

loop(0, 1);
return res;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var moveZeroes = function(nums: number[]): void {
let start = 0;
let end = nums.length - 1;
while (start < end) {
if (nums[start] === 0) {
nums.push(0);
nums.splice(start, 1);
end--;
continue;
}
start++;
}
};

1
2
3
4
5
6
7
8
9
10
11
12
/**
* @param {number[]} nums1
* @param {number} m
* @param {number[]} nums2
* @param {number} n
* @return {void} Do not return anything, modify nums1 in-place instead.
*/
var merge = function(nums1: number[], m: number, nums2: number[], n: number) {
let s1 = nums1.slice(0, m);
let s2 = nums2.slice(0, n);
[...s1, ...s2].sort((a, b)=>a-b).map((v, k) => nums1[k] = v);
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* @param {number[]} nums
* @return {number}
*/
var maxProduct = function(nums: number[]): number {
let max = nums[0];
let min = nums[0];
let res = nums[0];

for (let i = 1; i < nums.length; i++) {
let tmp = min;
min = Math.min(nums[i], Math.min(max * nums[i], min * nums[i]));
max = Math.max(nums[i], Math.max(max * nums[i], tmp * nums[i]));
res = Math.max(res, max);
}
return res;
};

1
2
3
4
5
6
7
/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function(s: string): boolean {
return s.replace(/[^0-9a-z]/gi, '').toLowerCase().split('').every((item, i, arr)=>item === arr[arr.length-1-i])
};