陣列(Array)
陣列(Array)介紹數據排成一列,儲存在連續的記憶體位置,在大多數的演算法中都經常會使用到,可以把它想像成停車場,每一輛車就是資料,停車格的號碼就是在陣列中的位置。
以下為陣列特性:
連續的記憶體位置
儲存相同類型的資料
擁有緩存局部性,能夠提高性能
隨機訪問速度快
插入刪除花費成本較大
(圖片來自於geeksforgeeks)
在陣列當中就包含元素和索引,如下圖所示
元素Element - 在陣列當中的每一個位置的項目。
索引Index - 陣列中元素的每個內存位置都由數字索引標識。
陣列 vs. 鏈結串列
存取
插入
刪除
陣列Array
快
慢
慢
鏈結串列Linked list
慢
快
快
複雜度時間複雜度
Action動作
平均
最壞
訪問(Access)
$O(1)$
$O(1)$
搜尋(Search)
$O(n)$
$O(n)$
插入(Insertion)
$O(n)$
$O(n)$
刪除(Deletion)
$O(n)$
$O(n)$
空間複雜度$O(n)$
基本操作以下為陣列常見操作
遍歷: ...
586. Customer Placing the Largest Number of Orders
586. Customer Placing the Largest Number of Orders(資訊來自於leetcode 586 Customer Placing the Largest Number of Orders)
下單數量最多的客戶
編寫一個 SQL 查詢來查找下單數量最多的客戶的 customer_number。
生成測試用例,以便恰好一個客戶下的訂單比任何其他客戶都多。
題目:編號為 3 的客戶有兩個訂單,大於客戶 1 或 2,因為他們每個人只有一個訂單。所以結果是 customer_number 3。
解題方式:select top 1 customer_numberfrom ordersgroup by customer_numberorder by count(order_number)desc
解題解析:用TOP 1來取代MAX()方法,再用COUNT() 函式與 GROUP BY 一起使用,而COUNT() 函式與 GROUP BY可以用下列案例來做舉例。
案例 1:演出過大多數 PG 電影的演員
COUNT() 函式本身可以告訴我們有多少演員在 PG ...
1407. Top Travellers
1407. Top TravellersFix Names in a Table
透過leetcode 1407Top Travellers來練習
使用table
id 是該表的主鍵。name 是用戶的名字。
id 是該表的主鍵。user_id 是經過距離“距離”的用戶的 id。
題目說明:編寫 SQL 查詢以報告每個使用者行駛的距離。
返回按降序排序的結果表,如果兩個或更多用戶行進的距離相同,則按其升序對它們進行排序。travelled_distancename
查詢結果格式位於以下範例中。
解題:以下為第一個解題方法
將兩個資料表join起來再 sum(isnull(R.distance,0))group by 時 M.id 也必須加入條件內,因為有可能出現不同id name相同的資料
SELECT M.name as name,sum(isnull(R.distance,0)) as travelled_distance FROM Users Mleft outer join Rides R on R.user_id = M.id group by M.id, M.name ...
深度優先搜尋DFS
深度優先搜尋DFS介紹節點選項是用 後進先出(LIFO) 方式進行管理,所以可以使用堆疊的資料結構
深度優先搜尋(DFS)演算法是一種遍歷圖形或樹形結構的方法,它會從起點開始遍歷,並儘可能深入每一個節點,直到找到目標節點或已經訪問過所有節點。在搜索過程中,DFS 會使用一個堆疊(Stack)來存儲所有還未訪問的節點。當一個節點被訪問時,它會被標記為已訪問,並將其相鄰的未訪問節點推入堆疊中,繼續遍歷直到找到目標節點或堆疊被清空。DFS 演算法容易實現,但可能會陷入無限循環或遍歷過多節點。
虛擬碼以下是一個基本的深度優先搜尋演算法的範本:
function DFS(G, s) { const stack = []; stack.push(s); while (stack.length > 0) { const v = stack.pop(); if (!G[v].visited) { G[v].visited = true; for (let u of G[v] ...
1890. The Latest Login in 2020
1890. The Latest Login in 2020(資訊來自於leetcode 1890 The Latest Login in 2020)
2020年最新登錄編寫一個 SQL 查詢來報告一年中所有用戶的最新2020登錄。不包括未登錄的用戶2020。
解題方式:select user_id,max(time_stamp) last_stampfrom Loginswhere year(time_stamp) = 2020group by user_id
解題解析:用戶 6 登錄了 3 次,但在 2020 年只登錄了一次,所以我們將此登錄包含在結果表中。用戶 8 在 2020 年兩次登錄他們的帳戶,一次在 2 月,一次在 12 月。我們在結果表中只包括最新的一個(12 月)。用戶 2 登錄他們的帳戶 2 次,但在 2020 年僅登錄一次,因此我們將此登錄包含在結果表中。用戶 14 在 2020 年沒有登錄,所以我們沒有將他們包含在結果表中。在抓取年度時也可以使用YEAR()函式,YEAR()函式只能使用date函數值,他可以方便我們抓出date函數的年度值。(資訊來自於 SQL ...
MySQL Test
MySQL TestMySQLQ1. When you have a subquery inside of the main query, which query is executed first?
[ ] The subquery is never executed. Only the main query is executed.
[ ] They are executed at the same time
[ ] the main query
[x] the subquery
Q2. You need to export the entire database, including the database objects, in addition to the data. Which command-line tool do you use?
[ ] mysqlexport
[ ] mysqladmin
[x] mysqldump
[ ] mysqld
Q3. You must ensure the accuracy and reliability of the data ...
Python Test
Python TestPython (Programming Language)Q1. What is an abstract class?
[ ] An abstract class is the name for any class from which you can instantiate an object.
[ ] Abstract classes must be redefined any time an object is instantiated from them.
[ ] Abstract classes must inherit from concrete classes.
[x] An abstract class exists only so that other “concrete” classes can inherit from the abstract class.
reference
Q2. What happens when you use the build-in function any() on a list?
[ ] The any() ...
627. Swap Salary
627. Swap Salary(資訊來自於leetcode 627 Swap Salary)
交換工資
這是一個叫做Salary的主Table,裡面的資料欄位有【id】,【name】,【sex】,【salary】id 是該表的主鍵列。性別列是(’m’,’f’)類型的 ENUM 值。該表包含有關員工的信息
題目:編寫一個 SQL 查詢以使用單個更新語句交換所有’f’和’m’值(即,將所有’f’值更改為’m’,反之亦然),並且沒有中間臨時表。
請注意,您必須編寫單個更新語句,不要為此問題編寫任何選擇語句。
查詢結果格式如下例所示。
資料庫內容為下圖
而我們希望出來的結果為下面這樣
把性別原本是’m’的改變成’f’。
解題方式: 第一種解題方式MS SQL Server update Salary set sex = case when sex = 'm' then 'f' else 'm' end第二種解題方式MySQL update Salary set sex = if(sex='m', ...
OOP Test
OOP TestObject-Oriented Programming (OOP)Q1. What is an example of dynamic binding?
[ ] any method
[ ] method overloading
[x] method overriding
[ ] compiling
Q2. For which case would the use of a static attribute be appropriate?
[ ] the number of people in each house in a small neighborhood
[ ] the lot size for each house in a small neighborhood
[ ] the color of each house in a small neighborhood
[x] the weather conditions for each house in a small neighborhood
Q3. 1 Why would you create an ab ...
快速排序(Quick Sort)
快速排序(Quick Sort)介紹
快速排序是一種分治算法(Divide and Conquer),它將原問題劃分為兩個子問題,一個是比基準值小的數,另一個是比基準值大的數。然後遞歸地解決這兩個子問題,最終得到排序後的結果。
(圖片來自data_structures_algorithms)
虛擬碼運用了分治法的概念,將大的數值移到左邊,小的數值移到右邊,再遞迴運算左右兩側的數值,直到最終整個數列有序
QUICK-SORT(p, r): if p < r: q = partition(p, r) QUICK-SORT(p, q - 1) QUICK-SORT(q + 1, r)// when calling QUICK-SORT first time, just pass 0 and array.length - 1 as parameters
基準值(pivot)並進行數值交換,使得數列的左半部分都小於等於基準值,右半部分都大於等於基準值。最後返回基準值的位置。
partition(p, r): x = A[r] // ...



