633 1 分鐘

# 思路 使用遞迴 # 參考程式碼 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next)...
483 1 分鐘

# 思路 使用 map 找出對應的括號 以 stack 儲存括號,對應則刪除 # 參考程式碼 static auto fast_io = []{ ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); return 0;}();class Solution {public: bool isValid(string s) { if (s.size() % 2) return false; unordered_map<char,...
414 1 分鐘

# 思路 將 vector 進行排序,頭尾必然為差最多的 string 將頭尾的共同 sub string 找出 # 參考程式碼 static auto fast_io = []{ ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); return 0;}();class Solution {public: string longestCommonPrefix(vector<string>& strs) {...
388 1 分鐘

# 思路 使用 map 儲存各符號的值 將符號的值加總,若當前符號大於前一個符號,則代表相減 # 參考程式碼 static auto fast_io = []{ ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); return 0;}();class Solution {public: int romanToInt(string s) { int ret = 0; char tmp = 'M';...
273 1 分鐘

# 思路 將 x 顛倒,再判斷是否與原數相同 # 參考程式碼 static auto fast_io = []{ ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); return 0;}();class Solution {public: bool isPalindrome(int x) { if (x < 0) return false; long x1 = x, x2 = 0; for (; x; x /= 10) x2 =...
432 1 分鐘

# 思路 使用 map 紀錄出現過的 nums 判斷 map 中是否有 target - nums[i] ,有則直接 return # 參考程式碼 static auto fast_io = []{ ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); return 0;}();class Solution {public: vector<int> twoSum(vector<int>& nums, int...
6.7k 6 分鐘

# A=B is a coding game that has only one instruction A=B on Steam 點此查看中文版本 2022.4.10 update chapter 6 2022.4.8 update chapter 5 2022.4.4 update chapter 4 2022.4.2 update chapter 3 2022.4.1 update chapter 1, 2 # Chapter 1 A=B instruction explanation string1 = string2 replace string2 with string1 #...
1.7k 2 分鐘

# 題目: UVa 11792 - Krochanska is Here # 題目說明 給 m 條路線,其中有重複的車站稱為 重點車站 ,求哪一個 重點車站 到其他所有 重點車站 的距離最短 (題目中文翻譯可參考: 天然呆 翻譯 UVa 11792 Krochanska is Here!) INPUT: 第一行輸入一個整數 t ,代表測資數 每筆測資先輸入兩個整數 n 、 m , n 為車站數、 m 為路線數 接下來有 m 行,每行輸入數個整數,代表此路線的車站,以 0 當作結尾 OUTPUT: 輸出哪一個 重點車站 到其他所有 重點車站 的距離最短 # 解題方法 此題為...
1.1k 1 分鐘

# 題目: UVa 10800 - Not That Kind of Graph # 題目說明 給一個由 R 、 F 、 C 組成的字串,使用 / 、 \ 、 _ 畫出圖表 R : 上升一格 F : 下降一格 C : 維持同一格 INPUT: 輸入一個整數 T ,代表測資數 每筆測資輸入一個字串 OUTPUT: 使用 / 、 \ 、 _ 畫出的圖表 # 解題方法 先計算此圖表的最大高度,存入變數 len 宣告一個二維陣列,將中點設為 len ,上下各擁有 len 個空間,長度則是輸入的字串長度 + 1 接著處理輸入字串的符號,在對應位置填入 / 、 \ 、 _ 最後再按照格式輸出 #...
659 1 分鐘

# 題目: UVa 11078 - Open Credit System # 題目說明 給 N 個學生的分數,學生的排序為較年長至較年輕,求較年長的學生最多比較年輕的學生多幾分 INPUT: 輸入一個整數 T ,代表測資數 每筆測資先輸入一個整數 N ,代表學生的數量 接下來有 N 個整數,代表學生的成績 (排愈前面代表學生愈年長) OUTPUT: 輸出較年長的學生最多比較年輕的學生多幾分 # 解題方法 由較年長至較年輕遍歷學生的成績,使用一個變數 Max 紀錄目前最高分,再依序比出差最多的值 # 參考程式碼 #include <iostream>#include...