八年級數(shù)學雙矩形問題;八年級數(shù)學雙矩形問題公式
2023-11-21 12:33:17
注:
黃色:簡單
綠色:可以重復練習
藍色:稍有難度
- 2. Add Two Numbers
- https://leetcode.com/problems/add-two-numbers/
- 用鏈表表示的數(shù)字,求和
- Input: l1 = [2,4,3], l2 = [5,6,4]
- Output: [7,0,8]
- 前綴 listnode
- 155. Min Stack
- https://leetcode.com/problems/min-stack/
- Input["MinStack","push","push","push","getMin","pop","top","getMin"][[],[-2],[0],[-3],[],[],[],[]]
- Output[null,null,null,null,-3,null,0,-2]
- 兩個棧:兩個都需要存全量的數(shù)據(jù),只是第二個每次都 push 當前的 min 而已
- 1019. Next Greater Node In Linked List
- https://leetcode.com/problems/next-greater-node-in-linked-list/
- 計算每個鏈表元素,最近一個比他大的元素 (以序列形式返回)
- Input: head = [2,1,5]
- Output: [5,5,0]
- O(N) stack 就行
- 641. Design Circular Deque
- https://leetcode.com/problems/design-circular-deque/
- 實現(xiàn)一個環(huán)形 queue
- Input ["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"] [[3], [1], [2], [3], [4], [], [], [], [4], []]
- Output [null, true, true, true, false, 2, true, true, true, 4]
- 數(shù)組 + index 模擬
- 526. Beautiful Arrangement
- https://leetcode.com/problems/beautiful-arrangement/
- [1, 2, ..., n] 的全排列,求其中符合要求的序列個數(shù):a[i] 被 i 整除 或者 i 被 a[i] 整除
- Input: n = 2
- Output: 2
- 先把每個位置上可選的候選集弄出來,然后 dfs (visited)
- 935. Knight Dialer
- https://leetcode.com/problems/knight-dialer/
- 手機鍵盤,每次撥號只能走 “馬”,起點隨機,撥 n 次可能撥出多少號碼
- Input: n = 2
- Output: 20
- dp:其實是很簡單的轉移方程 -> f(n) = sum(f(n-1)) 這種情況時不要緊張
- 452. Minimum Number of Arrows to Burst Balloons
- https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/
- 給定區(qū)間序列,求最少需要多少點可以把這些區(qū)間都覆蓋
- Input: points = [[10,16],[2,8],[1,6],[7,12]]
- Output: 2
- 和上課有點像,排序即可
- vector sort + 邊界處理 + 用指針引用 vector 可以減少很多時間
- 1400. Construct K Palindrome Strings
- https://leetcode.com/problems/construct-k-palindrome-strings/
- 給個字符串,用字符串里的所有字符,組成 k 個子串,且 k 個都是回文,問能不能做到
- Input: s = "annabelle", k = 2
- Output: true
- 奇數(shù)字符個數(shù) <= k 即可,腦經(jīng)急轉彎
- 1401. Circle and Rectangle Overlapping
- https://leetcode.com/problems/circle-and-rectangle-overlapping/
- 圓和矩形是否相交(矩形和坐標軸平行)
- Input: radius = 1, xCenter = 0, yCenter = 0, x1 = 1, y1 = -1, x2 = 3, y2 = 1
- Output: true
- 不能直接轉化為兩個矩形是否正交的問題
- https://hackmd.io/@bob81135/Leetcode_1401
- https://blog.csdn.net/fuxuemingzhu/article/details/105331519
- 970. Powerful Integers
- https://leetcode.com/problems/powerful-integers/
- 給定 i 和 j 和 t,求 i^x + j^y <= t 的數(shù)量
- Input: x = 2, y = 3, bound = 10
- Output: [2,3,4,5,7,9,10]
- 思路類似兩個指針
- a / b 有序序列,求 a[i] + b[j] <= target 的數(shù)量
- 其實是剪枝策略:較小的 a 和某個 b 都已經(jīng)超過了,那么較大的 a 不用和更大的 b 比較了
- 1798. Maximum Number of Consecutive Values You Can Make
- https://leetcode.com/problems/maximum-number-of-consecutive-values-you-can-make/
- 給你不同面額的硬幣各若干,求 從 0 開始的金額能連續(xù)構造出來多少
- Input: coins = [1,1,1,4]
- Output: 8
- 排序:if a[i] <= x+1, x+=a[i] else 后面的肯定也不行,因為構造不出來 x+1 了
- 811. Subdomain Visit Count
- https://leetcode.com/problems/subdomain-visit-count/
- 統(tǒng)計域名和子域名出現(xiàn)的次數(shù)
- Input: cpdomains = ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
- Output: ["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]
- 字符串處理
- 1915. Number of Wonderful Substrings
- https://leetcode.com/problems/number-of-wonderful-substrings/
- 求字符串的 wonderful 子串數(shù)量,wonderful = 最多只有一個字符出現(xiàn)奇數(shù)次
- Input: word = "aabb"
- Output: 9
- 用 bitmask 的形式來表現(xiàn)一個字符串中每個字符的奇偶狀態(tài)
- 根據(jù)條件, 我們找到與當前 bitmask 相同和差一位的所有**之前的**字符串的數(shù)量
- 2149. Rearrange Array Elements by Sign
- https://leetcode.com/problems/rearrange-array-elements-by-sign/
- 按照 偶奇偶奇 的順序排序
- Input: nums = [3,1,-2,-5,2,-4]
- Output: [3,-2,1,-5,2,-4]
- 重點是需要原地做,實現(xiàn)起來不麻煩
- 2038. Remove Colored Pieces if Both Neighbors are the Same Color
- https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/
- 只有連續(xù) AAA 或者連續(xù) BBB 才能刪除中間的,求最后是 A 不能刪還是 B 不能刪
- 如果是 B 不能則返回 true,否則返回 false
- Input: colors = "AAABABB"
- Output: true
- 因為刪除 A 或者 B 都不會影響后續(xù)的博弈,所以就遍歷計算數(shù)量即可
- 779. K-th Symbol in Grammar
- https://leetcode.com/problems/k-th-symbol-in-grammar/
- 0 -> 01 -> 0110 -> 01101001
- n 表示變化多少次,k 表示返回第 k 個字符
- Input: n = 2, k = 2
- Output: 1
- 找到規(guī)律就好解決了
- 1451. Rearrange Words in a Sentence
- https://leetcode.com/problems/rearrange-words-in-a-sentence/
- 按照長度重新排列(穩(wěn)定排序)
- Input: text = "Leetcode is cool"
- Output: "Is cool leetcode"
- 排序 + 字符串處理; 好像可以計數(shù)排序
- 856. Score of Parentheses
- https://leetcode.com/problems/score-of-parentheses/
- ()=1 && (a) = 2*a && ab=a+b,求字符串得分
- Input: s = "()()"
- Output: 2
- stack
- 需要區(qū)分記錄是 ( 還是中間結果
- 2120. Execution of All Suffix Instructions Staying in a Grid
- https://leetcode.com/problems/execution-of-all-suffix-instructions-staying-in-a-grid/
- DFS 機器人走格子,計算 “假設從 s 的每個 index 開始能走的步數(shù)”
- Input: n = 3, startPos = [0,1], s = "RRDDLU"
- Output: [1,5,4,3,1,0]
- 1234. Replace the Substring for Balanced String
- https://leetcode.com/problems/replace-the-substring-for-balanced-string/
- string 變成 4 個字符 (QWER) 出現(xiàn)的次數(shù)都相同,最小變多少 “連續(xù)的子串”
- Input: s = "QQQW"
- Output: 2
- Use 2-pointers algorithm to make sure all amount of characters outside the 2 pointers are smaller or equal to n/4.
- 滑動窗口
- 1286. Iterator for Combination
- https://leetcode.com/problems/iterator-for-combination/
- 字符串的全排列
- Input ["CombinationIterator", "next", "hasNext", "next", "hasNext", "next", "hasNext"] [["abc", 2], [], [], [], [], [], []]
- Output [null, "ab", true, "ac", true, "bc", false]
- 這道題就是全排列的逐個遞增式寫法
- 先找到需要 +1 的位置:nums[i]+1 在 i 后面
- 然后 nums[i] = nums[i]+1,然后把后面的數(shù)字 sort 一下之后依次填入
- 769. Max Chunks To Make Sorted
- https://leetcode.com/problems/max-chunks-to-make-sorted/
- 數(shù)組分段排序,排完本身也就有序了,問最多分多少段
- 如果序列的長度是 n,那么序列里的內(nèi)容肯定是 [0, n-1] 的某個排列
- Input: arr = [1,0,2,3,4]
- Output: 4
- 找最大值:如果前 i 個最大數(shù)等于 i,那么 a[i] 肯定可以可以單獨分一段,且前后最大段都不會受到影響
- o(n)
- 1376. Time Needed to Inform All Employees
- https://leetcode.com/problems/time-needed-to-inform-all-employees/
- 一棵樹,遍歷完,需要多少 cost
- n 是節(jié)點數(shù),manager 表示每個 index 的父親 index,informerTime 表示父親通知兒子的時間
- Input: n = 6, headID = 2, manager = [2,2,-1,2,2,2], informTime = [0,0,1,0,0,0]
- Output: 1
- dfs
- 36. Valid Sudoku
- https://leetcode.com/problems/valid-sudoku/
- 數(shù)獨
- Input: board = [["5","3",".",".","7",".",".",".","."] ,["6",".",".","1","9","5",".",".","."] ,[".","9","8",".",".",".",".","6","."] ,["8",".",".",".","6",".",".",".","3"] ,["4",".",".","8",".","3",".",".","1"] ,["7",".",".",".","2",".",".",".","6"] ,[".","6",".",".",".",".","2","8","."] ,[".",".",".","4","1","9",".",".","5"] ,[".",".",".",".","8",".",".","7","9"]]
- Output: true
- dfs:重點是方形的區(qū)間如何表達
- 522. Longest Uncommon Subsequence II
- https://leetcode.com/problems/longest-uncommon-subsequence-ii/
- 最長非公共子序列
- An uncommon subsequence between an array of strings is a string that is a subsequence of one string but not the others.
- A subsequence of a string s is a string that can be obtained after deleting any number of characters from s.
- Input: strs = ["aba","cdc","eae"]
- Output: 3
- 最終結果一定等于某個序列的長度
- 如果結果 < 所有的序列,那么最長的那個肯定也是的
- 647. Palindromic Substrings
- https://leetcode.com/problems/palindromic-substrings/
- 回文子串的數(shù)量
- Input: s = "aaa"
- Output: 6
- dp 即可 -> 寫的時候不要寫單層數(shù)組
- 994. Rotting Oranges
- https://leetcode.com/problems/rotting-oranges/
- 矩陣中,橘子壞影響四周,橘子分好壞,每分鐘壞橘子都會讓四周的好橘子變壞,求可以撐多少分鐘
- Input: grid = [[2,1,1],[1,1,0],[0,1,1]]
- Output: 4
- bfs
- 2245. Maximum Trailing Zeros in a Cornered Path
- https://leetcode.com/problems/maximum-trailing-zeros-in-a-cornered-path/
- 矩陣中最多轉一次且不能走重復路,求乘積最大的后綴 0 數(shù)量
- Input: grid = [[23,17,15,3,20],[8,1,20,27,11],[9,4,6,2,21],[40,9,1,10,6],[22,7,4,5,3]]
- Output: 3
- 以 [i,j] 為轉折點
- 877. Stone Game
- https://leetcode.com/problems/stone-game/
- 兩個人依次從首或者尾取硬幣,求最終先手是否穩(wěn)贏 (誰的數(shù)字和最大誰贏)
- Input: piles = [5,3,4,5]
- Output: true
- dp 即可
- a[i][j] = max(p[i]+ max(a[i+1][j-2], a[i+2][j-2]), p[i+j-1]+max(a[i][j-2], a[i+1][j-2]))
- j 是長度:常見的 dp 最外層循環(huán)的寫法
- 面對這種題目能用 dp 表達的肯定也能用 dfs,選一個好寫的即可
- 470. Implement Rand10() Using Rand7()
- https://leetcode.com/problems/implement-rand10-using-rand7/
- Input: n = 1
- Output: [2]
- (rand7()-1) * 7 + rand7():超過就不要唄
- 1882. Process Tasks Using Servers
- https://leetcode.com/problems/process-tasks-using-servers/
- 計算每個 task 最終會被哪個 server 處理
- server 數(shù)組表示的是權重 (每次都從不忙的 server 中取權重最小的執(zhí)行 task)
- tasks 數(shù)組表示的是每個任務執(zhí)行的時間
- cases
- Input: servers = [3,3,2], tasks = [1,2,3,2,1,2]
- Output: [2,2,0,2,1,2]
- priority_queue
- waiting_queue 和 running_queue,前者用完成時間排序,后者用 weight 排序
- 172. Factorial Trailing Zeroes
- https://leetcode.com/problems/factorial-trailing-zeroes/
- 階乘的后綴 0 數(shù)量
- Input: n = 5
- Output: 1
- 其實就是數(shù) 5 的倍數(shù)的數(shù)量
- 1300. Sum of Mutated Array Closest to Target
- https://leetcode.com/problems/sum-of-mutated-array-closest-to-target/
- 把序列中比 x 大的都變成 x 使得 sum 和 target 最接近,求 x
- Input: arr = [60864,25176,27249,21296,20204], target = 56803
- Output: 11361
- 先 sort 然后再用二分,target > sum 就往右邊去,否則就往左邊去
- 其實第二步不需要 log(n),直接遍歷用 o(n) 的就可以
- 1261. Find Elements in a Contaminated Binary Tree
- https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/
- 根據(jù)規(guī)則重建 tree 反查某個數(shù)在不在
- Input ["FindElements","find","find"][[[-1,null,-1]],[1],[2]
- Output[null,false,true]
- 1410. HTML Entity Parser
- https://leetcode.com/problems/html-entity-parser/
- 字符串處理,去掉特殊字符串
- Input: text = "& is an HTML entity but &ambassador is not."
- Output: "& is an HTML entity but &ambassador is not."
- 1477. Find Two Non-overlapping Sub-arrays Each With Target Sum
- https://leetcode.com/problems/find-two-non-overlapping-sub-arrays-each-with-target-sum/
- 找到兩個子序列,和為 target,且不正交,求滿足條件的子序列的元素數(shù)量和的最小值
- Input: arr = [7,3,4,7], target = 7
- Output: 2
- 只統(tǒng)計一序列的最小長度比較簡單:前綴和即可
- 如果統(tǒng)計兩個序列的最小長度,則在一個的基礎上記錄 min_len[i] 即可
- 648. Replace Words
- https://leetcode.com/problems/replace-words/
- 字符串替換成前綴
- Input: dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
- Output: "the cat was rat by the bat"
- 字典樹
- 451. Sort Characters By Frequency
- https://leetcode.com/problems/sort-characters-by-frequency/
- 按照字符出現(xiàn)的次數(shù)排序
- Input: s = "tree"
- Output: "eert"
- 1786. Number of Restricted Paths From First to Last Node
- https://leetcode.com/problems/number-of-restricted-paths-from-first-to-last-node/
- 圖的最短路徑:[i, j, weight],求從節(jié)點 1 到節(jié)點 n 的最短路徑
- Input: n = 5, edges = [[1,2,3],[1,3,3],[2,3,1],[1,4,2],[5,2,2],[3,5,1],[5,4,10]]
- Output: 3
- Dijkstra pop min:需要用 priority queue 才行
- floid i/j/k 三層
- 173. Binary Search Tree Iterator
- https://leetcode.com/problems/binary-search-tree-iterator/
- bst 樹支持 next 操作
- Input ["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"] [[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
- Output [null, 3, 7, true, 9, true, 15, true, 20, false]
- 用 stack 去模擬中序遍歷的過程
- 979. Distribute Coins in Binary Tree
- https://leetcode.com/problems/distribute-coins-in-binary-tree/
- 每個節(jié)點有 coin 可以往相鄰節(jié)點轉移,轉移多少次可以每個都有一個
- 保證所有節(jié)點初始 coin 數(shù)量和 = 節(jié)點數(shù)
- Input: root = [3,0,0]
- Output: 2
- 遞歸,重點 pair<int, int> 返回的是什么,是 <coin count, node count>
- 1910. Remove All Occurrences of a Substring
- https://leetcode.com/problems/remove-all-occurrences-of-a-substring/
- 找到 a 中所有匹配 b 的字串 然后刪除:中間可以拼出來新的要刪除的
- Input: s = "daabcbaabcbc", part = "abc"
- Output: "dab"
- 遍歷每次都比較最后的 a.substr(a.length()-b-1, b.length()) 即可
- 1737. Change Minimum Characters to Satisfy One of Three Conditions
- https://leetcode.com/problems/change-minimum-characters-to-satisfy-one-of-three-conditions/
- a/b 兩個字符串,每次操作可以把 a/b 的任意字符改成另外的任意字符
- 求最少操作次數(shù)使得 min(a) > max(b) or min(b) > max(a) or a/b 全相同
- Input: a = "dabadd", b = "cda"
- Output: 3
- 統(tǒng)計次數(shù) + 前綴和:以某個字符為邊界,a 全部小于等于 b 全部大于 (或者反過來)
- 388. Longest Absolute File Path
- https://leetcode.com/problems/longest-absolute-file-path/
- Input: input = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
- Output: 32
- 字符串處理最大難度是 \n \t 是一個字符
- 需要用 stack 記錄每層的信息
- 341. Flatten Nested List Iterator
- https://leetcode.com/problems/flatten-nested-list-iterator/
- 給定帶嵌套關系的 struct,將起轉成序列
- Input: nestedList = [1,[4,[6]]]
- Output: [1,4,6]
- stack + 遞歸
- 用模擬函數(shù)參數(shù)棧的方式處理嵌套的 struct
- 1387. Sort Integers by The Power Value
- https://leetcode.com/problems/sort-integers-by-the-power-value/
- 按照某種規(guī)則變換數(shù)字,最后變成 1;把 [lo,hi] 的數(shù)字按照 pow 排序,然后返回第 k 個數(shù)
- Input: lo = 12, hi = 15, k = 2
- Output: 13
- 直接算然后取第 k 大的數(shù)
- 1535. Find the Winner of an Array Game
- https://leetcode.com/problems/find-the-winner-of-an-array-game/
- a[0] 和 a[1] 比較,小的放到最后,問連續(xù)比較贏至少 k 次的是誰
- Input: arr = [2,1,3,5,4,6,7], k = 2
- Output: 5
- 如果是 k>size()-1 直接是最大
- 否則 a[i] 贏 說明 a[i] > max(a[0] ... a[i-1]) 且 a[i] > (a[i+1]... a[i+k-2])
- 958. Check Completeness of a Binary Tree
- https://leetcode.com/problems/check-completeness-of-a-binary-tree/
- In a complete binary tree, every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible:二叉樹是否是每層都優(yōu)先排 left 節(jié)點的
- Input: root = [1,2,3,4,5,6]
- Output: true
- 層次遍歷,同時處理 left == nil && right != nil 等特殊情況
- 2261. K Divisible Elements Subarrays
- https://leetcode.com/problems/k-divisible-elements-subarrays/
- 給定序列,尋找子序列的數(shù)量,子序列滿足:被 p 整除的數(shù)字個數(shù) <= k
- Input: nums = [2,3,3,2,2], k = 2, p = 2
- Output: 11
- 解釋
- [2], [2,3], [2,3,3], [2,3,3,2], [3], [3,3], [3,3,2], [3,3,2,2], [3,2], [3,2,2], [2,2]
- 還要去重,比如 [2]
- 從左往右的同時記錄 “能被 p 整除的數(shù)字” 的 index
- 2280. Minimum Lines to Represent a Line Chart
- https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart/
- 給定的坐標總共連成多少條線段
- Input: stockPrices = [[1,7],[2,6],[3,5],[4,4],[5,4],[6,3],[7,2],[8,1]]
- Output: 3
- 按照 x,y 的順序排序之后遍歷