原创 【 排序函數 】C/C++排序函數qsort/sort使用區別

/* 本文爲 C qsort 函數 in <stdlib.h> 和 C++ sort 函數 in <algorithm> 區別 */ #include <iostream> #include <algorithm> #i

原创 【 結構體傳參 】 結構體值傳遞方式

/* 關於結構體的傳值,爲值傳,即將結構體大小的字節複製一份進行傳遞。 */ #include <stdio.h> typedef struct node{ int a; int b; }node; void

原创 【 數據結構 】字典樹的構建及搜索代碼實現

#include <stdio.h> #include <stdlib.h> #define Alphabet_SIZE 26 // 數據結構 typedef struct tnode { int flag; // 是否爲

原创 【 數據結構 】哈夫曼樹與哈夫曼編碼

定義:按照字符的頻率(頻數)構建最優樹,即把頻率小的樹儘可能放在深層節點,頻率大的樹放在淺層節點,從而使得所有葉子節點的帶權路徑和最小。這樣做的好處是依照路徑重新對葉子節點的字符進行01編碼,從而使高頻率的字符編碼短,低頻率字符的編碼長,

原创 5. Longest Palindromic Substring

5. Longest Palindromic Substring 1. 暴力法,遍歷迴文,時間複雜度O( n³ ),無法AC,通過了43/103個測例 class Solution { public: string longes

原创 4. Median of Two Sorted Arrays

4. Median of Two Sorted Arrays 題解: 解題思路1,尋找第k小數(Kth)   結題思路2,暴力求解 class Solution { public: double findMedianSorte

原创 30. Substring with Concatenation of All Words

30. Substring with Concatenation of All Words 滑動窗口 初次嘗試失敗,會超時,213/230,準備看源碼 class Solution { private: bool mapEqua

原创 C++中map和iterator的使用

#include <iostream> #incldue <map> using namespace std; int main(){ map<char, int> mp; //建立map map<char, int

原创 8. String to Integer (atoi) 【有限狀態機】

8. String to Integer (atoi) 題解:         由於數字限制爲 int 32位,爲了簡化算法採用 long int 64位進行處理,算法採用編譯原理中有限狀態機的方案,初始狀態爲0。         0 狀

原创 1. Add Two Numbers

Link here see more. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *ne

原创 38. Count and Say

38. Count and Say 其實思路很簡單,遞歸調用求出n-1的字符串,然後遍歷判斷每個數字對應的次數,轉化爲str輸出即可。 class Solution { public: string countAndSay(in

原创 10. Regular Expression Matching

10. Regular Expression Matching 10. Regular Expression Matching 題解: 方法一:動態規劃         dp[i][j]表示 s 的前 i 個元素和 p 的前 j 個元素可

原创 25. Reverse Nodes in k-Group

25. Reverse Nodes in k-Group 參考算法:圖解k個一組翻轉鏈表     鏈表的翻轉就是將當前節點的下個節點的後繼節點置爲當前節點。 C++代碼 /** * Definition for singly-link

原创 33. Search in Rotated Sorted Array 非常簡單的算法

33. Search in Rotated Sorted Array 執行用時 :4 ms, 在所有 C++ 提交中擊敗了90.71%的用戶 內存消耗 :9 MB, 在所有 C++ 提交中擊敗了5.13%的用戶   從序列是排序的可以看出

原创 23. Merge k Sorted Lists

23. Merge k Sorted Lists 題解: 1. 優先級隊列法     本打算採用優先級隊列的方式,將 struct 中 的 operator 操作符重載,然後使用 STL<queue> 的 priority_queue 進