155. 最小栈

关键: 每次入栈缓存当前位置最小值, 出栈时同时操作混存栈

  1. MinStack.prototype.push = function(x) {
  2. this.nums.push(x);
  3. this.mins.push(this.size == 0 ? x : Math.min(this.mins[this.size-1], x));
  4. this.size++;
  5. };

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;
};