這個階段透過實作練習,讓你可以了解到撰寫迴圈、判斷式和找規律時,先將問題的架構找出,再透過輔助函式,最後就可以拼湊出結果。像是經典的 99 乘法表、NM 乘法表、印金字塔…等,都可以用這樣的思考邏輯寫出來。
課程相關資訊
[連結]:https://lidemy.com/p/alg101-leetcode
Course 42 ~ Course 50 (全部一共有 106 Courses)
請注意:本系列文章為個人對應課程的消化吸收後,所整理出來的內容。換言之,並不一定會包含全部的課程內容,也有可能會添加其他資源來說明。
內容
1. 不需要去思考要用何種內建函式去解題,而是去思考要如何用「迴圈、函式和判斷式」來解決問題
2. 內建函式是可以用「迴圈、函式和判斷式」實作出來的
3. 練習印出 99 乘法表、NM 乘法表、找水仙花數、印金字塔……等等,在訓練演算法時常見的題目
|
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
// Unit 4.5 function printPyramid(n){ for(let i=1; i <= n; i++){ buildPyramid(i,n) } function buildPyramid(i,n){ let pyramid = repeat(' ', n-i ) + repeat('*', 2*i-1 ) console.log(pyramid) } function repeat(str,number){ let result = "" for(let i=1; i <= number; i++){ result += str } return result } } printPyramid(10) // Unit 4.6 for(let i=1; i <= 9; i++){ for(let j=1; j<=9; j++){ console.log(i + '*' + j + '=' + i*j) } } // Unit 4.7 function findSquareNumber(n,m){ for(let i=n; i <= m; i++){ if(isSquare(i)){ console.log(i) } } function isSquare(n){ let root = Math.floor(Math.sqrt(n)) return root * root === n } } findSquareNumber(1,100) // Unit 4.8 function MultiplicationTable(n,m){ for(let i=1; i<=n; i++){ for(let j=1; j<=m; j++){ console.log(i + '*' + j + '=' + i*j) } } } MultiplicationTable(3,10) function findNarcissistic(n,m){ for(let i=n; i<=m; i++){ let digit = i.toString().length let numbers = i.toString().split('') if(isNarcissistic(i,numbers,digit)){ console.log(i) } } function isNarcissistic(raw, numbers, digit){ let sum = 0 numbers.forEach(d=>{ sum += Number(d) ** digit }) return sum === Number(raw) } } findNarcissistic(3,2000) |
相關文章
★全文分享★ [筆記] ALG101 – 先別急著寫 LeetCode22
![[筆記] ALG101 – 先別急著寫 LeetCode22 [筆記] ALG101 – 先別急著寫 LeetCode22](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
最後一個為有效率的求取最大整數連續和
★全文分享★ [筆記] ALG101 – 先別急著寫 LeetCode21
![[筆記] ALG101 – 先別急著寫 LeetCode21 [筆記] ALG101 – 先別急著寫 LeetCode21](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
先前做過的簡單排序法、搜尋數字,改用
★全文分享★ [筆記] ALG101 – 先別急著寫 LeetCode20
![[筆記] ALG101 – 先別急著寫 LeetCode20 [筆記] ALG101 – 先別急著寫 LeetCode20](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
接著來練習用迴圈的方法來解決陣列最短
★全文分享★ [筆記] ALG101 – 先別急著寫 LeetCode19
![[筆記] ALG101 – 先別急著寫 LeetCode19 [筆記] ALG101 – 先別急著寫 LeetCode19](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
接下來練習搜尋數字和連續整數和,先從
★全文分享★ [筆記] ALG101 – 先別急著寫 LeetCode18
![[筆記] ALG101 – 先別急著寫 LeetCode18 [筆記] ALG101 – 先別急著寫 LeetCode18](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
先求有,再求好。這個部分要稍微帶到演
★全文分享★ [筆記] ALG101 – 先別急著寫 LeetCode17
![[筆記] ALG101 – 先別急著寫 LeetCode17 [筆記] ALG101 – 先別急著寫 LeetCode17](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
接下來的兩題,其核心概念為 N 進位數和
★全文分享★ [筆記] ALG101 – 先別急著寫 LeetCode16
![[筆記] ALG101 – 先別急著寫 LeetCode16 [筆記] ALG101 – 先別急著寫 LeetCode16](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
國中程度競賽題的實作,其實難度本身不
★全文分享★ [筆記] ALG101 – 先別急著寫 LeetCode15
![[筆記] ALG101 – 先別急著寫 LeetCode15 [筆記] ALG101 – 先別急著寫 LeetCode15](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
接著要來進行一些國中程度競賽題的挑戰
★全文分享★ [筆記] ALG101 – 先別急著寫 LeetCode14
![[筆記] ALG101 – 先別急著寫 LeetCode14 [筆記] ALG101 – 先別急著寫 LeetCode14](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
這篇筆記下實作 String.prototype.padEnd, String.p
★全文分享★ [筆記] ALG101 – 先別急著寫 LeetCode13
![[筆記] ALG101 – 先別急著寫 LeetCode13 [筆記] ALG101 – 先別急著寫 LeetCode13](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
這篇筆記下實作 String.prototype.toLowerCase, Str
★全文分享★ [筆記] ALG101 – 先別急著寫 LeetCode12
![[筆記] ALG101 – 先別急著寫 LeetCode12 [筆記] ALG101 – 先別急著寫 LeetCode12](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
這篇筆記下實作 Array.prototype.fill, Array.proto
★全文分享★ [筆記] ALG101 – 先別急著寫 LeetCode11
![[筆記] ALG101 – 先別急著寫 LeetCode11 [筆記] ALG101 – 先別急著寫 LeetCode11](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
更多的練習是必要的,這篇筆記下實作 Arr
★全文分享★ [筆記] ALG101 – 先別急著寫 LeetCode10
![[筆記] ALG101 – 先別急著寫 LeetCode10 [筆記] ALG101 – 先別急著寫 LeetCode10](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
雖說 JavaScript 已經有不少的內建函式,例
★全文分享★ [筆記] ALG101 – 先別急著寫 LeetCode9
![[筆記] ALG101 – 先別急著寫 LeetCode9 [筆記] ALG101 – 先別急著寫 LeetCode9](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
到了經典題目的最後了,來練習求出最近
★全文分享★ [筆記] ALG101 – 先別急著寫 LeetCode8
![[筆記] ALG101 – 先別急著寫 LeetCode8 [筆記] ALG101 – 先別急著寫 LeetCode8](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
繼續更多的練習,來進行加減乘除、迴文
★全文分享★ [筆記] ALG101 – 先別急著寫 LeetCode7
![[筆記] ALG101 – 先別急著寫 LeetCode7 [筆記] ALG101 – 先別急著寫 LeetCode7](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
在盡可能不使用內建函式的情況下,來判
★全文分享★ [筆記] ALG101 – 先別急著寫 LeetCode6
![[筆記] ALG101 – 先別急著寫 LeetCode6 [筆記] ALG101 – 先別急著寫 LeetCode6](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
經歷過前幾個單元的準備,總算是要進入
★全文分享★ [筆記] ALG101 – 先別急著寫 LeetCode4
![[筆記] ALG101 – 先別急著寫 LeetCode4 [筆記] ALG101 – 先別急著寫 LeetCode4](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
在實際解決題目之前,要先理解平台修改
★全文分享★ [筆記] ALG101 – 先別急著寫 LeetCode3
![[筆記] ALG101 – 先別急著寫 LeetCode3 [筆記] ALG101 – 先別急著寫 LeetCode3](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
講述空間、時間和範圍型態的限制外,以
★全文分享★ [筆記] ALG101 – 先別急著寫 LeetCode2
![[筆記] ALG101 – 先別急著寫 LeetCode2 [筆記] ALG101 – 先別急著寫 LeetCode2](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
除了撰寫虛擬碼外,另外一件重要的環節
★全文分享★ [筆記] Lidemy 鋰學院 – 先別急著寫 LeetCode – 1
![[筆記] Lidemy 鋰學院 – 先別急著寫 LeetCode – 1 [筆記] Lidemy 鋰學院 – 先別急著寫 LeetCode – 1](https://smlpoints.com/wp-content/uploads/notes-lidemy-command-line-beginner-class-1.jpg)
近年來想要進行程式人員相關的面試前,
