大四狗找工做,要刷題了,leetcode上面題目比較適合面試算法類題目,也不純粹爲了蒙題,鍛鍊一下面試類型的思惟node
有N個數,其中只有一個數出現了一次,其餘都是兩次,找出那個數算法
把全部數求一下異或lua
Maximum Depth of Binary Tree:spa
求樹的最大深度code
遞歸遍歷一遍blog
Same Tree:遞歸
給兩個樹的根節點,看兩棵樹是否相同three
兩棵樹同時遍歷一遍element
輸出這個數倒過來的數
注意負數狀況,模擬一下便可
Best Time to Buy and Sell Stock II:
模擬買東西賣東西賺錢
模擬便可
給定N個節點,問有多少種不一樣的二叉樹
卡特蘭數經典案例,C(2n,n) / (n+1)
判斷一個鏈表是否含有環
從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:
刪除相同數值的節點鏈表,已排好序
掃一遍,跟前一個相同就刪了那個節點
給出target,求target應該插入的array的index
二分便可
經典的fbi數列
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; }
經典動態規劃,求最長連續序列和
去掉array中的element元素
不用額外資源,跟尾資源swap
合併兩個有序鏈表
Convert Sorted Array to Binary Search Tree:
Remove Duplicates from Sorted Array:
Best Time to Buy and Sell Stock:
Remove Nth Node From End of List:
Best Time to Buy and Sell Stock III: