# js解leetcode(67)-中等

### 1.所有可能的满二叉树

/**
* Definition for a binary tree node.
* function TreeNode(val) {
*     this.val = val;
*     this.left = this.right = null;
* }
*/
/**
* @param {number} N
* @return {TreeNode[]}
*/
var allPossibleFBT = function (N) {
if (!(N % 2)) return [];
const map = new Map();
map.set(1, [new TreeNode(0)]);
const getNode = (n) => {
if (map.has(n)) return map.get(n);
if (n === 1) {
return;
} else {
const res = [];
for (let i = 1; i <= n - 2; i += 2) {
const left = getNode(i);
const right = getNode(n - 1 - i);
const l1 = left.length;
const l2 = right.length;
for (let i = 0; i < l1; i++) {
for (let j = 0; j < l2; j++) {
const root = new TreeNode(0);
root.left = left[i];
root.right = right[j];
res.push(root);
}
}
}
map.set(n, res);
return res;
}
};
return getNode(N);
};

### 2.子数组按位或操作

/**
* @param {number[]} arr
* @return {number}
*/
var subarrayBitwiseORs = function(arr) {
const set1 = new Set();
let set2 = new Set();
for (const x of arr) {
const set3 = new Set();
for (const y of set2) {
}
set2 = set3;
for (const s of set2) {
}
}
return set1.size;
};

### 3.RLE迭代器

/**
* @param {number[]} A
*/
var RLEIterator = function (A) {
const l = A.length;
this.data = [];
for (let i = 0; i < l; i += 2) {
this.data.push([A[i], A[i + 1]]);
}
};

/**
* @param {number} n
* @return {number}
*/
RLEIterator.prototype.next = function (n) {
while (n) {
if (!this.data.length) return -1;
const item = this.data.shift();
if (item[0] >= n) {
this.data.unshift([item[0] - n, item[1]]);
n = 0;
return item[1];
} else {
n -= item[0];
}
}
};


### 4.股票价格跨度

var StockSpanner = function () {
this.data = [];
this.length = [];
};

/**
* @param {number} price
* @return {number}
*/
StockSpanner.prototype.next = function (price) {
let length = 1;
while (this.data.length && this.data[this.data.length - 1] <= price) {
this.data.pop();
length += this.length.pop();
}
this.data.push(price);
this.length.push(length);
return length
};

### 5.水果成篮

/**
* @param {number[]} tree
* @return {number}
*/
var totalFruit = function(tree) {
let max = 0;
const l = tree.length;
let left = 0,
right = 0;
let c = 0;
const set = new Set();
let last = 0;

while (right < l) {
if (set.size === 2 && !set.has(tree[right])) {
set.clear();
left = last;
}
const curV = right - left + 1;
max = Math.max(curV, max);
if (tree[last] !== tree[right]) {
last = right;
}
right++;
}
return max;
};

06-16 99
09-01 102
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客