leetcode: [0518 - 0522] 所刷题目

背景

记录一下,工作之所刷的 leetcode 题目。

01.twoSum

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function (nums, target) {
  for (var i = 1; i < nums.length; i++) {
    const idx = nums.indexOf(target - nums[i]);
    if (idx > -1 && idx !== i) {
      return [i, idx];
    }
  }
  return [];
};

15.threeSum

/**
 * @param {number[]} nums
 * @return {number[][]}
 */

// Given array nums = [-1, 0, 1, 2, -1, -4],
// A solution set is:
// [
//   [-1, 0, 1],
//   [-1, -1, 2]
// ]

var threeSum = function (nums) {
  const result = [];
  const length = nums.length;

  if (length === 3) return [nums];

  var sortedNums = num.sort((a, b) => a - b);

  for (let i = 0; i < l; i++) {
    if (i !== 0 && sortedNums[i] === sortedNums[i - 1]) continue;
    var j = i + 1;
    var k = l - 1;
    while (j < k) {
      var sum = temp[i] + temp[j] + temp[k];
      if (sum === 0) {
        res.push([temp[i], temp[j], temp[k]]);
        while (j++ < k && temp[j - 1] === temp[j]) {
          /* do nothing*/
        }
        while (k-- > j && temp[k] === temp[k + 1]) {
          /* do nothing*/
        }
      } else if (sum < 0) {
        j++;
      } else {
        k--;
      }
    }
  }

  return result;
};

70.climbStairs

/**
 * @param {number} n
 * @return {number}
 */
var climbStairs = function (n) {
  const array = [0, 1, 2];

  for (var i = 3; i <= n; i++) {
    array[i] = array[i - 1] + array[i - 2];
  }

  return array[n];
};

283.moveZeroes

/**
 * @param {number[]} nums
 * @return {void} Do not return anything, modify nums in-place instead.
 */
var moveZeroes = function (nums) {
  var validNumberCount = 0;
  for (var i = 0; i < nums.length; i++) {
    if (nums[i] !== 0) {
      nums[validNumberCount] = nums[i];
      validNumberCount++;
    }
  }

  for (var j = validNumberCount; j < nums.length; j++) {
    nums[j] = 0;
  }

  return nums;
};

11.maxArea

/**
 * @param {number[]} height
 * @return {number}
 */

//  solution 1: 暴力双循环
var maxArea = function (height) {
  var max = 0;

  for (var i = 0; i < height.length; i++) {
    for (var j = i + 1; j < height.length; j++) {
      max = Math.max(max, (j - i) * Math.min(height[i], height[j]));
    }
  }

  return max;
};

//  solution 1: 双指针
var maxArea = function (height) {
  var max = 0;
  var left = 0, right = height.length - 1;

  while (left < right) {
    var area = (right - left) * Math.min(height[left], height[right]);
    max = Math.max(max, area);
    
    if (height[left] < height[right]) {
      left++;
    } else {
      right--;
    }
  }

  return max;
};

84.largestRectangleArea

/**
 * @param {number[]} heights
 * @return {number}
 */

// ## 柱状图中最大的矩形
// solution1: 暴力循环
var largestRectangleArea = function (heights) {
  var maxArea = 0;

  for (var i = 0; i < heights.length; i++) {
    for (j = i; j < heights.length; j++) {
      var minHeight = Number.MAX_VALUE;
      for (var k = i; k <= j; k++) {
        minHeight += Math.min(minHeight, heights[k]);
      }
      maxArea = Math.max(maxArea, minHeight * (j - i + 1));
    }
  }

  return maxArea;
};

// solution2: 最小栈
// TODO

239.maxSlidingWindow

/*
 * @lc app=leetcode.cn id=239 lang=javascript
 *
 * [239] 滑动窗口最大值
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
// 解法1: 暴力
var maxSlidingWindow = function (nums, k) {
  var length = nums.length;
  if (!length) return [];

  var res = [];
  for (var i = 0; i < length - (k - 1); i++) {
    let max = Number.MIN_SAFE_INTEGER;
    for (j = i; j < i + k; j++) {
      max = Math.max(max, nums[j]);
    }
    res.push(max);
  }
  return res;
};
// @lc code=end

289.rotateArray

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {void} Do not return anything, modify nums in-place instead.
 */
// solution 1: 暴力, 每次移动一个
var rotate = function (nums, k) {
  let previous;
  for (var i = 0; i < k; i++) {
    previous = nums[nums.length - 1];
    for (var j = 0; j < nums.length; j++) {
      [previous, nums[j]] = [nums[j], previous];
    }
  }
};

88.mergeTwoArray

/**
 * @param {number[]} nums1
 * @param {number} m
 * @param {number[]} nums2
 * @param {number} n
 * @return {void} Do not return anything, modify nums1 in-place instead.
 */

// 解法1: 合并后排序
var merge = function (nums1, m, nums2, n) {
  for (var i = 0; i < nums2.length; i++) {
    nums1[m + i] = nums2[i];
  }
  nums1.sort((a, b) => a - b);
};

// 解法2: 判断元素大小, 直接插入
var merge = function (nums1, m, nums2, n) {
  let length = m + n;
  while(n > 0) {
    if(m <= 0) { // num1中的元素已经排完, 剩下的nums直接放进来
      nums1[--length] = nums2[--n];
      continue;
    }
    nums1[--length] = nums1[m-1] > nums2[n-1] ? nums1[--m] : nums2[--n];
  }
};

21.mergeTwoLists

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var mergeTwoLists = function (l1, l2) {
  if (l1 === null) return l2;
  if (l2 === null) return l1;

  if (l1.val < l2.val) {
    l1.next = mergeTwoLists(l1.next, l2);
    return l1;
  } else {
    l2.next = mergeTwoLists(l2.next, l1);
    return l2;
  }
};

42.trap 接雨水

/**
 * @param {number[]} height
 * @return {number}
 */
var trap = function (height) {
  let capacity = 0;
  const stack = [];

  for (let i = 0; i < height.length; i++) {
    while (stack.length !== 0 && height[stack[stack.length - 1]] < height[i]) {
      let cur = stack[stack.length - 1];
      stack.pop();
      if (stack.length === 0) {
        break;
      }
      let l = stack[stack.length - 1];
      let r = i;
      capacity += (Math.min(height[l], height[r]) - height[cur]) * (r - l - 1);
    }

    stack.push(i);
  }

  return capacity;
};

你可能感兴趣的:(leetcode)