算法學習記錄

題目來源leetcode,持續(xù)更新

學習文檔

動態(tài)規(guī)劃

動態(tài)規(guī)劃詳解

https://juejin.im/post/5e86d0ad6fb9a03c387f3342#heading-3

遞歸與動態(tài)規(guī)劃

https://leetcode-solution.cn/solutionDetail?url=https%3A%2F%2Fapi.github.com%2Frepos%2Fazl397985856%2Fleetcode%2Fcontents%2Fthinkings%2Fdynamic-programming.md

常用算法

遞歸和動態(tài)規(guī)劃

純粹的函數(shù)式編程中沒有循環(huán),只有遞歸亡鼠。

遞歸

遞歸三要素

  1. 一個問題的解可以分解為幾個子問題的解
  2. 子問題的求解思路除了規(guī)模之外铁坎,沒有任何區(qū)別
  3. 有遞歸終止條件

時間復雜度

……

動態(tài)規(guī)劃

如果說遞歸是從問題的結(jié)果倒推凿掂,直到問題的規(guī)囊挪ぃ縮小到尋常痴荐。 那么動態(tài)規(guī)劃就是從尋常入手列肢, 逐步擴大規(guī)模到最優(yōu)子結(jié)構(gòu)恰画。

動態(tài)規(guī)劃兩要素

  1. 狀態(tài)轉(zhuǎn)移方程
  2. 臨界條件

動態(tài)規(guī)劃本質(zhì)上是將大問題轉(zhuǎn)化為小問題,然后大問題的解是和小問題有關(guān)聯(lián)的瓷马,換句話說大問題可以由小問題進行計算得到拴还。

這一點是和遞歸一樣的, 但是動態(tài)規(guī)劃是一種類似查表的方法來縮短時間復雜度和空間復雜度欧聘。

解題記錄

前序遍歷

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */

var preorderTraversal = function(root) {
    if (!root) return []
    const stack = [root]
    const result = []
    while(stack.length) {
        const node = stack.pop()
        result.push(node.val) 
        if (node.right) {
            stack.push(node.right)
        }
        if (node.left) {
            stack.push(node.left)
        }
    }
    return result
};

中序遍歷

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var inorderTraversal = function(root) {
    let node = root
    const stack = []
    const result = []
    while(stack.length || node != null) {
        while(node) {
            stack.push(node)
            node = node.left
        }
        node = stack.pop()
        result.push(node.val)
        node = node.right
    }
    return result
};

后序遍歷

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */

// 兩個棧的寫法
var postorderTraversal = function(root) {
    if (!root) return []
    let node = root
    // 任務執(zhí)行棧
    const stack1 = [node];
    // 節(jié)點棧
    const stack2 = [];
    const result = [];
    while (stack1.length) {
        node = stack1.pop();
        stack2.push(node);
        if (node.left !== null) stack1.push(node.left); 
        if (node.right !== null) stack1.push(node.right); 
    }
    while (stack2.length) {
        node = stack2.pop();
        result.push(node.val)
    }
    return result
};

二叉樹最大深度

遞歸

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
// 遞歸
var maxDepth = function(root) {
    if (!root) return 0
    let result = 0
    function getDepth(node, tempDepth) {
        let temp = tempDepth + 1
        if (node.left == null && node.right == null) result = Math.max(temp, result)
        if (node.left !== null) getDepth(node.left, temp)
        if (node.right !== null) getDepth(node.right, temp)
    }
    getDepth(root, 0)
    return result
};

對稱二叉樹

// recursion
// var isSymmetric = function(root) {
//   if (!root) return true
//   function isEqual(left, right) {
//     if (!left || !right) {
//       return left == null && right == null
//     }
//     if (left.val == right.val) {
//       return isEqual(left.left, right.right) && isEqual(left.right, right.left) 
//     }
//     return false
//   }
//   return isEqual(root.left, root.right)
// }

// iteration
var isSymmetric = function(root) {
  if (!root) return true
  const queue = []
  queue.push(root)
  queue.push(root)
  while(queue.length) {
    let n1 = queue.shift()
    let n2 = queue.shift()
    if (n1 === null && n2 === null) continue
    if (n1 === null || n2 === null) return false
    if (n1.val !== n2.val) return false
    queue.push(n1.left)
    queue.push(n2.right)
    queue.push(n1.right)
    queue.push(n2.left)
  }
  return true
}

路徑總和

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} sum
 * @return {boolean}
 */

// recursion
var hasPathSum = function(root, sum) {
  if (!root) return false
  if (root.left === null && root.right === null) {
    return Boolean(sum - root.val == 0)
  }
  return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val)
};

// iteration
var hasPathSum = function(root, sum) {
  if (!root) return false
  cosnt stack = [root]
  while (stack.length) {
    if (root.left === null && root.right === null) {
      return Boolean(sum - root.val == 0)
    }
    if (root.right !== null) {
      stack.push(root.right, sum - root.val)
    }
    
    if (root.left !== null) {
      stack.push(root.left, sum - root.val)
    }
  }
  return false
}

中序與后序遍歷構(gòu)造二叉樹

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {number[]} inorder
 * @param {number[]} postorder
 * @return {TreeNode}
 */

// recursion
var buildTree = function(inorder, postorder) {
  const helper = (inorder) => {
    if(!inorder.length || !postorder.length) return null
    const value = postorder.pop()
    let index = inorder.indexOf(value)
    let node = new TreeNode(value)
    node.right = helper(inorder.slice(index + 1))
    if (index < 0) {
      node.left = null
    } else {
      node.left = helper(inorder.slice(0, index))
    }
    return node
  }
  return helper(inorder)
};

前序與中序遍歷構(gòu)造二叉樹

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {number[]} preorder
 * @param {number[]} inorder
 * @return {TreeNode}
 */
// recursion
var buildTree = function(preorder, inorder) {
  const helper = (inorder) => {
    if (!inorder.length || !preorder.length) return null
    const value = preorder.shift()
    const index = inorder.indexOf(value)
    if (index < 0) return null
    let node = new TreeNode(value)
    node.left = helper(inorder.slice(0, index))
    node.right = helper(inorder.slice(index + 1))
    return node
  }
  return helper(inorder)
};

填充每個節(jié)點的下一個右側(cè)節(jié)點指針

/**
 * // Definition for a Node.
 * function Node(val, left, right, next) {
 *    this.val = val === undefined ? null : val;
 *    this.left = left === undefined ? null : left;
 *    this.right = right === undefined ? null : right;
 *    this.next = next === undefined ? null : next;
 * };
 */

/**
 * @param {Node} root
 * @return {Node}
 */
// BFS O(N) O(N)
var connect = function(root) {
  if (!root) return root
  const queue = [root]
  while (queue.length) {
    let pre = null
    let size = queue.length
    for (let i = 0; i < size; i++) {
      let node = queue.shift()
      if (i > 0) pre.next = node
      if (i == size - 1) node.next = null
      pre = node
      if (node.left !== null) queue.push(node.left)
      if (node.right !== null) queue.push(node.right)
    }  
  }
  return root
};

// recursion 遞歸每個子樹寫next; 每個根節(jié)點的LR -> RL; 右側(cè)節(jié)點 -> null
// O(N) O(1)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末片林,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子怀骤,更是在濱河造成了極大的恐慌费封,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,599評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件晒喷,死亡現(xiàn)場離奇詭異孝偎,居然都是意外死亡,警方通過查閱死者的電腦和手機凉敲,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,629評論 3 385
  • 文/潘曉璐 我一進店門衣盾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來寺旺,“玉大人,你說我怎么就攤上這事势决∽杷埽” “怎么了?”我有些...
    開封第一講書人閱讀 158,084評論 0 348
  • 文/不壞的土叔 我叫張陵果复,是天一觀的道長陈莽。 經(jīng)常有香客問我,道長虽抄,這世上最難降的妖魔是什么走搁? 我笑而不...
    開封第一講書人閱讀 56,708評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮迈窟,結(jié)果婚禮上私植,老公的妹妹穿的比我還像新娘。我一直安慰自己车酣,他們只是感情好曲稼,可當我...
    茶點故事閱讀 65,813評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著湖员,像睡著了一般贫悄。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上娘摔,一...
    開封第一講書人閱讀 50,021評論 1 291
  • 那天窄坦,我揣著相機與錄音,去河邊找鬼晰筛。 笑死嫡丙,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的读第。 我是一名探鬼主播曙博,決...
    沈念sama閱讀 39,120評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼怜瞒!你這毒婦竟也來了父泳?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,866評論 0 268
  • 序言:老撾萬榮一對情侶失蹤吴汪,失蹤者是張志新(化名)和其女友劉穎惠窄,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體漾橙,經(jīng)...
    沈念sama閱讀 44,308評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡杆融,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,633評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了霜运。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片脾歇。...
    茶點故事閱讀 38,768評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡蒋腮,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出藕各,到底是詐尸還是另有隱情池摧,我是刑警寧澤,帶...
    沈念sama閱讀 34,461評論 4 333
  • 正文 年R本政府宣布激况,位于F島的核電站作彤,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏乌逐。R本人自食惡果不足惜竭讳,卻給世界環(huán)境...
    茶點故事閱讀 40,094評論 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望浙踢。 院中可真熱鬧代咸,春花似錦、人聲如沸成黄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,850評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽奋岁。三九已至,卻和暖如春荸百,著一層夾襖步出監(jiān)牢的瞬間闻伶,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,082評論 1 267
  • 我被黑心中介騙來泰國打工够话, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留蓝翰,地道東北人。 一個月前我還...
    沈念sama閱讀 46,571評論 2 362
  • 正文 我出身青樓女嘲,卻偏偏與公主長得像畜份,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子欣尼,可洞房花燭夜當晚...
    茶點故事閱讀 43,666評論 2 350