5

A selection of 100 most popular topics on LeetCode. This article is only at an easy level, suitable for novices who are new to algorithms and data structures and those who want to improve efficiently in a short period of time.

1. The sum of two numbers

https://leetcode-cn.com/problems/two-sum

method one
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function (nums, target) {
  for (let i = 0; i < nums.length; i++) {
    let diff = target - nums[i]
    for (let j = i + 1; j < nums.length; j++) {
      if (diff == nums[j]) {
        return [i, j]
      }
    }
  }
}
Method Two
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function (nums, target) {
  var temp = []
  for (var i = 0; i < nums.length; i++) {
    var dif = target - nums[i]
    if (temp[dif] != undefined) {
      return [temp[dif], i]
    }
    temp[nums[i]] = i
  }
}

14. The longest common prefix

https://leetcode-cn.com/problems/longest-common-prefix

Ideas:
  1. Traverse the array first
  2. Traverse the first string of the array again, compare each character in the string with the corresponding string subscript of each item in the array. If the difference is different, then jump out of the loop, find the common prefix pair by pair, and the final result It is the length j of the longest common prefix.
  3. The character of the intercept string length j is the longest common prefix
const strs = ['flower', 'flow', 'flight']
const longestCommonPrefix = function (strs) {
  if (strs === null || strs.length === 0) return ''
  let commonString = ''

  for (let i = 1; i < strs.length; i++) {
    let j = 0
    for (; j < strs[0].length && j < strs[i].length; j++) {
      if (strs[0][j] !== strs[i][j]) break
    }
    commonString = strs[0].substring(0, j)
  }
  return commonString
}
longestCommonPrefix(strs)

18.Delete the node of the linked list

https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof

var deleteNode = function (head, val) {
  if (head.val === val) return head.next
  let prev = head,
    node = prev.next
  while (node) {
    if (node.val === val) {
      prev.next = node.next
    }
    prev = node
    node = node.next
  }
  return head
}

20. Valid parentheses

https://leetcode-cn.com/problems/valid-parentheses

Method analysis:

Knowledge of the stack used in this question. The stack has the characteristics of FILO. The stack has the top and bottom of the stack. The so-called push is to push the element onto the stack; the so-called pop is to pop the top element of the stack. The stack is pushed into the stack first, and then popped out, so the stack can be used to detect whether the symbols are paired correctly.

Problem-solving ideas:
  1. The length of the valid bracket string must be an even number!
  2. Before the right parenthesis, it must be the corresponding left parenthesis to offset it!
  3. Before the right parenthesis, it is not the corresponding left parenthesis, then the string must not be a valid parenthesis!
var isValid = function (s) {
  let stack = []
  if (!s || s.length % 2) return false
  for (let item of s) {
    switch (item) {
      case '{':
      case '[':
      case '(':
        stack.push(item)
        break
      case '}':
        if (stack.pop() !== '{') return false
        break
      case '[':
        if (stack.pop() !== ']') return false
        break
      case '(':
        if (stack.pop() !== ')') return false
        break
    }
  }
  return !stack.length
}

21. Merge two ordered linked lists

https://leetcode-cn.com/problems/merge-two-sorted-lists

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

53. Maximum Subsequence Sum

https://leetcode-cn.com/problems/maximum-subarray

/**
 * @param {number[]} nums
 * @return {number}
 */
var maxSubArray = function (nums) {
  let ans = nums[0]
  let sum = 0
  for (const num of nums) {
    if (sum > 0) {
      sum += num
    } else {
      sum = num
    }
    ans = Math.max(ans, sum)
  }
  return ans
}

70. Climb stairs

https://leetcode-cn.com/problems/climbing-stairs

var climbStairs = function (n) {
  let dp = []
  dp[0] = 1
  dp[1] = 1
  for (let i = 2; i <= n; i++) {
    dp[i] = dp[i - 1] + dp[i - 2]
  }
  return dp[n]
}

101. Symmetric Binary Tree

https://leetcode-cn.com/problems/symmetric-tree

/**递归 代码
 * @param {TreeNode} root
 * @return {boolean}
 */
var isSymmetric = function (root) {
  const check = (left, right) => {
    if (left == null && right == null) {
      return true
    }
    if (left && right) {
      return (
        left.val === right.val &&
        check(left.left, right.right) &&
        check(left.right, right.left)
      )
    }
    return false // 一个子树存在一个不存在,肯定不对称
  }
  if (root == null) {
    // 如果传入的root就是null,对称
    return true
  }
  return check(root.left, root.right)
}

112. Path Sum

https://leetcode-cn.com/problems/path-sum

var hasPathSum = function (root, targetSum) {
  // 深度优先遍历
  if (root === null) {
    //1.刚开始遍历时
    //2.递归中间 说明该节点不是叶子节点
    return false
  }
  if (root.left === null && root.right === null) {
    return root.val - targetSum === 0
  }
  // 拆分成两个子树
  return (
    hasPathSum(root.left, targetSum - root.val) ||
    hasPathSum(root.right, targetSum - root.val)
  )
}

136. Numbers that only appear once

https://leetcode-cn.com/problems/single-number

/**
 * @param {number[]} nums
 * @return {number}
 */
var singleNumber = function (nums) {
  let ans = ''
  for (const num of nums) {
    ans ^= num
    console.log(ans)
  }
  return ans
}

155. Minimal stack

https://leetcode-cn.com/problems/min-stack

var MinStack = function () {
  this.x_stack = []
  this.min_stack = [Infinity]
}

MinStack.prototype.push = function () {
  this.x_stack.push(x)
  this.min_stack.push(Math.min(this.min_stack[this.min_stack.length - 1], x))
}
MinStack.prototype.pop = function () {
  this.x_stack.pop()
  this.min_stack.pop()
}
MinStack.prototype.top = function () {
  return this.x_stack[this.x_stack.length - 1]
}
MinStack.prototype.getMin = function () {
  return this.min_stack[this.min_stack.length - 1]
}

160. Intersect Linked List

https://leetcode-cn.com/problems/intersection-of-two-linked-lists

Method 1: Violence method

Ideas

For each node of the linked list A, go to the linked list B to traverse to see if there are the same nodes.

the complexity

Time complexity: O(M * N)O(M∗N), M, N are the lengths of two linked lists respectively.
Space complexity: O(1)O(1).

var getIntersectionNode = function (headA, headB) {
  if (!headA || !headB) return null
  let pA = headA
  while (pA) {
    let pB = headB
    while (pB) {
      if (pA === pB) return pA
      pB = pB.next
    }
    pA = pA.next
  }
}

Method 2: Hash table

Ideas

First traverse the linked list A, and use the hash table to record each node (note that the node reference rather than the node value should be stored).
Then traverse the linked list B, and find the node that has appeared in the hash table is the intersection of the two linked lists.

the complexity

Time complexity: O(M + N), M, N are the lengths of the two linked lists respectively.
Space complexity: O(N), N is the length of the linked list A.

var getIntersectionNode = function (headA, headB) {
  if (!headA || !headB) return null
  const hashmap = new Map()
  let pA = headA
  while (pA) {
    hashmap.set(pA, 1)
    pA = pA.next
  }
  let pB = headB
  while (pB) {
    if (hashmap.has(pB)) return pB
    pB = pB.next
  }
}

Method 3: Double pointer

If the linked list has no intersection

The two linked lists have the same length. After the first traversal, pA and pB are both null, and the traversal ends
The two linked lists have different lengths. After the two traversals, pA and pB are both null, and the traversal ends

the complexity

Time complexity: O(M + N), M, N are the lengths of the two linked lists respectively.
Space complexity: O(1).

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} headA
 * @param {ListNode} headB
 * @return {ListNode}
 */
var getIntersectionNode = function (headA, headB) {
  if (!headA || !headB) return null

  let pA = headA,
    pB = headB
  while (pA !== pB) {
    pA = pA === null ? headB : pA.next
    pB = pB === null ? headA : pB.next
  }
  return pA
}

206. Reverse Linked List

var reverseList = function (head) {
  let prev = null
  cur = head
  while (cur) {
    const next = cur.next
    cur.next = prev
    prev = cur
    cur = next
  }
  return prev
}

Scenario 2

var reverseList = function (head) {
  let prev = null
  cur = head
  while (cur) {
    const next = cur.next
    cur.next = prev
    prev = cur
    cur = next
  }
  return prev
}

234. Palindrome Linked List

https://leetcode-cn.com/problems/palindrome-linked-list

const isPalindrome = (head) => {
  const vals = []
  while (head) {
    // 丢进数组里
    vals.push(head.val)
    head = head.next
  }
  let start = 0,
    end = vals.length - 1 // 双指针
  while (start < end) {
    if (vals[start] != vals[end]) {
      // 理应相同,如果不同,不是回文
      return false
    }
    start++
    end-- // 双指针移动
  }
  return true // 循环结束也没有返回false,说明是回文
}

543. Diameter of Binary Tree

https://leetcode-cn.com/problems/diameter-of-binary-tree

method 1
var diameterOfBinaryTree = function (root) {
  // 默认为1是因为默认了根节点自身的路径长度
  let ans = 1
  function depth(rootNode) {
    if (!rootNode) {
      // 如果不存在根节点,则深度为0
      return 0
    }
    // 递归,获取左子树的深度
    let left = depth(rootNode.left)
    // 递归,获取右子树的深度
    let right = depth(rootNode.right)
    /* 关键点1
        L+R+1的公式是如何而来?
        等同于:左子树深度(节点个数) + 右子树深度(节点个数) + 1个根节点
        便是这株二叉树从最左侧叶子节点到最右侧叶子节点的最长路径
        类似于平衡二叉树的最小值节点到最大值节点的最长路径
        之所以+1是因为需要经过根节点
         */
    // 获取该树的最长路径和现有最长路径中最大的那个
    ans = Math.max(ans, left + right + 1)
    /* 关键点2
        已知根节点的左右子树的深度,
        则,左右子树深度的最大值 + 1,
        便是以根节点为数的最大深度*/
    return Math.max(left, right) + 1
  }
  depth(root)
  // 由于depth函数中已经默认加上数节点的自身根节点路径了,故此处需减1
  return ans - 1
}
Method 2
function height(node) {
  //求树高
  if (!node) return 0
  return 1 + Math.max(height(node.left), height(node.right))
}

var diameterOfBinaryTree = function (root) {
  if (!root) return 0
  let tempH = height(root.left) + height(root.right)
  return Math.max(
    tempH,
    diameterOfBinaryTree(root.left),
    diameterOfBinaryTree(root.right)
  )
}

617. Merging Binary Trees

https://leetcode-cn.com/problems/merge-two-binary-trees/

var mergeTrees = function (root1, root2) {
  if (root1 == null && root2) {
    return root2
  } else if (root2 == null && root1) {
    return root1
  } else if (root1 && root2) {
    root1.val = root1.val + root2.val
    //递归合并每一个节点
    root1.left = mergeTrees(root1.left, root2.left)
    root1.right = mergeTrees(root1.right, root2.right)
  }
  return root1
}

Note: Some of the solutions refer to LeetCode's best solution, students who need it can go to the official website of LeetCode to check.


frank
223 声望135 粉丝