4141 分鐘

# 思路 以變數i控制儲存位置,直接覆蓋重複的值 # 參考程式碼 static auto fast_io = [] { ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); return 0; }(); class Solution { public: int removeDuplicates(vector<int>& nums) { int i = 0; for (i
8171 分鐘

# 思路 使用遞迴 # 參考程式碼 /** * 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(n
5841 分鐘

# 思路 使用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; u
4871 分鐘

# 思路 將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) {
6101 分鐘

# 思路 使用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 &#x
3791 分鐘

# 思路 將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 =
5301 分鐘

# 思路 使用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 target
10k9 分鐘

# 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 s
2.2k2 分鐘

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

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