├── .github └── workflows │ └── main.yml ├── .gitignore ├── 1. Two Sum ├── Cargo.toml ├── README.md ├── Solution.cpp └── src │ └── main.rs ├── 10. Regular Expression Matching ├── Cargo.toml └── src │ └── main.rs ├── 1004. Max Consecutive Ones III ├── Cargo.toml └── src │ └── main.rs ├── 101. Symmetric Tree └── Solution.cpp ├── 102. Binary Tree Level Order Traversal └── Solution.cpp ├── 1044. Longest Duplicate Substring ├── Cargo.toml ├── src │ └── main.rs └── test ├── 1046. Last Stone Weight ├── Cargo.toml ├── Solution.cpp └── src │ └── main.rs ├── 105. Construct Binary Tree from Preorder and Inorder Traversal └── Solution.cpp ├── 109. Convert Sorted List to Binary Search Tree └── Solution.cpp ├── 11. Container With Most Water ├── Cargo.toml ├── REAMDE.md └── src │ └── main.rs ├── 115. Distinct Subsequences └── Solution.cpp ├── 12. Integer to Roman ├── Cargo.toml └── src │ └── main.rs ├── 120. Triangle ├── Cargo.toml └── src │ └── main.rs ├── 121. Best Time to Buy and Sell Stock └── Solution.cpp ├── 122. Best Time to Buy and Sell Stock II └── Solution.cpp ├── 125. Valid Palindrome ├── Cargo.toml └── src │ └── main.rs ├── 1254. Number of Closed Islands ├── Cargo.toml └── src │ └── main.rs ├── 1289. Day of the Week ├── Cargo.toml └── src │ └── lib.rs ├── 13. Roman to Integer ├── Cargo.toml └── src │ └── main.rs ├── 1301. Reformat Department Table ├── Cargo.toml └── src │ └── lib.rs ├── 131. Palindrome Partitioning ├── Cargo.toml └── src │ └── main.rs ├── 133. Clone Graph ├── go.mod ├── solution.go └── solution_test.go ├── 134. Gas Station └── Solution.cpp ├── 135. Candy ├── Cargo.toml ├── Solution.cpp └── src │ └── main.rs ├── 136. Single Number ├── Cargo.toml └── src │ └── main.rs ├── 14. Longest Common Prefix ├── Cargo.toml └── src │ └── main.rs ├── 1444. Number of Steps to Reduce a Number to Zero ├── Cargo.toml └── src │ └── main.rs ├── 149. Max Points on a Line └── Solution.cpp ├── 15. 3Sum ├── Cargo.toml ├── Solution.cpp └── src │ └── main.rs ├── 155. Min Stack ├── Cargo.toml └── src │ └── main.rs ├── 1575. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts ├── Cargo.toml └── src │ └── lib.rs ├── 16. 3Sum Closest ├── Cargo.toml └── src │ └── main.rs ├── 160. Intersection of Two Linked Lists └── Solution.cpp ├── 1632. Number of Good Ways to Split a String ├── Cargo.toml └── src │ └── lib.rs ├── 1658. Minimum Swaps to Arrange a Binary Grid ├── Cargo.toml └── src │ └── lib.rs ├── 1663. Detect Cycles in 2D Grid ├── Cargo.toml └── src │ └── lib.rs ├── 1664. Find Users With Valid E-Mails ├── Cargo.toml └── src │ └── lib.rs ├── 1669. Minimum Cost to Cut a Stick ├── .gdb_history ├── Cargo.toml └── src │ ├── list.rs │ └── main.rs ├── 168. Excel Sheet Column Title ├── Cargo.toml └── src │ └── main.rs ├── 17. Letter Combinations of a Phone Number ├── Cargo.toml └── src │ └── main.rs ├── 1750. Check If Two Expression Trees are Equivalent ├── Cargo.toml └── src │ └── main.rs ├── 1759. Find the Missing IDs ├── Cargo.toml └── src │ └── main.rs ├── 179. Largest Number ├── Cargo.toml └── src │ └── main.rs ├── 18. 4Sum ├── Cargo.toml └── src │ └── main.rs ├── 1801. Average Time of Process per Machine ├── Cargo.toml └── src │ └── lib.rs ├── 1802. Number of Students Unable to Eat Lunch ├── Cargo.toml └── src │ └── lib.rs ├── 1803. Average Waiting Time ├── Cargo.toml └── src │ └── lib.rs ├── 1807. Partitioning Into Minimum Number Of Deci-Binary Numbers ├── Cargo.toml └── src │ └── lib.rs ├── 1813. Maximum Erasure Value ├── Cargo.toml └── src │ └── lib.rs ├── 1814. Jump Game VI ├── Cargo.toml └── src │ └── lib.rs ├── 1815. Checking Existence of Edge Length Limited Paths ├── Cargo.toml └── src │ └── lib.rs ├── 1817. Calculate Money in Leetcode Bank ├── Cargo.toml └── src │ └── lib.rs ├── 1819. Construct the Lexicographically Largest Valid Sequence ├── Cargo.toml └── src │ └── lib.rs ├── 1824. Maximum Number of Eaten Apples ├── Cargo.toml └── src │ └── lib.rs ├── 1825. Find Minimum Time to Finish All Jobs ├── Cargo.toml └── src │ └── lib.rs ├── 1828. Count Ways to Distribute Candies ├── Cargo.toml └── src │ └── lib.rs ├── 19. Remove Nth Node From End of List ├── Cargo.toml ├── README.md └── src │ └── main.rs ├── 198. House Robber ├── Cargo.toml └── src │ └── main.rs ├── 2. Add Two Numbers ├── Cargo.toml └── src │ └── main.rs ├── 20. Valid Parentheses ├── Cargo.toml └── src │ └── main.rs ├── 200. Number of Islands ├── Cargo.toml ├── Solution.cpp └── src │ └── main.rs ├── 2027. Maximum Number of Removable Characters ├── Cargo.toml └── src │ └── main.rs ├── 2032. Largest Odd Number in String ├── Cargo.toml └── src │ └── lib.rs ├── 2037. Count Square Sum Triples ├── Cargo.toml └── src │ └── lib.rs ├── 2042. Maximum Product Difference Between Two Pairs ├── Cargo.toml └── src │ └── lib.rs ├── 21. Merge Two Sorted Lists ├── Cargo.toml └── src │ └── main.rs ├── 213. House Robber II ├── Cargo.toml └── src │ └── main.rs ├── 215. Kth Largest Element in an Array └── Solution.cpp ├── 217. Contains Duplicate ├── Cargo.toml ├── Solution.cpp └── src │ └── main.rs ├── 2180. Maximum Number of Tasks You Can Assign ├── Cargo.toml └── src │ └── lib.rs ├── 219. Contains Duplicate II ├── Cargo.toml ├── Solution.cpp └── src │ └── main.rs ├── 22. Generate Parentheses ├── Cargo.toml ├── Solution.cpp └── src │ ├── README.md │ └── main.rs ├── 220. Contains Duplicate III ├── Cargo.toml └── src │ └── main.rs ├── 2287. Minimum Number of Lines to Cover Points ├── Cargo.toml └── src │ └── lib.rs ├── 2299. Merge Nodes in Between Zeros ├── .vscode │ └── settings.json ├── Cargo.toml └── src │ └── lib.rs ├── 23. Merge k Sorted Lists ├── Cargo.toml └── src │ ├── main.rs │ └── main_bruteforce.rs ├── 2303. Unique Substrings With Equal Digit Frequency ├── Cargo.toml └── src │ └── lib.rs ├── 2309. Maximize Number of Subsequences in a String ├── Cargo.toml └── src │ └── lib.rs ├── 2315. The Change in Global Rankings ├── Cargo.toml └── src │ └── lib.rs ├── 2351. Number of Ways to Buy Pens and Pencils ├── Cargo.toml └── src │ └── lib.rs ├── 238. Product of Array Except Self ├── Cargo.toml ├── Solution.cpp └── src │ └── main.rs ├── 24. Swap Nodes in Pairs ├── Cargo.toml └── src │ └── main.rs ├── 240. Search a 2D Matrix II └── Solution.cpp ├── 25. Reverse Nodes in k-Group ├── Cargo.toml └── src │ └── main.rs ├── 26. Remove Duplicates from Sorted Array ├── Cargo.toml └── src │ └── main.rs ├── 27. Remove Element ├── Cargo.toml └── src │ └── main.rs ├── 28. Implement strStr() ├── Cargo.toml ├── Solution.cpp └── src │ └── main.rs ├── 29. Divide Two Integers ├── Cargo.toml └── src │ └── main.rs ├── 290. Word Pattern ├── Cargo.toml └── src │ └── main.rs ├── 3. Longest Substring Without Repeating Characters ├── Cargo.toml └── src │ └── main.rs ├── 30-Day LeetCoding Challenge ├── Perform String Shifts │ ├── Cargo.toml │ └── src │ │ └── main.rs └── counting_elements │ ├── Cargo.toml │ └── src │ └── main.rs ├── 30. Substring with Concatenation of All Words ├── Cargo.toml └── src │ └── main.rs ├── 31. Next Permutation ├── Cargo.toml ├── Solution.cpp └── src │ └── main.rs ├── 312. Burst Balloons └── Solution.cpp ├── 316. Remove Duplicate Letters ├── Cargo.toml └── src │ └── main.rs ├── 32. Longest Valid Parentheses ├── Cargo.toml └── src │ └── main.rs ├── 33. Search in Rotated Sorted Array ├── Cargo.toml └── src │ └── main.rs ├── 337. House Robber III ├── Cargo.toml └── src │ └── main.rs ├── 34. Find First and Last Position of Element in Sorted Array ├── Cargo.toml └── src │ └── main.rs ├── 343. Integer Break └── Solution.cpp ├── 347. Top K Frequent Elements ├── Cargo.toml └── src │ └── main.rs ├── 35. Search Insert Position ├── Cargo.toml └── src │ └── main.rs ├── 36. Valid Sudoku ├── Cargo.toml └── src │ └── main.rs ├── 368. Largest Divisible Subset ├── Cargo.toml └── src │ └── lib.rs ├── 37. Sudoku Solver ├── Cargo.toml └── src │ └── main.rs ├── 378. Kth Smallest Element in a Sorted Matrix ├── Cargo.toml └── src │ └── main.rs ├── 38. Count and Say ├── Cargo.toml └── src │ └── main.rs ├── 39. Combination Sum ├── Cargo.toml └── src │ └── main.rs ├── 4. Median of Two Sorted Arrays ├── Cargo.toml └── src │ └── main.rs ├── 40. Combination Sum II ├── Cargo.toml └── src │ └── main.rs ├── 41. First Missing Positive ├── Cargo.toml └── src │ └── main.rs ├── 416. Partition Equal Subset Sum ├── Cargo.toml └── src │ └── main.rs ├── 42. Trapping Rain Water ├── Cargo.toml ├── README.md └── src │ └── main.rs ├── 43. Multiply Strings ├── Cargo.toml └── src │ └── main.rs ├── 44. Wildcard Matching ├── Cargo.toml └── src │ ├── README.md │ └── main.rs ├── 448. Find All Numbers Disappeared in an Array └── Solution.cpp ├── 45. Jump Game II ├── Cargo.toml ├── README.md └── src │ └── main.rs ├── 46. Permutations ├── Cargo.toml └── src │ └── main.rs ├── 47. Permutations II ├── Cargo.toml └── src │ └── main.rs ├── 48. Rotate Image ├── Cargo.toml └── src │ └── main.rs ├── 49. Group Anagrams ├── Cargo.toml ├── README.md ├── Solution.cpp └── src │ └── main.rs ├── 493. Reverse Pairs └── Solution.cpp ├── 5. Longest Palindromic Substring ├── Cargo.toml └── src │ └── main.rs ├── 50. Pow(x, n) ├── Cargo.toml └── src │ └── main.rs ├── 51. N-Queens ├── Cargo.toml └── src │ └── main.rs ├── 52. N-Queens II ├── Cargo.toml └── src │ └── main.rs ├── 525. Contiguous Array ├── Cargo.toml ├── README.md ├── Solution.cpp └── src │ └── main.rs ├── 53. Maximum Subarray ├── Cargo.toml └── src │ └── main.rs ├── 54. Spiral Matrix ├── Cargo.toml └── src │ └── main.rs ├── 543. Diameter of Binary Tree └── Solution.cpp ├── 55. Jump Game ├── Cargo.toml └── src │ └── main.rs ├── 576. Out of Boundary Paths ├── Cargo.toml └── src │ └── lib.rs ├── 58. Length of Last Word ├── Cargo.toml └── src │ └── main.rs ├── 59. Spiral Matrix II ├── Cargo.toml └── src │ └── main.rs ├── 6. ZigZag Conversion ├── Cargo.toml └── src │ └── main.rs ├── 60. Permutation Sequence ├── Cargo.toml └── src │ └── main.rs ├── 605. Can Place Flowers ├── Cargo.toml └── src │ └── main.rs ├── 61. Rotate List ├── Cargo.toml ├── README.md └── src │ └── main.rs ├── 62. Unique Paths ├── Cargo.toml └── src │ └── main.rs ├── 63. Unique Paths II ├── Cargo.toml └── src │ └── main.rs ├── 639. Decode Ways II ├── Cargo.toml └── src │ └── main.rs ├── 64. Minimum Path Sum ├── Cargo.toml ├── Solution.cpp └── src │ └── main.rs ├── 65. Valid Number ├── Cargo.toml └── src │ └── main.rs ├── 66. Plus One ├── Cargo.toml └── src │ └── main.rs ├── 67. Add Binary ├── Cargo.toml └── src │ └── main.rs ├── 678. Valid Parenthesis String ├── Cargo.toml ├── Solution.cpp └── src │ └── main.rs ├── 679. 24 Game └── Solution.cpp ├── 69. Sqrt(x) ├── Cargo.toml └── src │ └── main.rs ├── 7. Reverse Integer ├── Cargo.toml ├── README.md └── src │ └── main.rs ├── 70. Climbing Stairs └── Solution.cpp ├── 704. 二分查找 ├── Cargo.toml └── src │ └── main.rs ├── 71. Simplify Path ├── README.md └── Solution.cpp ├── 72. Edit Distance ├── Cargo.toml └── src │ └── main.rs ├── 73. Set Matrix Zeroes └── Solution.cpp ├── 74. Search a 2D Matrix ├── Cargo.toml └── src │ └── main.rs ├── 753. Open the Lock ├── Cargo.toml └── src │ └── lib.rs ├── 77. Combinations └── Solution.cpp ├── 78. Subsets ├── Cargo.toml ├── README.md ├── Solution.cpp └── src │ └── main.rs ├── 782. Transform to Chessboard ├── Cargo.toml └── src │ └── main.rs ├── 784. Letter Case Permutation ├── Cargo.toml └── src │ └── main.rs ├── 79. Word Search ├── Cargo.toml └── src │ └── main.rs ├── 8. String to Integer (atoi) ├── Cargo.toml └── src │ └── main.rs ├── 80. Remove Duplicates from Sorted Array II ├── Cargo.toml └── src │ └── main.rs ├── 81. Search in Rotated Sorted Array II ├── Cargo.toml ├── Solution.cpp └── src │ └── main.rs ├── 82. Remove Duplicates from Sorted List II ├── Cargo.toml ├── Solution.cpp └── src │ └── main.rs ├── 83. Remove Duplicates from Sorted List └── Solution.cpp ├── 84. Largest Rectangle in Histogram ├── Cargo.toml └── src │ └── main.rs ├── 85. Maximal Rectangle └── Solution.cpp ├── 855. Count Unique Characters of All Substrings of a Given String ├── Cargo.toml └── src │ ├── lib.rs │ ├── lib_bkup.rs │ └── lib_bkup_ac.rs ├── 86. Partition List ├── Cargo.toml ├── Solution.cpp └── src │ └── main.rs ├── 88. Merge Sorted Array ├── Cargo.toml └── src │ └── main.rs ├── 883. Projection Area of 3D Shapes └── Solution.cpp ├── 89. Gray Code └── Solution.cpp ├── 9. Palindrome Number ├── Cargo.toml └── src │ └── main.rs ├── 90. Subsets II ├── Cargo.toml ├── Solution.cpp └── src │ └── main.rs ├── 91. Decode Ways ├── Cargo.toml └── src │ └── main.rs ├── 92. Reverse Linked List II ├── Cargo.toml └── src │ └── main.rs ├── 927. Three Equal Parts ├── Cargo.toml └── src │ └── main.rs ├── 93. Restore IP Addresses └── Solution.cpp ├── 931. Maximum Frequency Stack ├── Cargo.toml └── src │ └── lib.rs ├── 932. Beautiful Array └── Solution.cpp ├── 94. Binary Tree Inorder Traversal └── Solution.cpp ├── 95. Unique Binary Search Trees II └── Solution.cpp ├── 96. Unique Binary Search Trees └── Solution.cpp ├── 97. Interleaving String └── Solution.cpp ├── 98. Validate Binary Search Tree └── Solution.cpp ├── 99. Recover Binary Search Tree └── Solution.cpp ├── 995. Minimum Number of K Consecutive Bit Flips ├── Cargo.toml └── src │ └── main.rs ├── Cyclic Sort ├── 268. Missing Number │ ├── Cargo.toml │ └── src │ │ └── main.rs └── 287. Find the Duplicate Number │ ├── Cargo.toml │ └── src │ └── main.rs ├── Dynamic Programming ├── 321. Create Maximum Number │ ├── Cargo.toml │ └── src │ │ └── main.rs └── 887. Super Egg Drop │ ├── Cargo.toml │ ├── Solution.cpp │ └── src │ └── main.rs ├── LICENSE ├── Merge Intervals ├── 56. Merge Intervals │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 57. Insert Interval │ ├── Cargo.toml │ └── src │ │ └── main.rs └── 986. Interval List Intersections │ ├── Cargo.toml │ └── src │ └── main.rs ├── README.md ├── Sliding Window ├── 395. Longest Substring with At Least K Repeating Characters │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 76. Minimum Window Substring │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 904. Fruit Into Baskets │ ├── Cargo.toml │ └── src │ │ └── main.rs └── 992. Subarrays with K Different Integers │ ├── Cargo.toml │ └── src │ └── main.rs ├── Topological Sort ├── 207. Course Schedule │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 210. Course Schedule II │ ├── Cargo.toml │ └── src │ │ └── main.rs └── 310. Minimum Height Trees │ ├── Cargo.toml │ └── src │ └── main.rs ├── Tree ├── 103. Binary Tree Zigzag Level Order Traversal │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 111. Minimum Depth of Binary Tree │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 113. Path Sum II │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 236. Lowest Common Ancestor of a Binary Tree │ └── Solution.cpp └── 938. Range Sum of BST │ ├── Cargo.toml │ └── src │ └── main.rs ├── Two Heaps ├── 295. Find Median from Data Stream │ ├── Cargo.toml │ └── src │ │ └── main.rs └── 480. Sliding Window Median │ ├── Cargo.toml │ ├── expected │ ├── output │ └── src │ └── main.rs ├── Two Pointers ├── 202. Happy Number │ ├── Cargo.toml │ ├── Solution.cpp │ └── src │ │ └── main.rs ├── 234. Palindrome Linked List │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 457. Circular Array Loop │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 713. Subarray Product Less Than K │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 75. Sort Colors │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 844. Backspace String Compare │ ├── Cargo.toml │ ├── Solution.cpp │ └── src │ │ └── main.rs └── 977. Squares of a Sorted Array │ ├── Cargo.toml │ └── src │ └── main.rs ├── Weekly Contest 173 └── 5319. Remove Palindromic Subsequences │ ├── Cargo.toml │ └── src │ └── main.rs ├── Weekly Contest 174 └── 5328. The K Weakest Rows in a Matrix │ ├── Cargo.toml │ └── src │ └── main.rs ├── Weekly Contest 177 ├── 1360. Number of Days Between Two Dates │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 1361. Validate Binary Tree Nodes │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 1362. Closest Divisors │ ├── Cargo.toml │ └── src │ │ └── main.rs └── 1363. Largest Multiple of Three │ ├── Cargo.toml │ └── src │ └── main.rs ├── Weekly Contest 178 ├── 1365. How Many Numbers Are Smaller Than the Current Number │ └── Solution.cpp ├── 1366. Rank Teams by Votes │ └── Solution.cpp ├── 1367. Linked List in Binary Tree │ └── Solution.cpp └── 1368. Minimum Cost to Make at Least One Valid Path in a Grid │ └── Solution.cpp ├── Weekly Contest 179 ├── 1374. Generate a String With Characters That Have Odd Counts │ └── Solution.cpp ├── 1375. Bulb Switcher III │ └── Solution.cpp ├── p3 │ └── Solution.cpp └── p4 │ └── Solution.cpp ├── Weekly Contest 180 ├── 1380. Lucky Numbers in a Matrix │ └── Solution.cpp ├── 1381. Design a Stack With Increment Operation │ └── Solution.cpp ├── 1382. Balance a Binary Search Tree │ └── Solution.cpp └── 1383. Maximum Performance of a Team │ └── Solution.cpp ├── Weekly Contest 181 ├── 1389. Create Target Array in the Given Order │ └── Solution.cpp ├── 1390. Four Divisors │ └── Solution.cpp ├── 1391. Check if There is a Valid Path in a Grid │ ├── Cargo.toml │ ├── Solution.cpp │ └── src │ │ └── main.rs └── 1392. Longest Happy Prefix │ ├── Solution.cpp │ └── in ├── Weekly Contest 182 ├── 1394. Find Lucky Integer in an Array │ └── Solution.cpp ├── 1395. Count Number of Teams │ └── Solution.cpp ├── 1396. Design Underground System │ └── Solution.cpp └── 1397. Find All Good Strings │ └── Solution.cpp ├── Weekly Contest 183 ├── p1 │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── p2 │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── p3 │ ├── Cargo.toml │ └── src │ │ └── main.rs └── p4 │ ├── Cargo.toml │ └── src │ └── main.rs ├── cn ├── 1305. 两棵二叉搜索树中的所有元素 │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 1672. 最富有客户的资产总量 │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 385. 迷你语法分析器 │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 386. 字典序排数 │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 388. 文件的最长绝对路径 │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 396. 旋转函数 │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 479. 最大回文数乘积 │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 591. 标签验证器 │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 739. 每日温度 │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 819. 最常见的单词 │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 821. 字符的最短距离 │ ├── Cargo.toml │ └── src │ │ └── main.rs └── 824. 山羊拉丁文 │ ├── Cargo.toml │ └── src │ └── main.rs ├── image ├── 61.png ├── 61buf.png ├── 61split.png ├── LongestSubstringWithoutRepeatingCharacters.png ├── atoi.png └── integer_reverse.png ├── notes ├── book.toml └── src │ ├── SUMMARY.md │ ├── day1.md │ ├── day1 │ ├── lc27.md │ └── lc704.md │ ├── day10.md │ ├── day10 │ ├── lc232.md │ └── lc255.md │ ├── day11.md │ ├── day11 │ ├── lc1047.md │ ├── lc150.md │ └── lc20.md │ ├── day12.md │ ├── day13.md │ ├── day13 │ ├── lc239.md │ └── lc347.md │ ├── day14.md │ ├── day14 │ └── lc144.md │ ├── day15.md │ ├── day15 │ ├── lc101.md │ ├── lc102.md │ └── lc226.md │ ├── day16.md │ ├── day16 │ ├── lc104.md │ ├── lc111.md │ └── lc222.md │ ├── day17.md │ ├── day17 │ ├── lc110.md │ ├── lc257.md │ └── lc404.md │ ├── day18.md │ ├── day18 │ ├── lc106.md │ ├── lc112.md │ └── lc513.md │ ├── day19.md │ ├── day2.md │ ├── day2 │ ├── lc209.md │ ├── lc59.md │ └── lc977.md │ ├── day20.md │ ├── day20 │ ├── lc617.md │ ├── lc654.md │ ├── lc700.md │ └── lc98.md │ ├── day21.md │ ├── day21 │ ├── lc236.md │ ├── lc501.md │ └── lc530.md │ ├── day22.md │ ├── day22 │ ├── lc235.md │ ├── lc450.md │ └── lc701.md │ ├── day23.md │ ├── day23 │ ├── lc108.md │ ├── lc538.md │ └── lc669.md │ ├── day24.md │ ├── day24 │ └── lc77.md │ ├── day25.md │ ├── day25 │ ├── lc17.md │ └── lc216.md │ ├── day27.md │ ├── day27 │ ├── lc131.md │ ├── lc39.md │ └── lc40.md │ ├── day28.md │ ├── day28 │ ├── lc78.md │ ├── lc90.md │ └── lc93.md │ ├── day29.md │ ├── day29 │ ├── lc46.md │ ├── lc47.md │ └── lc491.md │ ├── day3.md │ ├── day3 │ ├── lc203.md │ ├── lc206.md │ └── lc707.md │ ├── day30.md │ ├── day30 │ ├── lc332.md │ ├── lc37.md │ └── lc51.md │ ├── day31.md │ ├── day31 │ ├── lc376.md │ ├── lc455.md │ └── lc53.md │ ├── day32.md │ ├── day32 │ ├── lc122.md │ ├── lc45.md │ └── lc55.md │ ├── day34.md │ ├── day34 │ ├── lc1005.md │ ├── lc134.md │ └── lc135.md │ ├── day35.md │ ├── day35 │ ├── lc406.md │ ├── lc452.md │ └── lc860.md │ ├── day36.md │ ├── day36 │ ├── lc435.md │ ├── lc56.md │ └── lc763.md │ ├── day37.md │ ├── day37 │ ├── lc738.md │ └── lc968.md │ ├── day38.md │ ├── day38 │ ├── lc509.md │ ├── lc70.md │ └── lc746.md │ ├── day39.md │ ├── day39 │ ├── lc62.md │ └── lc63.md │ ├── day4.md │ ├── day4 │ ├── lc02.07.md │ ├── lc142.md │ ├── lc19.md │ └── lc24.md │ ├── day41.md │ ├── day41 │ ├── lc343.md │ └── lc96.md │ ├── day42.md │ ├── day42 │ └── lc416.md │ ├── day43.md │ ├── day43 │ ├── lc1049.md │ ├── lc474.md │ └── lc494.md │ ├── day44.md │ ├── day44 │ ├── lc377.md │ └── lc518.md │ ├── day45.md │ ├── day45 │ ├── lc279.md │ ├── lc322.md │ └── lc70.md │ ├── day46.md │ ├── day46 │ └── lc139.md │ ├── day48.md │ ├── day48 │ ├── lc198.md │ ├── lc213.md │ └── lc337.md │ ├── day49.md │ ├── day49 │ ├── lc121.md │ └── lc122.md │ ├── day5.md │ ├── day50.md │ ├── day50 │ ├── lc123.md │ └── lc188.md │ ├── day51.md │ ├── day51 │ ├── lc309.md │ └── lc714.md │ ├── day52.md │ ├── day52 │ ├── lc300.md │ ├── lc674.md │ └── lc718.md │ ├── day53.md │ ├── day53 │ ├── lc1035.md │ ├── lc1143.md │ └── lc53.md │ ├── day55.md │ ├── day55 │ ├── lc115.md │ ├── lc392.md │ └── lc583.md │ ├── day56.md │ ├── day56 │ ├── lc516.md │ ├── lc647.md │ └── lc72.md │ ├── day57.md │ ├── day58.md │ ├── day58 │ ├── lc42.md │ ├── lc496.md │ ├── lc503.md │ ├── lc739.md │ └── lc84.md │ ├── day59.md │ ├── day6.md │ ├── day6 │ ├── lc1.md │ ├── lc202.md │ ├── lc242.md │ └── lc349.md │ ├── day60.md │ ├── day7.md │ ├── day7 │ ├── lc15.md │ ├── lc18.md │ ├── lc383.md │ └── lc454.md │ ├── day8.md │ ├── day8 │ ├── lc151.md │ ├── lc344.md │ ├── lc541.md │ ├── offer5.md │ └── offer58.md │ ├── day9.md │ ├── day9 │ ├── lc28.md │ └── lc459.md │ ├── lc.md │ └── remains.md └── script.sh /.gitignore: -------------------------------------------------------------------------------- 1 | a.py 2 | data.txt 3 | saved_dictionary.pkl 4 | .gitignore 5 | *.lock 6 | target/ 7 | book 8 | -------------------------------------------------------------------------------- /1. Two Sum/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "two_sum" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /1. Two Sum/README.md: -------------------------------------------------------------------------------- 1 | # 题意 2 | 3 | 返回的是元素的下标而不是元素值,不可排序。 4 | 5 | -------------------------------------------------------------------------------- /1. Two Sum/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | class Solution { 12 | public: 13 | // O(n) using hashmap 14 | vector twoSum(vector& nums, int target) { 15 | unordered_map map; 16 | for (int i=0; isecond, i}; 19 | map[nums[i]] = i; 20 | } 21 | } 22 | }; 23 | 24 | int main() { 25 | Solution s; 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /1. Two Sum/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | assert_eq!(Solution::two_sum(vec![2, 7, 11, 15], 9), [0, 1]); 3 | } 4 | 5 | struct Solution {} 6 | 7 | impl Solution { 8 | pub fn two_sum(nums: Vec, target: i32) -> Vec { 9 | use std::collections::HashMap; 10 | let mut map: HashMap = HashMap::new(); 11 | for i in 0..nums.len() { 12 | let pair = target - nums[i]; 13 | match map.get(&pair) { 14 | None => { map.insert(nums[i], i as i32); } 15 | Some(&idx) => { return vec![idx, i as i32] } 16 | } 17 | } 18 | unimplemented!() 19 | } 20 | } 21 | 22 | #[cfg(test)] 23 | mod test { 24 | use crate::*; 25 | 26 | #[test] 27 | fn basic() { 28 | assert_eq!(Solution::two_sum(vec![2, 7, 11, 15], 9), [0, 1]); 29 | } 30 | } -------------------------------------------------------------------------------- /10. Regular Expression Matching/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "regular_expression_matching" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /1004. Max Consecutive Ones III/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "max_consecutive_ones_three" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /1044. Longest Duplicate Substring/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "longest_duplicate_substring" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /1046. Last Stone Weight/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "last_stone_weight" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /1046. Last Stone Weight/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | class Solution { 15 | public: 16 | int lastStoneWeight(vector& stones) { 17 | priority_queue pq (stones.begin(), stones.end()); 18 | while (pq.size() >= 2) { 19 | int x = pq.top(); pq.pop(); 20 | int y = pq.top(); pq.pop(); 21 | if (x > y) { 22 | pq.push(x - y); 23 | } 24 | } 25 | return pq.size() == 0 ? 0 : pq.top(); 26 | } 27 | }; 28 | 29 | 30 | int main() { 31 | Solution a; 32 | 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /1046. Last Stone Weight/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("Hello, world!"); 3 | } 4 | 5 | struct Solution {} 6 | 7 | impl Solution { 8 | pub fn last_stone_weight(stones: Vec) -> i32 { 9 | use std::collections::BinaryHeap; 10 | let mut stones = BinaryHeap::from(stones); 11 | while stones.len() >= 2 { 12 | let x = stones.pop().unwrap(); 13 | let y = stones.pop().unwrap(); 14 | // x is guaranteed to >= y 15 | if x > y { 16 | stones.push(x - y); 17 | } 18 | } 19 | if let Some(x) = stones.pop() { 20 | x 21 | } else { 22 | 0 23 | } 24 | } 25 | } 26 | 27 | #[cfg(test)] 28 | mod test { 29 | use crate::*; 30 | 31 | #[test] 32 | fn basic() { 33 | assert_eq!(Solution::last_stone_weight(vec![2,7,4,1,8,1]), 1); 34 | } 35 | } -------------------------------------------------------------------------------- /11. Container With Most Water/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "container_with_most_water" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /11. Container With Most Water/REAMDE.md: -------------------------------------------------------------------------------- 1 | ## Insigths 2 | 3 | if v[i] < v[j], then for any pair (v[i], v[x], i < x < j) the water it can contain is <= the water the current pair can contain. 4 | 5 | [Discuss](https://leetcode.com/problems/container-with-most-water/discuss/6099/yet-another-way-to-see-what-happens-in-the-on-algorithm) 6 | 7 | ```cpp 8 | #include 9 | 10 | class Solution { 11 | public: 12 | int maxArea(vector& v) { 13 | // v.size() is at least 2 14 | int i = 0, j = v.size() - 1; 15 | int max = 0; 16 | while (i < j) { 17 | int tmp = (j - i) * std::min(v[i], v[j]); 18 | max = tmp > max ? tmp : max; 19 | if ( v[i] < v[j] ) i++; 20 | else j--; 21 | } 22 | return max; 23 | } 24 | }; 25 | ``` -------------------------------------------------------------------------------- /11. Container With Most Water/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("Hello, world!"); 3 | Solution::max_area(vec![1,2,3]); 4 | } 5 | 6 | struct Solution {} 7 | 8 | impl Solution { 9 | pub fn max_area(height: Vec) -> i32 { 10 | use std::cmp; 11 | let (mut l, mut r) = (0, height.len() - 1); 12 | let mut area = 0; 13 | while l < r { 14 | area = cmp::max(area, cmp::min(height[l], height[r]) * (r - l) as i32); 15 | if height[l] < height[r] { 16 | l += 1; 17 | } else { 18 | r -= 1; 19 | } 20 | } 21 | area 22 | } 23 | } 24 | 25 | #[cfg(test)] 26 | mod test { 27 | use super::*; 28 | 29 | #[test] 30 | fn basics() { 31 | assert_eq!(Solution::max_area(vec![1,8,6,2,5,4,8,3,7]), 49); 32 | // assert_eq!(Solution::max_area(vec![1,2,3,4,5,6,7,8,9]), 8); 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /12. Integer to Roman/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "integer_to_roman" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /120. Triangle/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "triangle" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /125. Valid Palindrome/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "_125" 3 | version = "0.1.0" 4 | edition = "2018" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /1254. Number of Closed Islands/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "number_of_closed_islands" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /1289. Day of the Week/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1289" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /13. Roman to Integer/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "roman_to_integer" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /1301. Reformat Department Table/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1301" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /131. Palindrome Partitioning/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "palindrome_partitioning" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /133. Clone Graph/go.mod: -------------------------------------------------------------------------------- 1 | module solution 2 | 3 | go 1.17 4 | -------------------------------------------------------------------------------- /133. Clone Graph/solution_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func Test1(t *testing.T) { 8 | node1 := new(Node) 9 | node1.Val = 1 10 | node2 := new(Node) 11 | node2.Val = 2 12 | node1.Neighbors = append(node1.Neighbors,node2) 13 | node2.Neighbors = append(node2.Neighbors,node1) 14 | res := cloneGraph(node1) 15 | if res == node1 { 16 | t.Fatalf(``) 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /135. Candy/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "candy" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /136. Single Number/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "single_number" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /136. Single Number/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("Hello, world!"); 3 | } 4 | 5 | struct Solution {} 6 | 7 | impl Solution { 8 | /// You should remember two things from this: 9 | // x xor x = 0 10 | // 0 xor x = x. 11 | // Now you have two more xor properties you can use to solve problems. Good Luck! 12 | pub fn single_number(nums: Vec) -> i32 { 13 | nums.iter().fold(0, |s, x| s^x) 14 | } 15 | } 16 | 17 | #[cfg(test)] 18 | mod test { 19 | use crate::*; 20 | 21 | #[test] 22 | fn basic() { 23 | assert_eq!(Solution::single_number(vec![1,2,3,2,1]), 3); 24 | } 25 | } -------------------------------------------------------------------------------- /14. Longest Common Prefix/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "longest_common_prefix" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /1444. Number of Steps to Reduce a Number to Zero/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1444" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /15. 3Sum/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "three_sum" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /155. Min Stack/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "min_stack" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /1575. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1575" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /16. 3Sum Closest/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "three_sum_closest" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /1632. Number of Good Ways to Split a String/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1632" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /1658. Minimum Swaps to Arrange a Binary Grid/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1658" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /1663. Detect Cycles in 2D Grid/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1663" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /1663. Detect Cycles in 2D Grid/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub fn get_smallest_string(n: i32, k: i32) -> String { 2 | let a = (k - n) / 25; // cnt of ending zero 3 | let b = (((k - n) % 25) as u8 + 'a' as u8) as char; // if valid, the char not a/z 4 | std::iter::repeat('a') 5 | .take(((n - a) as usize).saturating_sub(1)) 6 | .chain(std::iter::once(b).take((n - a) as usize)) 7 | .chain(std::iter::repeat('z').take(a as usize)) 8 | .collect() 9 | } 10 | 11 | #[cfg(test)] 12 | mod tests { 13 | use super::*; 14 | 15 | #[test] 16 | fn it_works() { 17 | let result = get_smallest_string(3, 27); 18 | assert_eq!(result, "aay".to_string()); 19 | let result = get_smallest_string(5, 73); 20 | assert_eq!(result, "aaszz".to_string()); 21 | let result = get_smallest_string(5, 130); 22 | assert_eq!(result, "zzzzz".to_string()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /1664. Find Users With Valid E-Mails/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1664" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /1669. Minimum Cost to Cut a Stick/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1669" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /168. Excel Sheet Column Title/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "_168" 3 | version = "0.1.0" 4 | edition = "2018" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /17. Letter Combinations of a Phone Number/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "letter_combinations_of_a_phone_number" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /1750. Check If Two Expression Trees are Equivalent/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1750" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /1759. Find the Missing IDs/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1759" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /179. Largest Number/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "largest_number" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /179. Largest Number/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("Hello, world!"); 3 | } 4 | -------------------------------------------------------------------------------- /18. 4Sum/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "four_sum" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /1801. Average Time of Process per Machine/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1801" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /1802. Number of Students Unable to Eat Lunch/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1802" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /1803. Average Waiting Time/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1803" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /1807. Partitioning Into Minimum Number Of Deci-Binary Numbers/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1807" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /1813. Maximum Erasure Value/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1813" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /1814. Jump Game VI/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1814" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /1815. Checking Existence of Edge Length Limited Paths/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1815" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | rand = "0.7.2" -------------------------------------------------------------------------------- /1817. Calculate Money in Leetcode Bank/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1817" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /1819. Construct the Lexicographically Largest Valid Sequence/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1819" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /1824. Maximum Number of Eaten Apples/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1824" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /1825. Find Minimum Time to Finish All Jobs/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1825" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /1828. Count Ways to Distribute Candies/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1828" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /19. Remove Nth Node From End of List/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "remove_nth_node_from_end_of_list" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /198. House Robber/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "house_robber" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /198. House Robber/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | assert_eq!(Solution::rob(vec![1,2,3,1]), 4); 3 | } 4 | 5 | struct Solution {} 6 | 7 | impl Solution { 8 | 9 | // 1 dp 10 | pub fn rob(nums: Vec) -> i32 { 11 | if nums.len() == 0 { return 0 } 12 | if nums.len() <= 1 { return nums[0] } 13 | let mut dp = vec![0; nums.len()]; 14 | dp[0] = nums[0]; 15 | dp[1] = std::cmp::max(nums[0], nums[1]); 16 | for i in 2..nums.len() { 17 | dp[i] = std::cmp::max(dp[i-1], dp[i-2] + nums[i]); 18 | } 19 | dp[nums.len() - 1] 20 | } 21 | } 22 | 23 | #[cfg(test)] 24 | mod test { 25 | use crate::*; 26 | 27 | #[test] 28 | fn basic() { 29 | assert_eq!(Solution::rob(vec![1,2,3,1]), 4); 30 | assert_eq!(Solution::rob(vec![2,7,9,3,1]), 12); 31 | assert_eq!(Solution::rob(vec![4]), 4); 32 | } 33 | } -------------------------------------------------------------------------------- /2. Add Two Numbers/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "add_two_numbers" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /20. Valid Parentheses/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "valid_parenthese" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /200. Number of Islands/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "number_of_islands" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /2027. Maximum Number of Removable Characters/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p2027" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /2032. Largest Odd Number in String/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p2032" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /2037. Count Square Sum Triples/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p2037" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /2037. Count Square Sum Triples/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub struct Solution {} 2 | 3 | impl Solution { 4 | pub fn min_moves_to_seat(mut seats: Vec, mut students: Vec) -> i32 { 5 | seats.sort(); 6 | students.sort(); 7 | seats.iter().zip(students).fold(0, |acc, (&x, y)| { 8 | acc + (x-y).abs() 9 | }) 10 | } 11 | } 12 | 13 | #[cfg(test)] 14 | mod tests { 15 | use crate::Solution; 16 | 17 | #[test] 18 | fn it_works() { 19 | assert_eq!(Solution::min_moves_to_seat(vec![3,1,5], vec![2,7,4]), 4); 20 | assert_eq!(Solution::min_moves_to_seat(vec![4,1,5,9], vec![1,3,2,6]), 7); 21 | assert_eq!(Solution::min_moves_to_seat(vec![2,2,6,6], vec![1,3,2,6]), 4); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /2042. Maximum Product Difference Between Two Pairs/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p2042" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /21. Merge Two Sorted Lists/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "merge_two_sorted_lists" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /213. House Robber II/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "house_robber_two" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /217. Contains Duplicate/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "contains_duplicate" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /217. Contains Duplicate/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | class Solution { 15 | public: 16 | bool containsDuplicate(vector& nums) { 17 | unordered_map map; 18 | for (int i : nums) { 19 | if (map.find(i) == map.end()) { 20 | map[i] = 1; 21 | } else { 22 | return true; 23 | } 24 | } 25 | return false; 26 | } 27 | }; 28 | 29 | int main() { 30 | Solution a; 31 | 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /217. Contains Duplicate/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | assert_eq!(Solution::contains_duplicate(vec![1,2,3,1]), true); 3 | } 4 | 5 | struct Solution {} 6 | 7 | impl Solution { 8 | pub fn contains_duplicate(nums: Vec) -> bool { 9 | use std::collections::HashMap; 10 | let mut map: HashMap = HashMap::new(); 11 | for i in nums { 12 | if map.contains_key(&i) { 13 | return true; 14 | } else { 15 | map.insert(i, 1); 16 | } 17 | } 18 | false 19 | } 20 | } 21 | 22 | #[cfg(test)] 23 | mod test { 24 | use crate::*; 25 | 26 | #[test] 27 | fn basic() { 28 | assert_eq!(Solution::contains_duplicate(vec![1,2,3,1]), true); 29 | assert_eq!(Solution::contains_duplicate(vec![1,2,3,4]), false); 30 | assert_eq!(Solution::contains_duplicate(vec![1,1,1,3,3,4,3,2,4,2]), true); 31 | } 32 | } -------------------------------------------------------------------------------- /2180. Maximum Number of Tasks You Can Assign/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p2180" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /219. Contains Duplicate II/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "contains_duplicate_two" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /22. Generate Parentheses/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "generate_parentheses" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /220. Contains Duplicate III/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "contains_duplicate_three" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /220. Contains Duplicate III/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("Hello, world!"); 3 | } 4 | -------------------------------------------------------------------------------- /2287. Minimum Number of Lines to Cover Points/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p2287" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /2299. Merge Nodes in Between Zeros/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /2299. Merge Nodes in Between Zeros/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p2299" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /23. Merge k Sorted Lists/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "merge_k_sorted_lists" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /2303. Unique Substrings With Equal Digit Frequency/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p2303" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /2303. Unique Substrings With Equal Digit Frequency/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub fn calculate_tax(brackets: Vec>, mut income: i32) -> f64 { 2 | let mut res = 0f64; 3 | let mut prevt = 0; 4 | for i in brackets { 5 | let t = i[0]; 6 | let p = i[1] as f64 / 100f64; 7 | if income <= t - prevt { 8 | res += income as f64 * p; 9 | break 10 | } else { 11 | income -= t - prevt; 12 | res += (t - prevt) as f64 * p; 13 | prevt = t; 14 | } 15 | } 16 | res 17 | } 18 | 19 | #[cfg(test)] 20 | mod tests { 21 | use super::*; 22 | 23 | #[test] 24 | fn it_works() { 25 | assert_eq!(calculate_tax(vec![vec![3,50],vec![7,10],vec![12,25]], 10), 2.65000); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /2309. Maximize Number of Subsequences in a String/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p2309" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /2309. Maximize Number of Subsequences in a String/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub fn greatest_letter(s: String) -> String { 2 | let mut up = 0usize; 3 | let mut low = 0usize; 4 | s.chars().for_each(|x| { 5 | if x.is_uppercase() { 6 | up |= 1 << (x as u8 - 'A' as u8); 7 | } else { 8 | low |= 1 << (x as u8 - 'a' as u8); 9 | } 10 | }); 11 | let i = core::mem::size_of::() * 8 - (up & low).leading_zeros() as usize; 12 | if i == 0 { return "".into() } 13 | ((i as u8 + 'A' as u8 - 1) as char).into() 14 | } 15 | 16 | #[cfg(test)] 17 | mod tests { 18 | use super::*; 19 | 20 | #[test] 21 | fn it_works() { 22 | let result = greatest_letter("lEeTcOdE".into()); 23 | // assert_eq!(result, 4); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /2315. The Change in Global Rankings/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p2315" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /2315. The Change in Global Rankings/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub fn count_asterisks(s: String) -> i32 { 2 | s.split('|') 3 | .enumerate() 4 | .filter_map(|(x, y)| if x & 1 == 0 { Some(y) } else { None }) 5 | .fold(0, |acc, x| acc + x.chars().filter(|&x| x == '*').count()) 6 | as i32 7 | } 8 | 9 | #[cfg(test)] 10 | mod tests { 11 | use super::*; 12 | 13 | #[test] 14 | fn it_works() { 15 | let result = count_asterisks("l|*e*et|c**o|*de|".into()); 16 | assert_eq!(result, 2); 17 | let result = count_asterisks("iamprogrammer".into()); 18 | assert_eq!(result, 0); 19 | let result = count_asterisks("yo|uar|e**|b|e***au|tifu|l".into()); 20 | assert_eq!(result, 5); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /2351. Number of Ways to Buy Pens and Pencils/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p2351" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /2351. Number of Ways to Buy Pens and Pencils/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub struct Solution {} 2 | 3 | impl Solution { 4 | pub fn repeated_character(s: String) -> char { 5 | let mut v = vec![0; 256]; 6 | for c in s.chars() { 7 | let i = c as usize; 8 | dbg!(i); 9 | if v[i] == 1 { return c } 10 | else { v[i] = 1 } 11 | } 12 | unreachable!() 13 | } 14 | } 15 | 16 | #[cfg(test)] 17 | mod tests { 18 | use crate::Solution; 19 | 20 | #[test] 21 | fn it_works() { 22 | Solution::repeated_character("abcdsa".into()); 23 | let result = 2 + 2; 24 | assert_eq!(result, 5); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /238. Product of Array Except Self/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "product_of_array_except_self" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /238. Product of Array Except Self/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | class Solution { 15 | public: 16 | vector productExceptSelf(vector& nums) { 17 | int l = nums.size(); 18 | vector res (l, 1); 19 | int x = 1; 20 | for (int i = 1; i < l; i ++ ) 21 | res[i] = x *= nums[i - 1]; 22 | x = 1; 23 | for (int i = l - 2; i >= 0; i -- ) 24 | res[i] *= x *= nums[i + 1]; 25 | return res; 26 | } 27 | }; 28 | 29 | int main() { 30 | Solution a; 31 | vector v = {1,2,3,4}; 32 | a.productExceptSelf(v); 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /238. Product of Array Except Self/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | assert_eq!(Solution::product_except_self(vec![1,2,3,4]), [24,12,8,6]); 3 | } 4 | 5 | struct Solution {} 6 | 7 | impl Solution { 8 | /// use the output vector to achieve O(n) space 9 | pub fn product_except_self(nums: Vec) -> Vec { 10 | let mut res = vec![1]; 11 | let mut x = 1; 12 | for i in 0..nums.len() - 1 { 13 | x *= nums[i]; 14 | res.push(x); 15 | } 16 | let mut x = 1; 17 | for i in (0..nums.len()).rev() { 18 | res[i] = res[i] * x; 19 | x *= nums[i]; 20 | } 21 | res 22 | } 23 | } 24 | 25 | #[cfg(test)] 26 | mod test { 27 | use crate::*; 28 | 29 | #[test] 30 | fn basic() { 31 | assert_eq!(Solution::product_except_self(vec![1,2,3,4]), [24,12,8,6]); 32 | } 33 | } -------------------------------------------------------------------------------- /24. Swap Nodes in Pairs/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "swap_nodes_in_pairs" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /240. Search a 2D Matrix II/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | class Solution { 14 | public: 15 | bool searchMatrix(vector>& matrix, int target) { 16 | int x = matrix.size(); 17 | if (x < 1) return false; 18 | int y = matrix[0].size(); 19 | if (y < 1) return false; 20 | int i = x - 1, j = 0; 21 | while (i >= 0 and j < y) { 22 | if (matrix[i][j] == target) return true; 23 | else if (matrix[i][j] < target) j ++ ; 24 | else if (matrix[i][j] > target) i -- ; 25 | } 26 | return false; 27 | } 28 | }; 29 | 30 | int main() { 31 | Solution a; 32 | 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /25. Reverse Nodes in k-Group/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "reverse_nodes_in_k_group" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /26. Remove Duplicates from Sorted Array/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "remove_duplicates_from_sorted_array" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /26. Remove Duplicates from Sorted Array/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | Solution::remove_duplicates(&mut vec![1,1,2]); 3 | } 4 | 5 | struct Solution {} 6 | 7 | impl Solution { 8 | pub fn remove_duplicates(nums: &mut Vec) -> i32 { 9 | let mut vec = vec![]; 10 | for i in nums.iter() { 11 | if Some(i) != vec.last() { 12 | vec.push(*i); 13 | } 14 | } 15 | for i in 0..vec.len() { 16 | nums[i] = vec[i]; 17 | } 18 | vec.len() as i32 19 | } 20 | } 21 | 22 | #[cfg(test)] 23 | mod test { 24 | use crate::*; 25 | 26 | #[test] 27 | fn basic() { 28 | assert_eq!(Solution::remove_duplicates(&mut vec![1,1,2]), 2); 29 | } 30 | } -------------------------------------------------------------------------------- /27. Remove Element/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "remove_element" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /28. Implement strStr()/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "implement_strStr" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /29. Divide Two Integers/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "divide_two_integers" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /290. Word Pattern/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "_290" 3 | version = "0.1.0" 4 | edition = "2018" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /3. Longest Substring Without Repeating Characters/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "longest_substring" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /30-Day LeetCoding Challenge/Perform String Shifts/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "perform_string_shifts" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /30-Day LeetCoding Challenge/counting_elements/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "counting_elements" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /30. Substring with Concatenation of All Words/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "substirng_with_concatenation_of_all_words" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | indicatif = "0.13.0" 11 | stopwatch= "0.0.7" -------------------------------------------------------------------------------- /31. Next Permutation/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "next_permutation" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /316. Remove Duplicate Letters/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "remove_duplicate_letters" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /32. Longest Valid Parentheses/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "longest_valid_parentheses" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /33. Search in Rotated Sorted Array/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "search_in_rotated_sorted_array" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /337. House Robber III/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "house_robber_three" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /34. Find First and Last Position of Element in Sorted Array/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "find_first_and_last_position_of_element_in_sorted_array" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /347. Top K Frequent Elements/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "top_k_frequent_elements" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /35. Search Insert Position/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "search_insert_position" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /35. Search Insert Position/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | Solution::search_insert(vec![1,3,5,6], 5); 3 | } 4 | 5 | struct Solution {} 6 | 7 | impl Solution { 8 | pub fn search_insert(nums: Vec, target: i32) -> i32 { 9 | for (i, x) in nums.iter().enumerate() { 10 | if *x >= target { 11 | return i as i32 12 | } 13 | } 14 | nums.len() as i32 15 | } 16 | } 17 | 18 | #[cfg(test)] 19 | mod test { 20 | use crate::*; 21 | 22 | #[test] 23 | fn basic() { 24 | assert_eq!(Solution::search_insert(vec![1,3,5,6], 5), 2); 25 | assert_eq!(Solution::search_insert(vec![1,3,5,6], 2), 1); 26 | assert_eq!(Solution::search_insert(vec![1,3,5,6], 7), 4); 27 | assert_eq!(Solution::search_insert(vec![1,3,5,6], 0), 0); 28 | 29 | } 30 | } -------------------------------------------------------------------------------- /36. Valid Sudoku/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "valid_sudoku" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /368. Largest Divisible Subset/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p368" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /37. Sudoku Solver/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "sudoku_solver" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /378. Kth Smallest Element in a Sorted Matrix/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "kth_smallest_element_in_a_sorted_matrix" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /38. Count and Say/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "count_and_say" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /39. Combination Sum/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "combination_sum" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /4. Median of Two Sorted Arrays/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "median_of_two_sorted_arrays" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /40. Combination Sum II/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "combination_sum_two" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /41. First Missing Positive/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "first_missing_positive" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /416. Partition Equal Subset Sum/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "partition_equal_subset_sum" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /42. Trapping Rain Water/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "trapping_rain_water" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /43. Multiply Strings/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "multiply_strings" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /44. Wildcard Matching/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "wildcard_matching" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /45. Jump Game II/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "jump_game_two" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /46. Permutations/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "permutations" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /47. Permutations II/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "permutations_two" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /48. Rotate Image/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rotate_image" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /49. Group Anagrams/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "group_anagrams" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /49. Group Anagrams/README.md: -------------------------------------------------------------------------------- 1 | 字符编码问题,组成相同的字符串要有相同的编码值 2 | 3 | 使用大基数进制的数字来表示近似hash -------------------------------------------------------------------------------- /5. Longest Palindromic Substring/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "palindromic_substring" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /50. Pow(x, n)/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "pow" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /51. N-Queens/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "n_queens" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /52. N-Queens II/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "n_queens_two" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /525. Contiguous Array/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "contiguous_array" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /525. Contiguous Array/README.md: -------------------------------------------------------------------------------- 1 | # Thoughts 2 | 3 | Stack ? Cannot 4 | 5 | Substring Problem Pattern ? Not 6 | 7 | Then what type of Problem is it? 8 | 9 | ## My first attempt 10 | 11 | Treat `0` as `-1` and `1` as `+1`. Then the whole binary sequence can be transferred into a height mountain digram. The solution is the same as finding the maximum distance between two same height. 12 | 13 | This can be Done in O(n^2). 14 | 15 | But unfortunately, it will cause TLE. 16 | 17 | ## My second attempt 18 | 19 | To get rid of the TLE, can we use DP? 20 | 21 | This is a (2n+1) x 1 one dimensional DP. 22 | 23 | The DP states are the two occurance of the current height. 24 | 25 | ## Insights 26 | 27 | The possible `Heights` are in the range of `[-n, +n]`, total 2n + 1 cases. 28 | 29 | So we can use a map to record the appearance of each height. -------------------------------------------------------------------------------- /53. Maximum Subarray/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "maximum_subarray" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /54. Spiral Matrix/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "spiral_matrix" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /55. Jump Game/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "jump_game" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /55. Jump Game/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("Hello, world!"); 3 | } 4 | 5 | struct Solution {} 6 | 7 | impl Solution { 8 | pub fn can_jump(nums: Vec) -> bool { 9 | let mut long = 0; 10 | let nums: Vec = nums.into_iter().map(|x| x as usize).collect(); 11 | for i in 0..nums.len() { 12 | if i + nums[i] > long { long = i + nums[i] } 13 | if long >= nums.len() - 1 { return true } 14 | if nums[i] == 0 && i == long { return false } 15 | } 16 | unimplemented!() 17 | } 18 | } 19 | 20 | #[cfg(test)] 21 | mod test { 22 | use crate::*; 23 | 24 | #[test] 25 | fn basic() { 26 | assert_eq!(Solution::can_jump(vec![2,3,1,1,4]), true); 27 | assert_eq!(Solution::can_jump(vec![3,2,1,0,4]), false); 28 | assert_eq!(Solution::can_jump(vec![0]), true); 29 | 30 | } 31 | } -------------------------------------------------------------------------------- /576. Out of Boundary Paths/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p576" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /58. Length of Last Word/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "length_of_last_word" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /59. Spiral Matrix II/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "spiral_matrix_two" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /6. ZigZag Conversion/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "zigzag" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /60. Permutation Sequence/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "permutation_sequence" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /605. Can Place Flowers/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "can_plant_flowers" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /61. Rotate List/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rotate_list" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /62. Unique Paths/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "unique_paths" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /62. Unique Paths/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | assert_eq!(Solution::unique_paths(3, 2), 3); 3 | } 4 | 5 | struct Solution {} 6 | 7 | impl Solution { 8 | pub fn unique_paths(m: i32, n: i32) -> i32 { 9 | let mut vec = vec![1; n as usize]; 10 | for _ in 0..m as usize - 1 { 11 | for i in 1..n as usize { 12 | vec[i] = vec[i] + vec[i-1]; 13 | } 14 | } 15 | vec[n as usize - 1] 16 | } 17 | } 18 | 19 | #[cfg(test)] 20 | mod test { 21 | use crate::*; 22 | 23 | #[test] 24 | fn basic() { 25 | assert_eq!(Solution::unique_paths(3, 2), 3); 26 | assert_eq!(Solution::unique_paths(7, 3), 28); 27 | assert_eq!(Solution::unique_paths(1, 2), 1); 28 | assert_eq!(Solution::unique_paths(2, 1), 1); 29 | } 30 | } -------------------------------------------------------------------------------- /63. Unique Paths II/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "unique_paths_two" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /639. Decode Ways II/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "decode_ways_two" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /64. Minimum Path Sum/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "minimum_path_sum" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /65. Valid Number/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "valid_number" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | regex = "1" -------------------------------------------------------------------------------- /66. Plus One/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "plus_one" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /66. Plus One/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("Hello, world!"); 3 | } 4 | 5 | struct Solution {} 6 | 7 | impl Solution { 8 | pub fn plus_one(mut digits: Vec) -> Vec { 9 | let mut i = digits.len() - 1; 10 | loop { 11 | if digits[i] != 9 { digits[i] += 1; break digits } 12 | /* digits[i] == 9 */ 13 | digits[i] = 0; 14 | if i == 0 { digits.insert(0, 1); break digits } 15 | i -= 1; 16 | } 17 | } 18 | } 19 | 20 | #[cfg(test)] 21 | mod test { 22 | use crate::*; 23 | 24 | #[test] 25 | fn basic() { 26 | assert_eq!(Solution::plus_one(vec![1,2,3]), vec![1,2,4]); 27 | assert_eq!(Solution::plus_one(vec![9,9]), vec![1,0,0]); 28 | } 29 | } -------------------------------------------------------------------------------- /67. Add Binary/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "add_binary" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /678. Valid Parenthesis String/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "valid_parenthesis_string" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /69. Sqrt(x)/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "_69" 3 | version = "0.1.0" 4 | edition = "2018" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /7. Reverse Integer/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "reverse_integer" 3 | version = "0.1.0" 4 | authors = ["aliyun "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /70. Climbing Stairs/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | class Solution { 13 | public: 14 | int climbStairs(int n) { 15 | vector fib (n+1, 1); 16 | for (int i=2; i<=n;i++) { 17 | fib[i] = fib[i-1] + fib[i-2]; 18 | } 19 | // for (int i: fib) cout << i; 20 | return fib[n]; 21 | } 22 | }; 23 | 24 | int main() { 25 | Solution a; 26 | 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /704. 二分查找/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "binary-search" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /71. Simplify Path/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | class Solution { 8 | public: 9 | string simplifyPath(string path) { 10 | string buf; 11 | vector stk; 12 | stringstream ss(path); 13 | while (getline(ss, buf, '/')) { 14 | if (buf == "" || buf == ".") continue; 15 | if (buf == ".." and not stk.empty()) stk.pop_back(); 16 | else if (buf != "..") stk.push_back(buf); 17 | } 18 | string res; 19 | for (auto str: stk) { 20 | res += '/' + str; 21 | } 22 | return res.empty() ? "/" : res; 23 | } 24 | }; 25 | 26 | 27 | int main() { 28 | Solution a; 29 | string x = a.simplifyPath("sg"); 30 | cout << x; 31 | } -------------------------------------------------------------------------------- /72. Edit Distance/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "edit_distance" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /74. Search a 2D Matrix/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "search_a_2d_matrix" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /753. Open the Lock/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p753" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /753. Open the Lock/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub struct Solution {} 2 | 3 | impl Solution { 4 | pub fn crack_safe(n: i32, k: i32) -> String { 5 | let node_num = k.pow(n as u32 - 1) as usize; 6 | let edge_num = k.pow(n as u32 ) as usize; 7 | let mut node = vec![k - 1; node_num]; // the max next of this node 8 | let mut res = "0".repeat(n as usize - 1).to_string(); 9 | let mut idx = 0; 10 | for _ in 0..edge_num { 11 | dbg!(idx, &node, &res); 12 | let edge = node[idx]; 13 | node[idx] -= 1; 14 | res.push_str(&edge.to_string()); 15 | idx = (idx * k as usize + edge as usize) % node_num; 16 | } 17 | res 18 | } 19 | } 20 | 21 | #[cfg(test)] 22 | mod tests { 23 | use crate::Solution; 24 | 25 | #[test] 26 | fn it_works() { 27 | assert_eq!(Solution::crack_safe(3, 3), "4"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /77. Combinations/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | // 19 8 | class Solution { 9 | public: 10 | vector> combine(int n, int k) { 11 | vector> v; 12 | vector buf; 13 | this->recur(n, k, v, buf); 14 | return v; 15 | } 16 | //recur 17 | void recur(int n, int k, vector> & v, vector & buf) { 18 | if ( k == 0 ) { 19 | v.push_back(buf); 20 | return; 21 | } else { 22 | for (int i=k; i<=n; i++) { 23 | buf.push_back(i); 24 | this->recur(i-1, k-1, v, buf); 25 | buf.pop_back(); 26 | } 27 | } 28 | } 29 | }; 30 | 31 | int main() { 32 | Solution a; 33 | } -------------------------------------------------------------------------------- /78. Subsets/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "subsets" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /78. Subsets/README.md: -------------------------------------------------------------------------------- 1 | # Permutations / Combinations / Subsets 2 | 3 | Given their exponential solution space, it is tricky to ensure that the generated solutions are **complete** and **non-redundant**. 4 | 5 | There are generally three strategies to do it: 6 | 7 | - Recursion 8 | - Backtracking 9 | - Lexicographic generation based on the mapping between **binary bitmasks** and the corresponding 10 | permutations / combinations / subsets. (this method has the best time complexity, and as a bonus, it generates lexicographically sorted output for the sorted inputs) 11 | 12 | ## Subset 13 | 14 | ![leetcode](https://leetcode.com/articles/Figures/78/recursion.png) 15 | 16 | **Recursion**, if I already know the subsets of the previous N items, how to construct the subset by adding just the N+1 item? 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /78. Subsets/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | class Solution { 9 | public: 10 | // bit mask 11 | vector> subsets(vector& nums) { 12 | int n = nums.size(), p = 1 << n; 13 | vector> sub(p); 14 | for (int i=0; i>j)&1 == 1) { 17 | sub[i].push_back(nums[j]); 18 | } 19 | } 20 | } 21 | return sub; 22 | } 23 | }; 24 | 25 | int main() { 26 | Solution a; 27 | } 28 | -------------------------------------------------------------------------------- /782. Transform to Chessboard/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "_782" 3 | version = "0.1.0" 4 | edition = "2018" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /784. Letter Case Permutation/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "letter_case_permutation" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /79. Word Search/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "word_search" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /8. String to Integer (atoi)/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "string_to_integer" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /80. Remove Duplicates from Sorted Array II/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "remove_duplicates_from_sorted_array_two" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /81. Search in Rotated Sorted Array II/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "search_in_rotated_sorted_array_two" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /82. Remove Duplicates from Sorted List II/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "remove_duplicates_from_sorted_list_two" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /82. Remove Duplicates from Sorted List II/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Definition for singly-linked list. 4 | struct ListNode { 5 | int val; 6 | ListNode *next; 7 | ListNode(int x) : val(x), next(NULL) {} 8 | }; 9 | 10 | class Solution { 11 | public: 12 | ListNode* deleteDuplicates(ListNode* head) { 13 | if (head == NULL || head->next == NULL) return head; 14 | auto p = head, c = head->next; 15 | while (c != NULL && p->val == c->val) c = c->next; 16 | if (c != p->next) return this->deleteDuplicates(c); 17 | auto rest = this->deleteDuplicates(c); 18 | p->next = rest; 19 | return p; 20 | } 21 | }; -------------------------------------------------------------------------------- /84. Largest Rectangle in Histogram/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "largest_rectangle_in_histogram" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /855. Count Unique Characters of All Substrings of a Given String/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p855" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /86. Partition List/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "partition_list" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /88. Merge Sorted Array/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "_88" 3 | version = "0.1.0" 4 | edition = "2018" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /9. Palindrome Number/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "palindrome_number" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /90. Subsets II/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "subsets_two" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /90. Subsets II/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | class Solution { 9 | public: 10 | vector> subsetsWithDup(vector& nums) { 11 | 12 | } 13 | }; 14 | 15 | int main() { 16 | Solution a; 17 | vector > totalset = {{}}; 18 | cout << totalset.size(); // 1 empty set 19 | } 20 | -------------------------------------------------------------------------------- /91. Decode Ways/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "decode_ways" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /92. Reverse Linked List II/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "reverse_linked_list_two" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /927. Three Equal Parts/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "three_equal_parts" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /931. Maximum Frequency Stack/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p931" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /932. Beautiful Array/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | class Solution { 14 | public: 15 | vector beautifulArray(int N) { 16 | vector res = {1}; 17 | while (res.size() < N) { 18 | vector tmp; 19 | for (int i : res) if (i * 2 - 1 <= N) tmp.push_back(i * 2 - 1); 20 | for (int i : res) if (i * 2 <= N) tmp.push_back(i * 2); 21 | res = tmp; 22 | } 23 | return res; 24 | } 25 | }; 26 | 27 | int main() { 28 | Solution a; 29 | 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /995. Minimum Number of K Consecutive Bit Flips/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "minimum_number_of_k_consecutive_bit_flips" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Cyclic Sort/268. Missing Number/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "missing_number" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Cyclic Sort/268. Missing Number/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | assert_eq!(Solution::missing_number(vec![3,0,1]), 2); 3 | } 4 | 5 | struct Solution {} 6 | 7 | impl Solution { 8 | pub fn missing_number(mut nums: Vec) -> i32 { 9 | nums.push(-1); 10 | let mut i = 0; 11 | while i < nums.len() { 12 | if nums[i] == i as i32 || nums[i] == -1 { i += 1; } 13 | else { 14 | let target = nums[i] as usize; 15 | nums.swap(i, target); 16 | } 17 | } 18 | nums.iter().position(|&x| x == -1).unwrap() as i32 19 | } 20 | } 21 | 22 | #[cfg(test)] 23 | mod test { 24 | use crate::*; 25 | 26 | #[test] 27 | fn basic() { 28 | assert_eq!(Solution::missing_number(vec![3,0,1]), 2); 29 | assert_eq!(Solution::missing_number(vec![9,6,4,2,3,5,7,0,1]), 8); 30 | 31 | } 32 | } -------------------------------------------------------------------------------- /Cyclic Sort/287. Find the Duplicate Number/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "find_the_duplicate_number" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Dynamic Programming/321. Create Maximum Number/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "create_maximum_number" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Dynamic Programming/887. Super Egg Drop/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "super_egg_drop" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Dynamic Programming/887. Super Egg Drop/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | class Solution { 15 | public: 16 | int superEggDrop(int K, int N) { 17 | vector oldv (K + 1, 0); 18 | vector newv (K + 1, 0); 19 | int cnt = 0; 20 | while (newv[K] < N) { 21 | for (int i = 1; i <= K; i ++) { 22 | newv[i] = oldv[i] + oldv[i - 1] + 1; 23 | } 24 | cnt ++ ; 25 | oldv.assign(newv.begin(), newv.end()); 26 | } 27 | return cnt; 28 | } 29 | }; 30 | 31 | int main() { 32 | Solution a; 33 | 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Merge Intervals/56. Merge Intervals/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "merge_intevals" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Merge Intervals/57. Insert Interval/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "insert_interval" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Merge Intervals/986. Interval List Intersections/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "internal_lists_intersections" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Sliding Window/395. Longest Substring with At Least K Repeating Characters/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "longest_substring_with_at_least_k_repeating_characters" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Sliding Window/76. Minimum Window Substring/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "minimum_window_substring" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Sliding Window/904. Fruit Into Baskets/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "fruit_into_baskets" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Sliding Window/992. Subarrays with K Different Integers/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "subarrays_with_k_different_integers" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Topological Sort/207. Course Schedule/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "course_schedule" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Topological Sort/210. Course Schedule II/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "course_schedule_two" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Topological Sort/310. Minimum Height Trees/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "minimum_height_trees" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Tree/103. Binary Tree Zigzag Level Order Traversal/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "binary_tree_zigzag_level_order_traversal" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Tree/111. Minimum Depth of Binary Tree/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "minimum_depth_of_binary_tree" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Tree/113. Path Sum II/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "path_sum_two" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Tree/938. Range Sum of BST/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "range_sum_of_BST" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Two Heaps/295. Find Median from Data Stream/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "find_median_from_data_stream" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Two Heaps/480. Sliding Window Median/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "sliding_window_median" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Two Pointers/202. Happy Number/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "happy_number" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Two Pointers/234. Palindrome Linked List/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "parlindrome_linked_list" 3 | version = "0.1.0" 4 | authors = ["ainevsia"] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Two Pointers/457. Circular Array Loop/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "circular_array_loop" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Two Pointers/713. Subarray Product Less Than K/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "subarray_product_less_than_k" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Two Pointers/75. Sort Colors/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "sort_solors" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Two Pointers/844. Backspace String Compare/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "backspace_string_compare" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Two Pointers/977. Squares of a Sorted Array/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "squares_of_a_sorted_array" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Weekly Contest 173/5319. Remove Palindromic Subsequences/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "remove_palindromic_subsequences" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Weekly Contest 174/5328. The K Weakest Rows in a Matrix/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "the_k_weakest_rows_in_a_matrix" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Weekly Contest 177/1360. Number of Days Between Two Dates/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Weekly Contest 177/1361. Validate Binary Tree Nodes/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p2" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Weekly Contest 177/1362. Closest Divisors/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p3" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Weekly Contest 177/1363. Largest Multiple of Three/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p4" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Weekly Contest 178/1365. How Many Numbers Are Smaller Than the Current Number/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | class Solution { 9 | public: 10 | vector smallerNumbersThanCurrent(vector& nums) { 11 | vector res (nums.size(), 0); 12 | for (int i=0; i< nums.size(); i++) { 13 | for (int j=0; j < nums.size(); j++) { 14 | if (nums[j] < nums[i]) {res[i] ++;} 15 | } 16 | } 17 | return res; 18 | } 19 | }; 20 | 21 | int main() { 22 | Solution s; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Weekly Contest 179/1374. Generate a String With Characters That Have Odd Counts/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | class Solution { 13 | public: 14 | string generateTheString(int n) { /* 1 <= n <= 500 */ 15 | string res(n-1,'a'); 16 | if (n%2) /* n is odd number */ 17 | res += 'a'; 18 | else 19 | res += 'b'; 20 | return res; 21 | } 22 | }; 23 | 24 | int main() { 25 | Solution s; 26 | s.generateTheString(1); 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Weekly Contest 179/1375. Bulb Switcher III/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | class Solution { 13 | public: 14 | // do not try to simulate in algorithm contest 15 | // find the inner regulation and abstraction 16 | int numTimesAllBlue(vector& light) { 17 | int res = 0, r = 0; 18 | for (int i=0; i a {2,1,3,5,4}; 27 | int x = s.numTimesAllBlue(a); 28 | cout << x; 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Weekly Contest 181/1389. Create Target Array in the Given Order/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | class Solution { 14 | public: 15 | vector createTargetArray(vector& nums, vector& index) { 16 | vector res; 17 | int len = nums.size(); 18 | for (int i=0; i"] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Weekly Contest 182/1397. Find All Good Strings/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | class Solution { 14 | public: 15 | int findGoodStrings(int n, string s1, string s2, string evil) { 16 | 17 | } 18 | }; 19 | 20 | int main() { 21 | Solution a; 22 | 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Weekly Contest 183/p1/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p1" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Weekly Contest 183/p1/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("Hello, world!"); 3 | } 4 | 5 | struct Solution {} 6 | 7 | impl Solution { 8 | pub fn min_subsequence(mut nums: Vec) -> Vec { 9 | nums.sort(); 10 | let mut total: i32 = nums.iter().sum(); 11 | let mut sum = 0; 12 | let mut res = vec![]; 13 | for &n in nums.iter().rev() { 14 | sum += n; 15 | total -= n; 16 | res.push(n); 17 | if sum > total { 18 | break 19 | } 20 | } 21 | res 22 | } 23 | } 24 | 25 | #[cfg(test)] 26 | mod test { 27 | use crate::*; 28 | 29 | #[test] 30 | fn basic() { 31 | assert_eq!(Solution::min_subsequence(vec![4,3,10,9,8]), [10,9]); 32 | assert_eq!(Solution::min_subsequence(vec![4,4,7,6,7]), [7,7,6]); 33 | assert_eq!(Solution::min_subsequence(vec![6]), [6]); 34 | 35 | } 36 | } -------------------------------------------------------------------------------- /Weekly Contest 183/p2/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p2" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Weekly Contest 183/p3/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p3" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Weekly Contest 183/p3/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("Hello, world!"); 3 | } 4 | -------------------------------------------------------------------------------- /Weekly Contest 183/p4/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p4" 3 | version = "0.1.0" 4 | authors = ["Ainevsia "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /Weekly Contest 183/p4/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("Hello, world!"); 3 | } 4 | -------------------------------------------------------------------------------- /cn/1305. 两棵二叉搜索树中的所有元素/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "all-elements-in-two-binary-search-trees" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /cn/1672. 最富有客户的资产总量/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "richest-customer-wealth" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /cn/1672. 最富有客户的资产总量/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("Hello, world!"); 3 | } 4 | 5 | struct Solution {} 6 | 7 | impl Solution { 8 | pub fn maximum_wealth(accounts: Vec>) -> i32 { 9 | accounts.iter().fold(0, |max_wealth, acc| { 10 | let wealth = acc.iter().sum(); 11 | if wealth > max_wealth { wealth } else { max_wealth } 12 | }) 13 | } 14 | } 15 | 16 | #[cfg(test)] 17 | mod test { 18 | use crate::*; 19 | 20 | #[test] 21 | fn basic() { 22 | assert_eq!(Solution::maximum_wealth(vec![vec![1,2,3],vec![3,2,1]]),6); 23 | assert_eq!(Solution::maximum_wealth(vec![vec![1,5],vec![7,3],vec![3,5]]),10); 24 | assert_eq!(Solution::maximum_wealth(vec![vec![2,8,7],vec![7,1,3],vec![1,9,5]]),17); 25 | } 26 | } -------------------------------------------------------------------------------- /cn/385. 迷你语法分析器/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "mini-parser" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /cn/386. 字典序排数/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "lexicographical-numbers" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /cn/388. 文件的最长绝对路径/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "longest-absolute-file-path" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /cn/396. 旋转函数/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rotate-function" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /cn/479. 最大回文数乘积/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "largest-palindrome-product" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /cn/591. 标签验证器/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "tag-validator" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /cn/739. 每日温度/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "daily-temperatures" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /cn/819. 最常见的单词/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "most-common-word" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /cn/821. 字符的最短距离/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "shortest-distance-to-a-character" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /cn/824. 山羊拉丁文/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "goat-latin" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /image/61.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ainevsia/Leetcode-Rust/57e687cf87b74360cbbbfc0f5954830b1a6fbc3c/image/61.png -------------------------------------------------------------------------------- /image/61buf.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ainevsia/Leetcode-Rust/57e687cf87b74360cbbbfc0f5954830b1a6fbc3c/image/61buf.png -------------------------------------------------------------------------------- /image/61split.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ainevsia/Leetcode-Rust/57e687cf87b74360cbbbfc0f5954830b1a6fbc3c/image/61split.png -------------------------------------------------------------------------------- /image/LongestSubstringWithoutRepeatingCharacters.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ainevsia/Leetcode-Rust/57e687cf87b74360cbbbfc0f5954830b1a6fbc3c/image/LongestSubstringWithoutRepeatingCharacters.png -------------------------------------------------------------------------------- /image/atoi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ainevsia/Leetcode-Rust/57e687cf87b74360cbbbfc0f5954830b1a6fbc3c/image/atoi.png -------------------------------------------------------------------------------- /image/integer_reverse.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ainevsia/Leetcode-Rust/57e687cf87b74360cbbbfc0f5954830b1a6fbc3c/image/integer_reverse.png -------------------------------------------------------------------------------- /notes/book.toml: -------------------------------------------------------------------------------- 1 | [book] 2 | authors = ["zhipengxu@sjtu.edu.cn"] 3 | language = "en" 4 | multilingual = false 5 | src = "src" 6 | title = "代码随想录算法训练营刷题笔记" 7 | -------------------------------------------------------------------------------- /notes/src/day10.md: -------------------------------------------------------------------------------- 1 | # 第五章 栈与队列part01 2 | 3 | 今日任务: 4 | ● 理论基础 5 | ● 232.用栈实现队列 6 | ● 225. 用队列实现栈 7 | 理论基础 8 | 9 | 了解一下 栈与队列的内部实现机智,文中是以C++为例讲解的。 10 | 11 | 文章讲解:https://programmercarl.com/%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html 12 | 13 | ## 232.用栈实现队列 14 | 15 | 大家可以先看视频,了解一下模拟的过程,然后写代码会轻松很多。 16 | 17 | 题目链接/文章讲解/视频讲解:https://programmercarl.com/0232.%E7%94%A8%E6%A0%88%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97.html 18 | 19 | 20 | ## 225. 用队列实现栈 21 | 22 | 可以大家惯性思维,以为还要两个队列来模拟栈,其实只用一个队列就可以模拟栈了。 23 | 24 | 建议大家掌握一个队列的方法,更简单一些,可以先看视频讲解 25 | 26 | 题目链接/文章讲解/视频讲解:https://programmercarl.com/0225.%E7%94%A8%E9%98%9F%E5%88%97%E5%AE%9E%E7%8E%B0%E6%A0%88.html 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /notes/src/day11.md: -------------------------------------------------------------------------------- 1 | # 第五章 栈与队列part02 2 | 今日内容: 3 | 4 | ● 20. 有效的括号 5 | ● 1047. 删除字符串中的所有相邻重复项 6 | ● 150. 逆波兰表达式求值 7 | 8 | 详细布置 9 | 10 | ## 20. 有效的括号 11 | 12 | 讲完了栈实现队列,队列实现栈,接下来就是栈的经典应用了。 13 | 14 | 大家先自己思考一下 有哪些不匹配的场景,在看视频 我讲的都有哪些场景,落实到代码其实就容易很多了。 15 | 16 | 题目链接/文章讲解/视频讲解:https://programmercarl.com/0020.%E6%9C%89%E6%95%88%E7%9A%84%E6%8B%AC%E5%8F%B7.html 17 | 18 | ## 1047. 删除字符串中的所有相邻重复项 19 | 20 | 栈的经典应用。 21 | 22 | 要知道栈为什么适合做这种类似于爱消除的操作,因为栈帮助我们记录了 遍历数组当前元素时候,前一个元素是什么。 23 | 24 | 题目链接/文章讲解/视频讲解:https://programmercarl.com/1047.%E5%88%A0%E9%99%A4%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E6%89%80%E6%9C%89%E7%9B%B8%E9%82%BB%E9%87%8D%E5%A4%8D%E9%A1%B9.html 25 | ## 150. 逆波兰表达式求值 26 | 27 | 本题不难,但第一次做的话,会很难想到,所以先看视频,了解思路再去做题 28 | 29 | 题目链接/文章讲解/视频讲解:https://programmercarl.com/0150.%E9%80%86%E6%B3%A2%E5%85%B0%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%B1%82%E5%80%BC.html 30 | 31 | -------------------------------------------------------------------------------- /notes/src/day11/lc1047.md: -------------------------------------------------------------------------------- 1 | # 1047. 删除字符串中的所有相邻重复项 2 | ## 题目描述 3 | 4 | 给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。 5 | 6 | 在 S 上反复执行重复项删除操作,直到无法继续删除。 7 | 8 | 在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。 9 | 10 | ## 解题思路 11 | 12 | ```rust 13 | struct Solution {} 14 | 15 | impl Solution { 16 | pub fn remove_duplicates(s: String) -> String { 17 | let mut v = vec![]; 18 | for c in s.chars() { 19 | if let Some(&x) = v.last() { 20 | if x == c { v.pop(); } 21 | else { v.push(c) } 22 | } else { v.push(c) } 23 | } 24 | v.iter().collect() 25 | } 26 | } 27 | ``` 28 | ## 学习感想 -------------------------------------------------------------------------------- /notes/src/day12.md: -------------------------------------------------------------------------------- 1 | # 休息日 2 | 3 | 小红书笔试薄砂我 -------------------------------------------------------------------------------- /notes/src/day13.md: -------------------------------------------------------------------------------- 1 | # 第五章 栈与队列part03 2 | 今日内容: 3 | 4 | ● 239. 滑动窗口最大值 5 | ● 347.前 K 个高频元素 6 | ● 总结 7 | 8 | 详细布置 9 | 10 | 11 | ## 239. 滑动窗口最大值 (一刷至少需要理解思路) 12 | 13 | 之前讲的都是栈的应用,这次该是队列的应用了。 14 | 15 | 本题算比较有难度的,需要自己去构造单调队列,建议先看视频来理解。 16 | 17 | 题目链接/文章讲解/视频讲解:https://programmercarl.com/0239.%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E6%9C%80%E5%A4%A7%E5%80%BC.html 18 | 19 | 20 | ## 347.前 K 个高频元素 (一刷至少需要理解思路) 21 | 22 | 大/小顶堆的应用, 在C++中就是优先级队列 23 | 24 | 本题是 大数据中取前k值 的经典思路,了解想法之后,不算难。 25 | 26 | 题目链接/文章讲解/视频讲解:https://programmercarl.com/0347.%E5%89%8DK%E4%B8%AA%E9%AB%98%E9%A2%91%E5%85%83%E7%B4%A0.html 27 | 28 | 29 | ## 总结 30 | 31 | 栈与队列做一个总结吧,加油 32 | 33 | https://programmercarl.com/%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97%E6%80%BB%E7%BB%93.html 34 | -------------------------------------------------------------------------------- /notes/src/day14.md: -------------------------------------------------------------------------------- 1 | # 第六章 二叉树part01 2 | 今日内容: 3 | 4 | ● 理论基础 5 | ● 递归遍历 6 | ● 迭代遍历 7 | ● 统一迭代 8 | 9 | 10 | 详细布置 11 | 12 | ## 理论基础 13 | 14 | 需要了解 二叉树的种类,存储方式,遍历方式 以及二叉树的定义 15 | 16 | 文章讲解:https://programmercarl.com/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html 17 | 18 | ## 递归遍历 (必须掌握) 19 | 20 | 二叉树的三种递归遍历掌握其规律后,其实很简单 21 | 22 | 题目链接/文章讲解/视频讲解:https://programmercarl.com/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E9%80%92%E5%BD%92%E9%81%8D%E5%8E%86.html 23 | 24 | ## 迭代遍历 (基础不好的录友,迭代法可以放过) 25 | 26 | 题目链接/文章讲解/视频讲解:https://programmercarl.com/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E8%BF%AD%E4%BB%A3%E9%81%8D%E5%8E%86.html 27 | 28 | ## 统一迭代 (基础不好的录友,迭代法可以放过) 29 | 30 | 这是统一迭代法的写法, 如果学有余力,可以掌握一下 31 | 32 | 题目链接/文章讲解:https://programmercarl.com/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E7%BB%9F%E4%B8%80%E8%BF%AD%E4%BB%A3%E6%B3%95.html 33 | -------------------------------------------------------------------------------- /notes/src/day15.md: -------------------------------------------------------------------------------- 1 | # 第六章 二叉树 part02 2 | 3 | 今日内容: 4 | 5 | ● 层序遍历 10 6 | ● 226.翻转二叉树 7 | ● 101.对称二叉树 2 8 | 9 | 详细布置 10 | 11 | ## 层序遍历 12 | 13 | 看完本篇可以一口气刷十道题,试一试, 层序遍历并不难,大家可以很快刷了十道题。 14 | 15 | 题目链接/文章讲解/视频讲解:https://programmercarl.com/0102.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E5%B1%82%E5%BA%8F%E9%81%8D%E5%8E%86.html 16 | 17 | ## 226.翻转二叉树 (优先掌握递归) 18 | 19 | 这道题目 一些做过的同学 理解的也不够深入,建议大家先看我的视频讲解,无论做过没做过,都会有很大收获。 20 | 21 | 题目链接/文章讲解/视频讲解:https://programmercarl.com/0226.%E7%BF%BB%E8%BD%AC%E4%BA%8C%E5%8F%89%E6%A0%91.html 22 | 23 | ## 101. 对称二叉树 (优先掌握递归) 24 | 25 | 先看视频讲解,会更容易一些。 26 | 27 | 题目链接/文章讲解/视频讲解:https://programmercarl.com/0101.%E5%AF%B9%E7%A7%B0%E4%BA%8C%E5%8F%89%E6%A0%91.html 28 | -------------------------------------------------------------------------------- /notes/src/day15/lc226.md: -------------------------------------------------------------------------------- 1 | # 226. 翻转二叉树 2 | 3 | ## 题目描述 4 | 5 | 给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。 6 | 7 | 8 | 9 | ## 解题思路 10 | 11 | 12 | ```cpp 13 | class Solution { 14 | public: 15 | TreeNode* invertTree(TreeNode* root) { 16 | traverse(root); 17 | return root; 18 | } 19 | void traverse(TreeNode* root) { 20 | if (root == NULL) return ; 21 | swap(root->left, root->right); 22 | traverse(root->left); 23 | traverse(root->right); 24 | } 25 | }; 26 | ``` 27 | ## 学习感想 -------------------------------------------------------------------------------- /notes/src/day16/lc104.md: -------------------------------------------------------------------------------- 1 | # 104. 二叉树的最大深度 2 | ## 题目描述 3 | 4 | 给定一个二叉树,找出其最大深度。 5 | 6 | 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。 7 | 8 | 说明: 叶子节点是指没有子节点的节点。 9 | 10 | 示例: 11 | 给定二叉树 [3,9,20,null,null,15,7], 12 | 13 | ## 解题思路 14 | 15 | ```cpp 16 | class Solution { 17 | public: 18 | int maxDepth(TreeNode* root) { 19 | if (root == NULL) return 0; 20 | else { 21 | int l = maxDepth(root->left); 22 | int r = maxDepth(root->right); 23 | int m = max(l, r); 24 | return m + 1; 25 | } 26 | } 27 | }; 28 | ``` 29 | 30 | ## 学习感想 31 | 32 | 递归 -------------------------------------------------------------------------------- /notes/src/day16/lc111.md: -------------------------------------------------------------------------------- 1 | # 111. 二叉树的最小深度 2 | ## 题目描述 3 | 4 | 给定一个二叉树,找出其最小深度。 5 | 6 | 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 7 | 8 | 说明:叶子节点是指没有子节点的节点。 9 | 10 | ## 解题思路 11 | 12 | 13 | ```cpp 14 | class Solution { 15 | public: 16 | int minDepth(TreeNode* root) { 17 | if (root == NULL) return 0; 18 | if (root->left == NULL && root->right == NULL) return 1; 19 | if (root->left == NULL) return minDepth(root->right) + 1; 20 | if (root->right == NULL) return minDepth(root->left) + 1; 21 | else return min(minDepth(root->left), minDepth(root->right)) + 1; 22 | } 23 | }; 24 | ``` 25 | 26 | ## 学习感想 27 | 28 | 写到这里我根本没有想明白为什么这个是对的。 -------------------------------------------------------------------------------- /notes/src/day17.md: -------------------------------------------------------------------------------- 1 | # 第六章 二叉树part04 2 | 3 | 今日内容: 4 | 5 | ● 110.平衡二叉树 6 | ● 257. 二叉树的所有路径 7 | ● 404.左叶子之和 8 | 9 | 详细布置 10 | 11 | 迭代法,大家可以直接过,二刷有精力的时候 再去掌握迭代法。 12 | 13 | ## 110.平衡二叉树 (优先掌握递归) 14 | 15 | 再一次涉及到,什么是高度,什么是深度,可以巩固一下。 16 | 17 | 题目链接/文章讲解/视频讲解:https://programmercarl.com/0110.%E5%B9%B3%E8%A1%A1%E4%BA%8C%E5%8F%89%E6%A0%91.html 18 | 19 | ## 257. 二叉树的所有路径 (优先掌握递归) 20 | 21 | 这是大家第一次接触到回溯的过程, 我在视频里重点讲解了 本题为什么要有回溯,已经回溯的过程。 22 | 23 | 如果对回溯 似懂非懂,没关系, 可以先有个印象。 24 | 25 | 题目链接/文章讲解/视频讲解:https://programmercarl.com/0257.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%89%80%E6%9C%89%E8%B7%AF%E5%BE%84.html 26 | 27 | ## 404.左叶子之和 (优先掌握递归) 28 | 29 | 其实本题有点文字游戏,搞清楚什么是左叶子,剩下的就是二叉树的基本操作。 30 | 31 | 题目链接/文章讲解/视频讲解:https://programmercarl.com/0404.%E5%B7%A6%E5%8F%B6%E5%AD%90%E4%B9%8B%E5%92%8C.html 32 | -------------------------------------------------------------------------------- /notes/src/day17/lc110.md: -------------------------------------------------------------------------------- 1 | # 110. 平衡二叉树 2 | 3 | ## 题目描述 4 | 5 | 给定一个二叉树,判断它是否是高度平衡的二叉树。 6 | 7 | 本题中,一棵高度平衡二叉树定义为: 8 | 9 | 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。 10 | 11 | ## 解题思路 12 | 13 | ```cpp 14 | class Solution { 15 | public: 16 | int f(TreeNode* root) { 17 | if (root == NULL) return 0; 18 | int l = f(root->left); 19 | if (l == -1) return -1; 20 | int r = f(root->right); 21 | if (r == -1) return -1; 22 | if (abs(l-r)>1) return -1; 23 | return max(l,r)+1; 24 | } 25 | 26 | bool isBalanced(TreeNode* root) { 27 | return f(root) != -1 ? true : false; 28 | } 29 | }; 30 | ``` 31 | ## 学习感想 -------------------------------------------------------------------------------- /notes/src/day17/lc257.md: -------------------------------------------------------------------------------- 1 | # 257. 二叉树的所有路径 2 | ## 题目描述 3 | 4 | 给你一个二叉树的根节点 root ,按 任意顺序 ,返回所有从根节点到叶子节点的路径。 5 | 6 | 叶子节点 是指没有子节点的节点。 7 | 8 | ## 解题思路 9 | 10 | ```cpp 11 | class Solution { 12 | void f(TreeNode* root, string path, vector &res) { 13 | path += to_string(root->val); 14 | if (!root->left && !root->right) { 15 | res.push_back(path); 16 | return; 17 | } 18 | if(root->left)f(root->left,path+"->",res); 19 | if(root->right)f(root->right,path+"->",res); 20 | } 21 | public: 22 | vector binaryTreePaths(TreeNode* root) { 23 | vector res; 24 | string path; 25 | if (!root) return res; 26 | f(root, path, res); 27 | return res; 28 | } 29 | }; 30 | ``` 31 | ## 学习感想 -------------------------------------------------------------------------------- /notes/src/day17/lc404.md: -------------------------------------------------------------------------------- 1 | # 404. 左叶子之和 2 | 3 | ## 题目描述 4 | 5 | 给定二叉树的根节点 root ,返回所有左叶子之和。 6 | 7 | ## 解题思路 8 | 9 | ```cpp 10 | class Solution { 11 | public: 12 | int sumOfLeftLeaves(TreeNode* root) { 13 | if (root == NULL) return 0; 14 | int r=0; 15 | if (root->left!=NULL&&root->left->left==NULL&&root->left->right==NULL)r+=root->left->val; 16 | return r+sumOfLeftLeaves(root->left)+sumOfLeftLeaves(root->right); 17 | } 18 | }; 19 | ``` 20 | ## 学习感想 -------------------------------------------------------------------------------- /notes/src/day18.md: -------------------------------------------------------------------------------- 1 | # 第六章 二叉树 part05 2 | 今日内容 3 | 4 | ● 513.找树左下角的值 5 | ● 112. 路径总和 113.路径总和ii 6 | ● 106.从中序与后序遍历序列构造二叉树 105.从前序与中序遍历序列构造二叉树 7 | 8 | 详细布置 9 | 10 | ## 找树左下角的值 11 | 12 | 本地递归偏难,反而迭代简单属于模板题, 两种方法掌握一下 13 | 14 | 题目链接/文章讲解/视频讲解:https://programmercarl.com/0513.%E6%89%BE%E6%A0%91%E5%B7%A6%E4%B8%8B%E8%A7%92%E7%9A%84%E5%80%BC.html 15 | 16 | ## 路径总和 17 | 18 | 本题 又一次设计要回溯的过程,而且回溯的过程隐藏的还挺深,建议先看视频来理解 19 | 20 | 112. 路径总和,和 113. 路径总和ii 一起做了。 优先掌握递归法。 21 | 22 | 题目链接/文章讲解/视频讲解:https://programmercarl.com/0112.%E8%B7%AF%E5%BE%84%E6%80%BB%E5%92%8C.html 23 | 24 | ## 从中序与后序遍历序列构造二叉树 25 | 26 | 本题算是比较难的二叉树题目了,大家先看视频来理解。 27 | 28 | 106.从中序与后序遍历序列构造二叉树,105.从前序与中序遍历序列构造二叉树 一起做,思路一样的 29 | 30 | 题目链接/文章讲解/视频讲解:https://programmercarl.com/0106.%E4%BB%8E%E4%B8%AD%E5%BA%8F%E4%B8%8E%E5%90%8E%E5%BA%8F%E9%81%8D%E5%8E%86%E5%BA%8F%E5%88%97%E6%9E%84%E9%80%A0%E4%BA%8C%E5%8F%89%E6%A0%91.html 31 | -------------------------------------------------------------------------------- /notes/src/day19.md: -------------------------------------------------------------------------------- 1 | # 休息日 2 | 3 | corctf 还是一题也做不出 我没用 -------------------------------------------------------------------------------- /notes/src/day20.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ainevsia/Leetcode-Rust/57e687cf87b74360cbbbfc0f5954830b1a6fbc3c/notes/src/day20.md -------------------------------------------------------------------------------- /notes/src/day20/lc617.md: -------------------------------------------------------------------------------- 1 | # 617. 合并二叉树 2 | 3 | 4 | ## 题目描述 5 | 6 | 给你两棵二叉树: root1 和 root2 。 7 | 8 | 想象一下,当你将其中一棵覆盖到另一棵之上时,两棵树上的一些节点将会重叠(而另一些不会)。你需要将这两棵树合并成一棵新二叉树。合并的规则是:如果两个节点重叠,那么将这两个节点的值相加作为合并后节点的新值;否则,不为 null 的节点将直接作为新二叉树的节点。 9 | 10 | 返回合并后的二叉树。 11 | 12 | 注意: 合并过程必须从两个树的根节点开始。 13 | ## 解题思路 14 | 15 | 16 | ```cpp 17 | class Solution { 18 | public: 19 | TreeNode* mergeTrees(TreeNode* r1, TreeNode* r2) { 20 | if(!r1&&!r2)return 0; 21 | TreeNode*l=mergeTrees(r1?r1->left:0,r2?r2->left:0); 22 | TreeNode*r=mergeTrees(r1?r1->right:0,r2?r2->right:0); 23 | return new TreeNode((r1?r1->val:0)+(r2?r2->val:0),l,r); 24 | } 25 | }; 26 | ``` 27 | ## 学习感想 -------------------------------------------------------------------------------- /notes/src/day20/lc700.md: -------------------------------------------------------------------------------- 1 | # 700. 二叉搜索树中的搜索 2 | 3 | 4 | ## 题目描述 5 | 6 | 给定二叉搜索树(BST)的根节点 root 和一个整数值 val。 7 | 8 | 你需要在 BST 中找到节点值等于 val 的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 null 。 9 | 10 | 11 | ## 解题思路 12 | 13 | 14 | ```cpp 15 | class Solution { 16 | public: 17 | TreeNode* searchBST(TreeNode* r, int val) { 18 | if(!r)return 0; 19 | if (r->val==val)return r; 20 | if (valval)return searchBST(r->left,val); 21 | return searchBST(r->right,val); 22 | } 23 | }; 24 | ``` 25 | 26 | 27 | ## 学习感想 28 | -------------------------------------------------------------------------------- /notes/src/day20/lc98.md: -------------------------------------------------------------------------------- 1 | # 98. 验证二叉搜索树 2 | 3 | ## 题目描述 4 | 5 | 给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。 6 | 7 | 有效 二叉搜索树定义如下: 8 | 9 | 节点的左子树只包含 小于 当前节点的数。 10 | 节点的右子树只包含 大于 当前节点的数。 11 | 所有左子树和右子树自身必须也是二叉搜索树。 12 | 13 | ## 解题思路 14 | 15 | 16 | ```cpp 17 | class Solution { 18 | public: 19 | bool f(TreeNode*r,long long l,long long ri){ 20 | if(!r)return 1; 21 | if(r->val>=ri||r->val<=l)return 0; 22 | return f(r->left,l,r->val)&&f(r->right,r->val,ri); 23 | } 24 | bool isValidBST(TreeNode* r) { 25 | return f(r,(long long)(-2147483648)-1,(long long)2147483647+1); 26 | } 27 | }; 28 | 29 | ``` 30 | ## 学习感想 31 | 32 | int 范围好坑啊 33 | 34 | `f(r,-2147483648-1,2147483647+1); ` 这样不会自动类型转换成longlong -------------------------------------------------------------------------------- /notes/src/day21/lc236.md: -------------------------------------------------------------------------------- 1 | # 236. 二叉树的最近公共祖先 2 | 3 | ## 题目描述 4 | 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 5 | 6 | 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。” 7 | 8 | ## 解题思路 9 | ```cpp 10 | class Solution { 11 | public: 12 | TreeNode*pp; 13 | TreeNode*res=0; 14 | TreeNode*qq; 15 | int f(TreeNode*r){ 16 | if(!r)return 0; 17 | int left=f(r->left);int right=f(r->right); 18 | int a=r==pp||r==qq?1:0; 19 | a+=left+right; 20 | if(!res&&a==2){res=r;}return a; 21 | } 22 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 23 | pp=p;qq=q; 24 | f(root); 25 | return res; 26 | } 27 | }; 28 | ``` 29 | 30 | f表示在子树中找到的个数,找到2个的时候就设置res就行了 31 | 32 | ## 学习感想 -------------------------------------------------------------------------------- /notes/src/day22/lc235.md: -------------------------------------------------------------------------------- 1 | # 235. 二叉搜索树的最近公共祖先 2 | 3 | ## 题目描述 4 | 5 | 给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。 6 | 7 | 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。” 8 | 9 | 例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5] 10 | 11 | ## 解题思路 12 | 13 | 当pq同时在两边的话,就是找到了;不是的话,那肯定就是在某一边 14 | 15 | ```cpp 16 | class Solution { 17 | public: 18 | int mi, ma; 19 | TreeNode*f(TreeNode*r){ 20 | if(!r)return 0; 21 | if(mi<=r->val&&r->val<=ma)return r; 22 | if(mi>r->val)return f(r->right);return f(r->left); 23 | } 24 | TreeNode* lowestCommonAncestor(TreeNode* r, TreeNode* p, TreeNode* q) { 25 | mi=min(p->val,q->val); 26 | ma=max(p->val,q->val); 27 | return f(r); 28 | } 29 | }; 30 | ``` 31 | ## 学习感想 -------------------------------------------------------------------------------- /notes/src/day22/lc701.md: -------------------------------------------------------------------------------- 1 | # 701. 二叉搜索树中的插入操作 2 | 3 | ## 题目描述 4 | 5 | 给定二叉搜索树(BST)的根节点 root 和要插入树中的值 value ,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据 保证 ,新值和原始二叉搜索树中的任意节点值都不同。 6 | 7 | 注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回 任意有效的结果 。 8 | 9 | ## 解题思路 10 | 11 | ```cpp 12 | class Solution { 13 | public: 14 | int v; 15 | TreeNode*f(TreeNode*r) {if(!r)return new TreeNode(v); 16 | if(r->valright=f(r->right);else r->left=f(r->left);return r; 17 | } 18 | TreeNode* insertIntoBST(TreeNode* r, int val) { 19 | v=val;r=f(r);return r; 20 | } 21 | }; 22 | ``` 23 | 24 | WA: 输入的树可能为空树 25 | 26 | ## 学习感想 -------------------------------------------------------------------------------- /notes/src/day23/lc108.md: -------------------------------------------------------------------------------- 1 | # 108. 将有序数组转换为二叉搜索树 2 | ## 题目描述 3 | 4 | 给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 高度平衡 二叉搜索树。 5 | 6 | 高度平衡 二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。 7 | 8 | ## 解题思路 9 | 10 | ```cpp 11 | class Solution { 12 | public: 13 | vectorv; 14 | TreeNode*f(int l,int r){ 15 | int n=r-l;if(n<=0)return 0; 16 | int m=l+n/2;return new TreeNode(v[m],f(l,m),f(m+1,r)); 17 | } 18 | TreeNode* sortedArrayToBST(vector&ve) { 19 | v=ve;int n=v.size(); 20 | return f(0,n); 21 | } 22 | }; 23 | ``` 24 | 25 | ## 学习感想 -------------------------------------------------------------------------------- /notes/src/day23/lc538.md: -------------------------------------------------------------------------------- 1 | # 538. 把二叉搜索树转换为累加树 2 | ## 题目描述 3 | 4 | 给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。 5 | 6 | 提醒一下,二叉搜索树满足下列约束条件: 7 | 8 | 节点的左子树仅包含键 小于 节点键的节点。 9 | 节点的右子树仅包含键 大于 节点键的节点。 10 | 左右子树也必须是二叉搜索树。 11 | 注意:本题和 1038: https://leetcode-cn.com/problems/binary-search-tree-to-greater-sum-tree/ 相同 12 | 13 | ## 解题思路 14 | 15 | ```cpp 16 | class Solution { 17 | public: 18 | int s=0; 19 | TreeNode* convertBST(TreeNode*r) { 20 | if(!r)return r; 21 | convertBST(r->right); 22 | s+=r->val; 23 | r->val=s; 24 | convertBST(r->left); 25 | return r; 26 | } 27 | }; 28 | ``` 29 | ## 学习感想 -------------------------------------------------------------------------------- /notes/src/day23/lc669.md: -------------------------------------------------------------------------------- 1 | # 669. 修剪二叉搜索树 2 | 3 | ## 题目描述 4 | 5 | 给你二叉搜索树的根节点 root ,同时给定最小边界low 和最大边界 high。通过修剪二叉搜索树,使得所有节点的值在[low, high]中。修剪树 不应该 改变保留在树中的元素的相对结构 (即,如果没有被移除,原有的父代子代关系都应当保留)。 可以证明,存在 唯一的答案 。 6 | 7 | 所以结果应当返回修剪好的二叉搜索树的新的根节点。注意,根节点可能会根据给定的边界发生改变。 8 | 9 | 10 | ## 解题思路 11 | 12 | ```cpp 13 | class Solution { 14 | public: 15 | int l, ri; 16 | TreeNode*f(TreeNode*r){ 17 | if(!r)return r; 18 | if(r->val>=l&&r->val<=ri) { 19 | r->left=f(r->left); 20 | r->right=f(r->right); 21 | return r; 22 | } 23 | if(r->valright);return f(r->left); 24 | } 25 | TreeNode* trimBST(TreeNode* r, int low, int high) { 26 | l=low;ri=high;r=f(r);return r; 27 | } 28 | }; 29 | ``` 30 | ## 学习感想 -------------------------------------------------------------------------------- /notes/src/day24.md: -------------------------------------------------------------------------------- 1 | # 第七章 回溯算法part01 2 | 今日内容: 3 | 4 | ● 理论基础 5 | ● 77. 组合 6 | 7 | 详细布置 8 | 9 | 理论基础 10 | 11 | 其实在讲解二叉树的时候,就给大家介绍过回溯,这次正式开启回溯算法,大家可以先看视频,对回溯算法有一个整体的了解。 12 | 13 | 题目链接/文章讲解:https://programmercarl.com/%E5%9B%9E%E6%BA%AF%E7%AE%97%E6%B3%95%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html 14 | 视频讲解:https://www.bilibili.com/video/BV1cy4y167mM 15 | 16 | 17 | > 虽然回溯法很难,很不好理解,但是回溯法并不是什么高效的算法。 18 | 19 | > 回溯法解决的问题都可以抽象为树形结构,是的,我指的是所有回溯法的问题都可以抽象为树形结构! 20 | 21 | ## 77. 组合 22 | 23 | 对着 在 回溯算法理论基础 给出的 代码模板,来做本题组合问题,大家就会发现 写回溯算法套路。 24 | 25 | 在回溯算法解决实际问题的过程中,大家会有各种疑问,先看视频介绍,基本可以解决大家的疑惑。 26 | 27 | 本题关于剪枝操作是大家要理解的重点,因为后面很多回溯算法解决的题目,都是这个剪枝套路。 28 | 29 | 题目链接/文章讲解:https://programmercarl.com/0077.%E7%BB%84%E5%90%88.html 30 | 视频讲解:https://www.bilibili.com/video/BV1ti4y1L7cv 31 | 剪枝操作:https://www.bilibili.com/video/BV1wi4y157er 32 | -------------------------------------------------------------------------------- /notes/src/day24/lc77.md: -------------------------------------------------------------------------------- 1 | # 77. 组合 2 | 3 | ## 题目描述 4 | 5 | 给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的组合。 6 | 7 | 你可以按 任何顺序 返回答案。 8 | 9 | ## 解题思路 10 | 11 | ```cpp 12 | class Solution { 13 | public: 14 | vector>res; 15 | vectorcur; 16 | int n,k; 17 | void bt(int start){ 18 | if(cur.size()==k){res.push_back(cur);return;} 19 | for(int i=start;i<=n-(k-cur.size())+1;i++){ 20 | cur.push_back(i); 21 | bt(i+1); 22 | cur.pop_back(); 23 | } 24 | } 25 | vector> combine(int n, int k) { 26 | this->n=n;this->k=k;bt(1);return res; 27 | } 28 | }; 29 | ``` 30 | 31 | ## 学习感想 -------------------------------------------------------------------------------- /notes/src/day25.md: -------------------------------------------------------------------------------- 1 | # 第七章 回溯算法part02 2 | 3 | 今日内容: 4 | 5 | ● 216.组合总和III 6 | ● 17.电话号码的字母组合 7 | 8 | 详细布置 9 | 10 | ## 216.组合总和III 11 | 12 | 如果把 组合问题理解了,本题就容易一些了。 13 | 14 | 题目链接/文章讲解:https://programmercarl.com/0216.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8CIII.html 15 | 视频讲解:https://www.bilibili.com/video/BV1wg411873x 16 | 17 | ## 17.电话号码的字母组合 18 | 19 | 本题大家刚开始做会有点难度,先自己思考20min,没思路就直接看题解。 20 | 21 | 题目链接/文章讲解:https://programmercarl.com/0017.%E7%94%B5%E8%AF%9D%E5%8F%B7%E7%A0%81%E7%9A%84%E5%AD%97%E6%AF%8D%E7%BB%84%E5%90%88.html 22 | 视频讲解:https://www.bilibili.com/video/BV1yV4y1V7Ug 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /notes/src/day25/lc216.md: -------------------------------------------------------------------------------- 1 | # 216. 组合总和 III 2 | 3 | ## 题目描述 4 | 5 | 找出所有相加之和为 n 的 k 个数的组合,且满足下列条件: 6 | 7 | 只使用数字1到9 8 | 每个数字 最多使用一次 9 | 返回 所有可能的有效组合的列表 。该列表不能包含相同的组合两次,组合可以以任何顺序返回。 10 | 11 | 12 | ## 解题思路 13 | 14 | 15 | ```cpp 16 | class Solution { 17 | public: 18 | vector>res; 19 | vectorcur; 20 | int cursum=0; 21 | int k,n; 22 | void bt(int start){ 23 | if(cur.size()==k&&cursum==n){res.push_back(cur);return;} 24 | for(int i=start;i<10&&cursum> combinationSum3(int k, int n) { 31 | this->k=k;this->n=n;bt(1);return res; 32 | } 33 | }; 34 | ``` 35 | ## 学习感想 -------------------------------------------------------------------------------- /notes/src/day27.md: -------------------------------------------------------------------------------- 1 | # 第七章 回溯算法part03 2 | 3 | 详细布置 4 | 5 | ## 39. 组合总和 6 | 7 | 本题是 集合里元素可以用无数次,那么和组合问题的差别 其实仅在于 startIndex上的控制 8 | 9 | 题目链接/文章讲解:https://programmercarl.com/0039.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8C.html 10 | 视频讲解:https://www.bilibili.com/video/BV1KT4y1M7HJ 11 | 12 | ## 40.组合总和II 13 | 14 | 本题开始涉及到一个问题了:去重。 15 | 16 | 注意题目中给我们 集合是有重复元素的,那么求出来的 组合有可能重复,但题目要求不能有重复组合。 17 | 18 | 题目链接/文章讲解:https://programmercarl.com/0040.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8CII.html 19 | 视频讲解:https://www.bilibili.com/video/BV12V4y1V73A 20 | 21 | ## 131.分割回文串 22 | 23 | 本题较难,大家先看视频来理解 分割问题,明天还会有一道分割问题,先打打基础。 24 | 25 | https://programmercarl.com/0131.%E5%88%86%E5%89%B2%E5%9B%9E%E6%96%87%E4%B8%B2.html 26 | 视频讲解:https://www.bilibili.com/video/BV1c54y1e7k6 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /notes/src/day27/lc131.md: -------------------------------------------------------------------------------- 1 | # 131.分割回文串 2 | 3 | ```cpp 4 | class Solution { 5 | public: 6 | string s;vector>res;vectorcur; 7 | bool valid(int l,int r){ 8 | int lptr=l,rptr=r;while(lptr<=r){if(s[lptr]!=s[rptr])return false;lptr++;rptr--;}return true; 9 | } 10 | void bt(int start){ 11 | if(start==s.size()){res.push_back(cur);return;} 12 | for(int i=start;i> partition(string s) { 21 | this->s=s;bt(0);return res; 22 | } 23 | }; 24 | ``` 25 | -------------------------------------------------------------------------------- /notes/src/day27/lc39.md: -------------------------------------------------------------------------------- 1 | # 39. 组合总和 2 | 3 | ```cpp 4 | class Solution { 5 | public: 6 | vector>res; 7 | vectorcur;vectorcandidates; 8 | int s=0,t; 9 | void bt(int start){ 10 | if(s>=t){if(s==t)res.push_back(cur);return;} 11 | for(int j=start;j> combinationSum(vector& candidates, int target) { 18 | t=target;this->candidates=candidates;bt(0);return res; 19 | } 20 | }; 21 | ``` 22 | -------------------------------------------------------------------------------- /notes/src/day27/lc40.md: -------------------------------------------------------------------------------- 1 | # 40.组合总和II 2 | 3 | ```cpp 4 | class Solution { 5 | public: 6 | vector>res; 7 | vectorcur;vectorcandidates; 8 | int s=0,t; 9 | void bt(int start){ 10 | if(s>=t){if(s==t)res.push_back(cur);return;} 11 | for(int j=start;jstart&&candidates[j]==candidates[j-1])continue; 15 | cur.push_back(i);s+=i; 16 | bt(j+1); 17 | cur.pop_back();s-=i; 18 | } 19 | } 20 | vector> combinationSum2(vector& candidates, int target) { 21 | t=target;sort(candidates.begin(),candidates.end());this->candidates=candidates;bt(0);return res; 22 | } 23 | }; 24 | ``` 25 | 26 | `if(j>start&&candidates[j]==candidates[j-1])continue;`这而想了很久,一直以为是j大于0 27 | 28 | 为了结果不重复,所以剪枝是必须要进行的操作 29 | -------------------------------------------------------------------------------- /notes/src/day28.md: -------------------------------------------------------------------------------- 1 | # 28 第七章 回溯算法 2 | ● 93.复原IP地址 3 | ● 78.子集 4 | ● 90.子集II 5 | 6 | 详细布置 7 | 8 | ## 93.复原IP地址 9 | 10 | 本期本来是很有难度的,不过 大家做完 分割回文串 之后,本题就容易很多了 11 | 12 | 题目链接/文章讲解:https://programmercarl.com/0093.%E5%A4%8D%E5%8E%9FIP%E5%9C%B0%E5%9D%80.html 13 | 视频讲解:https://www.bilibili.com/video/BV1XP4y1U73i/ 14 | 15 | ## 78.子集 16 | 17 | 子集问题,就是收集树形结构中,每一个节点的结果。 整体代码其实和 回溯模板都是差不多的。 18 | 19 | 题目链接/文章讲解:https://programmercarl.com/0078.%E5%AD%90%E9%9B%86.html 20 | 视频讲解:https://www.bilibili.com/video/BV1U84y1q7Ci 21 | ## 90.子集II 22 | 23 | 大家之前做了 40.组合总和II 和 78.子集 ,本题就是这两道题目的结合,建议自己独立做一做,本题涉及的知识,之前都讲过,没有新内容。 24 | 25 | 题目链接/文章讲解:https://programmercarl.com/0090.%E5%AD%90%E9%9B%86II.html 26 | 视频讲解:https://www.bilibili.com/video/BV1vm4y1F71J 27 | -------------------------------------------------------------------------------- /notes/src/day28/lc78.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # 78.子集 5 | 6 | ```cpp 7 | class Solution { 8 | public:vector>res;vectorcur;vectorv;void bt(int start){res.push_back(cur); 9 | if(start>=v.size())return;for(int i=start;i> subsets(vector& nums) { 14 | v=nums;bt(0);return res; 15 | } 16 | }; 17 | ``` 18 | 19 | 如果把 子集问题、组合问题、分割问题都抽象为一棵树的话,那么**组合问题和分割问题都是收集树的叶子节点,而子集问题是找树的所有节点**! 20 | -------------------------------------------------------------------------------- /notes/src/day28/lc90.md: -------------------------------------------------------------------------------- 1 | 2 | # 90.子集II 3 | 4 | ```cpp 5 | class Solution { 6 | public:vector>res;vectorcur;vectorv;void bt(int start){res.push_back(cur); 7 | if(start==v.size())return;for(int i=start;istart&&v[i]==v[i-1])continue; 9 | cur.push_back(v[i]);bt(i+1);cur.pop_back(); 10 | } 11 | } 12 | vector> subsetsWithDup(vector& nums) { 13 | sort(nums.begin(),nums.end());v=nums;bt(0);return res; 14 | } 15 | }; 16 | ``` 17 | -------------------------------------------------------------------------------- /notes/src/day28/lc93.md: -------------------------------------------------------------------------------- 1 | 2 | # 93.复原IP地址 3 | 4 | ```cpp 5 | class Solution { 6 | public: 7 | vectorres;string cur;string s;void bt(int start,int cnt){ 8 | if(cnt==4||start>=s.size()){if(cnt==4&&start==s.size())res.push_back(string(cur.begin(),cur.end()-1));return;} 9 | for(int i=1;i<=3;i++){ 10 | string sub=string(s.begin()+start,s.begin()+start+i); 11 | if(valid(sub)){ 12 | auto l=cur.size(); 13 | cur+=sub+"."; 14 | bt(start+i,cnt+1); 15 | cur.erase(l); 16 | } 17 | } 18 | } 19 | bool valid(string s){ 20 | if(s.size()==0)return false; 21 | if(s[0]=='0')return s.size()==1; 22 | int a=stoi(s);return a>=0 && a<=255; 23 | } 24 | vector restoreIpAddresses(string s) {this->s=s; 25 | bt(0,0);return res; 26 | } 27 | }; 28 | ``` 29 | -------------------------------------------------------------------------------- /notes/src/day29.md: -------------------------------------------------------------------------------- 1 | # 第七章 回溯算法part05 2 | 3 | * 491.递增子序列 4 | * 46.全排列 5 | * 47.全排列 II 6 | 7 | 详细布置 8 | 9 | ## 491.递增子序列 10 | 11 | 本题和大家刚做过的 90.子集II 非常像,但又很不一样,很容易掉坑里。 12 | https://programmercarl.com/0491.%E9%80%92%E5%A2%9E%E5%AD%90%E5%BA%8F%E5%88%97.html 13 | 14 | 视频讲解:https://www.bilibili.com/video/BV1EG4y1h78v 15 | 16 | 17 | ## 46.全排列 18 | 本题重点感受一下,排列问题 与 组合问题,组合总和,子集问题的区别。 为什么排列问题不用 startIndex 19 | https://programmercarl.com/0046.%E5%85%A8%E6%8E%92%E5%88%97.html 20 | 视频讲解:https://www.bilibili.com/video/BV19v4y1S79W 21 | 22 | ## 47.全排列 II 23 | 本题 就是我们讲过的 40.组合总和II 去重逻辑 和 46.全排列 的结合,可以先自己做一下,然后重点看一下 文章中 我讲的拓展内容。 used[i - 1] == true 也行,used[i - 1] == false 也行 24 | 25 | https://programmercarl.com/0047.%E5%85%A8%E6%8E%92%E5%88%97II.html 26 | 27 | 视频讲解:https://www.bilibili.com/video/BV1R84y1i7Tm 28 | -------------------------------------------------------------------------------- /notes/src/day29/lc46.md: -------------------------------------------------------------------------------- 1 | 2 | # 46.全排列 3 | 4 | 5 | https://programmercarl.com/0046.%E5%85%A8%E6%8E%92%E5%88%97.html#%E7%AE%97%E6%B3%95%E5%85%AC%E5%BC%80%E8%AF%BE 6 | ```cpp 7 | class Solution { 8 | public: 9 | vectorused; 10 | vectorcur; 11 | vectorv; 12 | vector>res; 13 | void bt() { 14 | if(cur.size()==v.size()) 15 | {res.push_back(cur);return;} 16 | for(int i=0;i> permute(vector& nums) { 23 | v=nums;used=vector(v.size(),0);bt();return res; 24 | } 25 | }; 26 | ``` 27 | -------------------------------------------------------------------------------- /notes/src/day29/lc47.md: -------------------------------------------------------------------------------- 1 | # 47.全排列 II 2 | 3 | ```cpp 4 | class Solution { 5 | public: 6 | vectorused; 7 | vectorcur; 8 | vectorv; 9 | vector>res; 10 | void bt() { 11 | if(cur.size()==v.size()){ 12 | res.push_back(cur);return; 13 | } 14 | for(int i=0;i0&&v[i]==v[i-1]&&used[i-1]==0)continue; 17 | used[i]=1; 18 | cur.push_back(v[i]); 19 | bt(); 20 | cur.pop_back(); 21 | used[i]=0; 22 | } 23 | } 24 | vector> permuteUnique(vector& nums) { 25 | v=nums;sort(v.begin(),v.end()); 26 | used=vector(v.size(),0); 27 | bt(); 28 | return res; 29 | } 30 | }; 31 | ``` 32 | 33 | 如何剪枝同一层使用过的:`&&used[i-1]==0`,一下子想不到。 34 | 35 | -------------------------------------------------------------------------------- /notes/src/day29/lc491.md: -------------------------------------------------------------------------------- 1 | 2 | # 491.递增子序列 3 | 4 | ```cpp 5 | class Solution { 6 | public:vector>res; 7 | vectorcur; 8 | vectorv; 9 | void bt(int start){ 10 | if(cur.size()>1)res.push_back(cur); 11 | if(start>=v.size())return; 12 | unordered_set uset; 13 | for(int i=start;i=cur.back()) 15 | { 16 | if(uset.find(v[i])!=uset.end())continue; 17 | uset.insert(v[i]); 18 | cur.push_back(v[i]);bt(i+1);cur.pop_back(); 19 | } 20 | } 21 | 22 | } 23 | vector> findSubsequences(vector& nums) {v=nums;bt(0);return res;} 24 | }; 25 | 26 | ``` 27 | 28 | 本层访问过的元素不再访问,误以为是前后不用重复的就行,需要使用set 29 | -------------------------------------------------------------------------------- /notes/src/day31.md: -------------------------------------------------------------------------------- 1 | # 第八章 贪心算法 part01 2 | 3 | ● 理论基础 4 | ● 455.分发饼干 5 | ● 376. 摆动序列 6 | ● 53. 最大子序和 7 | 8 | 贪心算法其实就是没有什么规律可言,所以大家了解贪心算法 就了解它没有规律的本质就够了。 9 | 10 | 不用花心思去研究其规律, 没有思路就立刻看题解。 11 | 12 | 基本贪心的题目 有两个极端,要不就是特简单,要不就是死活想不出来。 13 | 14 | 学完贪心之后再去看动态规划,就会了解贪心和动规的区别。 15 | 16 | 详细布置 17 | 18 | 理论基础 19 | 20 | https://programmercarl.com/%E8%B4%AA%E5%BF%83%E7%AE%97%E6%B3%95%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html 21 | 22 | ## 455.分发饼干 23 | 24 | https://programmercarl.com/0455.%E5%88%86%E5%8F%91%E9%A5%BC%E5%B9%B2.html 25 | 26 | ## 376. 摆动序列 27 | 28 | https://programmercarl.com/0376.%E6%91%86%E5%8A%A8%E5%BA%8F%E5%88%97.html 29 | 30 | ## 53. 最大子序和 31 | 32 | https://programmercarl.com/0053.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C.html 33 | -------------------------------------------------------------------------------- /notes/src/day31/lc376.md: -------------------------------------------------------------------------------- 1 | # 376. 摆动序列 2 | 3 | ```cpp 4 | class Solution { 5 | public: 6 | int wiggleMaxLength(vector&v) { 7 | // 8 | auto tail = unique(v.begin(),v.end()); 9 | v.erase(tail,v.end()); 10 | // 11 | if(v.size()<=2)return v.size(); 12 | int cnt = 0; 13 | for (int i = 1; i < v.size() - 1; i ++ ) { 14 | int pdir = v[i] - v[i-1]; 15 | int cdir = v[i+1] - v[i]; 16 | cnt += pdir*cdir<0?1:0; 17 | } 18 | return cnt+2; 19 | } 20 | }; 21 | ``` 22 | 23 | 去重之后就不用考虑这么多复杂的情况 24 | -------------------------------------------------------------------------------- /notes/src/day31/lc455.md: -------------------------------------------------------------------------------- 1 | # 455. 分发饼干 2 | 3 | ```cpp 4 | // Custom comparator function for sorting in reverse order 5 | bool reverseComparator(int a, int b) { 6 | return a > b; // '>' will sort in descending order (reverse), '<' will sort in ascending order 7 | } 8 | class Solution { 9 | public: 10 | int findContentChildren(vector& g, vector& s) { 11 | sort(s.begin(),s.end(),reverseComparator); 12 | sort(g.begin(),g.end(),reverseComparator); 13 | int cnt = 0; 14 | int p = 0; 15 | for (int cookiesize : s) { 16 | while (p < g.size() && g[p] > cookiesize) p ++ ; 17 | if (p >= g.size()) break; 18 | // if (cookiesize >= g[p]) { 19 | cnt ++ ; 20 | p ++; 21 | // } 22 | } 23 | return cnt; 24 | } 25 | }; 26 | ``` 27 | -------------------------------------------------------------------------------- /notes/src/day31/lc53.md: -------------------------------------------------------------------------------- 1 | 2 | # 53. 最大子数组和 3 | 4 | ```cpp 5 | class Solution { 6 | public: 7 | int maxSubArray(vector& nums) { 8 | int res = INT_MIN; 9 | int cnt = 0; 10 | for (int num : nums) { 11 | cnt += num; 12 | res = max(res,cnt); 13 | if (cnt < 0) cnt = 0; 14 | } 15 | return res; 16 | } 17 | }; 18 | ``` 19 | 20 | `res = max(res,cnt);`和`if (cnt < 0) cnt = 0;`这两行顺序一开始搞错了, 导致input只是一个-1的时候有问题 21 | -------------------------------------------------------------------------------- /notes/src/day32.md: -------------------------------------------------------------------------------- 1 | # 第八章 贪心算法 part02 2 | ● 122.买卖股票的最佳时机II 3 | ● 55. 跳跃游戏 4 | ● 45.跳跃游戏II 5 | 6 | 详细布置 7 | 8 | ## 122.买卖股票的最佳时机II 9 | 10 | 本题解法很巧妙,大家可以看题思考一下,在看题解。 11 | 12 | https://programmercarl.com/0122.%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%9C%BAII.html 13 | 14 | ## 55. 跳跃游戏 15 | 16 | 本题如果没接触过,很难想到,所以不要自己憋时间太久,读题思考一会,没思路立刻看题解 17 | 18 | https://programmercarl.com/0055.%E8%B7%B3%E8%B7%83%E6%B8%B8%E6%88%8F.html 19 | 20 | ## 45.跳跃游戏II 21 | 22 | 本题同样不容易想出来。贪心就是这样,有的时候 会感觉简单到离谱,有时候,难的不行,主要是不容易想到。 23 | 24 | https://programmercarl.com/0045.%E8%B7%B3%E8%B7%83%E6%B8%B8%E6%88%8FII.html 25 | -------------------------------------------------------------------------------- /notes/src/day32/lc122.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # 122. 买卖股票的最佳时机 II 4 | 5 | 6 | 如果想到其实最终利润是可以分解的,那么本题就很容易了! 7 | 8 | 9 | ```cpp 10 | class Solution { 11 | public: 12 | int maxProfit(vector& prices) { 13 | int res = 0; 14 | for ( int i = 1; i < prices.size(); i ++ ) { 15 | int a = prices[i] - prices[i-1]; 16 | res += a>0?a:0; 17 | } 18 | return res; 19 | } 20 | }; 21 | ``` 22 | 23 | -------------------------------------------------------------------------------- /notes/src/day32/lc45.md: -------------------------------------------------------------------------------- 1 | # 45.跳跃游戏 II 2 | 3 | ```cpp 4 | class Solution { 5 | public: 6 | int jump(vector& nums) { 7 | int res = 1; 8 | int predist = nums[0]; 9 | int maxdist = predist; 10 | int i = 0; 11 | if(nums.size()==1)return 0; 12 | if (maxdist>=nums.size()-1) return 1; 13 | while (i <= maxdist) { 14 | int nextdist = i+nums[i]; 15 | maxdist=max(maxdist,nextdist); 16 | if (maxdist>=nums.size()-1) break; 17 | if (i == predist) { 18 | res ++ ; 19 | predist = maxdist; 20 | } 21 | i ++ ; 22 | 23 | } 24 | return res + 1; 25 | } 26 | }; 27 | ``` 28 | 29 | -------------------------------------------------------------------------------- /notes/src/day32/lc55.md: -------------------------------------------------------------------------------- 1 | # 55. 跳跃游戏 2 | 3 | ```cpp 4 | class Solution { 5 | public: 6 | bool canJump(vector& nums) { 7 | int dst = 0; 8 | int i = 0; 9 | while (i <= dst) { 10 | dst = max(dst,i+nums[i]); 11 | i ++ ; 12 | if (dst >= nums.size() - 1) return true; 13 | } 14 | return false; 15 | } 16 | }; 17 | ``` 18 | -------------------------------------------------------------------------------- /notes/src/day34.md: -------------------------------------------------------------------------------- 1 | # 第八章 贪心算法 part03 2 | ● 1005.K次取反后最大化的数组和 3 | ● 134. 加油站 4 | ● 135. 分发糖果 5 | 6 | 详细布置 7 | 8 | ## 1005.K次取反后最大化的数组和 9 | 本题简单一些,估计大家不用想着贪心 ,用自己直觉也会有思路。 10 | https://programmercarl.com/1005.K%E6%AC%A1%E5%8F%96%E5%8F%8D%E5%90%8E%E6%9C%80%E5%A4%A7%E5%8C%96%E7%9A%84%E6%95%B0%E7%BB%84%E5%92%8C.html 11 | 12 | 13 | ## 134. 加油站 14 | 本题有点难度,不太好想,推荐大家熟悉一下方法二 15 | https://programmercarl.com/0134.%E5%8A%A0%E6%B2%B9%E7%AB%99.html 16 | 17 | 18 | ## 135. 分发糖果 19 | 本题涉及到一个思想,就是想处理好一边再处理另一边,不要两边想着一起兼顾,后面还会有题目用到这个思路 20 | https://programmercarl.com/0135.%E5%88%86%E5%8F%91%E7%B3%96%E6%9E%9C.html 21 | -------------------------------------------------------------------------------- /notes/src/day34/lc1005.md: -------------------------------------------------------------------------------- 1 | # 1005.K次取反后最大化的数组和 2 | 3 | ```cpp 4 | class Solution { 5 | public: 6 | int largestSumAfterKNegations(vector& v, int k) { 7 | sort(v.begin(),v.end()); 8 | for(int& i: v){ 9 | if (i >= 0) break; 10 | i=-i;k--;if(k==0)break; 11 | }if(k==0)return accumulate(v.begin(), v.end(), 0); 12 | sort(v.begin(),v.end()); 13 | while (k>0){v[0]=-v[0];k--;} 14 | return accumulate(v.begin(), v.end(), 0); 15 | } 16 | }; 17 | ``` 18 | 19 | -------------------------------------------------------------------------------- /notes/src/day34/lc134.md: -------------------------------------------------------------------------------- 1 | # 134. 加油站 2 | 3 | ```cpp 4 | class Solution { 5 | public: 6 | int canCompleteCircuit(vector& g, vector& c) { 7 | int n = g.size(); 8 | vector h(n,0); 9 | // h[i] -> gas change from i-1 -> i 10 | h[0] = g[n-1] - c[n-1]; 11 | for (int i = 1 ; i < n ; i ++ ) { 12 | h[i] = g[i-1]-c[i-1]; 13 | } 14 | int start = 0; 15 | int pos = start; 16 | int csum = 0; 17 | while (pos < start + n && start < n) { 18 | csum += h[(1+pos)%n]; 19 | if (csum < 0) { 20 | start = pos + 1; 21 | csum = 0; 22 | } 23 | pos ++ ; 24 | } 25 | return start& v) { 7 | // 1,3,4,5,2 8 | // 1,2,3,4,1 9 | // 10 | vectorres(v.size(),1); 11 | for (int i = 1 ; i < v.size() ; i ++ ) { 12 | if (v[i] > v[i-1]) res[i] = res[i-1] + 1; 13 | } 14 | for (int i = v.size() - 2; i >= 0 ; i -- ) { 15 | if (v[i] > v[i+1] && res[i] <= res[i+1]) res[i] = res[i+1] + 1; 16 | } 17 | return accumulate(res.begin(),res.end(),0); 18 | } 19 | }; 20 | ``` 21 | 22 | WA了一发漏了`&& res[i] <= res[i+1]` 23 | 24 | -------------------------------------------------------------------------------- /notes/src/day35.md: -------------------------------------------------------------------------------- 1 | # 第八章 贪心算法 part04 2 | 3 | ● 860.柠檬水找零 4 | ● 406.根据身高重建队列 5 | ● 452. 用最少数量的箭引爆气球 6 | 7 | 详细布置 8 | 9 | ## 860.柠檬水找零 10 | 11 | 本题看上好像挺难,其实挺简单的,大家先尝试自己做一做。 12 | https://programmercarl.com/0860.%E6%9F%A0%E6%AA%AC%E6%B0%B4%E6%89%BE%E9%9B%B6.html 13 | 14 | ## 406.根据身高重建队列 15 | 16 | 本题有点难度,和分发糖果类似,不要两头兼顾,处理好一边再处理另一边。 17 | https://programmercarl.com/0406.%E6%A0%B9%E6%8D%AE%E8%BA%AB%E9%AB%98%E9%87%8D%E5%BB%BA%E9%98%9F%E5%88%97.html 18 | 19 | ## 452. 用最少数量的箭引爆气球 20 | 21 | 本题是一道 重叠区间的题目,好好做一做,因为明天三道题目,都是 重叠区间。 22 | https://programmercarl.com/0452.%E7%94%A8%E6%9C%80%E5%B0%91%E6%95%B0%E9%87%8F%E7%9A%84%E7%AE%AD%E5%BC%95%E7%88%86%E6%B0%94%E7%90%83.html -------------------------------------------------------------------------------- /notes/src/day35/lc452.md: -------------------------------------------------------------------------------- 1 | # 452. 用最少数量的箭引爆气球 2 | 3 | 想不出来用什么数据结构 4 | 5 | ```cpp 6 | class Solution { 7 | public: 8 | int findMinArrowShots(vector>& v) { 9 | int res = 1; 10 | sort(v.begin(),v.end()); 11 | for ( int i = 1 ; i < v.size(); i ++ ) { 12 | if ( v[i][0] > v[i-1][1] ) { 13 | res ++ ; 14 | } else { 15 | v[i][1] = min(v[i][1],v[i-1][1]); 16 | } 17 | } 18 | return res; 19 | } 20 | }; 21 | ``` 22 | 23 | 发现不用数据结构,要点是每次右端点取重合的最小值 24 | 25 | ```plaintext 26 | ====== 27 | ==================== 28 | ``` 29 | 30 | -------------------------------------------------------------------------------- /notes/src/day36.md: -------------------------------------------------------------------------------- 1 | # 第八章 贪心算法 part05 2 | ● 435. 无重叠区间 3 | ● 763.划分字母区间 4 | ● 56. 合并区间 5 | 6 | 详细布置 7 | 8 | 今天的三道题目,都算是 重叠区间 问题,大家可以好好感受一下。 都属于那种看起来好复杂,但一看贪心解法,惊呼:这么巧妙! 9 | 还是属于那种,做过了也就会了,没做过就很难想出来。 10 | 不过大家把如下三题做了之后, 重叠区间 基本上差不多了 11 | ## 435. 无重叠区间 12 | 13 | https://programmercarl.com/0435.%E6%97%A0%E9%87%8D%E5%8F%A0%E5%8C%BA%E9%97%B4.html 14 | ## 763.划分字母区间 15 | 16 | https://programmercarl.com/0763.%E5%88%92%E5%88%86%E5%AD%97%E6%AF%8D%E5%8C%BA%E9%97%B4.html 17 | ## 56. 合并区间 18 | 本题相对来说就比较难了。 19 | 20 | https://programmercarl.com/0056.%E5%90%88%E5%B9%B6%E5%8C%BA%E9%97%B4.html -------------------------------------------------------------------------------- /notes/src/day36/lc763.md: -------------------------------------------------------------------------------- 1 | # 763.划分字母区间 2 | 3 | ```cpp 4 | class Solution { 5 | public: 6 | vector partitionLabels(string s) { 7 | int v[26] = {0}; 8 | vector res; 9 | for (int i = 0; i < s.size(); i ++ ) { 10 | v[s[i]-'a']=i; 11 | } 12 | int last = v[s[0]-'a']; 13 | int pre = 0; 14 | for (int i = 0; i < s.size(); i ++ ) { 15 | last = max(last,v[s[i]-'a']); 16 | if (last == i) { 17 | res.push_back(i - pre + 1); 18 | pre = i + 1; 19 | } 20 | } 21 | return res; 22 | } 23 | }; 24 | ``` 25 | 26 | -------------------------------------------------------------------------------- /notes/src/day37.md: -------------------------------------------------------------------------------- 1 | # 第八章 贪心算法 part06 2 | 3 | ● 738.单调递增的数字 4 | ● 968.监控二叉树 5 | ● 总结 6 | 7 | 详细布置 8 | 9 | ## 738.单调递增的数字 10 | https://programmercarl.com/0738.%E5%8D%95%E8%B0%83%E9%80%92%E5%A2%9E%E7%9A%84%E6%95%B0%E5%AD%97.html 11 | 12 | ## 968.监控二叉树 (可以跳过) 13 | 14 | 本题是贪心和二叉树的一个结合,比较难,一刷大家就跳过吧。 15 | https://programmercarl.com/0968.%E7%9B%91%E6%8E%A7%E4%BA%8C%E5%8F%89%E6%A0%91.html 16 | ## 总结 17 | 18 | 可以看看贪心算法的总结,贪心本来就没啥规律,能写出个总结篇真的不容易了。 19 | https://programmercarl.com/%E8%B4%AA%E5%BF%83%E7%AE%97%E6%B3%95%E6%80%BB%E7%BB%93%E7%AF%87.html 20 | 21 | -------------------------------------------------------------------------------- /notes/src/day38/lc509.md: -------------------------------------------------------------------------------- 1 | # 509. 斐波那契数 2 | 3 | ```cpp 4 | class Solution { 5 | public: 6 | int fib(int n) { 7 | vector dp = vector(n+1,0); 8 | if (n <= 1) return n; 9 | dp[0] = 0; 10 | dp[1] = 1; 11 | for ( int i = 2; i < n+1; i++) { 12 | dp[i]=dp[i-1]+dp[i-2]; 13 | } 14 | return dp[n]; 15 | } 16 | }; 17 | ``` -------------------------------------------------------------------------------- /notes/src/day38/lc70.md: -------------------------------------------------------------------------------- 1 | # 70. 爬楼梯 2 | 3 | ```cpp 4 | class Solution { 5 | public: 6 | // 1-> 1; 2 -> 2; 3-> 3 7 | int climbStairs(int n) { 8 | vectordp(n);if(n<=3)return n; 9 | dp[0]=1;dp[1]=2;for(int i=2;i& cost) { 7 | // dp[i] => 到达i的最小花费 8 | // dp[i] = min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]) 9 | int n=cost.size(); 10 | vectordp(n+1); 11 | dp[0]=0;dp[1]=0; 12 | for(int i=2;i>dp(m, vector(n)); 8 | // dp[i][j] 到达坐标ij的不同路径树 9 | // dp[i][j] = (i>0?dp[i-1][j]:0)+(j>0?dp[i][j-1]:0); 10 | for(int j=0;j0?dp[i-1][j]:0)+(j>0?dp[i][j-1]:0); 13 | } 14 | return dp[m-1][n-1]; 15 | } 16 | }; 17 | ``` -------------------------------------------------------------------------------- /notes/src/day39/lc63.md: -------------------------------------------------------------------------------- 1 | # 63. 不同路径 II 2 | 3 | ```cpp 4 | class Solution { 5 | public: 6 | int uniquePathsWithObstacles(vector>& obstacleGrid) { 7 | // dp[i][j] 到达坐标ij的不同路径树 8 | // dp[i][j] = obstacleGrid[i][j]==1?0:(i>0?dp[i-1][j]:0)+(j>0?dp[i][j-1]:0); 9 | int m=obstacleGrid.size();int n=obstacleGrid[0].size(); 10 | vector>dp(m, vector(n,0)); 11 | int cnt = 1; 12 | for(int j=0;j0?dp[i-1][j]:0)+(j>0?dp[i][j-1]:0); 18 | } 19 | return dp[m-1][n-1]; 20 | } 21 | }; 22 | ``` -------------------------------------------------------------------------------- /notes/src/day41.md: -------------------------------------------------------------------------------- 1 | # 第九章 动态规划part03 2 | 3 | ● 343. 整数拆分 4 | ● 96.不同的二叉搜索树 5 | 6 | 详细布置 7 | 8 | 今天两题都挺有难度,建议大家思考一下没思路,直接看题解,第一次做,硬想很难想出来。 9 | ## 343. 整数拆分 10 | 11 | https://programmercarl.com/0343.%E6%95%B4%E6%95%B0%E6%8B%86%E5%88%86.html 12 | 视频讲解:https://www.bilibili.com/video/BV1Mg411q7YJ 13 | ## 96.不同的二叉搜索树 14 | 15 | https://programmercarl.com/0096.%E4%B8%8D%E5%90%8C%E7%9A%84%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91.html 16 | 视屏讲解:https://www.bilibili.com/video/BV1eK411o7QA 17 | -------------------------------------------------------------------------------- /notes/src/day41/lc343.md: -------------------------------------------------------------------------------- 1 | # 343. 整数拆分 2 | 3 | ```cpp 4 | class Solution { 5 | public: 6 | int integerBreak(int n) { 7 | // dp[i] -> 对于正整数i 将其拆分为k个数之和的乘机最大值 8 | vector dp(n+1); 9 | dp[0]=0; 10 | dp[1]=1; 11 | dp[2]=1; 12 | for (int i = 3; i <= n; i ++ ) { 13 | for (int j = 1; j <= i - j; j ++) { 14 | dp[i] = max(dp[i], j * max(i-j,dp[i-j])); 15 | } 16 | } 17 | return dp[n]; 18 | } 19 | }; 20 | ``` -------------------------------------------------------------------------------- /notes/src/day41/lc96.md: -------------------------------------------------------------------------------- 1 | # 96.不同的二叉搜索树 2 | 3 | 想了一下,不会做 4 | 5 | ```cpp 6 | class Solution { 7 | public: 8 | int numTrees(int n) { 9 | vector dp(n+1,0); 10 | if (n<=2)return n; 11 | if (n==3)return 5; 12 | // dp[i] -> 恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种 13 | dp[0] = 1; 14 | dp[1] = 1; 15 | dp[2] = 2; 16 | dp[3] = 5; 17 | for ( int i = 4; i <= n; i ++ ) 18 | for ( int j = 0; j < i; j ++ ) 19 | dp[i] += dp[j]*dp[i-j-1]; 20 | return dp[n]; 21 | } 22 | }; 23 | ``` 24 | 25 | 居然是要看形状,有点在猜一个公式的感觉 -------------------------------------------------------------------------------- /notes/src/day42.md: -------------------------------------------------------------------------------- 1 | # 第九章 动态规划part04 2 | 3 | ● 01背包问题,你该了解这些! 4 | ● 01背包问题,你该了解这些! 滚动数组 5 | ● 416. 分割等和子集 6 | 7 | 正式开始背包问题,背包问题还是挺难的,虽然大家可能看了很多背包问题模板代码,感觉挺简单,但基本理解的都不够深入。 8 | 9 | 如果是直接从来没听过背包问题,可以先看文字讲解慢慢了解 这是干什么的。 10 | 11 | 如果做过背包类问题,可以先看视频,很多内容,是自己平时没有考虑到位的。 12 | 13 | 背包问题,力扣上没有原题,大家先了解理论,今天就安排一道具体题目。 14 | 15 | 详细布置 16 | ## 01背包问题 二维 17 | https://programmercarl.com/%E8%83%8C%E5%8C%85%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%8001%E8%83%8C%E5%8C%85-1.html 18 | 视频讲解:https://www.bilibili.com/video/BV1cg411g7Y6 19 | ## 01背包问题 一维 20 | https://programmercarl.com/%E8%83%8C%E5%8C%85%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%8001%E8%83%8C%E5%8C%85-2.html 21 | 视频讲解:https://www.bilibili.com/video/BV1BU4y177kY 22 | ## 416. 分割等和子集 23 | 本题是 01背包的应用类题目 24 | https://programmercarl.com/0416.%E5%88%86%E5%89%B2%E7%AD%89%E5%92%8C%E5%AD%90%E9%9B%86.html 25 | 视频讲解:https://www.bilibili.com/video/BV1rt4y1N7jE 26 | 27 | -------------------------------------------------------------------------------- /notes/src/day42/lc416.md: -------------------------------------------------------------------------------- 1 | # 416. 分割等和子集 2 | 3 | 第一眼不会 4 | 5 | ```cpp 6 | class Solution { 7 | public: 8 | bool canPartition(vector& nums) { 9 | // dp[i] 容量为s//2的01背包 10 | int sum = 0; 11 | for (int i = 0; i < nums.size(); i ++ ) { 12 | sum += nums[i]; 13 | } 14 | if (sum & 1) return false; 15 | int target = sum >> 1; 16 | vector dp (1+target); 17 | for (int i = 0; i < nums.size(); i ++ ) { 18 | for (int j = target; j >= nums[i]; j -- ) { 19 | dp[j] = max(dp[j],dp[j-nums[i]]+nums[i]); 20 | } 21 | } 22 | return dp[target] == target; 23 | } 24 | }; 25 | ``` 26 | 27 | 一下子想不出转换成01背包的想法 -------------------------------------------------------------------------------- /notes/src/day43.md: -------------------------------------------------------------------------------- 1 | # 第九章 动态规划 part05 2 | ● 1049. 最后一块石头的重量 II 3 | ● 494. 目标和 4 | ● 474.一和零 5 | 6 | 详细布置 7 | ## 1049. 最后一块石头的重量 II 8 | 9 | 本题就和 昨天的 416. 分割等和子集 很像了,可以尝试先自己思考做一做。 10 | 视频讲解:https://www.bilibili.com/video/BV14M411C7oV 11 | https://programmercarl.com/1049.%E6%9C%80%E5%90%8E%E4%B8%80%E5%9D%97%E7%9F%B3%E5%A4%B4%E7%9A%84%E9%87%8D%E9%87%8FII.html 12 | ## 494. 目标和 13 | 大家重点理解 递推公式:dp[j] += dp[j - nums[i]],这个公式后面的提问 我们还会用到。 14 | 视频讲解:https://www.bilibili.com/video/BV1o8411j73x 15 | https://programmercarl.com/0494.%E7%9B%AE%E6%A0%87%E5%92%8C.html 16 | 17 | ## 474.一和零 18 | 通过这道题目,大家先粗略了解, 01背包,完全背包,多重背包的区别,不过不用细扣,因为后面 对于 完全背包,多重背包 还有单独讲解。 19 | 视频讲解:https://www.bilibili.com/video/BV1rW4y1x7ZQ 20 | https://programmercarl.com/0474.%E4%B8%80%E5%92%8C%E9%9B%B6.html 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /notes/src/day43/lc1049.md: -------------------------------------------------------------------------------- 1 | # 1049. 最后一块石头的重量 II 2 | 3 | ```cpp 4 | class Solution { 5 | public: 6 | int lastStoneWeightII(vector& nums) { 7 | int sum = 0; 8 | for (int i = 0; i < nums.size(); i ++ ) { 9 | sum += nums[i]; 10 | } 11 | int target = sum >> 1; 12 | vector dp (1+target,0); 13 | for (int i = 0; i < nums.size(); i ++ ) { 14 | for (int j = target; j >= nums[i]; j -- ) { 15 | dp[j] = max(dp[j],dp[j-nums[i]]+nums[i]); 16 | } 17 | } 18 | return sum - dp[target]*2; 19 | } 20 | }; 21 | ``` -------------------------------------------------------------------------------- /notes/src/day43/lc474.md: -------------------------------------------------------------------------------- 1 | # 474.一和零 2 | 3 | ```cpp 4 | class Solution { 5 | public: 6 | int findMaxForm(vector& strs, int m, int n) { 7 | // dp[i][j] 表示子集中最多i个0和j个1的最大子集长度 8 | vector>dp(1+m,vector(1+n,0)); 9 | // x = count(s.begin(),s.end(),'0'); 10 | // y = s.size() - x; 11 | // dp[i][j] = max(dp[i][j],dp[i-x][j-y]+1) 12 | for (int u = 0; u < strs.size() ; u ++ ) { 13 | string s = strs[u]; 14 | int x = count(s.begin(),s.end(),'0'); 15 | int y = s.size() - x; 16 | for ( int i = m ; i >= x; i -- ) { 17 | for ( int j = n ; j >= y; j -- ) { 18 | dp[i][j] = max(dp[i][j],dp[i-x][j-y]+1); 19 | } 20 | } 21 | } 22 | return dp[m][n]; 23 | } 24 | }; 25 | ``` 26 | 27 | 这个题很舒服,自己一下写过的 -------------------------------------------------------------------------------- /notes/src/day43/lc494.md: -------------------------------------------------------------------------------- 1 | # 494.目标和 2 | 3 | 一开始不会做,一种心的dp推导公式 4 | 5 | ```cpp 6 | class Solution { 7 | public: 8 | int findTargetSumWays(vector& nums, int target) { 9 | int sum = 0; 10 | for (int i = 0; i < nums.size(); i ++ ) { 11 | sum += nums[i]; 12 | } 13 | int left = (target + sum); 14 | if (left & 1) return 0; 15 | left = left >> 1; 16 | vectordp(left+1); 17 | dp[0] = 1; 18 | // dp[j] 表示:填满j(包括j)这么大容积的包,有dp[j]种方法 19 | for ( int i = 0; i < nums.size() ; i ++ ) { 20 | for (int j = dp.size() - 1; j >= nums[i] ; j -- ) { 21 | dp[j] += dp[j-nums[i]]; 22 | } 23 | } 24 | return dp[left]; 25 | } 26 | }; 27 | ``` -------------------------------------------------------------------------------- /notes/src/day44.md: -------------------------------------------------------------------------------- 1 | # 第九章 动态规划part06 2 | ● 完全背包 3 | ● 518. 零钱兑换 II 4 | ● 377. 组合总和 Ⅳ 5 | 6 | 详细布置 7 | 8 | 力扣上没有纯粹的完全背包的题目,所以大家看本篇了解一下 完全背包的理论 9 | 10 | 后面的两道题目,都是完全背包的应用,做做感受一下 11 | 12 | ## 完全背包 13 | 视频讲解:https://www.bilibili.com/video/BV1uK411o7c9 14 | https://programmercarl.com/%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%98%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80%E5%AE%8C%E5%85%A8%E8%83%8C%E5%8C%85.html 15 | 16 | ## 518. 零钱兑换 II 17 | 视频讲解:https://www.bilibili.com/video/BV1KM411k75j 18 | https://programmercarl.com/0518.%E9%9B%B6%E9%92%B1%E5%85%91%E6%8D%A2II.html 19 | 20 | ## 377. 组合总和 Ⅳ 21 | 视频讲解:https://www.bilibili.com/video/BV1V14y1n7B6 22 | https://programmercarl.com/0377.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8C%E2%85%A3.html 23 | -------------------------------------------------------------------------------- /notes/src/day44/lc377.md: -------------------------------------------------------------------------------- 1 | # 377. 组合总和 Ⅳ 2 | 3 | 不会做,抄了随想录 4 | 5 | ```cpp 6 | class Solution { 7 | public: 8 | int combinationSum4(vector& nums, int target) { 9 | // dp[i] 表示总和为i的元素组合的个数 10 | vectordp(1+target); 11 | dp[0]=1; 12 | for(int j=0;j<=target;j++){ 13 | for(int i=0;i=nums[i]) 15 | dp[j]+=dp[j-nums[i]]; 16 | } 17 | } 18 | return dp[target]; 19 | } 20 | }; 21 | ``` -------------------------------------------------------------------------------- /notes/src/day44/lc518.md: -------------------------------------------------------------------------------- 1 | # 518. 零钱兑换 II 2 | 3 | 第一道完全背包的题目,抄了随想录 4 | 5 | ```cpp 6 | class Solution { 7 | public: 8 | int change(int amount, vector& coins) { 9 | // dp[j]:凑成总金额j的货币组合数为dp[j] 10 | vectordp(amount+1); 11 | dp[0]=1; 12 | // dp[j] += dp[j - coins[i]]; 13 | for(int i = 0; i < coins.size(); i ++ ) { 14 | for (int j = coins[i]; j <= amount; j ++ ) { // 完全背包,需要重复计算之前的组合数 15 | dp[j] += dp[j-coins[i]]; 16 | } 17 | } 18 | return dp[amount]; 19 | } 20 | }; 21 | ``` -------------------------------------------------------------------------------- /notes/src/day45.md: -------------------------------------------------------------------------------- 1 | # 第九章 动态规划part07 2 | 3 | ● 70. 爬楼梯 (进阶) 4 | ● 322. 零钱兑换 5 | ● 279.完全平方数 6 | 7 | 详细布置 8 | 9 | ## 70. 爬楼梯 (进阶) 10 | 11 | 这道题目 爬楼梯之前我们做过,这次再用完全背包的思路来分析一遍 12 | 13 | https://programmercarl.com/0070.%E7%88%AC%E6%A5%BC%E6%A2%AF%E5%AE%8C%E5%85%A8%E8%83%8C%E5%8C%85%E7%89%88%E6%9C%AC.html 14 | 15 | ## 322. 零钱兑换 16 | 17 | 如果求组合数就是外层for循环遍历物品,内层for遍历背包。 18 | 如果求排列数就是外层for遍历背包,内层for循环遍历物品。 19 | 20 | 这句话结合本题 大家要好好理解。 21 | 视频讲解:https://www.bilibili.com/video/BV14K411R7yv 22 | https://programmercarl.com/0322.%E9%9B%B6%E9%92%B1%E5%85%91%E6%8D%A2.html 23 | 24 | ## 279.完全平方数 25 | 本题 和 322. 零钱兑换 基本是一样的,大家先自己尝试做一做 26 | 视频讲解:https://www.bilibili.com/video/BV12P411T7Br 27 | https://programmercarl.com/0279.%E5%AE%8C%E5%85%A8%E5%B9%B3%E6%96%B9%E6%95%B0.html 28 | -------------------------------------------------------------------------------- /notes/src/day45/lc279.md: -------------------------------------------------------------------------------- 1 | # 279.完全平方数 2 | 3 | 4 | 喜喜这个自己做的 5 | ```cpp 6 | class Solution { 7 | public: 8 | int numSquares(int n) { 9 | // dp[i] 和为 i 的完全平方数的最少数量 10 | // dp[j] = min(dp[j],dp[j-i*i]+1) 11 | vectordp(1+n,INT_MAX); 12 | dp[0]=0; 13 | for(int i = 1; i * i <= n; i ++ ) { 14 | for(int j = i*i; j <= n; j ++ ) { 15 | dp[j] = min(dp[j],dp[j-i*i]+1); 16 | } 17 | } 18 | return dp[n]; 19 | } 20 | }; 21 | ``` -------------------------------------------------------------------------------- /notes/src/day45/lc322.md: -------------------------------------------------------------------------------- 1 | # 322. 零钱兑换 2 | 3 | 抄了随想录 4 | 5 | ```cpp 6 | class Solution { 7 | public: 8 | int coinChange(vector& coins, int amount) { 9 | // if (amount == 0) return 0; 10 | // dp[i] -> 可以凑成i所需的 最少的硬币个数 11 | // dp[i] = min(dp[i],dp[i-coins[j]]+1) 12 | vectordp(amount+1,INT_MAX); 13 | dp[0]=0; 14 | for(int i=0;i 爬到有i个台阶的楼顶,有dp[i]种方法 9 | vector dp(1+n); 10 | dp[0]=1; 11 | for(int i=0;i<=n;i++) { 12 | for(int j=1;j<=m;j++) { 13 | if(i-j>=0) 14 | dp[i]+=dp[i-j]; 15 | } 16 | } 17 | return dp[n]; 18 | } 19 | }; 20 | ``` -------------------------------------------------------------------------------- /notes/src/day46.md: -------------------------------------------------------------------------------- 1 | # 第九章 动态规划part08 2 | ● 139.单词拆分 3 | ● 关于多重背包,你该了解这些! 4 | ● 背包问题总结篇! 5 | 6 | 详细布置 7 | 8 | 关于 多重背包,力扣上没有相关的题目,所以今天大家的重点就是回顾一波 自己做的背包题目吧。 9 | 10 | ## 139.单词拆分 11 | 视频讲解:https://www.bilibili.com/video/BV1pd4y147Rh 12 | https://programmercarl.com/0139.%E5%8D%95%E8%AF%8D%E6%8B%86%E5%88%86.html 13 | 14 | ## 关于多重背包,你该了解这些! 15 | https://programmercarl.com/%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%98%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80%E5%A4%9A%E9%87%8D%E8%83%8C%E5%8C%85.html 16 | 17 | ## 背包问题总结篇! 18 | https://programmercarl.com/%E8%83%8C%E5%8C%85%E6%80%BB%E7%BB%93%E7%AF%87.html 19 | -------------------------------------------------------------------------------- /notes/src/day46/lc139.md: -------------------------------------------------------------------------------- 1 | # 139. 单词拆分 2 | 3 | 不会 4 | 5 | ```cpp 6 | class Solution { 7 | public: 8 | bool wordBreak(string s, vector& wordDict) { 9 | // dp[i] -> 长度为i的字符串是否可以拼出来 10 | // dp[j] = true if dp[j-i] == true and [i:j] in wordDict 11 | unordered_set wordSet(wordDict.begin(),wordDict.end()); 12 | vectordp(s.size()+1,false); 13 | dp[0]=true; 14 | for (int i = 1 ; i<=s.size();i++) { 15 | for (int j = 0; j < i; j ++ ) { 16 | string word = s.substr(j,i-j); 17 | if (dp[j] && wordSet.find(word)!= wordSet.end()) 18 | dp[i] = true; 19 | } 20 | } 21 | return dp[s.size()]; 22 | } 23 | }; 24 | ``` -------------------------------------------------------------------------------- /notes/src/day48.md: -------------------------------------------------------------------------------- 1 | # 第九章 动态规划part09 2 | ● 198.打家劫舍 3 | ● 213.打家劫舍II 4 | ● 337.打家劫舍III 5 | 6 | 详细布置 7 | 8 | 今天就是打家劫舍的一天,这个系列不算难,大家可以一口气拿下。 9 | 10 | ## 198.打家劫舍 11 | 视频讲解:https://www.bilibili.com/video/BV1Te411N7SX 12 | https://programmercarl.com/0198.%E6%89%93%E5%AE%B6%E5%8A%AB%E8%88%8D.html 13 | 14 | ## 213.打家劫舍II 15 | 视频讲解:https://www.bilibili.com/video/BV1oM411B7xq 16 | https://programmercarl.com/0213.%E6%89%93%E5%AE%B6%E5%8A%AB%E8%88%8DII.html 17 | 18 | ## 337.打家劫舍III 19 | 视频讲解:https://www.bilibili.com/video/BV1H24y1Q7sY 20 | https://programmercarl.com/0337.%E6%89%93%E5%AE%B6%E5%8A%AB%E8%88%8DIII.html 21 | -------------------------------------------------------------------------------- /notes/src/day48/lc198.md: -------------------------------------------------------------------------------- 1 | # 198. 打家劫舍 2 | 3 | 这个是抄随想录的 4 | 5 | 6 | ```cpp 7 | class Solution { 8 | public: 9 | int rob(vector& nums) { 10 | if (nums.size() == 0) return 0; 11 | if (nums.size() == 1) return nums[0]; 12 | // dp[i]:考虑下标i(包括i)以内的房屋,最多可以偷窃的金额为dp[i] 13 | // dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]); 14 | vector dp(nums.size()); 15 | dp[0] = nums[0]; 16 | dp[1] = max(nums[0], nums[1]); 17 | for (int i = 2; i < nums.size(); i++) { 18 | dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]); 19 | } 20 | return dp[nums.size() - 1]; 21 | } 22 | }; 23 | ``` -------------------------------------------------------------------------------- /notes/src/day49.md: -------------------------------------------------------------------------------- 1 | # 第九章 动态规划part10 2 | 3 | ● 121. 买卖股票的最佳时机 4 | ● 122.买卖股票的最佳时机II 5 | 6 | 详细布置 7 | 8 | 股票问题是一个动态规划的系列问题,今日安排的题目不多,大家可以慢慢消化。 9 | 10 | ## 121. 买卖股票的最佳时机 11 | 视频讲解:https://www.bilibili.com/video/BV1Xe4y1u77q 12 | https://programmercarl.com/0121.%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%9C%BA.html 13 | 14 | 15 | 16 | 17 | 18 | ## 122.买卖股票的最佳时机II 19 | 视频讲解:https://www.bilibili.com/video/BV1D24y1Q7Ls 20 | https://programmercarl.com/0122.%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%9C%BAII%EF%BC%88%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%89.html 21 | -------------------------------------------------------------------------------- /notes/src/day49/lc121.md: -------------------------------------------------------------------------------- 1 | # 121. 买卖股票的最佳时机 2 | 3 | 一开始这样子没有用动规 4 | 5 | ```cpp 6 | class Solution { 7 | public: 8 | int maxProfit(vector& prices) { 9 | int res = 0; 10 | int cmin = INT_MAX; 11 | for (int i = 0; i < prices.size(); i ++ ) { 12 | cmin = min(cmin, prices[i]); 13 | if (prices[i] > cmin) { 14 | res = max(res, prices[i] - cmin); 15 | } 16 | } 17 | return res; 18 | } 19 | }; 20 | ``` -------------------------------------------------------------------------------- /notes/src/day5.md: -------------------------------------------------------------------------------- 1 | # 休息日 2 | 3 | 休息日我用来补进度了 -------------------------------------------------------------------------------- /notes/src/day50.md: -------------------------------------------------------------------------------- 1 | # 第九章 动态规划part11 2 | 3 | ● 123.买卖股票的最佳时机III 4 | ● 188.买卖股票的最佳时机IV 5 | 6 | 详细布置 7 | 8 | ## 123.买卖股票的最佳时机III 9 | 10 | 这道题一下子就难度上来了,关键在于至多买卖两次,这意味着可以买卖一次,可以买卖两次,也可以不买卖。 11 | 视频讲解:https://www.bilibili.com/video/BV1WG411K7AR 12 | https://programmercarl.com/0123.%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%9C%BAIII.html 13 | 14 | ## 188.买卖股票的最佳时机IV 15 | 本题是123.买卖股票的最佳时机III 的进阶版 16 | 视频讲解:https://www.bilibili.com/video/BV16M411U7XJ 17 | https://programmercarl.com/0188.%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%9C%BAIV.html 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /notes/src/day50/lc123.md: -------------------------------------------------------------------------------- 1 | # 123.买卖股票的最佳时机III 2 | 3 | 4 | 自己写的时候漏掉了`dp[0][3]=-prices[0];`这个条件 5 | 6 | ```cpp 7 | class Solution { 8 | public: 9 | int maxProfit(vector& prices) { 10 | // 一天一共就有五个状态, 11 | 12 | // 没有操作 (其实我们也可以不设置这个状态) 13 | // 第一次持有股票 14 | // 第一次不持有股票 15 | // 第二次持有股票 16 | // 第二次不持有股票 17 | // dp[i][j]中 i表示第i天,j为 [0 - 4] 五个状态,dp[i][j]表示第i天状态j所剩最大现金。 18 | vector>dp(prices.size(), vector(5,0)); 19 | dp[0][1]=dp[0][3]=-prices[0]; 20 | 21 | for(int i=1;i& prices) { 9 | vector>dp(prices.size(), vector(2*k+1,0)); 10 | for(int j=0;j& v) { 9 | // dp[i][j] 表示第i天的最大现金 10 | // 0 持有 11 | // 1 非持有且不在冷冻期 12 | // 2 非持有且冷冻期(刚出售) 13 | vector>dp(v.size(),vector(3,0)); 14 | dp[0][0]=-v[0]; 15 | for(int i=1;i& v, int fee) { 7 | // dp[i][j] 8 | // 0 持有 9 | // 1 不持有 10 | vector>dp(v.size(),vector(2,0)); 11 | dp[0][0]=-v[0]; 12 | for(int i=1;i& v) { 8 | // dp[i] 表示以i结尾的LIS的长度 9 | int res = 1; 10 | vectordp(v.size(),1); 11 | for(int i = 1;iv[j])dp[i]=max(dp[i],dp[j]+1); 14 | } 15 | res = max(res, dp[i]); 16 | } 17 | return res; 18 | } 19 | }; 20 | ``` -------------------------------------------------------------------------------- /notes/src/day52/lc674.md: -------------------------------------------------------------------------------- 1 | # 674. 最长连续递增序列 2 | 3 | 4 | 好像比上一题还简单 5 | 6 | ```cpp 7 | class Solution { 8 | public: 9 | int findLengthOfLCIS(vector& v) { 10 | // dp[i] 表示以i结尾的CLIS的长度 11 | int res = 1; 12 | vectordp(v.size(),1); 13 | for(int i = 1;iv[i-1])dp[i]=max(dp[i],dp[i-1]+1); 15 | res = max(res, dp[i]); 16 | } 17 | return res; 18 | } 19 | }; 20 | ``` -------------------------------------------------------------------------------- /notes/src/day52/lc718.md: -------------------------------------------------------------------------------- 1 | # 718. 最长重复子数组 2 | 3 | 抄了随想路 4 | 5 | ```cpp 6 | class Solution { 7 | public: 8 | int findLength(vector& nums1, vector& nums2) { 9 | // dp[i][j] :以下标i - 1为结尾的A,和以下标j - 1为结尾的B,最长重复子数组长度为dp[i][j]。 (特别注意: “以下标i - 1为结尾的A” 标明一定是 以A[i-1]为结尾的字符串 ) 10 | vector> dp (nums1.size() + 1, vector(nums2.size() + 1, 0)); 11 | int result = 0; 12 | for (int i = 1; i <= nums1.size(); i++) { 13 | for (int j = 1; j <= nums2.size(); j++) { 14 | if (nums1[i - 1] == nums2[j - 1]) { 15 | dp[i][j] = dp[i - 1][j - 1] + 1; 16 | } 17 | if (dp[i][j] > result) result = dp[i][j]; 18 | } 19 | } 20 | return result; 21 | } 22 | }; 23 | ``` -------------------------------------------------------------------------------- /notes/src/day53.md: -------------------------------------------------------------------------------- 1 | 第九章 动态规划part14 2 | ● 1143.最长公共子序列 3 | ● 1035.不相交的线 4 | ● 53. 最大子序和 动态规划 5 | 6 | 详细布置 7 | 8 | 1143.最长公共子序列 9 | 10 | 体会一下本题和 718. 最长重复子数组 的区别 11 | 视频讲解:https://www.bilibili.com/video/BV1ye4y1L7CQ 12 | https://programmercarl.com/1143.%E6%9C%80%E9%95%BF%E5%85%AC%E5%85%B1%E5%AD%90%E5%BA%8F%E5%88%97.html 13 | 14 | 1035.不相交的线 15 | 16 | 其实本题和 1143.最长公共子序列 是一模一样的,大家尝试自己做一做。 17 | 视频讲解:https://www.bilibili.com/video/BV1h84y1x7MP 18 | https://programmercarl.com/1035.%E4%B8%8D%E7%9B%B8%E4%BA%A4%E7%9A%84%E7%BA%BF.html 19 | 20 | 53. 最大子序和 21 | 22 | 这道题我们用贪心做过,这次 再用dp来做一遍 23 | 视频讲解:https://www.bilibili.com/video/BV19V4y1F7b5 24 | https://programmercarl.com/0053.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C%EF%BC%88%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%89.html 25 | 26 | -------------------------------------------------------------------------------- /notes/src/day53/lc1035.md: -------------------------------------------------------------------------------- 1 | # 1035.不相交的线 2 | 3 | 原来需要转化成‘最长公共子序列的长度’,一下子真不会 4 | 5 | ```cpp 6 | class Solution { 7 | public: 8 | int maxUncrossedLines(vector& v, vector& w) { 9 | // dp[i][j]:长度为[0, i - 1]的字符串text1与长度为[0, j - 1]的字符串text2的最长公共子序列为dp[i][j] 10 | vector>dp(v.size()+1, vector(w.size()+1, 0)); 11 | int result = 0; 12 | for (int i = 1; i <= v.size(); i++) { 13 | for (int j = 1; j <= w.size(); j++) { 14 | if (v[i - 1] == w[j - 1]) { 15 | dp[i][j] = dp[i - 1][j - 1] + 1; 16 | } else { 17 | dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); 18 | } 19 | if (dp[i][j] > result) result = dp[i][j]; 20 | } 21 | } 22 | return result; 23 | } 24 | }; 25 | ``` -------------------------------------------------------------------------------- /notes/src/day53/lc1143.md: -------------------------------------------------------------------------------- 1 | # 1143.最长公共子序列 2 | 3 | 抄了随想录 4 | 5 | ```cpp 6 | class Solution { 7 | public: 8 | int longestCommonSubsequence(string v, string w) { 9 | // dp[i][j]:长度为[0, i - 1]的字符串text1与长度为[0, j - 1]的字符串text2的最长公共子序列为dp[i][j] 10 | vector>dp(v.size()+1, vector(w.size()+1, 0)); 11 | int result = 0; 12 | for (int i = 1; i <= v.size(); i++) { 13 | for (int j = 1; j <= w.size(); j++) { 14 | if (v[i - 1] == w[j - 1]) { 15 | dp[i][j] = dp[i - 1][j - 1] + 1; 16 | } else { 17 | dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); 18 | } 19 | if (dp[i][j] > result) result = dp[i][j]; 20 | } 21 | } 22 | return result; 23 | } 24 | }; 25 | ``` -------------------------------------------------------------------------------- /notes/src/day53/lc53.md: -------------------------------------------------------------------------------- 1 | # 53. 最大子序和 2 | 3 | 自己做的 喜喜 4 | 5 | ```cpp 6 | class Solution { 7 | public: 8 | int maxSubArray(vector& v) { 9 | // dp[i] 以前i个数结尾的连续子数组最大和 10 | int res = v[0]; 11 | vectordp(v.size(),0); 12 | dp[0]=res; 13 | for(int i=1;i>dp(s.size()+1,vector(t.size()+1,0)); 15 | dp[0][0]=1; 16 | // for(int i=1;i>dp(s.size()+1,vector(t.size()+1,0)); 13 | for(int i=1;i>dp(s.size()+1,vector(t.size()+1,0)); 12 | for(int j=1;j>dp(s.size(),vector(s.size(),0)); 9 | for (int i = 0; i < s.size(); i++) dp[i][i] = 1; 10 | for (int i = s.size() - 1; i >= 0; i--) { 11 | for (int j = i + 1; j < s.size(); j++) { 12 | if (s[i] == s[j]) { 13 | dp[i][j] = dp[i + 1][j - 1] + 2; 14 | } else { 15 | dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]); 16 | } 17 | } 18 | } 19 | return dp[0][s.size() - 1]; 20 | } 21 | }; 22 | ``` -------------------------------------------------------------------------------- /notes/src/day56/lc647.md: -------------------------------------------------------------------------------- 1 | # 647. 回文子串 2 | 3 | 抄了随想录 4 | 5 | ```cpp 6 | class Solution { 7 | public: 8 | int countSubstrings(string s) { 9 | // 布尔类型的dp[i][j]:表示区间范围[i,j] (注意是左闭右闭)的子串是否是回文子串,如果是dp[i][j]为true,否则为false。 10 | vector> dp(s.size(), vector(s.size(), false)); 11 | int result = 0; 12 | for (int i = s.size() - 1; i >= 0; i--) { // 注意遍历顺序 13 | for (int j = i; j < s.size(); j++) { 14 | if (s[i] == s[j]) { 15 | if (j - i <= 1) { // 情况一 和 情况二 16 | result++; 17 | dp[i][j] = true; 18 | } else if (dp[i + 1][j - 1]) { // 情况三 19 | result++; 20 | dp[i][j] = true; 21 | } 22 | } 23 | } 24 | } 25 | return result; 26 | } 27 | }; 28 | ``` -------------------------------------------------------------------------------- /notes/src/day56/lc72.md: -------------------------------------------------------------------------------- 1 | # 72. 编辑距离 2 | 3 | ```cpp 4 | class Solution { 5 | public: 6 | int minDistance(string s, string t) { 7 | // s[0:i-1] vs t[0:j-1] 最近编辑距离为dp[i][j] 8 | vector>dp(s.size()+1,vector(t.size()+1,0)); 9 | for(int j=1;j& v) { 9 | int res = 0; 10 | vectors; 11 | for(int i=0;i nextGreaterElement(vector&v1,vector&v2) { 9 | vectorres(v1.size(),-1); 10 | unordered_map m; // key:下标元素,value:下标 11 | for (int i = 0; i < v1.size(); i++) { 12 | m[v1[i]] = i; 13 | } 14 | vectors; 15 | for(int i=0;i nextGreaterElements(vector& v) { 7 | vectorres(v.size(),-1); 8 | vectors; 9 | int n = v.size(); 10 | for(int i=0;i dailyTemperatures(vector& v) { 7 | vectorres(v.size(),0); 8 | // 1 9 | // 1 1 10 | // 1 1 1 11 | vectors; 12 | for(int i=0;i, target: i32) -> Vec { 21 | use std::collections::HashMap; 22 | let mut map: HashMap = HashMap::new(); 23 | for (idx, i) in nums.iter().enumerate() { 24 | if map.contains_key(&(target - i)) { 25 | return vec![*map.get(&(target - i)).unwrap(), idx as i32] 26 | } else { 27 | map.insert(*i, idx as i32); 28 | } 29 | } 30 | todo!() 31 | } 32 | } 33 | ``` 34 | 35 | 36 | ## 学习感想 -------------------------------------------------------------------------------- /notes/src/day60.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ainevsia/Leetcode-Rust/57e687cf87b74360cbbbfc0f5954830b1a6fbc3c/notes/src/day60.md -------------------------------------------------------------------------------- /notes/src/day7/lc383.md: -------------------------------------------------------------------------------- 1 | # 383. 赎金信 2 | 3 | ## 题目描述 4 | 5 | 给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。 6 | 7 | 如果可以,返回 true ;否则返回 false 。 8 | 9 | magazine 中的每个字符只能在 ransomNote 中使用一次。 10 | 11 | ## 解题思路 12 | 13 | ```rust 14 | 15 | struct Solution {} 16 | 17 | impl Solution { 18 | pub fn can_construct(ransom_note: String, magazine: String) -> bool { 19 | use std::collections::HashMap; 20 | let mut map: HashMap = HashMap::new(); 21 | for c in magazine.chars() { 22 | *map.entry(c).or_default() += 1; 23 | } 24 | for c in ransom_note.chars() { 25 | if ! map.contains_key(&c) { return false } 26 | let a = map.get_mut(&c).unwrap(); 27 | *a -= 1; 28 | if *a == 0 { map.remove(&c); } 29 | } 30 | true 31 | } 32 | } 33 | ``` 34 | 35 | ## 学习感想 36 | 37 | 普通hashmap 的使用 -------------------------------------------------------------------------------- /notes/src/day8/lc344.md: -------------------------------------------------------------------------------- 1 | # 344. 反转字符串 2 | 3 | ## 题目描述 4 | 5 | 编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 s 的形式给出。 6 | 7 | 不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。 8 | 9 | ## 解题思路 10 | 11 | ```rust 12 | 13 | struct Solution {} 14 | impl Solution { 15 | pub fn reverse_string(s: &mut Vec) { 16 | let n = s.len(); 17 | for i in 0..n/2 { 18 | let tmp = s[i]; 19 | s[i] = s[n-1-i]; 20 | s[n-1-i] = tmp; 21 | } 22 | } 23 | } 24 | ``` 25 | 26 | ## 学习感想 -------------------------------------------------------------------------------- /notes/src/day8/offer58.md: -------------------------------------------------------------------------------- 1 | # 剑指 Offer 58 - II. 左旋转字符串 2 | 3 | ## 题目描述 4 | 5 | 字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。 6 | 7 | ## 解题思路 8 | 9 | 想不到的思路 10 | 11 | ```rust 12 | 13 | struct Solution {} 14 | 15 | impl Solution { 16 | pub fn reverse_left_words(s: String, n: i32) -> String { 17 | let mut v: Vec = s.chars().collect(); 18 | Self::reverse2(&mut v[..n as usize]); 19 | Self::reverse2(&mut v[n as usize..]); 20 | Self::reverse2(&mut v[..]); 21 | v.iter().collect() 22 | } 23 | 24 | pub fn reverse2(s: &mut [char]) { 25 | let n = s.len(); 26 | for i in 0..n/2 { 27 | let tmp = s[i]; 28 | s[i] = s[n-1-i]; 29 | s[n-1-i] = tmp; 30 | } 31 | } 32 | } 33 | ``` 34 | 35 | ## 学习感想 -------------------------------------------------------------------------------- /notes/src/lc.md: -------------------------------------------------------------------------------- 1 | # 2 | ## 题目描述 3 | ## 解题思路 4 | ## 学习感想 -------------------------------------------------------------------------------- /notes/src/remains.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ainevsia/Leetcode-Rust/57e687cf87b74360cbbbfc0f5954830b1a6fbc3c/notes/src/remains.md --------------------------------------------------------------------------------