Leetcode 練習:2021-12-29

December 29, 2021

Single Number

題號:136

var singleNumber = function(nums) {
  if (nums.length === 1) return nums[0]
  let result = nums[0]
  for (var i = 1; i < nums.length; i++) {
    result ^=  nums[i] //重複的 1 會被抵消。
  }
  return result
}

Linked List Cycle

題號:141

var hasCycle = function(head) {
  if (!head || !head.next) return false
  let slow = fast = head
  while (fast && fast.next) {
    slow = slow.next
    fast = fast.next.next
    if (slow === fast) return true
  }
  return false
}
var hasCycle = function(head) {
  const set = new Set()
  while (head) {
    if (set.has(head)) return true
    set.add(head)
    head = head.next
  }
  return false
}

Populating Next Right Pointers in Each Node

題號:116

var connect = function(root) {
  const recur = (rt, pa, left) => {
    if(!rt) return
    if (left) rt.next = pa.right
    else if(!left && pa && pa.next) rt.next = pa.next.left
    recur(rt.left, rt, true)
    recur(rt.right, rt, false)
  }
  recur(root)
  return root
}

Profile picture

Written by Wei Hung who is thinking. You should follow them on Twitter