Skip to content

Commit be3ea55

Browse files
committed
add 413 700 scripts.
1 parent 6395cb2 commit be3ea55

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

413-arithmetic-slices.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} A
3+
* @return {number}
4+
*/
5+
const numberOfArithmeticSlices = function(A) {
6+
const arr = [];
7+
let count = 0;
8+
for (let i = 1; i < A.length - 1; i++) {
9+
if (A[i] - A[i - 1] === A[i + 1] - A[i]) {
10+
count += 1;
11+
} else {
12+
arr.push(count);
13+
count = 0;
14+
}
15+
}
16+
arr.push(count);
17+
return arr.reduce((ac, el) => ac + calc(el), 0);
18+
};
19+
20+
function calc(num) {
21+
return (num * (num + 1)) / 2;
22+
}
23+
24+
console.log(numberOfArithmeticSlices([1, 2, 3, 4]));

700-search-in-a-binary-search-tree.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
* @param {number} val
11+
* @return {TreeNode}
12+
*/
13+
const searchBST = function(root, val) {
14+
if (root === null) return [];
15+
16+
if (root.val === val) {
17+
return root;
18+
} else if (root.val < val) {
19+
return searchBST(root.right, val);
20+
} else {
21+
return searchBST(root.left, val);
22+
}
23+
};

0 commit comments

Comments
 (0)