[Leetcode] 7. Reverse Integer

7. Reverse Integer Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Math 一、題目 Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2^31, 2^31-1], then return 0. Assume the environment does not allow you to store 64-bit integers (signed or unsigned). Example 1: Input: x = 123 Output: 321 Example 2: Input: x = -123 Output: -321 Example 3: ...

October 25, 2022 · 2 分鐘 · Rain Hu

[Leetcode] 6. Zigzag Conversion

6. Zigzag Conversion Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: String 一、題目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to dispaly this pattern in a fixed font for better legibility) \( \quad\texttt{P A H N}\\ \quad\texttt{APLSIIG}\\ \quad\texttt{Y I R}\\ \) And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows: string convert(string s, int numRows ...

October 25, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 5. Longest Palindromic Substring

5. Longest Substring Without Repeating Characters Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: String、Dynamic Programming 一、題目 Given a string s, return the longest palindromic substring in s. A string is called a palindrome string if the reverse of that string is the same of the original string. Example 1: Input: s = “babad” Output: “bab” Explanation: “aba” is also a valid answer. Example 2: Input: s = “cbbd” Output: “bb” Constraints: 1 <= s.length <= 1000 s consists of only digits and English letters. 二、分析 注意 palindrome string 的特性: 當長度為 1 時,必為 palindrome string 當長度為 2 時,兩個字元必須相同才為 palindrome string 當長度 >2 時,palindrome string 必須滿足 最左邊的字元等於最右邊的字元,即 s[left] == s[right] 除去最左邊的字元跟最右邊的字元,必須為 palindrome string, 即 s.substr(left+1, len-2) 為 palindromic。 三、解題 1. Dynamic Prograimming Time complexity: \(O(n^2)\) Space complexity: \(O(n^2)\) string longestPalindrome(string s) { int n = s.length(); string res; bool dp[n][n]; memset(dp, false, sizeof(dp)); int len = 0; for (int j = 0; j < n; j++) { for (int i = 0; i <= j; i++) { if (i == j) { // 長度為 1 dp[i][j] = true; } else if (j - i == 1) { // 長度為 2 dp[i][j] = s[i] == s[j]; } else { // 長度 > 2 dp[i][j] = s[i] == s[j] && dp[i+1][j-1]; } if (dp[i][j] && j - i + 1 > len) { // 比較長度 len = j - i + 1; res = s.substr(i, len); } } } return res; } 回目錄 Catalog ...

October 25, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 4. Median of Two Sorted Arrays

4. Median of Two Sorted Arrays Hardness: \(\color{red}\textsf{Hard}\) Ralated Topics: Array、Binary Search、Divide and Conquer 一、題目 Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2. Example 2: Input: nums1 = [1,2], nums2 = [3,4] Output: 2.50000 Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. Constraints: ...

October 25, 2022 · 3 分鐘 · Rain Hu

[LeetCode] 3. Longest Substring Without Repeating Characters

3. Longest Substring Without Repeating Characters Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Hash Table、String、Sliding Window 一、題目 Given a string s, find the length of the longest substring without repeating characters. Example 1: Input: s = “abcabcbb” Output: 3 Explanation: The answer is “abc”, with the length of 3. Example 2: Input: s = “bbbbb” Output: 1 Explanation: The answer is “b”, with the length of 1. Example 3: Input: s = “pwwkew” Output: 3 Explanation: The answer is “wke”, with the length of 3. Notice that the answer must be a substring, “pwke” is a subsequence and not a substring. Constraints: ...

October 25, 2022 · 1 分鐘 · Rain Hu

[LeetCode] 2. Add Two Numbers

2. Add Two Numbers Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Linked List、Math、Recursion 一、題目 You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example 1: Input: l1 = [2,4,3], l2 = [5,6,4] Output: [7,0,8] Explanation: 342 + 465 = 807 Example 2: ...

October 25, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 1297. Maximum Number of Occurrences of a Substring

難度分: 1748 這題的 minSize 和 maxSize 在 26 以內,範圍不會太大,可以用定長度的 sliding window 硬解。 class Solution { public: int maxFreq(string s, int maxLetters, int minSize, int maxSize) { int res = 0; for (int i = minSize; i <= maxSize; i++) { res = max(res, maxFreqWithWindowSize(s, maxLetters, i)); } return res; } int maxFreqWithWindowSize(string& s, int th, int k) { int n = s.size(); int cnt[26]; memset(cnt, 0, sizeof(cnt)); int uq = 0; int res = 0; unordered_map<string, int> map; for (int i = 0; i < k; i++) { if (cnt[s[i]-'a']++ == 0) uq++; } if (uq <= th) { map[s.substr(0, k)]++; res = 1; }; for (int i = k; i < n; i++) { if (cnt[s[i]-'a']++ == 0) uq++; if (--cnt[s[i-k]-'a'] == 0) uq--; if (uq <= th) { string t = s.substr(i-k+1, k); res = max(res, ++map[t]); } } return res; } };

October 25, 2022 · 1 分鐘 · Rain Hu

[LeetCode] 1652. Defuse the Bomb

這題要用 sliding window 解需要處理 circular iteration class Solution { public: vector<int> decrypt(vector<int>& code, int k) { int n = code.size(); vector<int> res(n, 0); if (k == 0) return res; int m = abs(k); int start = k > 0 ? 1 : n+k; int sum = 0; for (int i = 0; i < m; i++) { sum += code[(start + i) % n]; } res[0] = sum; for (int i = m, j = 1; i < m + n - 1; i++, j++) { int in = (start + i) % n; int out = (start + i - k) % n; sum += (code[(start + i) % n] - code[(start + i - m) % n]); res[j] = sum; } return res; } };

October 25, 2022 · 1 分鐘 · Rain Hu

[LeetCode] 1. Two Sum

1. Two Sum Hardness: \(\color{green}\textsf{Easy}\) Ralated Topics: Array、Hash Table 一、題目 Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. Example 1: Input: nums = [2,7,11,15], taget = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0,1]. Example 2: ...

October 25, 2022 · 1 分鐘 · Rain Hu

[LeetCode] 1423. Maximum Points You Can Obtain from Cards

難度分: 1574 這題同樣是定長度的 sliding window,但要透過一點轉換,變成求 window = n - k 的最小和 class Solution { public: int maxScore(vector<int>& cardPoints, int k) { int n = cardPoints.size(); int m = n-k; int sum = 0; for (int i = 0; i < m; i++) { sum += cardPoints[i]; } int total = sum; int min_window_sum = sum; for (int i = m; i < n; i++) { sum += (cardPoints[i] - cardPoints[i-m]); total += cardPoints[i]; min_window_sum = min(min_window_sum, sum); } return total - min_window_sum; } };

October 25, 2022 · 1 分鐘 · Rain Hu