LeetCode日記3

(2015/11/3)node

LeetCode-36 Valid Sudoku:(easy)數組

1)使用數據結構數據結構

set<char> emptyset;函數

vector<set<char>> mp(27, emptyset);spa

2)下標0-8存儲行中的數字,9-17存儲列中的數字,18-26存儲3×3塊中數字。指針

3)雙重for循環中,i表示行,9 + j表示列,18 + i / 3 + 3 * (j / 3)表示塊。code



(2015/11/12)orm

LeetCode-38 Count and Say:(easy)排序

1)判斷參數n,返回直接輸出的部分。遞歸

2)雙重for循環。


LeetCode-58 Length of Last Word:(easy)

LeetCode-66 Plus One:(easy)

LeetCode-67 Add Binary:(easy)


LeetCode-70 Climbing Stairs:(easy)

1)爬樓梯,裴波那契數列。

2)公式F(1)=1, F(2)=2,........F(n)=F(n-1)+F(n-2)。


LeetCode-83 Remove Duplicates from Sorted List:(easy)

1)刪除已排序的鏈表中的重複節點。

2)處理輸入爲空鏈表的狀況。

3)使用兩個指針,一個記錄上一個節點,一個尋找下一個不一樣的節點。找到就作指針的變換(不管變換先後是否真的改變了指針)。


LeetCode-88 Merge Sorted Array:(easy)

1)題目理解:m和n是數組中已經初始化的元素。也就是說數組的大小可能比m和n大,且數組中可能有未初始化的元素。


LeetCode-100 Same Tree:(easy)

1)錯誤思路:分別計算出兩棵樹的中序遍歷和先序遍歷,判斷他們是否相等。

錯誤的例子:[1,1] 和 [1,NULL,1]

2)正確思路:同時對兩棵樹進行遍歷。遞歸實現。



(2015/11/13)

LeetCode-101 Symmetric Tree:(cant)

1)錯誤思路:對樹進行 」左子樹-根-右子樹「和」右子樹-根-左子樹「遍歷後序列相等,則樹是對稱的。(異想天開)

錯誤的例子:[1,2,3,3,NULL,2,NULL]

2)正確思路:同100題,必須同時向兩邊進行遍歷。

3)參考:https://leetcode.com/discuss/26705/15-lines-of-c-solution-8-ms

遞歸對二叉樹進行對稱遍歷:

class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(root == NULL) return true;
        else return func(root->left, root->right);
    }
private:
    bool func(TreeNode *p, TreeNode *q){
        if(p == NULL && q == NULL) return true;
        else if(p == NULL || q == NULL) return false;
        else{
            if(p->val != q->val) return false;
            else return func(p->left, q->right) && func(p->right, q->left);
        }
    }

};


LeetCode-102 Binary Tree Level Order Traversal:(easy)

對二叉樹按層遍歷。

1)思路:用隊列存放每一層的節點的指針。


LeetCode-104 Maximum Depth of Binary Tree:(easy)

求二叉樹葉子節點的最大深度。

1)思路:形參ans存放目前葉子節點的最大深度(引用);形參now存放當前此次遍歷的當前深度。

class Solution {
public:
    int maxDepth(TreeNode* root) {
        int ans = 0;
        DFS(root, ans, 0);
        return ans;
    }
private:
    void DFS(TreeNode *T, int &ans, int now){
        if(T == NULL){
            if(now > ans) ans = now;
        }
        else{
            now++;
            DFS(T->left, ans, now);
            DFS(T->right, ans, now);
        }
        return;
    }
};


LeetCode-107 Binary Tree Level Order Traversal II:(easy)

LeetCode-102 Binary Tree Level Order Traversal。

1)最後在返回前,對vector<vector<int>> 變量進行一次reverse函數調用便可。


LeetCode-110 Balanced Binary Tree:(easy)

a height-balanced binary tree:a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

基本概念:

1)百度百科-平衡二叉樹:空樹,或者它的左右兩棵子樹的高度差的絕對值不超過1,而且左右兩棵子樹也是平衡二叉樹

2)樹的高度(樹的深度):樹中結點的最大層次。

3)結點的層次:從根開始定義起,根爲第一層,根的孩子爲第二層。

解題思路:

1)計算二叉樹深度的子函數:

int depth(TreeNode *T){
        if(T == NULL) return 0;
        else return max(depth(T->left) + 1, depth(T->right) + 1);
    }

2)判斷是不是平衡二叉樹的函數(flag做爲主函數中傳入的標誌)

    void func(TreeNode *T, bool &flag){
        if(flag == false) return;
        if(T == NULL) return;
        if(abs(depth(T->left) - depth(T->right)) > 1){
            flag = false;
            return;
        }
        else{
            func(T->left, flag);
            func(T->right, flag);
        }
    }


LeetCode-111 Minimum Depth of Binary Tree:(easy)

基本概念:

1)二叉樹的minimum depth:從根到葉子結點的最短路徑上,的結點數目。

2)葉子結點是左右子樹都爲空的結點。此題中,當只有一個根結點時,能夠視根結點爲葉子結點,不然不能夠。

錯誤思路:

1)不能經過求:左右子樹的樹高的最小值來計算。由於求樹的高度,實際上是求出了最大深度。

2)不能碰到空結點,就覺得是葉子結點。葉子結點的判斷要經過左右子樹。

AC代碼:

class Solution {
public:
    int minDepth(TreeNode* root) {
        int mindepth = 0;
        func(root, mindepth, 0);
        return mindepth;
    }
    void func(TreeNode *T, int &mindepth, int now){
        if(T == NULL) return;
        now++;
        if(T->left == NULL && T->right == NULL){
            if(mindepth == 0) mindepth = now;
            if(now < mindepth) mindepth = now;
            return;
        }
        func(T->left, mindepth, now);
        func(T->right, mindepth, now);
        return;
    }
};


LeetCode-112 Path Sum:(easy)

1)到葉子結點進行一次判斷便可。


LeetCode-118 Pascal's Triangle:(easy)

LeetCode-119 Pascal's Triangle II:(easy)(稍微修改118便可,注意這裏第0層是[1],第一層是[1,1])



(2015/11/14)

LeetCode-125 Valid Palindrome:(easy)

題目理解:

迴文串的判斷。

1)alphanumeric characters:字母和數字字符

2)ignoring cases:例如"       ",是迴文串。

C++的細節問題:

1)對string變量,從兩端向中間遍歷的問題。在for循環中,string的下標定義爲整型變量,若定義爲string::size_type,則可能會形成溢出。


LeetCode-226 Invert Binary Tree:(easy)

1)遞歸,一層一層向下反轉便可。


LeetCode-257 Binary Tree Paths:(easy)

1)關鍵:判斷葉子節點。

2)int轉換爲string。


LeetCode-235 Lowest Common Ancestor of a Binary Search Tree:(easy)(do more time)

1)遞歸方法

TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q)

結點p和q的值爲1,其餘結點的值爲0;

樹的值爲樹上全部節點的值的和。

每一個節點的值 =  左子樹的值 + 右子樹的值 + 本結點的值。(遞歸)

2)第一個值爲2的結點,就是所要找的結點。

class Solution {
public:
	TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
		bool find = false;
		TreeNode *ans;
		func(root, ans, find, p, q);
		return ans;
	}
	int func(TreeNode *T, TreeNode *&ans, bool &find, TreeNode *p, TreeNode *q){
		if (T == NULL || find) return 0;
		int val = 0;
		if (T == p) val++;
		if (T == q) val++;
		val += func(T->left, ans, find, p, q) + func(T->right, ans, find, p, q);
		if (!find && val == 2){
			ans = T;
			find = true;
		}
		return val;
	}
};


LeetCode-94 Binary Tree Inorder Traversal:(遞歸easy)(迭代:參考《數據結構》)

1)迭代法思路:用棧存放子樹的根結點。

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> ans;
        stack<TreeNode *> stk;
        TreeNode *p = root;
        while(p || !stk.empty()){
            if(p){
                stk.push(p);
                p = p->left;
            }
            else{
                p = stk.top();
                stk.pop();
                ans.push_back(p->val);
                p = p->right;
            }
        }
        return ans;
    }
};
相關文章
相關標籤/搜索