Skip to content

Commit 6395cb2

Browse files
committed
add scripts
1 parent 4527275 commit 6395cb2

7 files changed

+212
-0
lines changed

513-find-bottom-left-tree-value.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number}
11+
*/
12+
const findBottomLeftValue = function(root) {
13+
const res = []
14+
single(root, 0, res)
15+
return res[res.length - 1][0].val
16+
};
17+
18+
function single(node, row, arr) {
19+
if (node == null) {
20+
return null
21+
}
22+
if (row < arr.length) {
23+
arr[row].push(node)
24+
} else {
25+
arr[row] = [node]
26+
}
27+
single(node.left, row + 1, arr)
28+
single(node.right, row + 1, arr)
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number[]}
11+
*/
12+
const largestValues = function(root) {
13+
const res = []
14+
single(root, 0, res)
15+
return res
16+
};
17+
18+
function single(node, row, arr) {
19+
if (node == null) {
20+
return null
21+
}
22+
if (row < arr.length) {
23+
if (node.val > arr[row]) {
24+
arr[row] = node.val
25+
}
26+
} else {
27+
arr[row] = node.val
28+
}
29+
single(node.left, row + 1, arr)
30+
single(node.right, row + 1, arr)
31+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const singleNonDuplicate = function(nums) {
6+
let i=0;
7+
while(true){
8+
if(nums[i]==nums[i+1]) {
9+
i+=2;
10+
} else {
11+
return nums[i];
12+
}
13+
}
14+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number[]}
11+
*/
12+
const averageOfLevels = function(root) {
13+
const res = []
14+
layer(res, [root])
15+
return res.map(el => el.val / el.num)
16+
17+
};
18+
19+
function layer(arr, args) {
20+
const item = {
21+
val: args.reduce((ac, el) => ac + el.val, 0),
22+
num: args.length
23+
}
24+
arr.push(item)
25+
const children = []
26+
args.forEach(el => {
27+
el.left === null ? null : children.push(el.left)
28+
el.right === null ? null : children.push(el.right)
29+
})
30+
if(children.length) {
31+
layer(arr, children)
32+
}
33+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number} n
3+
* @return {boolean}
4+
*/
5+
const hasAlternatingBits = function(n) {
6+
const str = bin(n)
7+
for(let i = 1, prev = str.charAt(0); i < str.length; i++) {
8+
if (str.charAt(i) === prev) {
9+
return false
10+
}
11+
prev = str.charAt(i)
12+
}
13+
return true
14+
};
15+
16+
function bin(num){
17+
return (num >>> 0).toString(2)
18+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @param {number} L
3+
* @param {number} R
4+
* @return {number}
5+
*/
6+
const countPrimeSetBits = function(L, R) {
7+
let res = 0
8+
for(let i = L; i <= R; i++) {
9+
if (chkPrime(i)) {
10+
res += 1
11+
}
12+
}
13+
return res
14+
};
15+
16+
function chkPrime(num) {
17+
const str = bin(num)
18+
const snum = setNum(str)
19+
return isPrime(snum)
20+
}
21+
22+
function setNum(str) {
23+
let num = 0
24+
for(let i = 0; i < str.length; i++) {
25+
if (str.charAt(i) === '1') {
26+
num += 1
27+
}
28+
}
29+
return num
30+
}
31+
32+
function bin(num) {
33+
return (num >>> 0).toString(2)
34+
}
35+
function isPrime(num) {
36+
for(let i = 2; i < num; i++) {
37+
if(num % i === 0) return false;
38+
}
39+
return num !== 1;
40+
}

835-image-overlap.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @param {number[][]} A
3+
* @param {number[][]} B
4+
* @return {number}
5+
*/
6+
const largestOverlap = function(A, B) {
7+
const N = A.length
8+
const count = constructMatrix(2*N, 2*N, 0)
9+
for (let i = 0; i < N; i++) {
10+
for (let j = 0; j < N; j++) {
11+
if (A[i][j] === 1) {
12+
for(let ii = 0; ii < N; ii++) {
13+
for(let jj = 0; jj < N; jj++) {
14+
if(B[ii][jj] === 1) {
15+
count[i-ii+N][j-jj+N] += 1
16+
}
17+
}
18+
}
19+
}
20+
}
21+
}
22+
let ans = 0
23+
24+
for(let row of count) {
25+
for(let v of row) {
26+
ans = Math.max(ans, v)
27+
}
28+
}
29+
return ans
30+
};
31+
32+
function constructMatrix(row, col, init = 0) {
33+
const matrix = []
34+
for(let i = 0; i < row; i++) {
35+
matrix[i] = []
36+
for(let j = 0; j < col; j++) {
37+
matrix[i][j] = init
38+
}
39+
}
40+
return matrix
41+
}
42+
43+
console.log(largestOverlap([[1,1,0],
44+
[0,1,0],
45+
[0,1,0]],[[0,0,0],
46+
[0,1,1],
47+
[0,0,1]]))

0 commit comments

Comments
 (0)