發表文章

目前顯示的是有「Data Structure」標籤的文章

LeetCode資料結構_Array

  LeetCode常見題目 LeetCode 1. Two Sum LeetCode 268. Missing Number LeetCode 283. Move Zeroes LeetCode 26. Remove Duplicates from Sorted Array LeetCode 27.Remove Element LeetCode.485. Max Consecutive Ones

LeetCode_Stack和Tree

圖片
LeetCode1299. Replace Elements with Greatest Element on Right Side(Easy) https://coolmandiary.blogspot.com/2021/08/leetcode1299-replace-elements-with.html Stack 利用堆疊(Stack)先進後出(FILO,First-in-Last-out)、後進先出(LIFO,Last-In-First-Out=>LIFO)的特性來解題 amazon,google,snapchat,uber,阿里,腾讯,百度,字节 *** LeetCode 155. Min Stack(Medium) https://coolmandiary.blogspot.com/2021/08/leetcode-155-min-stackmediumstack.html amazon,airbnb,facebook,google,microsoft,twitter,阿里,腾讯,百度,字节 *** LeetCode20.Valid Parentheses(Easy) https://coolmandiary.blogspot.com/2021/08/leetcode20valid-parentheseseasystack.html LeetCode946. Validate Stack Sequences(Medium) https://coolmandiary.blogspot.com/2021/08/leetcode946-validate-stack.html LeetCode678. Valid Parenthesis String(Medium) https://coolmandiary.blogspot.com/2021/08/leetcode678-valid-parenthesis.html 阿里,腾讯,百度,字节 *** LeetCode32.Longest Valid Parentheses(Hard) https://coolmandiary.blogspot.com/2021/08/leetcode32longest-valid-parentheseshard.html 阿里,腾讯,百度,字节 *** LeetCode94...

LeetCode資料結構_Linked List

圖片
Array跟LinkedList之間差異 Array是index based的結構,LinkedList則是Reference based。 Array會存在sequential memory locations,LinkedList則是sequence of the elements,每一個element都會link下一個element。(Array節省記憶體使用空間,LinkedList需要儲存pointer,所以需要較大的空間。) Array裡面會儲存同樣結構的資料,LinkedList裡面每個儲存的Object都會包含Data與Link。 Array必須先宣告要多大的空間,LinkedList則不受限。 Array存取資料需要index,LinkedList需要從頭開始尋找資料。 Array:Random Access即是隨機存取,又被大家稱為直接存取。在compile的時候,便分配好空間,因此是固定的。在記憶體儲存是連續的。 Array:O(1), 直接存取或隨機存取 LinkedList:Sequencial Access則是需要循序走訪才能到達指定的目標。在run time的時候分配空間,因此是彈性的。記憶體中隨機儲存位置。 LinkedList:O(n), 每次存取資料都需要循序走訪 https://visualgo.net/en/list 單鏈串列與雙鏈串列的比較 單鏈串列 優點: 1.較節省空間 2.插入與刪除節點時,需要修改的鏈結個 數較少 缺點: 1.插入與刪除節點時,需提供前一節點的指標 2.不能雙向移動 雙鏈串列 優點: 1.雙向移動 2.插入與刪除節點時,不需要給前一節點指標 (可以用前一個節點或後一個節點取得) 缺點: 1.較佔用空間 2.插入與刪除時,需要改變較多的鏈結欄位 Array 優點在於有Index可更快速存取但相應預先定義好的固定大小帶來空間利用與新增移除元素容易造成浪費空間跟較不彈性 LinkedList較容易新增與移除元素但缺點就在於只能依序讀取且要多一個空間紀錄下一個node位置。 LeetCode常見題目 21. Merge Two Sorted Lists (Easy) https://leetcode.com/problems/merge-two-sorted-lists/ 2. Add Two Numbers...

LeetCode資料結構_BigO

圖片
  一個數學記號,用來來評估⼀一個函式的 時間複雜度 或 空間複雜度 。 時間複雜度 同一個函式的執⾏行行時間因為 input 的不同⽽而產⽣生的變化 空間複雜度 同⼀個函式所需的空間因為 input 的不同⽽而產⽣生的變化 O(1) void sayTruth(int x) { for (int i = 0; i < 10; i++) { std::cout << "test"; } } O(N) void sayTruth(int x) { for (int i = 0; i < x; i++) { std::cout << "test"; } } O(N^2) void sayTruth(int x) { for (int i = 0; i < x; i++) { for (int j = 0; j < x; j++) { std::cout << "test"; } } } 常數或倍數 不會改變複雜度相對於 input 的關係,不用考慮 (皆為已知,不會影響一個函數複雜度本質) O(2N) void sayTruth(int x) { for (int i = 0; i < x; i++) { std::cout << "test"; } for (int i = 0; i < x; i++) { std::cout << "test"; } } O(2n) 並沒有改變執⾏行行時間是線性成長的事實 計算 Big O 時不考慮倍數 因此O(2N) = O(N) O(N+1) void sayTruth(int x) { for (int i = 0; i < x + 1; i++) { std::cout << "test"; } } O(n + 1) 並沒有改變執⾏行行時間是線性成長的事實 ...