發表文章

[Linux]Configuring an SSH login without password

  STEP1. 首先產生SSH金鑰 ssh-keygen STEP2. 詢問金鑰要存放在哪個目錄下,空白就是預設目錄 Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/shock_hsu/.ssh/id_rsa): STEP3. 詢問要步要設定passphrase,空白就是不設定 STEP4. 把公鑰放到伺服器上,有兩種做法 第一種,直接將公鑰複製到指定伺服器上的 authorized_keys 檔案中 echo "public key string" >> ~/.ssh/authorized_keys 第二種,使用 ssh-copy-id ssh-copy-id [username]@[host_server]

[Linux]清除系統上被佔用的Port

 查詢被佔用port的PID sudo netstat -lpn |grep 8400 清除PID kill -9 28907

[Linux]使用Ansible自動化管理

圖片
Ansible可實現自動化管理遠端系統,主要組成環境有: Control node - A system on which Ansible is installed. Managed node - A remote system, or host. Inventory - A list of managed nodes that are logically organized. Control Node 根據Ansible( 官方文件 )進行安裝,這邊Control Node用的OS是Ubuntu。 $ sudo apt install ansible 檢查安裝結果 Managed node Managed node準備建立虛擬機並安裝CentOS/7。 Inventory  接著可以開始撰寫腳本,假設要安裝指定packages,腳本架構大致如下: aio.yaml inventory.ini roles |_install_packages |_tasks |_main.yml 編輯aio.yaml: hosts - 決定要控制哪些host主機 roles - 引入相關檔案、變數、任務等等,並提升可用性 - hosts: all roles: - role: install_packages 編輯/roles/install_packages/tasks/main.yml --- # tasks file for install_packages - name: Install epel-release package: name: - epel-release - name: Install packages package: name: - net-tools - vim - git - lsof - python3 - python3-libs - python3-devel 編輯inventory.ini: ansible_host - 編輯Managed host IP address ansible_port - 編輯Managed host port ansible...

[Linux]Ubuntu在Hyper-V上設定螢幕解析度

 開啟 terminal,輸入下面指令 sudo vim /etc/default/grub 更改這邊的設定 GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" 更改後如下 GRUB_CMDLINE_LINUX_DEFAULT="quiet splash video=hyperv_fb:1920x1080" 執行 update-grub sudo update-grub 重啟 VM reboot

[LeetCode]Search Insert Position

 Search Insert Position 給一個排序且沒有重複的整數陣列,尋找目標值在陣列哪個位置,如果沒有結果則回傳該整數應該在的位置。 Example 1 Input: nums = [1,3,5,6], target = 5 Output: 2 Example 2 Input: nums = [1,3,5,6], target = 2 Output: 1 Example 3 Input: nums = [1,3,5,6], target = 7 Output: 4 解法 class Solution { /** * @param Integer[] $nums * @param Integer $target * @return Integer */ function searchInsert($nums, $target) { $count = count($nums); $data = 0; for ($i = 0; $i < $count; $i++) { if ($target == $nums[$i]) { $data = $i; break; } if ($target > $nums[$i]) { $data = $i + 1; } } return $data; } }

[Linux]如何使用rsync

Linux 的 rsync 可以同步、備份檔案與目錄,他的動作跟 cp 類似,但其中的差別在於, rsync 在執行第二次會跳過重複的檔案,只針對異動過檔案進行複製。 常見參數: -v:verbose 模式,輸出比較詳細的訊息。 -r:遞迴(recursive)備份所有子目錄下的目錄與檔案。 -a:封裝備份模式,相當於 -rlptgoD,遞迴備份所有子目錄下的目錄與檔案,保留連結檔、檔案的擁有者、群組、權限以及時間戳記。 -z:啟用壓縮。 -h:將數字以比較容易閱讀的格式輸出。 rsync -avh /sites /mnt/mls 參考網址 : https://zh.wikipedia.org/wiki/Rsync https://blog.gtwang.org/linux/rsync-local-remote-file-synchronization-commands/  

win10的outlook無法搜尋到最新的郵件

圖片
User反應他的Outlook可以搜尋到昨天的信件,但是沒辦法搜尋今天最新的信,檢查發現他的作業系統為 Windows 10 專業版 20H1,網路爬文發現原來是 KB5008212 這在搞,因此先執行系統更新看看,更新後作業系統為 Windows Update 至最新版本為 20H2,搜尋後 KB5008212 也更新後移除了。 如果更新作業系統無法解決,可以參考下面文章的解決方式。 Outlook 搜尋無法顯示在 Windows Update KB5008212 之後的最新電子郵件

[LeetCode]Remove Duplicates from Sorted Array

Remove Duplicates from Sorted Array 把有一排序過的整數陣列,移除重複元素後,計算該陣列有多少唯一值。這題運用的概念是 In-place algorithm 原地演算法或是就地演算法,利用本身的資料結構進行變換的演算法。 Example 1 Input: nums = [1,1,2] Output: 2, nums = [1,2,_] Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively. It does not matter what you leave beyond the returned k (hence they are underscores). Example 2 Input: nums = [0,0,1,1,1,2,2,3,3,4] Output: 5, nums = [0,1,2,3,4,_,_,_,_,_] Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively. It does not matter what you leave beyond the returned k (hence they are underscores). 解法 用 PHP 內建函數 array_unique() 移除陣列中的重複的值。 class Solution { /** * @param Integer[] $nums * @return Integer */ function removeDuplicates(&$nums) { $nums = array_unique($nums); return count($nums); } } 用原地演算法,判斷是否與上個元素是否相同,如果相同則移除該元素。 class Solution { /** ...

[LeetCode]Remove Element

 Remove Element 設定一個只有整數的陣列,並移除指定的整數,最後返回該陣列剩幾個元素。 Example 1 Input: nums = [3,2,2,3], val = 3 Output: 2, nums = [2,2,_,_] Example 2 Input: nums = [0,1,2,2,3,0,4,2], val = 2 Output: 5, nums = [0,1,4,0,3,_,_,_] 解法 class Solution { /** * @param Integer[] $nums * @param Integer $val * @return Integer */ function removeElement(&$nums, $val) { $count = count($nums); for ($i = 0; $i < $count; $i++) { if ($nums[$i] === $val) unset($nums[$i]); } return count($nums); } }

[LeetCode]Merge Two Sorted Lists

圖片
Merge Two Sorted Lists 合併兩個已排序的鍊表 Example 1 Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4] Example 2 Input: list1 = [], list2 = [] Output: [] Example 3 Input: list1 = [], list2 = [0] Output: [0] 解法 一開始的想法是取出兩個表的值進行排序,最後再重新產出新的鏈表。 /** * Definition for a singly-linked list. * class ListNode { * public $val = 0; * public $next = null; * function __construct($val = 0, $next = null) { * $this->val = $val; * $this->next = $next; * } * } */ class Solution { /** * @param ListNode $list1 * @param ListNode $list2 * @return ListNode */ function mergeTwoLists($list1, $list2) { $arr_dic = array(); while (true) { # 取出兩陣列的值 if (isset($list1->val)) $arr_dic[] = $list1->val; if (isset($list2->val)) $arr_dic[] = $list2->val; $list1 = $list1->next; $list2 = $list2->next; ...

[Linux]命令歷史

圖片
history:查詢歷史記錄 history -c:清空歷史命令 history N:顯示歷史中最近的N條命令 history -d:刪除某條歷史 history -a:將當前終端新執行的命令寫入歷史記錄文件(文件由變數HISTFILE決定,預設為用戶家目錄下的.bash_history) history -w /路徑/文件名.txt:將歷史記錄寫入指定文件 history -r:讀取歷史文件追加到歷史列表 !N:重覆執行第N條歷史 !!:重覆執行上一條命令

[LeetCode]Valid Parentheses

圖片
Valid Parentheses 檢查字串當中,是否包含有效的括號 ()、[]、{}。其有效定義為 括號的頭尾必須是相同類型的括號 括號的頭尾的順序必須正確 Example 1 Input: s = "()" Output: true Example 2 Input: s = "()[]{}" Output: true Example 3 Input: s = "(]" Output: false 解法 可用正規式進行比對,並把匹配的括號移除,反覆執行直到再也找不到對應的括號。但執行效率大多落在60-70ms,明顯不是最佳解。 class Solution { /** * @param String $s * @return Boolean */ function isValid($s) { if ( preg_match('/\(\)/', $s, $matches) || preg_match('/\[\]/', $s, $matches) || preg_match('/\{\}/', $s, $matches) ) { if (preg_match('/\(\)/', $s, $matches)) $s = str_replace('()', '', $s); if (preg_match('/\[\]/', $s, $matches)) $s = str_replace('[]', '', $s); if (preg_match('/\{\}/', $s, $matches)) $s = str_replace('{}', '', $s); if ...

[LeetCode]Longest Common Prefix

圖片
Longest Common Prefix 在一陣列中,找出所有字串共同的字首且長度最長的字首,如果沒有匹配的結果回傳空值。 Example 1 Input: strs = ["flower","flow","flight"] Output: "fl" Example 2 Input: strs = ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings. 解法 先找出陣列中最短的字串,並利用該字串一一去比對,只要符合是共同字首且長度最長的字首變停止比對,反之回傳空值。 class Solution { /** * @param String[] $strs * @return String */ function longestCommonPrefix($strs) { // 找出陣列中長度最小的字串 $min_len = strlen($strs[0]); $now_len = strlen($strs[0]); $min_len_str = $strs[0]; foreach ($strs as $i => $str) { $now_len = strlen($str); if ($min_len > $now_len) { $min_len = $now_len; $min_len_str = $str; } } // 一一比對陣列中的字串 for ($i = $min_len; $i > 0; $i--) { $count = 0; foreach ($strs as $...

[Linux]查詢硬碟使用量

圖片
 檢查硬碟使用量 df df -h

[LeetCode]Roman to Integer

Roman to Integer 羅馬數字(Roman Numbers)共有7個 I, V, X, L, C, D, M。 Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 舉例來說羅馬數字的 II 表示 2、12 -> XII、27 -> XXVII。但往右加的數字不會超過三位,因此產生右加左減的計算方式: 在較大的羅馬數字的右邊記上較小的羅馬數字,表示大數字加小數字 在較大的羅馬數字的左邊記上較小的羅馬數字,表示大數字減小數字 IV -> 4 IX -> 9 XL -> 40 XC -> 90 CD -> 400 CM -> 900 Example 1 Input: s = "III" Output: 3 Explanation: III = 3. Example 2 Input: s = "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3. Example 3 Input: s = "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. 解法 class Solution { /** * @param String $s * @return Integer */ function romanToInt($s) { $arr_list = array( 'I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100,...

[LeetCode]Palindrome Number

Palindrome Number 給一整數 x ,如果反轉該數字一樣的話,則回傳 true。 Example 1 Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left. Example 2 Input: x = -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3 Input: x = 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. 解法 Palindrome 回文,也就是正讀反讀都能讀通,比如 12321 從前面看或後面看都是一樣的數字,但 -123321 的回文是 12321- ,所以負整數無法符合回文的條件。 將原始整數利用除以 10 取出餘數,加總該餘數及位數,直到原始整數不能再除為止。 class Solution { /** * @param Integer $x * @return Boolean */ function isPalindrome($x) { $temp_x = $x; $new_x = 0; while ($temp_x != null) { $d = $temp_x % 10; $new_x = $new_x * 10 + $d; $temp_x = intval($temp_x / 10); } if ($new_x == $x && $new_x >= 0) return true; else return ...

[Linux]mount 掛載基本指令

查看目前有幾個磁區 root@ml01:~# fdisk -l Disk /dev/sdb: 500.1 GB, 500107862016 bytes 255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00007967 Device Boot Start End Blocks Id System Disk /dev/sda: 500.1 GB, 500107862016 bytes 255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000b2d66 Device Boot Start End Blocks Id System /dev/sda1 * 2048 391167 194560 83 Linux /dev/sda2 391168 195702783 97655808 83 Linux /dev/sda3 195702784 824848383 314572800 83 Linux Disk /dev/sdd: 2000.4 GB, 2000398934016 bytes 255 heads, 63 sectors/track, 243201...

[LeetCode]Add Two Numbers

Add Two Numbers 相加兩個非負整數的鏈表,計算加總後的數字。 Example 1 Input: l1 = [2,4,3], l2 = [5,6,4] Output: [7,0,8] Explanation: 342 + 465 = 807. Example 2 Input: l1 = [0], l2 = [0] Output: [0] Example 3 Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] Output: [8,9,9,9,0,0,0,1] 解法 PHP /** * Definition for a singly-linked list. * class ListNode { * public $val = 0; * public $next = null; * function __construct($val = 0, $next = null) { * $this->val = $val; * $this->next = $next; * } * } */ class Solution { /** * @param ListNode $l1 * @param ListNode $l2 * @return ListNode */ function addTwoNumbers($l1, $l2) { $sum = 0; $carry = 0; $arr_sum = []; while ($l1 != '' || $l2 != '') { $sum = $l1->val + $l2->val + $carry; if ($sum >= 10) { $carry = intval($sum / 10); $sum = $sum % 10; } else { $...

[LeetCode]Two Sum

Two Sum  給一串整數陣列為 nums 和一個整數 target ,需要回傳兩個加總起來等於 target 的兩個索引值。 輸入的陣列一定只會有一個解,且索引值不能重複使用。 Example 1 Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. Example 2 Input: nums = [3,2,4], target = 6 Output: [1,2] Example 3 Input: nums = [3,3], target = 6 Output: [0,1] 解法 暴力解法 利用兩層for迴圈進行計算,以 [2, 7, 11, 15] 為例,(2, 7)、(2, 11)、(2, 15)、(7, 11)...以此類推,當加總等於目標值即回傳結果。 function twoSum($nums, $target) { for ($i = 0; $i < count($nums); $i++) { for ($j = $i + 1; $j < count($nums); $j++) { if ($nums[$i] + $nums[$j] == $target) return [$i, $j]; } } } 雜湊解法 將目標值與當前數值相減後比對雜湊表,並把當前的 value-key 存放到雜湊表, 重複此操作直到找到結果。 nums = (11, 7, 2, 15) target = 9 current_key: 0, current_num: 11, remaining: -2 map: {11:0}, return: null current_key: 1, current_num: 7, remaining: 2 map: {11:0, 7:1}, return: null current_key: 2, current_num: 2, remaining: 7 map: {11:0, 7:1}, return: [2, 1] PHP f...

Laravel 部署到 Heroku

圖片
建立新專案,可以設定 laravel 的版本 composer create-project --prefer-dist laravel/laravel blog "5.4.*" 定義 Procfile web: vendor/bin/heroku-php-apache2 public/ 部署至 Heroku heroku create 新增、初始 Repository git init; git add .; git commit -am 'Set git initialize'; git add remote origin <heroku git remote>; Open app 出現錯誤訊息 原因出在缺少應用程式的密鑰,找到 .env 中的 APP_KEY,打開 Heroku dashboard 新增變數 APP_KEY=value (check your .env file for this) 如果要開啟 debug 功能 APP_DEBUG=true 重新整理畫面就可以看到大大的 Laravel How to deploy a Laravel App into Heroku Hosting Your Laravel App on Heroku