瀛楄妭璺冲姩鏈€甯歌€冪殑 64 閬揓S绠楁硶棰?/a>

Posted fengmao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了瀛楄妭璺冲姩鏈€甯歌€冪殑 64 閬揓S绠楁硶棰?/a>相关的知识,希望对你有一定的参考价值。

缂樿捣

鐜板湪澶у巶闈㈣瘯涓紝绠楁硶棰樺嚑涔庝负蹇呰€冮」锛屼笖杩戝嚑骞撮鐜?LeetCode 鐪熼锛屾绡囦负鎷垮埌瀛楄妭銆佽吘璁€佷含涓?Offer 鐨勭瑪鑰呮湰浜哄湪鍑嗗闈㈣瘯杩囩▼涓翰鑷埛杩囦互鍙婇亣鍒拌繃楂橀绠楁硶棰樸€傛枃绔犲唴瀹逛細鍒嗘ā鍧楁暣鐞嗭紝瀵逛簬绗旇€呭湪闈㈣瘯杩囩▼涓亣鍒扮殑鐪熼锛屼細缁欎簣鐫€閲?銆愷煍ャ€戞爣鍑恒€?/p>

鍚屾椂锛屽彲浠ユ涓嶅姘旂殑璇达紝濡傛灉浣犲噯澶囨椂闂存湁闄愶紝鍙堟兂杩芥眰绠楁硶棰樺噯澶囨晥鐜囨渶澶у寲锛岄偅涔堜綘鍙渶瑕佹寜鐓уぇ绾叉妸涓嬮潰鐨勯鐩埛瀹岋紝骞舵妸浠g爜鐑傜啛浜庡績锛屽氨鍑犱箮鍙互搴斿 90% 鐨勯潰璇曠畻娉曡€冮浜嗐€?/p>

鏁寸悊杩欑瘒鍐呭鐨勭洰鐨勪竴涓槸绗旇€呭湪涔嬪墠鍑嗗闈㈣瘯鏃剁殑涓€鐐圭Н绱紝鑰屽畠纭疄涔熷府鍔╃瑪鑰呭湪闈㈣瘯绠楁硶棰樹腑杩囧叧鏂╁皢锛屽悓鏃跺憿锛屼篃甯屾湜鑳藉鍦ㄩ噾涓夐摱鍥涚粰浜堟嫾鎼忕殑浣狅紝涓€鐐圭偣甯姪灏卞ソ锛侌煉?/p>

鏂囨湯鏈夌鍒?锛氾級馃槇

鏈瘒鍐呭鍖呮嫭濡備笅妯″潡锛?/p>

  • 楂橀绠楁硶棰樼郴鍒楋細閾捐〃
  • 銆愷煍ャ€戙€愭湁鐪熼銆戦珮棰戠畻娉曢绯诲垪锛氬瓧绗︿覆
  • 銆愷煍ャ€戙€愭湁鐪熼銆戦珮棰戠畻娉曢绯诲垪锛氭暟缁勯棶棰?/li>
  • 楂橀绠楁硶棰樼郴鍒楋細浜屽弶鏍?/li>
  • 銆愷煍ャ€戦珮棰戠畻娉曢绯诲垪锛氭帓搴忕畻娉?/li>
  • 銆愷煍ャ€戦珮棰戠畻娉曢绯诲垪锛氫簩鍒嗘煡鎵?/li>
  • 銆愷煍ャ€戦珮棰戠畻娉曢绯诲垪锛氬姩鎬佽鍒?/li>
  • 楂橀绠楁硶棰樼郴鍒楋細BFS
  • 銆愷煍ャ€戦珮棰戠畻娉曢绯诲垪锛氭爤
  • 銆愷煍ャ€戦珮棰戠畻娉曢绯诲垪锛欴FS
  • 銆愷煍ャ€戦珮棰戠畻娉曢绯诲垪锛氬洖婧畻娉?/li>

鍏朵腑鏍囸煍ョ殑閮ㄥ垎浠h〃闈炲父楂橀鐨勮€冮锛屽叾涓笉涔忕瑪鑰呴亣鍒扮殑鍘熼銆傚叾涓浜庢瘡涓€绫伙紝棣栧厛浼氬垪鍑哄寘鍚殑鑰冮锛岀劧鍚庨拡瀵规瘡涓€閬撹€冮浼氱粰鍑洪毦搴︺€佽€冨療鐭ヨ瘑鐐广€佹槸鍚︽槸闈㈣瘯鐪熼锛屽湪姣忛亾棰樿缁嗕粙缁嶆椂锛岃繕浼氱粰鍑烘瘡閬撻鐨?LeetCode 閾炬帴锛屽府鍔╄鑰呯悊瑙i鎰忥紝浠ュ強鑳藉杩涜瀹為檯鐨勬祴楠岋紝杩樺彲浠ヨ鐪嬪叾浠栦汉鐨勭瓟妗堬紝鏇村ソ鐨勫府鍔╁噯澶囥€?/p>

楂橀绠楁硶棰樼郴鍒楋細閾捐〃

绗旇€呴亣鍒扮殑楂橀閾捐〃棰樹富瑕佸寘鍚繖鍑犻亾锛?/p>

  • 閫氳繃閾捐〃鐨勫悗缁亶鍘嗗垽鏂洖鏂囬摼琛ㄩ棶棰?銆愮畝鍗曘€?/li>
  • 閾捐〃鐨勫弽鍚戣緭鍑?銆愮畝鍗曘€?/li>
  • 鍚堝苟 K 涓崌搴忛摼琛?銆愬洶闅俱€?/li>
  • K涓竴缁勭炕杞摼琛?銆愬洶闅俱€?/li>
  • 鐜舰閾捐〃 銆愮畝鍗曘€?/li>
  • 鎺掑簭閾捐〃 銆愪腑绛夈€?/li>
  • 鐩镐氦閾捐〃 銆愮畝鍗曘€?/li>

鍓嶅簭閬嶅巻鍒ゆ柇鍥炴枃閾捐〃

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細234 鍥炴枃閾捐〃锛堢畝鍗曪級

棰樿В1

鍒╃敤閾捐〃鐨勫悗缁亶鍘嗭紝浣跨敤鍑芥暟璋冪敤鏍堜綔涓哄悗搴忛亶鍘嗘爤锛屾潵鍒ゆ柇鏄惁鍥炴枃

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
  *
  */
var isPalindrome = function(head) {
    let left = head;
    function traverse(right) {
        if (right == null) return true;
        let res = traverse(right.next);
        res = res && (right.val === left.val);
        left = left.next;
        return res;
    }
    return traverse(head);
};

澶嶅埗浠g爜

棰樿В2

閫氳繃 蹇€佹參鎸囬拡鎵鹃摼琛ㄤ腑鐐癸紝鐒跺悗鍙嶈浆閾捐〃锛屾瘮杈冧袱涓摼琛ㄤ袱渚ф槸鍚︾浉绛夛紝鏉ュ垽鏂槸鍚︽槸鍥炴枃閾捐〃

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
  *
  */
var isPalindrome = function(head) {
    // 鍙嶈浆 slower 閾捐〃
    let right = reverse(findCenter(head));
    let left = head;
    // 寮€濮嬫瘮杈?    while (right != null) {
        if (left.val !== right.val) {
            return false;
        }
        left = left.next;
        right = right.next;
    }
    return true;
}
function findCenter(head) {
    let slower = head, faster = head;
    while (faster && faster.next != null) {
        slower = slower.next;
        faster = faster.next.next;
    }
    // 濡傛灉 faster 涓嶇瓑浜?null锛岃鏄庢槸濂囨暟涓紝slower 鍐嶇Щ鍔ㄤ竴鏍?    if (faster != null) {
        slower = slower.next;
    }
    return slower;
}
function reverse(head) {
    let prev = null, cur = head, nxt = head;
    while (cur != null) {
        nxt = cur.next;
        cur.next = prev;
        prev = cur;
        cur = nxt;
    }
    return prev;
}

澶嶅埗浠g爜

鍙嶈浆閾捐〃

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細206 鍙嶈浆閾捐〃锛堢畝鍗曪級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function(head) {
    if (head == null || head.next == null) return head;
    let last = reverseList(head.next);
    head.next.next = head;
    head.next = null;
    return last;
};

澶嶅埗浠g爜

鍚堝苟K涓崌搴忛摼琛?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細23 鍚堝苟K涓崌搴忛摼琛紙鍥伴毦锛?/a>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode[]} lists
 * @return {ListNode}
 */
var mergeKLists = function(lists) {
    if (lists.length === 0) return null;
    return mergeArr(lists);
};
function mergeArr(lists) {
    if (lists.length <= 1) return lists[0];
    let index = Math.floor(lists.length / 2);
    const left = mergeArr(lists.slice(0, index))
    const right = mergeArr(lists.slice(index));
    return merge(left, right);
}
function merge(l1, l2) {
    if (l1 == null && l2 == null) return null;
    if (l1 != null && l2 == null) return l1;
    if (l1 == null && l2 != null) return l2;
    let newHead = null, head = null;
    while (l1 != null && l2 != null) {
        if (l1.val < l2.val) {
            if (!head) {
                newHead = l1;
                head = l1;
            } else {
                newHead.next = l1;
                newHead = newHead.next;
            }
            l1 = l1.next;
        } else {
            if (!head) {
                newHead = l2;
                head = l2;
            } else {
                newHead.next = l2;
                newHead = newHead.next;
            }
            l2 = l2.next;
        }
    }
    newHead.next = l1 ? l1 : l2;
    return head;
}

澶嶅埗浠g爜

K 涓竴缁勭炕杞摼琛?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細25 K 涓竴缁勭炕杞摼琛紙鍥伴毦锛?/a>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} k
 * @return {ListNode}
 */
var reverseKGroup = function(head, k) {
    let a = head, b = head;
    for (let i = 0; i < k; i++) {
        if (b == null) return head;
        b = b.next;
    }
    const newHead = reverse(a, b);
    a.next = reverseKGroup(b, k);
    return newHead;
};
function reverse(a, b) {
    let prev = null, cur = a, nxt = a;
    while (cur != b) {
        nxt = cur.next;
        cur.next = prev;
        prev = cur;
        cur = nxt;
    }
    return prev;
}

澶嶅埗浠g爜

鐜舰閾捐〃

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細141 鐜舰閾捐〃锛堢畝鍗曪級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {boolean}
 */
var hasCycle = function(head) {
    if (head == null || head.next == null) return false;
    let slower = head, faster = head;
    while (faster != null && faster.next != null) {
        slower = slower.next;
        faster = faster.next.next;
        if (slower === faster) return true;
    }
    return false;
};

澶嶅埗浠g爜

鎺掑簭閾捐〃

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細148 鎺掑簭閾捐〃锛堜腑绛夛級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var sortList = function(head) {
    if (head == null) return null;
    let newHead = head;
    return mergeSort(head);
};
function mergeSort(head) {
    if (head.next != null) {
        let slower = getCenter(head);
        let nxt = slower.next;
        slower.next = null;
        console.log(head, slower, nxt);
        const left = mergeSort(head);
        const right = mergeSort(nxt);
        head = merge(left, right);
    }
    return head;
}
function merge(left, right) {
    let newHead = null, head = null;
    while (left != null && right != null) {
        if (left.val < right.val) {
            if (!head) {
                newHead = left;
                head = left;
            } else {
                newHead.next = left;
                newHead = newHead.next;
            }
            left = left.next;
        } else {
            if (!head) {
                newHead = right;
                head = right;
            } else {
                newHead.next = right;
                newHead = newHead.next;
            }
            right = right.next;
        }
    }
    newHead.next = left ? left : right;
    return head;
}
function getCenter(head) {
    let slower = head, faster = head.next;
    while (faster != null && faster.next != null) {
        slower = slower.next;
        faster = faster.next.next;
    }
    return slower;
}

澶嶅埗浠g爜

鐩镐氦閾捐〃

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細160 鐩镐氦閾捐〃锛堢畝鍗曪級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * 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) {
    let lastHeadA = null;
    let lastHeadB = null;
    let originHeadA = headA;
    let originHeadB = headB;
    if (!headA || !headB) {
        return null;
    }
    while (true) {
        if (headB == headA) {
            return headB;
        }
        if (headA && headA.next == null) {
            lastHeadA = headA;
            headA = originHeadB;
        } else {
            headA = headA.next;
        }
        if (headB && headB.next == null) {
            lastHeadB = headB
            headB = originHeadA;
        } else {
            headB = headB.next;
        }
        if (lastHeadA && lastHeadB && lastHeadA != lastHeadB) {
            return null;
        }
    }
    return null;
};

澶嶅埗浠g爜

銆愷煍ャ€戦珮棰戠畻娉曢绯诲垪锛氬瓧绗︿覆

涓昏鏈変互涓嬪嚑绫婚珮棰戣€冮锛?/p>

  • 鏈€闀垮洖鏂囧瓙涓?銆愪腑绛夈€戙€愬弻鎸囬拡銆戙€愰潰璇曠湡棰樸€?/li>
  • 鏈€闀垮叕鍏卞墠缂€ 銆愮畝鍗曘€戙€愬弻鎸囬拡銆?/li>
  • 鏃犻噸澶嶅瓧绗︾殑鏈€闀垮瓙涓层€愪腑绛夈€戙€愬弻鎸囬拡銆?/li>
  • 鏈€灏忚鐩栧瓙涓?銆愬洶闅俱€戙€愭粦鍔ㄧ獥鍙c€戙€愰潰璇曠湡棰樸€?/li>

銆愰潰璇曠湡棰樸€戞渶闀垮洖鏂囧瓙涓层€愬弻鎸囬拡銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細5 鏈€闀垮洖鏂囧瓙涓诧紙涓瓑锛?/a>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {string} s
 * @return {string}
 */
var longestPalindrome = function(s) {
    if (s.length === 1) return s;
    let maxRes = 0, maxStr = \'\';
    for (let i = 0; i < s.length; i++) {
        let str1 = palindrome(s, i, i);
        let str2 = palindrome(s, i, i + 1);   
        if (str1.length > maxRes) {
            maxStr = str1;
            maxRes = str1.length;
        }
        if (str2.length > maxRes) {
            maxStr = str2;
            maxRes = str2.length;
        }
    }
    return maxStr;
};
function palindrome(s, l, r) {
    while (l >= 0 && r < s.length && s[l] === s[r]) {
        l--;
        r++;
    }
    return s.slice(l + 1, r);
}

澶嶅埗浠g爜

鏈€闀垮叕鍏卞墠缂€銆愬弻鎸囬拡銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細14 鏈€闀垮叕鍏卞墠缂€锛堢畝鍗曪級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {string[]} strs
 * @return {string}
 */
var longestCommonPrefix = function(strs) {
    if (strs.length === 0) return "";
    let first = strs[0];
    if (first === "") return "";
    let minLen = Number.MAX_SAFE_INTEGER;
    for (let i = 1; i < strs.length; i++) {
        const len = twoStrLongestCommonPrefix(first, strs[i]);
        minLen = Math.min(len, minLen);
    }
    return first.slice(0, minLen);
};
function twoStrLongestCommonPrefix (s, t) {
    let i = 0, j = 0;
    let cnt = 0;
    while (i < s.length && j < t.length) {
        console.log(s[i], t[j], cnt)
        if (s[i] === t[j])  {
            cnt++;
        } else {
            return cnt;
        }
        i++;
        j++;
    }
    return cnt;
}

澶嶅埗浠g爜

鏃犻噸澶嶅瓧绗︾殑鏈€闀垮瓙涓层€愬弻鎸囬拡銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細3 鏃犻噸澶嶅瓧绗︾殑鏈€闀垮瓙涓诧紙涓瓑锛?/a>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function(s) {
  let window = {};
  let left = 0, right = 0;
  let maxLen = 0, maxStr = \'\';
  while (right < s.length) {
    let c = s[right];
    right++;
    if (window[c]) window[c]++;
    else window[c] = 1
    while (window[c] > 1) {
      let d = s[left];
      left++;
      window[d]--;
    }
    if (maxLen < right - left) {
      maxLen = right - left;
    }
  }
  return maxLen;
};

澶嶅埗浠g爜

銆愰潰璇曠湡棰樸€?鏈€灏忚鐩栧瓙涓层€愭粦鍔ㄧ獥鍙c€?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細76 鏈€灏忚鐩栧瓙涓诧紙鍥伴毦锛?/a>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {string} s
 * @param {string} t
 * @return {string}
 */
var minWindow = function(s, t) {
    let need = {}, window = {};
    for (let c of t) {
        if (!need[c]) need[c] = 1;
        else need[c]++;
    }
    let left = 0, right = 0;
    let valid = 0, len = Object.keys(need).length;
    let minLen = s.length + 1, minStr = \'\';
    while (right < s.length) {
        const d = s[right];
        right++;
        if (!window[d]) window[d] = 1;
        else window[d]++;
        if (need[d] && need[d] === window[d]) {
            valid++;
        }
        console.log(\'left - right\', left, right);
        while (valid === len) {
            if (right - left < minLen) {
                minLen = right - left;
                minStr = s.slice(left, right);
            }
            console.lo
            let c = s[left];
            left++;
            window[c]--;
            if (need[c] && window[c] < need[c]) {
                valid--;
            }
        }
    }
    return minStr;
};

澶嶅埗浠g爜

銆愷煍ャ€戦珮棰戠畻娉曢绯诲垪锛氭暟缁勯棶棰?/h2>

涓昏鏈夊嚑绫婚珮棰戣€冮锛?/p>

  • 淇勭綏鏂濞冧俊灏侀棶棰樸€愬洶闅俱€戙€愭帓搴?鏈€闀夸笂鍗囧瓙搴忓垪銆戙€愰潰璇曠湡棰樸€?/li>
  • 鏈€闀胯繛缁€掑搴忓垪 銆愮畝鍗曘€戙€愬弻鎸囬拡銆?/li>
  • 鏈€闀胯繛缁簭鍒椼€愬洶闅俱€戙€愬搱甯岃〃銆?/li>
  • 鐩涙渶澶氭按鐨勫鍣ㄣ€愬洶闅俱€戙€愰潰璇曠湡棰樸€?/li>
  • 瀵绘壘涓や釜姝e簭鏁扮粍鐨勪腑浣嶆暟銆愬洶闅俱€戙€愬弻鎸囬拡銆?/li>
  • 鍒犻櫎鏈夊簭鏁扮粍涓殑閲嶅椤广€愮畝鍗曘€戙€愬揩鎱㈡寚閽堛€?/li>
  • 鍜屼负K鐨勫瓙鏁扮粍銆愪腑绛夈€戙€愬搱甯岃〃銆?/li>
  • nSum 闂銆愮郴鍒椼€戙€愮畝鍗曘€戙€愬搱甯岃〃銆?/li>
  • 鎺ラ洦姘淬€愬洶闅俱€戙€愭毚鍔?澶囧繕褰曚紭鍖栥€戙€愰潰璇曠湡棰樸€?/li>
  • 璺宠穬娓告垙銆愮郴鍒椼€戙€愪腑绛夈€戙€愯椽蹇冪畻娉曘€?/li>

銆愰潰璇曠湡棰樸€戜縿缃楁柉濂楀▋淇″皝闂銆愭帓搴?鏈€闀夸笂鍗囧瓙搴忓垪銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細354 淇勭綏鏂濞冧俊灏侀棶棰橈紙鍥伴毦锛?/a>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[][]} envelopes
 * @return {number}
 */
var maxEnvelopes = function(envelopes) {
  if (envelopes.length === 1) return 1;
    envelopes.sort((a, b) => {
        if (a[0] !== b[0]) return a[0] - b[0];
        else return b[1] - a[1];
    });
    let LISArr = [];
    for (let [key, value] of envelopes) {
      LISArr.push(value);
    }
    console.log( LISArr);
    return LIS(LISArr);
};
function LIS(arr) {
  let dp = [];
  let maxAns = 0;
  for (let i = 0; i < arr.length; i++) {
    dp[i] = 1;
  }
  for (let i = 1; i < arr.length; i++) {
    for (let j = i; j >= 0; j--) {
      if (arr[i] > arr[j]) {
        dp[i] = Math.max(dp[i], dp[j] + 1)
      }
      maxAns = Math.max(maxAns, dp[i]);
    }
  }
  return maxAns;
}

澶嶅埗浠g爜

鏈€闀胯繛缁€掑搴忓垪銆愬揩鎱㈡寚閽堛€?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細674 鏈€闀胯繛缁€掑搴忓垪锛堢畝鍗曪級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[]} nums
 * @return {number}
 */
var findLengthOfLCIS = function(nums) {
    if (nums.length === 0) return 0;
    const n = nums.length;
    let left = 0, right = 1;
    let globalMaxLen = 1, maxLen = 1;
    while (right < n) {
        if (nums[right] > nums[left]) maxLen++;
        else {
            maxLen = 1;
        }
        left++;
        right++;
        globalMaxLen = Math.max(globalMaxLen, maxLen);
    }
    return globalMaxLen;
};

澶嶅埗浠g爜

鏈€闀胯繛缁簭鍒?銆愬搱甯岃〃銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細128 鏈€闀胯繛缁簭鍒楋紙鍥伴毦锛?/a>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[]} nums
 * @return {number}
 */
var longestConsecutive = function(nums) {
    if (nums.length === 0) return 0;
    const set = new Set(nums);
    const n = nums.length;
    let globalLongest = 1;
    for (let i = 0; i < n; i++) {
        if (!set.has(nums[i] - 1)) {
            let longest = 1;
            let currentNum = nums[i];
            while (set.has(currentNum + 1)) {
                currentNum += 1;
                longest++;
            }
            globalLongest = Math.max(globalLongest, longest);
        }
    }
    return globalLongest;
};

澶嶅埗浠g爜

銆愰潰璇曠湡棰樸€戠洓鏈€澶氭按鐨勫鍣ㄣ€愬搱甯岃〃銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細11 鐩涙渶澶氭按鐨勫鍣紙涓瓑锛?/a>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[]} height
 * @return {number}
 */
var maxArea = function(height) {
    let n = height.length;
    let left = 0, right = n - 1;
    let maxOpacity = 0;
    while (left < right) {
        let res = Math.min(height[left], height[right]) * (right - left);
        maxOpacity = Math.max(maxOpacity, res);
        if (height[left] < height[right]) left++
        else right--;
    }
    return maxOpacity;
};

澶嶅埗浠g爜

瀵绘壘涓や釜姝e簭鏁扮粍鐨勪腑浣嶆暟銆愬弻鎸囬拡銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細4 瀵绘壘涓や釜姝e簭鏁扮粍鐨勪腑浣嶆暟锛堝洶闅撅級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number}
 */
var findMedianSortedArrays = function(nums1, nums2) {
    let m = nums1.length, n = nums2.length;
    let i = 0, j = 0;
    let newArr = [];
    while (i < m && j < n) {
        if (nums1[i] < nums2[j]) {
            newArr.push(nums1[i++]);
        } else {
            newArr.push(nums2[j++]);
        }
    }
    newArr = newArr.concat(i < m ? nums1.slice(i) : nums2.slice(j));
    const len = newArr.length;
    console.log(newArr)
    if (len % 2 === 0) {
        return (newArr[len / 2] + newArr[len / 2 - 1]) / 2;
    } else {
        return newArr[Math.floor(len / 2)];
    }
};

澶嶅埗浠g爜

鍒犻櫎鏈夊簭鏁扮粍涓殑閲嶅椤广€愬揩鎱㈡寚閽堛€?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細26 鍒犻櫎鏈夊簭鏁扮粍涓殑閲嶅椤癸紙绠€鍗曪級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[]} nums
 * @return {number}
 */
var removeDuplicates = function(nums) {
  if (nums.length <= 1) return nums.length;
  let lo = 0, hi = 0;
  while (hi < nums.length) {
    while (nums[lo] === nums[hi] && hi < nums.length) hi++;
    if (nums[lo] !== nums[hi] && hi < nums.length) {
      lo++;
      nums[lo] = nums[hi];
    }
    hi++;
  }
  return lo + 1;
};

澶嶅埗浠g爜

鍜屼负K鐨勫瓙鏁扮粍銆愬搱甯岃〃銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細560 鍜屼负K鐨勫瓙鏁扮粍锛堜腑绛夛級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var subarraySum = function(nums, k) {
    let cnt = 0;
    let sum0_i = 0, sum0_j = 0;
    let map = new Map();
    map.set(0, 1);
    for (let i = 0; i <= nums.length; i++) {
        sum0_i += nums[i];
        sum0_j = sum0_i - k;
        console.log(\'map\', sum0_j, map.get(sum0_j))
        if (map.has(sum0_j)) {
            cnt += map.get(sum0_j);
        }
        let sumCnt = map.get(sum0_i) || 0;
        map.set(sum0_i, sumCnt + 1);
    }
    return cnt;
};

澶嶅埗浠g爜

nSum闂銆愬搱甯岃〃銆戙€愮郴鍒椼€?/h3>
  • 馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細1 涓ゆ暟涔嬪拰锛堢畝鍗曪級
  • 馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細167 涓ゆ暟涔嬪拰 II - 杈撳叆鏈夊簭鏁扮粍锛堢畝鍗曪級
  • 馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細15 涓夋暟涔嬪拰锛堜腑绛夛級
  • 馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細18 鍥涙暟涔嬪拰锛堜腑绛夛級

鍙楅檺浜庣瘒骞咃紝杩欓噷鍙粰鍑虹涓€閬撻鐨勪唬鐮佹ā鏉匡紝涔熸槸涓€闈㈠父鑰冪湡棰樸€?/p>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function(nums, target) {
  let map2 = new Map();
  for (let i = 0; i < nums.length; i++) {
    map2.set(nums[i], i);
  }
  for (let i = 0; i < nums.length; i++) {
    if (map2.has(target - nums[i]) && map2.get(target - nums[i]) !== i) return [i, map2.get(target - nums[i])]
  }
};

澶嶅埗浠g爜

銆愰潰璇曠湡棰樸€戞帴闆ㄦ按銆愭毚鍔?澶囧繕褰曚紭鍖栥€?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細42 鎺ラ洦姘达紙鍥伴毦锛?/a>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[]} height
 * @return {number}
 */
var trap = function(height) {
    let l_max = [], r_max = [];
    let len = height.length;
    let maxCapacity = 0;
    for (let i = 0; i < len; i++) {
        l_max[i] = height[i];
        r_max[i] = height[i];
    }
    for (let i = 1; i < len; i++) {
        l_max[i] = Math.max(l_max[i - 1], height[i]);
    }
    for (let j = len - 2; j >= 0; j--) {
        r_max[j] = Math.max(r_max[j + 1], height[j]);
    }
    for (let i = 0; i < len; i++) {
        maxCapacity += Math.min(l_max[i], r_max[i]) - height[i];
    }
    return maxCapacity;
};

澶嶅埗浠g爜

璺宠穬娓告垙銆愯椽蹇冪畻娉曘€戙€愮郴鍒椼€?/h3>
  • 馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細55 璺宠穬娓告垙锛堜腑绛夛級
  • 馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細45 璺宠穬娓告垙 II锛堜腑绛夛級

鍙楅檺浜庣瘒骞咃紝杩欓噷鍙粰鍑虹涓€閬撻鐨勪唬鐮佹ā鏉匡紝涔熸槸涓€闈㈠父鑰冪湡棰樸€?/p>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[]} nums
 * @return {boolean}
 */
var canJump = function(nums) {
    let faster = 0;
    for (let i = 0; i < nums.length - 1; i++) {
        faster = Math.max(faster, i + nums[i]);
        if (faster <= i) return false;
    }
    return faster >= nums.length - 1;
};

澶嶅埗浠g爜

楂橀绠楁硶棰樼郴鍒楋細浜屽弶鏍?/h2>

涓昏鏈変互涓嬪嚑绫婚珮棰戣€冮锛?/p>

  • 浜屽弶鏍戠殑鏈€杩戝叕鍏辩鍏堛€愮畝鍗曘€戙€愪簩鍙夋爲銆?/li>
  • 浜屽弶鎼滅储鏍戜腑鐨勬悳绱€愮畝鍗曘€戙€愪簩鍙夋爲銆?/li>
  • 鍒犻櫎浜屽弶鎼滅储鏍戜腑鐨勮妭鐐广€愪腑绛夈€戙€愪簩鍙夋爲銆?/li>
  • 瀹屽叏浜屽弶鏍戠殑鑺傜偣涓暟銆愪腑绛夈€戙€愪簩鍙夋爲銆?/li>
  • 浜屽弶鏍戠殑閿娇褰㈠眰搴忛亶鍘嗐€愪腑绛夈€戙€愪簩鍙夋爲銆?/li>

浜屽弶鏍戠殑鏈€杩戝叕鍏辩鍏堛€愪簩鍙夋爲銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細236 浜屽弶鏍戠殑鏈€杩戝叕鍏辩鍏堬紙绠€鍗曪級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {TreeNode}
 */
let visited;let parent;
var lowestCommonAncestor = function(root, p, q) {
    visited = new Set();
    parent = new Map();
    dfs(root);
    while (p != null) {
        visited.add(p.val);
        p = parent.get(p.val);
    }
    while (q != null) {
        if (visited.has(q.val)) {
            return q;
        }
        q = parent.get(q.val);
    }
    return null;
};
function dfs(root) {
    if (root.left != null) {
        parent.set(root.left.val, root);
        dfs(root.left);
    }
    if (root.right != null) {
        parent.set(root.right.val, root);
        dfs(root.right);
    }
}

澶嶅埗浠g爜

浜屽弶鎼滅储鏍戜腑鐨勬悳绱€愪簩鍙夋爲銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細700 浜屽弶鎼滅储鏍戜腑鐨勬悳绱紙绠€鍗曪級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} val
 * @return {TreeNode}
 */
var searchBST = function(root, val) {
    if (root == null) return null;
    if (root.val === val) return root;
    if (root.val > val) {
        return searchBST(root.left, val);
    } else if (root.val < val) {
        return searchBST(root.right, val);
    }
};

澶嶅埗浠g爜

鍒犻櫎浜屽弶鎼滅储鏍戜腑鐨勮妭鐐广€愪簩鍙夋爲銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細450 鍒犻櫎浜屽弶鎼滅储鏍戜腑鐨勮妭鐐癸紙涓瓑锛?/a>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} key
 * @return {TreeNode}
 */
var deleteNode = function(root, key) {
    if (root == null) return null;
    if (root.val === key) {
        if (root.left == null && root.right == null) return null;
        if (root.left == null) return root.right;
        if (root.right == null) return root.left;
        if (root.left != null && root.right != null)  {
            let target = getMinTreeMaxNode(root.left);
            root.val = target.val;
            root.left = deleteNode(root.left, target.val);
        }
    }
    if (root.val < key) {
        root.right = deleteNode(root.right, key);
    } else if (root.val > key) {
        root.left = deleteNode(root.left, key);
    }
    return root;
};
function getMinTreeMaxNode(root) {
    if (root.right == null) return root;
    return getMinTreeMaxNode(root.right);
}

澶嶅埗浠g爜

瀹屽叏浜屽弶鏍戠殑鑺傜偣涓暟銆愪簩鍙夋爲銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細222 瀹屽叏浜屽弶鏍戠殑鑺傜偣涓暟锛堜腑绛夛級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var countNodes = function(root) {
    if (root == null) return 0;
    let l = root, r = root;
    let lh = 0, rh = 0;
    while (l != null) {
      l = l.left;
      lh++;
    }
    while (r != null) {
      r = r.right;
      rh++;
    }
    if (lh === rh) {
      return Math.pow(2, lh) - 1;
    }
    return 1 + countNodes(root.left) + countNodes(root.right);
};

澶嶅埗浠g爜

浜屽弶鏍戠殑閿娇褰㈠眰搴忛亶鍘嗐€愪簩鍙夋爲銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細103 浜屽弶鏍戠殑閿娇褰㈠眰搴忛亶鍘嗭紙涓瓑锛?/a>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
let res;
var zigzagLevelOrder = function(root) {
    if (root == null) return [];
    res = [];
    BFS(root, true);
    return res;
};
function BFS(root, inOrder) {
    let arr = [];
    let resItem = [];
    let node;
    let stack1 = new Stack();
    let stack2 = new Stack();
    // 鍒ゆ柇浜ゆ崲鏃舵満
    let flag;
    stack1.push(root);
    res.push([root.val]);
    inOrder = !inOrder;
    while (!stack1.isEmpty() || !stack2.isEmpty()) {
        if (stack1.isEmpty()) {
            flag = \'stack1\';
        } else if (stack2.isEmpty()) {
            flag = \'stack2\';
        }
        // 鍐冲畾鍙栭偅涓爤閲岄潰鐨勫厓绱?        if (flag === \'stack2\' && !stack1.isEmpty()) node = stack1.pop();
        else if (flag === \'stack1\' && !stack2.isEmpty()) node = stack2.pop();
        if (inOrder) {
            if (node.left) {
                if (flag === \'stack1\') {
                    stack1.push(node.left);
                } else {
                    stack2.push(node.left);
                }
                resItem.push(node.left.val);
            }
            if (node.right) {
                if (flag === \'stack1\') {
                    stack1.push(node.right);
                } else {
                    stack2.push(node.right);
                }
                resItem.push(node.right.val);
            }
        } else {
            if (node.right) {
                if (flag === \'stack1\') {
                    stack1.push(node.right);
                } else {
                    stack2.push(node.right);
                }
                resItem.push(node.right.val);
            }
            if (node.left) {
                if (flag === \'stack1\') {
                    stack1.push(node.left);
                } else {
                    stack2.push(node.left);
                }
                resItem.push(node.left.val);
            }
        }
        // 鍒ゆ柇涓嬫缈昏浆鐨勬椂鏈?        if ((flag === \'stack2\' && stack1.isEmpty()) || (flag === \'stack1\' && stack2.isEmpty())) {
            inOrder = !inOrder;
            // 闇€瑕佺炕杞簡锛屽氨鍔犱竴杞€?            if (resItem.length > 0) {
                res.push(resItem);
            }   
            resItem = [];
        }
    }
}
class Stack {
    constructor() {
        this.count = 0;
        this.items = [];
    }
    push(element) {
        this.items[this.count] = element;
        this.count++;
    }
    pop() {
        if (this.isEmpty()) return undefined;
        const element = this.items[this.count - 1];
        delete this.items[this.count - 1];
        this.count--;
        return element;
    }
    size() {
        return this.count;
    }
    isEmpty() {
        return this.size() === 0;
    }
}

澶嶅埗浠g爜

銆愷煍ャ€戦珮棰戠畻娉曢绯诲垪锛氭帓搴忕畻娉?/h2>

涓昏鏈変互涓嬪嚑绫婚珮棰戣€冮锛?/p>

  • 鐢ㄦ渶灏戞暟閲忕殑绠紩鐖嗘皵鐞冦€愪腑绛夈€戙€愭帓搴忋€?/li>
  • 鍚堝苟鍖洪棿銆愪腑绛夈€戙€愭帓搴忕畻娉?鍖洪棿闂銆戙€愰潰璇曠湡棰樸€?/li>

鐢ㄦ渶灏戞暟閲忕殑绠紩鐖嗘皵鐞冦€愭帓搴忕畻娉曘€?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細452 鐢ㄦ渶灏戞暟閲忕殑绠紩鐖嗘皵鐞冿紙涓瓑锛?/a>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[][]} points
 * @return {number}
 */
var findMinArrowShots = function(points) {
    if (points.length === 0) return 0;
    points.sort((a, b) => a[1] - b[1]);
    let cnt = 1;
    let resArr = [points[0]];
    let curr, last;
    for (let i = 1; i < points.length; i++) {
        curr = points[i];
        last = resArr[resArr.length - 1];
        if (curr[0] > last[1]) {
            resArr.push(curr);
            cnt++;
        }
    }
    return cnt;
};

澶嶅埗浠g爜

鍚堝苟鍖洪棿銆愭帓搴忕畻娉?鍖洪棿闂銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細56 鍚堝苟鍖洪棿锛堜腑绛夛級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[][]} intervals
 * @return {number[][]}
 */
var merge = function(intervals) {
    if (intervals.length === 0) return [];
    intervals.sort((a, b) => a[0] - b[0]);
    let mergeArr = [intervals[0]];
    let last, curr;
    for (let j = 1; j < intervals.length; j++) {
        last = mergeArr[mergeArr.length - 1];
        curr = intervals[j];
        if (last[1] >= curr[0]) {
            last[1] = Math.max(curr[1], last[1]);
        } else {
            mergeArr.push(curr);
        }
    }
    return mergeArr;
};

澶嶅埗浠g爜

楂橀绠楁硶棰樼郴鍒楋細浜屽垎鏌ユ壘

涓昏鏈変互涓嬪嚑绫婚珮棰戣€冮锛?/p>

  • 瀵绘壘涓や釜姝e簭鏁扮粍鐨勪腑浣嶆暟銆愬洶闅俱€戙€愪簩鍒嗘煡鎵俱€?/li>
  • 鍒ゆ柇瀛愬簭鍒椼€愮畝鍗曘€戙€愪簩鍒嗘煡鎵俱€?/li>
  • 鍦ㄦ帓搴忔暟缁勪腑鏌ユ壘鍏冪礌鐨勭涓€涓拰鏈€鍚庝竴涓綅缃€愪腑绛夈€戙€愪簩鍒嗘煡鎵俱€?/li>

瀵绘壘涓や釜姝e簭鏁扮粍鐨勪腑浣嶆暟銆愪簩鍒嗘煡鎵俱€?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細4 瀵绘壘涓や釜姝e簭鏁扮粍鐨勪腑浣嶆暟锛堝洶闅撅級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number}
 */
var findMedianSortedArrays = function(nums1, nums2) {
    let m = nums1.length, n = nums2.length;
    let i = 0, j = 0;
    let newArr = [];
    while (i < m && j < n) {
        if (nums1[i] < nums2[j]) {
            newArr.push(nums1[i++]);
        } else {
            newArr.push(nums2[j++]);
        }
    }
    newArr = newArr.concat(i < m ? nums1.slice(i) : nums2.slice(j));
    const len = newArr.length;
    console.log(newArr)
    if (len % 2 === 0) {
        return (newArr[len / 2] + newArr[len / 2 - 1]) / 2;
    } else {
        return newArr[Math.floor(len / 2)];
    }
};

澶嶅埗浠g爜

鍒ゆ柇瀛愬簭鍒椼€愪簩鍒嗘煡鎵俱€?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細392 鍒ゆ柇瀛愬簭鍒楋紙绠€鍗曪級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {string} s
 * @param {string} t
 * @return {boolean}
 */
var isSubsequence = function(s, t) {
    let hash = {};
    for (let i = 0; i < t.length; i++) {
        if (!hash[t[i]]) hash[t[i]] = [];
        hash[t[i]].push(i);
    }
    let lastMaxIndex = 0;
    for (let i = 0; i < s.length; i++) {
        if (hash[s[i]]) {
            const index = binarySearch(hash[s[i]], lastMaxIndex);
            console.log(\'index\', index, hash[s[i]]);
            if (index === -1) return false;
            lastMaxIndex = hash[s[i]][index] + 1;
        } else return false;
    }
    return true;
};
function binarySearch(array, targetIndex) {
    let left = 0, right = array.length;
    while (left < right) {
        let mid = left + Math.floor((right - left) / 2);
        if (array[mid] >= targetIndex) {
            right = mid;
        } else if (array[mid] < targetIndex) {
            left = mid + 1;
        }
    }
    if (left >= array.length || array[left] < targetIndex) return -1;
    return left;
}

澶嶅埗浠g爜

馃拋 鍦ㄦ帓搴忔暟缁勪腑鏌ユ壘鍏冪礌鐨勭涓€涓拰鏈€鍚庝竴涓綅缃€愪簩鍒嗘悳绱€?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細34 鍦ㄦ帓搴忔暟缁勪腑鏌ユ壘鍏冪礌鐨勭涓€涓拰鏈€鍚庝竴涓綅缃紙涓瓑锛?/a>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var searchRange = function(nums, target) {
    const left = leftBound(nums, target);
    const right = rightBound(nums, target);
    return [left, right];
};
function leftBound(nums, target) {
    let left = 0;
    let right = nums.length - 1;
    while (left <= right) {
        let mid = Math.floor(left + (right - left) / 2);
        if (nums[mid] === target) {
            right = mid - 1;
        } else if (nums[mid] < target) {
            left = mid + 1;
        } else if (nums[mid] > target) {
            right = mid - 1;
        }
    }
    if (left >= nums.length || nums[left] !== target) {
        return -1;
    }
    return left;
}
function rightBound(nums, target) {
    let left = 0;
    let right = nums.length - 1;
    while (left <= right) {
        let mid = Math.floor(left + (right - left) / 2);
        if (nums[mid] === target) {
            left = mid + 1;
        } else if (nums[mid] < target) {
            left = mid + 1;
        } else if (nums[mid] > target) {
            right = mid - 1;
        }
    }
    if (right < 0 || nums[right] !== target) {
        return -1;
    }
    return right;
}

澶嶅埗浠g爜

銆愷煍ャ€戦珮棰戠畻娉曢绯诲垪锛氬姩鎬佽鍒?/h2>

涓昏鏈変互涓嬪嚑绫婚珮棰戣€冮锛?/p>

  • 鏈€闀块€掑瀛愬簭鍒椼€愪腑绛夈€戙€愬姩鎬佽鍒掋€?/li>
  • 闆堕挶鍏戞崲銆愪腑绛夈€戙€愬姩鎬佽鍒掋€戙€愰潰璇曠湡棰樸€?/li>
  • 鏈€闀垮叕鍏卞瓙搴忓垪 銆愪腑绛夈€戙€愬姩鎬佽鍒掋€戙€愰潰璇曠湡棰樸€?/li>
  • 缂栬緫璺濈 銆愬洶闅俱€戙€愬姩鎬佽鍒掋€?/li>
  • 鏈€闀垮洖鏂囧瓙搴忓垪銆愪腑绛夈€戙€愬姩鎬佽鍒掋€戙€愰潰璇曠湡棰樸€?/li>
  • 鏈€澶у瓙搴忓拰銆愮畝鍗曘€戙€愬姩鎬佽鍒掋€戙€愰潰璇曠湡棰樸€?/li>
  • 涔板崠鑲$エ鐨勬渶浣虫椂鏈虹郴鍒椼€愮郴鍒椼€戙€愬姩鎬佽鍒掋€戙€愰潰璇曠湡棰樸€?/li>

鏈€闀块€掑瀛愬簭鍒椼€愬姩鎬佽鍒掋€?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細300 鏈€闀块€掑瀛愬簭鍒楋紙涓瓑锛?/a>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[]} nums
 * @return {number}
 */
var lengthOfLIS = function(nums) {
    let maxLen = 0, n = nums.length;
    let dp = [];
    for (let i = 0; i < n; i++) {
        dp[i] = 1;
    }
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < i; j++) {
            if (nums[i] > nums[j]) {
                dp[i] = Math.max(dp[i], dp[j] + 1);
            }
        }
        maxLen = Math.max(maxLen, dp[i]);
    }
    return maxLen;
};

澶嶅埗浠g爜

銆愰潰璇曠湡棰樸€?闆堕挶鍏戞崲銆愬姩鎬佽鍒掋€?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細322 闆堕挶鍏戞崲锛堜腑绛夛級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[]} coins
 * @param {number} amount
 * @return {number}
 */
var coinChange = function(coins, amount) {
  if (amount === 0) return 0;
  let dp = [];
  for (let i = 0; i <= amount; i++) {
    dp[i] = amount + 1;
  }
  dp[0] = 0;
  for (let i = 0; i <= amount; i++) {
    for (let j = 0; j < coins.length; j++) {
      if (i >= coins[j]) {
        dp[i] = Math.min(dp[i - coins[j]] + 1, dp[i])
      }
    }
  }
  return dp[amount] === amount + 1 ? -1 : dp[amount];
};

澶嶅埗浠g爜

銆愰潰璇曠湡棰樸€?鏈€闀垮叕鍏卞瓙搴忓垪銆愬姩鎬佽鍒掋€?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細1143 鏈€闀垮叕鍏卞瓙搴忓垪锛堜腑绛夛級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {string} text1
 * @param {string} text2
 * @return {number}
 */
var longestCommonSubsequence = function(text1, text2) {
    let n1 = text1.length, n2 = text2.length;
    let dp = [];
    for (let i = -1; i < n1; i++) {
        dp[i] = [];
        for (let j = -1; j < n2;j++) {
            dp[i][j] = 0;
        }
    }
    for (let i = 0; i < n1; i++) {
        for (let j = 0; j < n2; j++) {
            if (text1[i] === text2[j]) {
                dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - 1] + 1);
            } else {
                dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1])
            }
        }
    }
    return dp[n1 - 1][n2 - 1];
};

澶嶅埗浠g爜

缂栬緫璺濈銆愬姩鎬佽鍒掋€?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細72 缂栬緫璺濈锛堝洶闅撅級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {string} word1
 * @param {string} word2
 * @return {number}
 */
var minDistance = function(word1, word2) {
  let len1 = word1.length, len2 = word2.length;
  let dp = [];
  for (let i = 0; i <= len1; i++) {
    dp[i] = [];
    for (let j = 0; j <= len2; j++) {
      dp[i][j] = 0;
      if (i === 0) {
        dp[i][j] = j;
      }
      if (j === 0) {
        dp[i][j] = i;
      }
    }
  }
  for (let i = 1; i <= len1; i++) {
    for (let j = 1; j <= len2; j++) {
      if (word1[i - 1] === word2[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1];
      } else {
        dp[i][j] = Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + 1);
      }
    }
  }
  return dp[len1][len2];
};

澶嶅埗浠g爜

銆愰潰璇曠湡棰樸€戞渶闀垮洖鏂囧瓙搴忓垪銆愬姩鎬佽鍒掋€?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細516 鏈€闀垮洖鏂囧瓙搴忓垪锛堜腑绛夛級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {string} s
 * @return {number}
 */
var longestPalindromeSubseq = function(s) {
    let dp = [];
    for (let i = 0; i < s.length; i++) {
        dp[i] = [];
        for (let j = 0; j < s.length; j++) {
            dp[i][j] = 0;
        }
        dp[i][i] = 1;
    }
    for (let i = s.length - 1; i >= 0; i--) {
        for (let j = i + 1; j < s.length; j++) {
            if (s[i] === s[j]) {
                dp[i][j] = dp[i + 1][j - 1] + 2;
            } else {
                dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]);
            }
        }
    }
    return dp[0][s.length - 1];
};

澶嶅埗浠g爜

銆愰潰璇曠湡棰樸€戰煉?鏈€澶у瓙搴忓拰銆愬姩鎬佽鍒掋€?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細53 鏈€澶у瓙搴忓拰锛堢畝鍗曪級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[]} nums
 * @return {number}
 */
var maxSubArray = function(nums) {
    let maxSum = -Infinity;
    let dp = [], n = nums.length;
    for (let i = -1; i < n; i++) {
        dp[i] = 0;
    }
    for (let i = 0; i < n; i++) {
        dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]);
        maxSum = Math.max(maxSum, dp[i]);
    }
    return maxSum;
};

澶嶅埗浠g爜

銆愰潰璇曠湡棰樸€戰煉?涔板崠鑲$エ鐨勬渶浣虫椂鏈恒€愬姩鎬佽鍒掋€?/h3>
  • 馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細121 涔板崠鑲$エ鐨勬渶浣虫椂鏈猴紙绠€鍗曪級銆愰潰璇曠湡棰樸€?/li>
  • 馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細122 涔板崠鑲$エ鐨勬渶浣虫椂鏈?II锛堢畝鍗曪級
  • 馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細123 涔板崠鑲$エ鐨勬渶浣虫椂鏈?III锛堝洶闅撅級
  • 馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細188 涔板崠鑲$エ鐨勬渶浣虫椂鏈篒V锛堝洶闅撅級
  • 馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細309 涔板崠鑲$エ鐨勬渶浣虫椂鏈哄惈鍐峰喕鏈燂紙涓瓑锛?/a>
  • 馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細714 涔板崠鑲$エ鐨勬渶浣虫椂鏈哄惈鎵嬬画璐癸紙涓瓑锛?/a>

鍙楅檺浜庣瘒骞咃紝杩欓噷鍙粰鍑虹涓€閬撻鐨勪唬鐮佹ā鏉匡紝涔熸槸涓€闈㈠父鑰冪湡棰橈紝绗旇€呭湪闈㈣瘯瀛楄妭璺冲姩鏃跺氨閬囧埌杩囥€?/p>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function(prices) {
  let dp = [];
  for (let i = -1; i < prices.length; i++) {
    dp[i] = []
    for (let j = 0; j <= 1; j++) {
      dp[i][j] = [];
      dp[i][j][0] = 0;
      dp[i][j][1] = 0;
      if (i === -1) {
        dp[i][j][1] = -Infinity;
      }
      if (j === 0) {
        dp[i][j][1] = -Infinity;
      }
      if (j === -1) {
        dp[i][j][1] = -Infinity;
      }
    }
  }
  for (let i = 0; i < prices.length; i++) {
    for (let j = 1; j <= 1; j++) {
      dp[i][j][0] = Math.max(dp[i - 1][j][0], dp[i - 1][j][1] + prices[i]);
      dp[i][j][1] = Math.max(dp[i - 1][j][1], dp[i - 1][j - 1][0] - prices[i]);
    }
  }
  return dp[prices.length - 1][1][0];
};

澶嶅埗浠g爜

楂橀绠楁硶棰樼郴鍒楋細BFS

涓昏鏈変互涓嬪嚑绫婚珮棰戣€冮锛?/p>

  • 鎵撳紑杞洏閿併€愪腑绛夈€戙€怋FS銆?/li>
  • 浜屽弶鏍戠殑鏈€灏忔繁搴︺€愮畝鍗曘€戙€怋FS銆?/li>

鎵撳紑杞洏閿併€怋FS銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細752 鎵撳紑杞洏閿侊紙涓瓑锛?/a>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {string[]} deadends
 * @param {string} target
 * @return {number}
 */
var openLock = function(deadends, target) {
  let queue = new Queue();
  let visited = new Set();
  let step = 0;
  queue.push(\'0000\');
  visited.add(\'0000\');
  while (!queue.isEmpty()) {
    let size = queue.size();
    for (let i = 0; i < size; i++) {
      let str = queue.pop();
      if (deadends.includes(str)) continue;
      if (target === str) {
        return step;
      }
      for (let j = 0; j < 4; j++) {
        let plusStr = plusOne(str, j);
        let minusStr = minusOne(str, j);
        if (!visited.has(plusStr)) {
          queue.push(plusStr);
          visited.add(plusStr)
        }
        if (!visited.has(minusStr)) {
          queue.push(minusStr);
          visited.add(minusStr)
        }
      }
    }
    step++;
  }
  return -1;
};
function plusOne(str, index) {
  let strArr = str.split(\'\');
  if (strArr[index] === \'9\') {
    strArr[index] = \'0\'
  } else {
    strArr[index] = (Number(strArr[index]) + 1).toString()
  }
  return strArr.join(\'\');
}
function minusOne(str, index) {
  let strArr = str.split(\'\');
  if (strArr[index] === \'0\') {
    strArr[index] = \'9\'
  } else {
    strArr[index] = (Number(strArr[index]) - 1).toString()
  }
  return strArr.join(\'\');
}
class Queue {
  constructor() {
    this.items = [];
    this.count = 0;
    this.lowerCount = 0;
  }
  push(elem) {
    this.items[this.count++] = elem;
  }
  pop() {
    if (this.isEmpty()) {
      return;
    }
    const elem = this.items[this.lowerCount];
    delete this.items[this.lowerCount];
    this.lowerCount++;
    return elem;
  }
  isEmpty() {
    if (this.size() === 0) return true;
    return false;
  }
  size() {
    return this.count - this.lowerCount;
  }
}

澶嶅埗浠g爜

浜屽弶鏍戠殑鏈€灏忔繁搴︺€怋FS銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細111 浜屽弶鏍戠殑鏈€灏忔繁搴︼紙绠€鍗曪級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var minDepth = function(root) {
  if (root == null) return 0;
  let depth = 1;
  let queue = new Queue();
  queue.push(root);
  while (!queue.isEmpty()) {
    let size = queue.size();
    for (let i = 0; i < size; i++) {
      const node = queue.pop();
      if (node.left == null && node.right == null) return depth;
      if (node.left) {
        queue.push(node.left);
      }
      if (node.right) {
        queue.push(node.right);
      }
    }
    depth++;
  }
  return depth;
};
class Queue {
  constructor() {
    this.items = [];
    this.count = 0;
    this.lowerCount = 0;
  }
  push(elem) {
    this.items[this.count++] = elem;
  }
  pop() {
    if (this.isEmpty()) {
      return;
    }
    const elem = this.items[this.lowerCount];
    delete this.items[this.lowerCount];
    this.lowerCount++;
    return elem;
  }
  isEmpty() {
    if (this.size() === 0) return true;
    return false;
  }
  size() {
    return this.count - this.lowerCount;
  }
}

澶嶅埗浠g爜

銆愷煍ャ€戦珮棰戠畻娉曢绯诲垪锛氭爤

涓昏鏈変互涓嬪嚑绫婚珮棰戣€冮锛?/p>

  • 鏈€灏忔爤銆愮畝鍗曘€戙€愭爤銆?/li>
  • 鏈夋晥鐨勬嫭鍙枫€愪腑绛夈€戙€愭爤銆戙€愰潰璇曠湡棰樸€?/li>
  • 绠€鍖栬矾寰勩€愪腑绛夈€戙€愭爤銆?/li>
  • 涓嬩竴涓洿澶у厓绱?銆愮郴鍒椼€戙€愭爤銆?/li>

鏈€灏忔爤銆愭爤銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細155 鏈€灏忔爤锛堢畝鍗曪級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * initialize your data structure here.
 */
var MinStack = function() {
    this.stack = [];
    this.minArr = [];
    this.count = 0;
    this.min = Number.MAX_SAFE_INTEGER;
};
/** 
 * @param {number} x
 * @return {void}
 */
MinStack.prototype.push = function(x) {
    this.min = Math.min(this.min, x);
    this.minArr[this.count] = this.min;
    this.stack[this.count] = x;
    this.count++;
};
/**
 * @return {void}
 */
MinStack.prototype.pop = function() {
    const element = this.stack[this.count - 1];
    if (this.count - 2 >= 0) this.min = this.minArr[this.count - 2];
    else  this.min = Number.MAX_SAFE_INTEGER;
    delete this.stack[this.count - 1];
    delete this.minArr[this.count - 1];
    this.count--;
    return element;
};
/**
 * @return {number}
 */
MinStack.prototype.top = function() {
    if (this.count >= 1) {
        return this.stack[this.count - 1];
    }
    return null;
};
/**
 * @return {number}
 */
MinStack.prototype.getMin = function() {
    const element = this.minArr[this.count - 1];
    return element;
};
/**
 * Your MinStack object will be instantiated and called as such:
 * var obj = new MinStack()
 * obj.push(x)
 * obj.pop()
 * var param_3 = obj.top()
 * var param_4 = obj.getMin()
 */

澶嶅埗浠g爜

銆愮郴鍒椼€戜笅涓€涓洿澶у厓绱?銆愭爤銆?/h3>
  • 馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細496 涓嬩竴涓洿澶у厓绱?I锛堢畝鍗曪級
  • 馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細503 涓嬩竴涓洿澶у厓绱?II锛堜腑绛夛級

鍙楅檺浜庣瘒骞咃紝杩欓噷鍙粰鍑虹涓€閬撻鐨勪唬鐮佹ā鏉?/p>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[]} nums
 * @return {number[]}
 */
var nextGreaterElements = function(nums) {
    let ans = [];
    let stack = new Stack();
    const n = nums.length;
    for (let i = 2 * n - 1; i >= 0; i--) {
        while (!stack.isEmpty() && stack.top() <= nums[i % n]) {
            stack.pop();
        }
        ans[i % n] = stack.isEmpty() ? -1 : stack.top();
        stack.push(nums[i % n]);
    }
    return ans;
};
class Stack {
    constructor() {
        this.count = 0;
        this.items = [];
    }
    top() {
        if (this.isEmpty()) return undefined;
        return this.items[this.count - 1];
    }
    push(element) {
        this.items[this.count] = element;
        this.count++;
    }
    pop() {
        if (this.isEmpty()) return undefined;
        const element = this.items[this.count - 1];
        delete this.items[this.count - 1];
        this.count--;
        return element;
    }
    isEmpty() {
        return this.size() === 0;
    }
    size() {
        return this.count;
    }
}

澶嶅埗浠g爜

銆愰潰璇曠湡棰樸€戞湁鏁堢殑鎷彿銆愭爤銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細20 鏈夋晥鐨勬嫭鍙凤紙涓瓑锛?/a>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function(s) {
    if (s.length === 0) {
        return true;
    }
    if (s.length % 2 !== 0) {
        return false;
    }
    let map = {
        \')\': \'(\',
        \']\': \'[\',
        \'}\': \'{\',
    };
    let left = [\'(\', \'[\', \'{\'];
    let right = [\')\', \']\', \'}\'];
    let stack = new Stack();
    for (let i = 0; i < s.length; i++) {
        if (!right.includes(s[i])) {
            stack.push(s[i]);
        } else {
            const matchStr = map[s[i]];
            while (!stack.isEmpty()) {
                const element = stack.pop();
                if (left.includes(element) && matchStr !== element)  return false;
                if (element === matchStr) break;
            }
        }
    }
    return stack.isEmpty();
};
class Stack {
    constructor() {
        this.count = 0;
        this.items = [];
    }
    push(element) {
        this.items[this.count] = element;
        this.count++;
    }
    pop() {
        if (this.isEmpty()) return undefined;
        const element = this.items[this.count - 1];
        delete this.items[this.count - 1];
        this.count--;
        return element;
    }
    isEmpty() {
        return this.size() === 0;
    }
    size() {
        return this.count;
    }
}

澶嶅埗浠g爜

绠€鍖栬矾寰勩€愭爤銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細71 绠€鍖栬矾寰勶紙涓瓑锛?/a>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {string} path
 * @return {string}
 */
var simplifyPath = function(path) {
    let newPath = path.split(\'/\');
    newPath = newPath.filter(item => item !== "");
    const stack = new Stack();
    for (let s of newPath) {
        if (s === \'..\') stack.pop();
        else if (s !== \'.\') stack.push(s);
    }
    if (stack.isEmpty()) return \'/\';
    let str = \'\';
    while (!stack.isEmpty()) {
        const element = stack.pop();
        str = \'/\' + element + str;
    }
    return str;
};
function handleBack(stack, tag, num) {
    if (!stack.isEmpty()) return num;
    const element = stack.pop();
    if (element === \'..\') return handleBack(stack, tag, num + 1);
    else {
        stack.push(element);
        return num;
    }
}
class Stack {
    constructor() {
        this.count = 0;
        this.items = [];
    }
    push(element) {
        this.items[this.count] = element;
        this.count++;
    }
    pop() {
        if (this.isEmpty()) return undefined;
        const element = this.items[this.count - 1];
        delete this.items[this.count - 1];
        this.count--;
        return element;
    }
    size() {
        return this.count;
    }
    isEmpty() {
        return this.size() === 0;
    }
}

澶嶅埗浠g爜

銆愷煍ャ€戦珮棰戠畻娉曢绯诲垪锛欴FS

涓昏鏈変互涓嬪嚑绫婚珮棰戣€冮锛?/p>

  • 宀涘笨鐨勬渶澶ч潰绉€愪腑绛夈€戙€怐FS銆?/li>
  • 鐩稿悓鐨勬爲銆愮畝鍗曘€戙€怐FS銆?/li>

宀涘笨鐨勬渶澶ч潰绉€怐FS銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細695 宀涘笨鐨勬渶澶ч潰绉紙涓瓑锛?/a>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[][]} grid
 * @return {number}
 */
let maxX, maxY;let visited;let globalMaxArea;
var maxAreaOfIsland = function(grid) {
    visited = new Set();
    maxX = grid.length;
    maxY = grid[0].length;
    globalMaxArea = 0;
    for (let i = 0; i < maxX; i++) {
        for (let j = 0; j < maxY; j++) {
            if (grid[i][j] === 1) {
                visited.add(`(${i}, ${j})`);
                globalMaxArea = Math.max(globalMaxArea, dfs(grid, i, j));
            }
            visited.clear();
        }
    }
    return globalMaxArea;
};
function dfs(grid, x, y) {
    let res = 1;
    for (let i = -1; i <= 1; i++) {
        for (let j = -1; j <= 1; j++) {
            if (Math.abs(i) === Math.abs(j)) continue;
            const newX = x + i;
            const newY = y + j;
            if (newX >= maxX || newX < 0 || newY >= maxY || newY < 0) continue;
            if (visited.has(`(${newX}, ${newY})`)) continue;
            visited.add(`(${newX}, ${newY})`);
            const areaCnt = grid[newX][newY]
            if (areaCnt === 1) {
                const cnt = dfs(grid, newX, newY);
                res += cnt;
            } 
        }
    }
    return res;
}

澶嶅埗浠g爜

鐩稿悓鐨勬爲銆怐FS銆?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細100 鐩稿悓鐨勬爲锛堢畝鍗曪級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {boolean}
 */
var isSameTree = function(p, q) {
    if (p == null && q == null) return true;
    if (p == null || q == null) return false;
    if (p.val !== q.val) return false;
    return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
};

澶嶅埗浠g爜

銆愷煍ャ€戦珮棰戠畻娉曢绯诲垪锛氬洖婧畻娉?/h2>

涓昏鏈変互涓嬪嚑绫婚珮棰戣€冮锛?/p>

  • N鐨囧悗銆愬洶闅俱€戙€愬洖婧畻娉曘€戙€愰潰璇曠湡棰樸€?/li>
  • 鍏ㄦ帓鍒椼€愪腑绛夈€戙€愬洖婧畻娉曘€?/li>
  • 鎷彿鐢熸垚銆愪腑绛夈€戙€愬洖婧畻娉曘€?/li>
  • 澶嶅師 IP 鍦板潃銆愪腑绛夈€戙€愬洖婧畻娉曘€?/li>
  • 瀛愰泦 銆愮畝鍗曘€戙€愬洖婧畻娉曘€?/li>

銆愰潰璇曠湡棰樸€慛鐨囧悗銆愬洖婧畻娉曘€?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細51 N鐨囧悗锛堝洶闅撅級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number} n
 * @return {string[][]}
 */
let result = [];
var solveNQueens = function(n) {
    result = [];
    let board = [];
    for (let i = 0; i < n; i++) {
      board[i] = [];
      for (let j = 0; j < n; j++) {
        board[i][j] = \'.\'
      }
    }
    backtrack(0, board, n);
    return result;
};
function deepClone(board) {
  let res = [];
  for (let i = 0; i < board.length; i++) {
    res.push(board[i].join(\'\'));
  }
  return res;
}
function backtrack(row, board, n) {
    if (row === n) {
      result.push(deepClone(board));
      return;
    }
    for (let j = 0; j < n; j++) {
        if (checkInValid(board, row, j, n)) continue;
        board[row][j] = \'Q\';
        backtrack(row + 1, board, n);
        board[row][j] = \'.\';
      }
}
function checkInValid(board, row, column, n) {
  // 琛?  for (let i = 0; i < n; i++) {
    if (board[i][column] === \'Q\') return true;
  }
  for (let i = row - 1, j = column + 1; i >= 0 && j < n; i--, j++) {
    if (board[i][j] === \'Q\') return true;
  }
  for (let i = row - 1, j = column - 1; i >= 0 && j >= 0; i--, j--) {
    if (board[i][j] === \'Q\') return true;
  }
  return false;
}

澶嶅埗浠g爜

鍏ㄦ帓鍒椼€愬洖婧畻娉曘€?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細46 鍏ㄦ帓鍒楋紙涓瓑锛?/a>

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number[]} nums
 * @return {number[][]}
 */
let results = [];var permute = function(nums) {
    results = [];
    backtrack(nums, []);
    return results;
};
function backtrack(nums, track) {
    if (nums.length === track.length) {
        results.push(track.slice());
        return;
    }
    for (let i = 0; i < nums.length; i++) {
        if (track.includes(nums[i])) continue;
        track.push(nums[i]);
        backtrack(nums, track);
        track.pop();
    }
}

澶嶅埗浠g爜

鎷彿鐢熸垚銆愬洖婧畻娉曘€?/h3>

馃憠聽銆怢eetCode 鐩撮€氳溅銆戯細22 鎷彿鐢熸垚锛堜腑绛夛級

棰樿В

鈫掔偣鍑诲睍寮€鏌ョ湅


/**
 * @param {number} n
 * @return {string[]}
 */
var generateParenthesis = function(n) {
    let validRes = [];
    backtrack(n * 2, validRes, \'\');
    return validRes;
};
function backtrack(len, validRes, bracket) {
    if (bracket.length === len) {
        if (isValidCombination(bracket)) {
            validRes.push(bracket);
        }
        return;
    }
    for (let str of [\'(\', \')\']) {
        bracket += str;
        backtrack(len, validRes, bracket);
        bracket = bracket.slice(0, bracket.length - 1);
    }
}
function isValidCombination

以上是关于瀛楄妭璺冲姩鏈€甯歌€冪殑 64 閬揓S绠楁硶棰?/a>的主要内容,如果未能解决你的问题,请参考以下文章

鎶栭煶鐭棰戝唴瀹圭悊瑙e拰鎺ㄨ崘绠楁硶

甯歌闈㈣瘯绠楁硶锛氭敮鎸佸悜閲忔満

[娣卞叆娴呭嚭LB]鎵嬫妸鎵嬪甫浣犲疄鐜颁竴涓礋杞藉潎琛″櫒

甯歌闈㈣瘯绠楁硶锛氭湸绱犺礉鍙舵柉

绠楁硶-鍔ㄦ€佽鍒掞紙浜岋級

绠楁硶-鍔ㄦ€佽鍒掞紙涓夛級