leetcode刷題總結一

大四狗找工做,要刷題了,leetcode上面題目比較適合面試算法類題目,也不純粹爲了蒙題,鍛鍊一下面試類型的思惟node

Single Number面試

有N個數,其中只有一個數出現了一次,其餘都是兩次,找出那個數算法

把全部數求一下異或lua

Maximum Depth of Binary Treespa

求樹的最大深度code

遞歸遍歷一遍blog

Same Tree遞歸

給兩個樹的根節點,看兩棵樹是否相同three

兩棵樹同時遍歷一遍element

Reverse Integer

輸出這個數倒過來的數

注意負數狀況,模擬一下便可

Best Time to Buy and Sell Stock II

模擬買東西賣東西賺錢

模擬便可

Unique Binary Search Trees

給定N個節點,問有多少種不一樣的二叉樹

卡特蘭數經典案例,C(2n,n) / (n+1)

Linked List Cycle

判斷一個鏈表是否含有環

從head出發,一個一次一步,一個一次兩步,若相交則有環,不然走到頭就結束說明沒有

Binary Tree Inorder Traversal

中序遍歷

Binary Tree Preorder Traversal

前序遍歷

Populating Next Right Pointers in Each Node

把每層的節點按從左到右順序連接起來,最後一個節點next指向null

void connect(TreeLinkNode *root) {
        if (root == NULL) return;
        connect(root->left);
        connect(root->right);
        TreeLinkNode *l = root->left;
        TreeLinkNode *r = root->right;
        while (l) {
            l->next = r;
            l = l->right;
            r = r->left;
        }
    }

 

Remove Duplicates from Sorted List

刪除相同數值的節點鏈表,已排好序

掃一遍,跟前一個相同就刪了那個節點

Search Insert Position

給出target,求target應該插入的array的index

二分便可

Climbing Stairs

經典的fbi數列

Single Number II

single numberI增強版,one,two,three分別表明出現的次數

 int singleNumber(int A[], int n) {
        int one , two , three;
        one = two = three = 0;
        for (int i = 0;i < n;i ++) {
            two |= one&A[i];
            one ^= A[i];
            three = one & two;
            one &= ~three;
            two &= ~three;
        }
        return one;
    }

 

Maximum Subarray

經典動態規劃,求最長連續序列和

Remove Element

去掉array中的element元素

不用額外資源,跟尾資源swap

Merge Two Sorted Lists

合併兩個有序鏈表

Balanced Binary Tree

Convert Sorted Array to Binary Search Tree

Remove Duplicates from Sorted Array

Swap Nodes in Pairs

Symmetric Tree

Merge Sorted Array

Sort Colors

Plus One

Permutations

Minimum Path Sum

Container With Most Water

Best Time to Buy and Sell Stock

Linked List Cycle II

Set Matrix Zeroes

Path Sum

Remove Nth Node From End of List

Sum Root to Leaf Numbers

Minimum Depth of Binary Tree

Length of Last Word

Palindrome Number

Valid Parentheses

Jump Game

Triangle

Validate Binary Search Tree

Pow(x, n)

Next Permutation

Jump Game II

Sqrt(x)

Best Time to Buy and Sell Stock III

Rotate List

Reorder List

Evaluate Reverse Polish Notation

Two Sum

Reverse Words in a String

相關文章
相關標籤/搜索