155. 最小栈
关键: 每次入栈缓存当前位置最小值, 出栈时同时操作混存栈
MinStack.prototype.push = function(x) {
this.nums.push(x);
this.mins.push(this.size == 0 ? x : Math.min(this.mins[this.size-1], x));
this.size++;
};
384. 打乱数组
(Fisher-Yates)洗牌算法
~~nums.sort(() => Math.random() - .5) ~~
Solution.prototype.shuffle = function() {
let nums = this.nums;
for (let i=0; i<nums.length; i++) {
let j = Math.floor(Math.random() * (i+1));
[nums[i], nums[j]] = [nums[j], nums[i]];
}
return this.nums;
};