[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] 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] Catalog

Catalog 一、依題號 1-500(45) 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Array 5. Longest Palindromic Substring 6. Zigzag Conversion 7. Reverse Integer 8. String to Integer (atoi) 9. Palindrome Number 10. Regular Expression Matching 11. Container With Most Water 12. Integer to Roman 13. Roman to Integer 14. Longest Common Prefix 15. 3Sum 16. 3Sum Closet 17. Letter Combinations of a Phone Number 18. 4Sum 19. Remove Nth Node From End of List 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 23. Merge k Sorted Lists 24. Swap Nodes in Pairs 25. Reverse Nodes in k-Group 53. Maximum Subarray 70. Climbing Stairs 79. Word Search 100. Same Tree 124. Binary Tree Maximum Path Sum 132. Palindrome Partitioning II 149. Max Points on a Line 151. Reverse Words in a String 198. House Robber 212. Word Search II 213. House Robber II 223. Rectangle Area 279. Perfect Squares 290. Word Pattern 300. Longest Increasing Subsequence 322. Coin Change 328. Odd Even Linked List 337. House Robber III 347. Top K Frequent Elements 374. Guess Number Higher or Lower II 433. Minimum Genetic Mutation 446. Arithmetic Slices II - Subsequence 452. Minimum Number of Arrows to Burst Balloons ...

October 25, 2022 · 13 分鐘 · Rain Hu

[Leetcode] 347. Top K Frequent Elements

347. Top K Frequent Elements Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Array、Hash Table、Divide and Conquer、Sorting、Heap (Priority Queue)、Bucket Sort、Counting、Quickselect 一、題目 Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order. Example 1: Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2] Example 2: Input: nums = [1], k = 1 Output: [1] Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 k is in the range [1, the number of unique elements in the array]. It is guaranteed that the answer is unique. Follow up: Your algorithm’s time complexity must be better than O(n log n), where n is the array’s size. ...

July 23, 2022 · 2 分鐘 · Rain Hu

[Leetcode] Maximum Frequency Stack 最大頻率堆疊

題目 題目描述 設計一個類似 stack 的資料結構,實行 push() 跟 pop() 的功能,其中 pop() 會丟出 stack 中出現最多次的元素。 FreqStack class 必須實現: FreqStack() 建構子必須初始化一個空的 FreqStack。 void push(int val) 將 val 推至 stack 的頂端。 int pop() 將 stack 中最頻繁出現的元素移除,並返回。 如果 stack 中最頻繁出現的元素出現平手的狀況,則返回平手的元素中最接近 stack 頂端的元素。 題目範例 輸入 [“FreqStack”, “push”, “push”, “push”, “push”, “push”, “push”, “pop”, “pop”, “pop”, “pop”] [[], [5], [7], [5], [7], [4], [5], [], [], [], []] 輸出 [null, null, null, null, null, null, null, 5, 7, 5, 4] ...

March 19, 2022 · 3 分鐘 · Rain Hu