發表文章

目前顯示的是 2月, 2022的文章

[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 funct