這篇是 Udemy 上的知名課程 – Master the Coding Interview 的部分進修心得。這篇對應的內容是「Data Structures: Array」關於陣列的第二部分簡介。
課程相關資訊
[連結]:https://www.udemy.com/course/master-the-coding-interview-data-structures-algorithms/
課程對應章節:67~73
請注意:本系列文章為個人對應課程的消化吸收後,所整理出來的內容。換言之,並不一定會包含全部的課程內容,也有可能會添加其他資源來說明。
內容
背景知識
- 1. 可以將 String 轉換成 Array 來方便操作
2. Array 方便查找、將元素加至最後(push)、將元素從陣列的最後移除並回傳移除的元素(pop)、有 index
3. 若要 insert 或是 delete 元素,有很高機會需要遍例整個陣列長度 O(n)
範例程式碼
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
// 經典的 reverse 問題 function reverse(str){ let str = str.trim() if(!str || typeof str !== 'string'){ return "Please check your input"; } if(str.length<2){ return str; } const resultArray = [] for(let i=str.length-1;i>=0;i--){ resultArray.push(str[i]); } return resultArray.join(""); } function reverse(str){ return str.split("").reverse().join(""); } const reverse = str => [...str].reverse().join(""); // 合併陣列並排序 function mergeSortedArrays(array1, array2){ const mergedArray = []; let array1Item = array1[0]; let array2Item = array2[0]; let i = 1; let j = 1; if(array1.length === 0) { return array2; } if(array2.length === 0) { return array1; } while (array1Item || array2Item){ if(array2Item === undefined || array1Item < array2Item){ mergedArray.push(array1Item); array1Item = array1[i]; i++; } else { mergedArray.push(array2Item); array2Item = array2[j]; j++; } } return mergedArray; } function mergeSortedArrays(arr1,arr2){ let length1 = arr1.length; let length2 = arr2.length; if( length1 === length2 ) return "No array can be merged!" if( length1 === 0) return arr2; if( length2 === 0) return arr1; return [...arr1,...arr2].sort( (a,b) => a-b > 0 ? 1 : -1 ); } |
相關文章
★全文分享★ [筆記] Master The Coding Interview – 16
![[筆記] Master The Coding Interview – 16 [筆記] Master The Coding Interview – 16](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 15
![[筆記] Master The Coding Interview – 15 [筆記] Master The Coding Interview – 15](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 14
![[筆記] Master The Coding Interview – 14 [筆記] Master The Coding Interview – 14](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 13
![[筆記] Master The Coding Interview – 13 [筆記] Master The Coding Interview – 13](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 12
![[筆記] Master The Coding Interview – 12 [筆記] Master The Coding Interview – 12](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 11
![[筆記] Master The Coding Interview – 11 [筆記] Master The Coding Interview – 11](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 10
![[筆記] Master The Coding Interview – 10 [筆記] Master The Coding Interview – 10](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 9
![[筆記] Master The Coding Interview – 9 [筆記] Master The Coding Interview – 9](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 8
![[筆記] Master The Coding Interview – 8 [筆記] Master The Coding Interview – 8](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 7
![[筆記] Master The Coding Interview – 7 [筆記] Master The Coding Interview – 7](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 6
![[筆記] Master The Coding Interview – 6 [筆記] Master The Coding Interview – 6](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 5
![[筆記] Master The Coding Interview – 5 [筆記] Master The Coding Interview – 5](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 3
![[筆記] Master The Coding Interview – 3 [筆記] Master The Coding Interview – 3](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 2
![[筆記] Master The Coding Interview – 2 [筆記] Master The Coding Interview – 2](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
這篇是 Udemy 上的知名課程 – Master the Coding
★全文分享★ [筆記] Master The Coding Interview – 1
![[筆記] Master The Coding Interview – 1 [筆記] Master The Coding Interview – 1](https://smlpoints.com/wp-content/uploads/notes-master-the-coding-interview-1.jpg)
工程師的面試中,不免俗的會遇見用各種
