7771 分鐘

# 思路 如果當前node與下一個node相同,將當前node接到下下一個node # 參考程式碼 /** * 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,
4321 分鐘

# 思路 動態規劃 dp[0]與dp[1]為1 之後dp[i]的方法數為dp[i - 1] + dp[i - 2] # 參考程式碼 static auto fast_io = [] { ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); return 0; }(); class Solution { public: int climbStairs(int n) { int dp[n + 1];
2281 分鐘

# 思路 開根號 # 參考程式碼 static auto fast_io = [] { ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); return 0; }(); class Solution { public: int mySqrt(int x) { return sqrt(x); } };
7771 分鐘

# 思路 以另一個變數ret儲存a + b的值,並處理進位 # 參考程式碼 static auto fast_io = [] { ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); return 0; }(); class Solution { public: string addBinary(string a, string b) { reverse(a.begin(), a.end()); reve
6871 分鐘

# 思路 將vector最後+1 需要一路往前處理進位 # 參考程式碼 static auto fast_io = [] { ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); return 0; }(); class Solution { public: vector<int> plusOne(vector<int>& digits) { ++digits[digits.si
3541 分鐘

# 思路 以stringstream拆分字串 # 參考程式碼 static auto fast_io = [] { ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); return 0; }(); class Solution { public: int lengthOfLastWord(string s) { string word; stringstream ss(s); w
4901 分鐘

# 思路 以cur紀錄目前遍歷的最大值 以ret紀錄全部的最大值 # 參考程式碼 static auto fast_io = [] { ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); return 0; }(); class Solution { public: int maxSubArray(vector<int>& nums) { int ret = nums[0];
3651 分鐘

# 思路 直接調用lower_bound找到位置,再減去nums.begin()得到位置的整數 # 參考程式碼 static auto fast_io = [] { ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); return 0; }(); class Solution { public: int searchInsert(vector<int>& nums, int target) {
6051 分鐘

# 思路 先記錄haystack與needle的size 從0開始依序比對haystack 的 sub string與needle # 參考程式碼 static auto fast_io = [] { ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); return 0; }(); class Solution { public: int strStr(string haystack, string needle) {
4561 分鐘

# 思路 以i紀錄位置,依序遍歷 # 參考程式碼 static auto fast_io = [] { ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); return 0; }(); class Solution { public: int removeElement(vector<int>& nums, int val) { if (!nums.size()) return 0;