[Day12] Priority Queue in Java

思路 大->小 or 小->大排列 實作 測試方法 import java.util.Comparator; // 比較器 import java.util.PriorityQueue; // priority queue public class Priority_Queue2 { public static void main(String[] args) { // default Integer[] nums = new Integer[]{4,3,1,5,2}; PriorityQueue<Integer> pq = new PriorityQueue<>(); for (int i = 0 ; i < nums.length; i++) { pq.add(nums[i]); } while (true) { if (pq.size() == 0) break; // 跑完無內容 Integer num_min = pq.poll(); // 取出最小值 by default System....

August 14, 2022 · 2 min · Yish

[Day11] FIFO: Queue 先進先出(LinkedList) in Java

思路 先進先出 紀錄開始與結尾元素 實作 測試方法 public class Queue_List2 { static class Node { Integer val; Node next; public Node(Integer val) { this.val = val; } } Integer[] nums; Node node_front = null; Node node_end = null; public Queue_List2(Integer[] nums) { this.nums = nums; } public void build_queue(){ for (int i = 0 ; i < nums.length; i++) { offer(nums[i]); } } public static void main(String[] args) { Integer[] nums = new Integer[]{1,2,3,4,5}; Queue_List2 queue_list = new Queue_List2(nums); queue_list....

August 13, 2022 · 1 min · Yish

[Day10] FIFO: Queue 先進先出(Array) in Java

思路 先進先出 紀錄開始與結尾元素 實作 測試方法 public class Queue_Array3 { Integer[] ary; Integer[] queue; Integer i_front = null; Integer i_end = null; public Queue_Array3(Integer[] ary){ this.ary = ary; } public void build_queue(){ this.queue = new Integer[this.ary.length]; for (int i = 0 ; i < ary.length; i++) { offer(ary[i]); } } public static void main(String[] args) { Integer[] nums = new Integer[]{1,2,3,4,5}; Queue_Array3 queue_array = new Queue_Array3(nums); queue_array.build_queue(); // full - extend the storage size queue_array....

August 13, 2022 · 2 min · Yish

Support Enum Type With Sqlite in Laravel Migration

The reference of this article is here. Recently, I have needed to upgrade the old version of laravel project, but in the migrations, I found that the enum type is not supported with sqlite in unit testing. Fortunately, I found the solution, here is the code: class UpdateUserStatus extends Migration { // if it doesn't set the type in sqlite, caused error; // [Doctrine\DBAL\DBALException] // Unknown database type enum requested, // Doctrine\DBAL\Platforms\MySqlPlatform may not support it....

August 12, 2022 · 1 min · Yish

[Day9] LIFO: Stack 後進先出(LinkedList) in Java

思路 先進後出 紀錄最上面的元素 實作 測試方法 public class Stack_List2 { // 配置節點 static class Node { Integer val; Node next; public Node(Integer val) { this.val = val; } } public static void main(String[] args) { Integer[] nums = new Integer[]{1,2,3,4,5}; Stack_List2 stack_list = new Stack_List2(nums); stack_list.build_stack(); // full - extend the storage size stack_list.push(6); Integer num = stack_list.pop(); num = stack_list.pop(); num = stack_list.pop(); num = stack_list.pop(); num = stack_list....

August 12, 2022 · 1 min · Yish

[Day8] LIFO: Stack 後進先出(Array) in Java

思路 先進後出 紀錄最上面的元素 實作 測試方法 public class Stack_Array2 { Integer[] ary; // 傳入陣列 Integer[] stack; Integer i_top = null; // LIFO 後進先出 set top public Stack_Array2(Integer[] ary) { this.ary = ary; } public void build_stack(){ this.stack = new Integer[this.ary.length]; for (int i = 0; i < this.ary.length; i++) { push(ary[i]); } } public static void main(String[] args) { Integer[] nums = new Integer[]{1,2,3,4,5}; Stack_Array2 stack_array = new Stack_Array2(nums); stack_array....

August 10, 2022 · 2 min · Yish