206.反转链表

  1. /**
  2. * Definition for singly-linked list.
  3. * function ListNode(val, next) {
  4. * this.val = (val===undefined ? 0 : val)
  5. * this.next = (next===undefined ? null : next)
  6. * }
  7. */
  8. /**
  9. * @param {ListNode} head
  10. * @return {ListNode}
  11. */
  12. // 核心思路以单个节点操作,缓存下个,指针指向pre,缓存当前节点给下个用
  13. var reverseList = function(head) {
  14. let pre = null
  15. let curr = head
  16. while(curr) {
  17. let next = curr.next
  18. curr.next = pre
  19. pre = curr
  20. curr = next
  21. }
  22. // 一定返回的是pre因为构成了新链表
  23. return pre
  24. };

112 路径总和

  1. /**
  2. * Definition for a binary tree node.
  3. * function TreeNode(val, left, right) {
  4. * this.val = (val===undefined ? 0 : val)
  5. * this.left = (left===undefined ? null : left)
  6. * this.right = (right===undefined ? null : right)
  7. * }
  8. */
  9. /**
  10. * @param {TreeNode} root
  11. * @param {number} targetSum
  12. * @return {boolean}
  13. */
  14. // 回忆下dfs、bfs
  15. // bfs层次遍历:每个子节点放到数组里,处理每个节点后,pop。然后while结束
  16. // dfs递归找下个节点,先找终止条件。然后执行函数,再递归
  17. // 本题思路直接累减
  18. var hasPathSum = function(root, targetSum) {
  19. if (!root) {
  20. return false;
  21. }
  22. if (!root.left && !root.right && root.val === targetSum) {
  23. return true;
  24. }
  25. return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val)
  26. };

102. 二叉树的层序遍历

大思路理解,每层维护一个q,然后while循环,但是里面细节处理不太明白

  1. /**
  2. * Definition for a binary tree node.
  3. * function TreeNode(val, left, right) {
  4. * this.val = (val===undefined ? 0 : val)
  5. * this.left = (left===undefined ? null : left)
  6. * this.right = (right===undefined ? null : right)
  7. * }
  8. */
  9. /**
  10. * @param {TreeNode} root
  11. * @return {number[][]}
  12. */
  13. var levelOrder = function(root) {
  14. let q = [root]
  15. let res = []
  16. if (!root) return []
  17. while(q.length) {
  18. const currentSize = q.length
  19. res.push([])
  20. for(let j = 1; j <= currentSize; ++j) {
  21. let tmp = q.shift()
  22. res[res.length - 1].push(tmp.val)
  23. if (tmp.left) {
  24. q.push(tmp.left)
  25. }
  26. if (tmp.right) {
  27. q.push(tmp.right)
  28. }
  29. }
  30. }
  31. return res
  32. };

15. 三数之和
题解
https://leetcode-cn.com/problems/3sum/solution/by-veneno-o-xmwf/

  1. /**
  2. * @param {number[]} nums
  3. * @return {number[][]}
  4. */
  5. // 先sort升序,
  6. // 第一层循环,找剩余的target
  7. // 开始双指针
  8. // 如果重复就跳过
  9. var threeSum = function(nums) {
  10. let list = nums.sort((a, b) => a - b)
  11. let res = []
  12. // 为什么是-2?
  13. for(let i = 0; i < list.length - 2; i++) {
  14. // 重复检测
  15. if (list[i] === list[i - 1]) continue
  16. const target = -list[i]
  17. let j = i + 1
  18. let k = list.length - 1
  19. while(j < k) {
  20. // 重复检测
  21. // 这里两个重复不明白
  22. if (j > i + 1 && list[j] === list[j - 1]) {
  23. j++
  24. continue
  25. }
  26. if (list[k] === list[k + 1]) {
  27. k--
  28. continue
  29. }
  30. if (list[j] + list[k] === target) {
  31. res.push([list[i], list[j], list[k]])
  32. j++
  33. k--
  34. } else if (list[j] + list[k] < target) {
  35. j++
  36. } else {
  37. k--
  38. }
  39. }
  40. return res
  41. }
  42. };