avatar
文章
325
標籤
86
分類
15

首頁
文章
標籤
分類
關於
Larry's notes
搜尋
首頁
文章
標籤
分類
關於
LeetCode - 7 解題紀錄
發表於2020-08-03|LeetCode
題目: LeetCode - 7. Reverse Integer 題目說明給一個整數,將此整數反轉。 解題思路從最後面一位一位拿出來即可,需要特別注意若是過程中超過 int 的界限就直接回傳 0。 參考解法1234567891011121314class Solution {public: int reverse(int x) { int ans = 0, max = INT_MAX / 10, min = INT_MIN / 10; while(x != 0) { if(ans > max || ans < min) return 0; ans = ans * 10 + x % 10; x /= 10; } return ans; }};
LeetCode - 705 解題紀錄 / August LeetCoding Challenge Day 2
發表於2020-08-02|LeetCode
題目: LeetCode - 705. Design HashSet 題目說明設計一個 Hash_set 的 Class。 解題思路使用 Unordered_set 實作即可。 參考解法12345678910111213141516171819202122// fast IOstatic auto __ = [](){ std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0;}();class MyHashSet {public: /** Initialize your data structure here. */ MyHashSet() {} void add(int key) { hs.insert(key); } void remove(int key) { hs.erase(key); } ...
LeetCode - 3 解題紀錄
發表於2020-08-01|LeetCode
題目: LeetCode - 3. Longest Substring Without Repeating Characters 題目說明給一個 String,找出這個 String 裡面最長的不重複子陣列。 解題思路定義一個陣列紀錄每個英文字母最後出現的位置,使用一個迴圈,j 為子陣列的右端點,i 為子陣列的左端點,每一次 i 都更新為 max(i, v[s[j] + 1]) 這樣就可以確保子陣列中沒有重複的元素,此時子陣列的長度為 j - i + 1,最後更新 maxL 即可。 參考解法12345678910111213141516171819202122// fast IOstatic auto __ = [](){ std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0;}();class Solution {public: int lengthOfLongestSubstring( ...
LeetCode - 72 解題紀錄
發表於2020-08-01|LeetCode
題目: LeetCode - 72. Edit Distance 題目說明給兩個 String word1、word2,求最少的操作數使得 word1 等於 word2。操作有以下三種: 插入 刪除 取代 解題思路動態規劃,核心想法為,目前這兩個字是否相等?若相等則表示不需要操作 dp[i][j] = dp[i - 1][j - 1],若不相等則 dp[i][j] 為 dp[i - 1][j]、dp[i][j - 1]、dp[i - 1][j - 1] 三者最小值加一。需要注意的是,邊界需要初始化。 參考解法1234567891011121314151617181920212223242526// fast IOstatic auto __ = [](){ std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0;}();class Solution {public: int minDista ...
LeetCode - 520 解題紀錄 / August LeetCoding Challenge Day 1
發表於2020-08-01|LeetCode
題目: LeetCode - 520. Detect Capital 題目說明給一個 String 代表一個單字,求單字是否符合以下條件: 所有字母都為大寫 所有字母都為小寫 只有第一個字母為大寫 解題思路紀錄大寫字母的數量及最後一個大寫字母的 index,最後再判斷即可。 參考解法123456789101112class Solution {public: bool detectCapitalUse(string word) { int index, count = 0, n = word.size(); for(int i = 0; i < n; ++i) if(word[i] >= 'A' && word[i] <= 'Z') ++count, index = i; if(!count || (count == 1 && index == 0) || count = ...
LeetCode - 338 解題紀錄
發表於2020-07-31|LeetCode
題目: LeetCode - 338. Counting Bits 題目說明給一個 num,回傳一個代表 0 ~ num 的二進制中 1 的數量的陣列。 解題思路觀察答案的陣列可以知道,對於 i 來說,如果 i 是奇數則 res[i] = res[i / 2] + 1,如果 i 是偶數則 res[i] = res[i / 2]。 參考解法123456789class Solution {public: vector<int> countBits(int num) { vector<int> res(num + 1); for(int i = 1; i <= num; ++i) res[i] = res[i / 2] + i % 2; return res; }};
LeetCode - 1035 解題紀錄
發表於2020-07-31|LeetCode
題目: LeetCode - 1035. Uncrossed Lines 題目說明給兩個陣列,將陣列中相同的元素連線,連線不能交叉,求最大連線數。 解題思路其實題目的答案就是兩者的 LCS。找 LCS 的作法可參考本篇文章:LeetCode - 1143 解題紀錄。 參考解法12345678910111213141516171819// fast IOstatic auto __ = [](){ std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0;}();class Solution {public: int maxUncrossedLines(vector<int>& A, vector<int>& B) { int m = A.size(), n = B.size(); auto dp = vector&l ...
LeetCode - 986 解題紀錄
發表於2020-07-31|LeetCode
題目: LeetCode - 986. Interval List Intersections 題目說明給兩個代表區間的陣列,求兩陣列的交集。 解題思路使用雙指針,s 為兩者開頭較大的值,e 為兩者結束較小的值,若是 s <= e 則代表這是一段交集,最後看哪個的結尾比較小,就往前移動那個指針。 參考解法123456789101112131415161718192021222324// fast IOstatic auto __ = [](){ std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0;}();class Solution {public: vector<vector<int>> intervalIntersection(vector<vector<int>>& A, vector<vector<int& ...
LeetCode - 451 解題紀錄
發表於2020-07-31|LeetCode
題目: LeetCode - 451. Sort Characters By Frequency 題目說明給一個 String,將 String 裡面的字元按照個數排序,例如: tree -> eert or eetr。需要注意的是大小寫為不同的字元。 解題思路先使用 hash_map 得到每個字出現的次數,再用 Vector 排序,最後串接到 String 即可。 參考解法12345678910111213141516171819202122232425262728// fast IOstatic auto __ = [](){ std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0;}();class Solution {public: static bool cmp(const pair<char, int>& l,const pair<char, in ...
LeetCode - 1277 解題紀錄
發表於2020-07-31|LeetCode
題目: LeetCode - 1277. Count Square Submatrices with All Ones 題目說明給一個二維陣列,裡面包含 0 和 1,求出其中值為 1 的元素能組出幾個不同的正方形。 解題思路對於陣列中的每個元素來說,若該值為 1,能組出的正方形個數為,左邊那個元素最多能組出的正方形個數、上面那個元素最多能組出的正方形個數 和 左上角那個元素最多能組出的正方形個數,三者的最小值 + 1,依照此想法做動態規劃即可。 參考解法12345678910111213141516171819202122// fast IOstatic auto __ = [](){ std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0;}();class Solution {public: int countSquares(vector<vector<int>> ...
1…252627…33
avatar
Larry Lai
文章
325
標籤
86
分類
15
Follow Me
最新評論
正在載入中...
分類
  • 1091DataStructures-YzuCse13
  • 1091LinearAlgebra-YzuCse5
  • 1092IntroductionToAlgorithms-YzuCse12
  • Bitwise1
  • C++5
  • Data Mining1
  • Design Pattern2
  • Hexo1
標籤
0-1 Knapsack AI Anaconda Articulation Points BFS Back Tracking Bellman Ford Bitwise Butterfly C++ CPE - 2020/06/09 CPE - 2020/10/20 CPE - 2020/12/22 Coin Change Computational Geometry Convex Hull DFS Data Mining Deep Learning Design Pattern Dijkstra Dinic Divide and Conquer Dynamic Programming Edmonds-Karp FPS Floyd Cycle Detection Algorithm Floyd-Warshall Graham Scan Graph Hexo Index Sort Kadane Kaggle Kruskcal LDS LIS LeetCode LeetCode - Easy LeetCode - Hard
文章
  • 七月 20235
  • 六月 20231
  • 三月 20231
  • 二月 20231
  • 一月 20231
  • 十一月 20223
  • 十月 20222
  • 七月 20225
網站資訊
文章數目 :
325
已運行時間 :
本站總字數 :
118k
最後更新時間 :
User Map
©2020 - 2023 By Larry Lai
本地搜尋