├── website ├── content │ ├── docs │ │ ├── shortcodes │ │ │ ├── section │ │ │ │ ├── page1.md │ │ │ │ ├── page2.md │ │ │ │ └── _index.md │ │ │ ├── _index.md │ │ │ ├── buttons.md │ │ │ └── details.md │ │ └── example │ │ │ └── collapsed │ │ │ └── 3rd-level │ │ │ └── 4th-level.md │ ├── ChapterTwo │ │ └── Binary_Indexed_Tree.md │ ├── ChapterThree │ │ └── _index.md │ └── ChapterFour │ │ └── _index.md ├── themes │ └── book │ │ ├── layouts │ │ ├── partials │ │ │ └── docs │ │ │ │ ├── inject │ │ │ │ ├── footer.html │ │ │ │ ├── content-after.html │ │ │ │ ├── content-before.html │ │ │ │ ├── menu-after.html │ │ │ │ └── menu-before.html │ │ │ │ ├── toc.html │ │ │ │ ├── comments.html │ │ │ │ ├── menu-bundle.html │ │ │ │ ├── date.html │ │ │ │ ├── title.html │ │ │ │ ├── post-meta.html │ │ │ │ ├── header.html │ │ │ │ └── taxonomy.html │ │ ├── _default │ │ │ ├── list.html │ │ │ ├── single.html │ │ │ └── _markup │ │ │ │ └── render-heading.html │ │ ├── shortcodes │ │ │ ├── hint.html │ │ │ ├── columns.html │ │ │ ├── section.html │ │ │ ├── details.html │ │ │ ├── button.html │ │ │ ├── mermaid.html │ │ │ ├── tab.html │ │ │ ├── expand.html │ │ │ ├── tabs.html │ │ │ └── katex.html │ │ ├── posts │ │ │ ├── single.html │ │ │ └── list.html │ │ └── taxonomy │ │ │ ├── list.html │ │ │ └── taxonomy.html │ │ ├── .gitignore │ │ ├── exampleSite │ │ ├── content │ │ │ ├── docs │ │ │ │ ├── shortcodes │ │ │ │ │ ├── section │ │ │ │ │ │ ├── page1.md │ │ │ │ │ │ ├── page2.md │ │ │ │ │ │ └── _index.md │ │ │ │ │ ├── _index.md │ │ │ │ │ ├── buttons.md │ │ │ │ │ └── details.md │ │ │ │ └── example │ │ │ │ │ └── collapsed │ │ │ │ │ └── 3rd-level │ │ │ │ │ └── 4th-level.md │ │ │ └── posts │ │ │ │ └── _index.md │ │ ├── assets │ │ │ ├── _variables.scss │ │ │ └── _custom.scss │ │ └── resources │ │ │ └── _gen │ │ │ └── assets │ │ │ └── scss │ │ │ ├── book.scss_50fc8c04e12a2f59027287995557ceff.json │ │ │ └── example │ │ │ └── book.scss_50fc8c04e12a2f59027287995557ceff.json │ │ ├── images │ │ ├── tn.png │ │ └── screenshot.png │ │ ├── archetypes │ │ ├── posts.md │ │ └── docs.md │ │ ├── assets │ │ ├── _variables.scss │ │ ├── sw-register.js │ │ ├── plugins │ │ │ ├── _dark.scss │ │ │ └── _scrollbars.scss │ │ ├── book.scss │ │ ├── menu-reset.js │ │ └── _print.scss │ │ ├── static │ │ ├── favicon.png │ │ ├── LeetCode_Icon.png │ │ ├── LeetCode_logo.png │ │ ├── LeetCode_logo2048.png │ │ ├── apple-touch-icon-120x120.png │ │ ├── apple-touch-icon-152x152.png │ │ ├── apple-touch-icon-180x180.png │ │ ├── apple-touch-icon-512x512.png │ │ ├── apple-touch-icon-60x60.png │ │ ├── apple-touch-icon-76x76.png │ │ ├── apple-touch-icon-1024x1024.png │ │ ├── fonts │ │ │ ├── roboto-v19-latin-700.woff │ │ │ ├── roboto-v19-latin-700.woff2 │ │ │ ├── roboto-v19-latin-regular.woff │ │ │ ├── roboto-v19-latin-300italic.woff │ │ │ ├── roboto-v19-latin-300italic.woff2 │ │ │ ├── roboto-v19-latin-regular.woff2 │ │ │ ├── roboto-mono-v6-latin-regular.woff │ │ │ └── roboto-mono-v6-latin-regular.woff2 │ │ ├── katex │ │ │ └── fonts │ │ │ │ ├── KaTeX_AMS-Regular.ttf │ │ │ │ ├── KaTeX_Main-Bold.ttf │ │ │ │ ├── KaTeX_Main-Bold.woff │ │ │ │ ├── KaTeX_Main-Bold.woff2 │ │ │ │ ├── KaTeX_Main-Italic.ttf │ │ │ │ ├── KaTeX_Math-Italic.ttf │ │ │ │ ├── KaTeX_AMS-Regular.woff │ │ │ │ ├── KaTeX_AMS-Regular.woff2 │ │ │ │ ├── KaTeX_Fraktur-Bold.ttf │ │ │ │ ├── KaTeX_Fraktur-Bold.woff │ │ │ │ ├── KaTeX_Main-Italic.woff │ │ │ │ ├── KaTeX_Main-Italic.woff2 │ │ │ │ ├── KaTeX_Main-Regular.ttf │ │ │ │ ├── KaTeX_Main-Regular.woff │ │ │ │ ├── KaTeX_Math-Italic.woff │ │ │ │ ├── KaTeX_Math-Italic.woff2 │ │ │ │ ├── KaTeX_Size1-Regular.ttf │ │ │ │ ├── KaTeX_Size2-Regular.ttf │ │ │ │ ├── KaTeX_Size3-Regular.ttf │ │ │ │ ├── KaTeX_Size4-Regular.ttf │ │ │ │ ├── KaTeX_Caligraphic-Bold.ttf │ │ │ │ ├── KaTeX_Fraktur-Bold.woff2 │ │ │ │ ├── KaTeX_Fraktur-Regular.ttf │ │ │ │ ├── KaTeX_Fraktur-Regular.woff │ │ │ │ ├── KaTeX_Main-BoldItalic.ttf │ │ │ │ ├── KaTeX_Main-BoldItalic.woff │ │ │ │ ├── KaTeX_Main-Regular.woff2 │ │ │ │ ├── KaTeX_Math-BoldItalic.ttf │ │ │ │ ├── KaTeX_Math-BoldItalic.woff │ │ │ │ ├── KaTeX_SansSerif-Bold.ttf │ │ │ │ ├── KaTeX_SansSerif-Bold.woff │ │ │ │ ├── KaTeX_SansSerif-Bold.woff2 │ │ │ │ ├── KaTeX_SansSerif-Italic.ttf │ │ │ │ ├── KaTeX_Script-Regular.ttf │ │ │ │ ├── KaTeX_Script-Regular.woff │ │ │ │ ├── KaTeX_Script-Regular.woff2 │ │ │ │ ├── KaTeX_Size1-Regular.woff │ │ │ │ ├── KaTeX_Size1-Regular.woff2 │ │ │ │ ├── KaTeX_Size2-Regular.woff │ │ │ │ ├── KaTeX_Size2-Regular.woff2 │ │ │ │ ├── KaTeX_Size3-Regular.woff │ │ │ │ ├── KaTeX_Size3-Regular.woff2 │ │ │ │ ├── KaTeX_Size4-Regular.woff │ │ │ │ ├── KaTeX_Size4-Regular.woff2 │ │ │ │ ├── KaTeX_Caligraphic-Bold.woff │ │ │ │ ├── KaTeX_Caligraphic-Bold.woff2 │ │ │ │ ├── KaTeX_Fraktur-Regular.woff2 │ │ │ │ ├── KaTeX_Main-BoldItalic.woff2 │ │ │ │ ├── KaTeX_Math-BoldItalic.woff2 │ │ │ │ ├── KaTeX_SansSerif-Italic.woff │ │ │ │ ├── KaTeX_SansSerif-Italic.woff2 │ │ │ │ ├── KaTeX_SansSerif-Regular.ttf │ │ │ │ ├── KaTeX_SansSerif-Regular.woff │ │ │ │ ├── KaTeX_Typewriter-Regular.ttf │ │ │ │ ├── KaTeX_Caligraphic-Regular.ttf │ │ │ │ ├── KaTeX_Caligraphic-Regular.woff │ │ │ │ ├── KaTeX_Caligraphic-Regular.woff2 │ │ │ │ ├── KaTeX_SansSerif-Regular.woff2 │ │ │ │ ├── KaTeX_Typewriter-Regular.woff │ │ │ │ └── KaTeX_Typewriter-Regular.woff2 │ │ └── svg │ │ │ ├── menu.svg │ │ │ ├── toc.svg │ │ │ ├── calendar.svg │ │ │ ├── edit.svg │ │ │ └── translate.svg │ │ ├── i18n │ │ ├── nb.yaml │ │ ├── en.yaml │ │ ├── de.yaml │ │ ├── pt.yaml │ │ ├── sv.yaml │ │ ├── es.yaml │ │ ├── cs.yaml │ │ ├── fr.yaml │ │ ├── uk.yaml │ │ ├── ru.yaml │ │ ├── zh.yaml │ │ ├── ja.yaml │ │ ├── ko.yaml │ │ ├── cn.yaml │ │ └── jp.yaml │ │ └── theme.toml ├── static │ └── logo.png ├── archetypes │ └── default.md └── resources │ └── _gen │ └── assets │ └── scss │ ├── book.scss_50fc8c04e12a2f59027287995557ceff.json │ └── leetcode │ └── book.scss_50fc8c04e12a2f59027287995557ceff.json ├── logo.png ├── topic ├── Sort.png ├── Stack.png ├── Union_Find.png ├── Backtracking.png ├── Linked_List.png ├── Segment_Tree.png ├── Two_pointers.png ├── Sliding_Window.png ├── Bit_Manipulation.png └── Binary_Indexed_Tree.png ├── leetcode ├── 1025.Divisor-Game │ └── 1025. Divisor Game.go ├── 9990085.Maximal-Rectangle │ └── 85. Maximal Rectangle.go ├── 0141.Linked-List-Cycle │ ├── 141. Linked List Cycle_test.go │ ├── README.md │ └── 141. Linked List Cycle.go ├── 0142.Linked-List-Cycle-II │ └── 142. Linked List Cycle II_test.go ├── 9990316.Remove-Duplicate-Letters │ └── 316. Remove Duplicate Letters.go ├── 0172.Factorial-Trailing-Zeroes │ └── 172. Factorial Trailing Zeroes.go ├── 0344.Reverse-String │ └── 344. Reverse String.go ├── 1108.Defanging-an-IP-Address │ └── 1108. Defanging an IP Address.go ├── 0136.Single-Number │ └── 136. Single Number.go ├── 0263.Ugly-Number │ └── 263. Ugly Number.go ├── 0836.Rectangle-Overlap │ └── 836. Rectangle Overlap.go ├── 0268.Missing-Number │ └── 268. Missing Number.go ├── 0190.Reverse-Bits │ └── 190. Reverse Bits.go ├── 0338.Counting-Bits │ └── 338. Counting Bits.go ├── 0461.Hamming-Distance │ └── 461. Hamming Distance.go ├── 1037.Valid-Boomerang │ └── 1037. Valid Boomerang.go ├── 0258.Add-Digits │ └── 258. Add Digits.go ├── 1185.Day-of-the-Week │ └── 1185. Day of the Week.go ├── 0070.Climbing-Stairs │ └── 70. Climbing Stairs.go ├── 0206.Reverse-Linked-List │ ├── README.md │ └── 206. Reverse Linked List.go ├── 0389.Find-the-Difference │ └── 389. Find the Difference.go ├── 0007.Reverse-Integer │ └── 7. Reverse Integer.go ├── 0171.Excel-Sheet-Column-Number │ └── 171. Excel Sheet Column Number.go ├── 1089.Duplicate-Zeros │ └── 1089. Duplicate Zeros.go ├── 0001.Two-Sum │ └── 1. Two Sum.go ├── 0717.1-bit-and-2-bit-Characters │ └── 717. 1-bit and 2-bit Characters.go ├── 0371.Sum-of-Two-Integers │ └── 371. Sum of Two Integers.go ├── 1470.Shuffle-the-Array │ └── 1470. Shuffle the Array.go ├── 1295.Find-Numbers-with-Even-Number-of-Digits │ └── 1295. Find Numbers with Even Number of Digits.go ├── 1137.N-th-Tribonacci-Number │ └── 1137. N-th Tribonacci Number.go ├── 0096.Unique-Binary-Search-Trees │ └── 96. Unique Binary Search Trees.go ├── 0781.Rabbits-in-Forest │ └── 781. Rabbits in Forest.go ├── 0217.Contains-Duplicate │ └── 217. Contains Duplicate.go ├── 1287.Element-Appearing-More-Than-In-Sorted-Array │ └── 1287. Element Appearing More Than 25% In Sorted Array.go ├── 0122.Best-Time-to-Buy-and-Sell-Stock-II │ └── 122. Best Time to Buy and Sell Stock II.go ├── 0283.Move-Zeroes │ ├── 283. Move Zeroes.go │ └── README.md ├── 0961.N-Repeated-Element-in-Size-2N-Array │ └── 961. N-Repeated Element in Size 2N Array.go ├── 1281.Subtract-the-Product-and-Sum-of-Digits-of-an-Integer │ └── 1281. Subtract the Product and Sum of Digits of an Integer.go ├── 1313.Decompress-Run-Length-Encoded-List │ └── 1313. Decompress Run-Length Encoded List.go ├── 1306.Jump-Game-III │ └── 1306. Jump Game III.go ├── 0453.Minimum-Moves-to-Equal-Array-Elements │ └── 453. Minimum Moves to Equal Array Elements.go ├── 0575.Distribute-Candies │ └── 575. Distribute Candies.go ├── 1389.Create-Target-Array-in-the-Given-Order │ └── 1389. Create Target Array in the Given Order.go ├── 0766.Toeplitz-Matrix │ └── 766. Toeplitz Matrix.go ├── 1154.Day-of-the-Year │ └── 1154. Day of the Year.go ├── 1221.Split-a-String-in-Balanced-Strings │ └── 1221. Split a String in Balanced Strings.go ├── 0275.H-Index-II │ └── 275. H-Index II.go ├── 0397.Integer-Replacement │ └── 397. Integer Replacement.go ├── 0891.Sum-of-Subsequence-Widths │ └── 891. Sum of Subsequence Widths.go ├── 0191.Number-of-1-Bits │ └── 191. Number of 1 Bits.go ├── 0387.First-Unique-Character-in-a-String │ ├── 387. First Unique Character in a String.go │ └── README.md ├── 1304.Find-N-Unique-Integers-Sum-up-to-Zero │ └── 1304. Find N Unique Integers Sum up to Zero.go ├── 0050.Powx-n │ └── 50. Pow(x, n).go ├── 0598.Range-Addition-II │ └── 598. Range Addition II.go ├── 1051.Height-Checker │ └── 1051. Height Checker.go ├── 0350.Intersection-of-Two-Arrays-II │ └── 350. Intersection of Two Arrays II.go ├── 0367.Valid-Perfect-Square │ └── 367. Valid Perfect Square.go ├── 0409.Longest-Palindrome │ └── 409. Longest Palindrome.go ├── 0455.Assign-Cookies │ └── 455. Assign Cookies.go ├── 0832.Flipping-an-Image │ └── 832. Flipping an Image.go ├── 0349.Intersection-of-Two-Arrays │ └── 349. Intersection of Two Arrays.go ├── 1217.Play-with-Chips │ └── 1217. Play with Chips.go ├── 0342.Power-of-Four │ └── 342. Power of Four.go ├── 0454.4Sum-II │ └── 454. 4Sum II.go ├── 0704.Binary-Search │ └── 704. Binary Search.go ├── 0867.Transpose-Matrix │ └── 867. Transpose Matrix.go ├── 0027.Remove-Element │ └── 27. Remove Element.go ├── 0041.First-Missing-Positive │ └── 41. First Missing Positive.go ├── 0485.Max-Consecutive-Ones │ └── 485. Max Consecutive Ones.go ├── 0633.Sum-of-Square-Numbers │ └── 633. Sum of Square Numbers.go ├── 1207.Unique-Number-of-Occurrences │ └── 1207. Unique Number of Occurrences.go ├── 0168.Excel-Sheet-Column-Title │ └── 168. Excel Sheet Column Title.go ├── 0009.Palindrome-Number │ └── 9. Palindrome Number.go ├── 0441.Arranging-Coins │ └── 441. Arranging Coins.go ├── 0645.Set-Mismatch │ └── 645. Set Mismatch.go ├── 1017.Convert-to-Base--2 │ └── 1017. Convert to Base -2.go ├── 1464.Maximum-Product-of-Two-Elements-in-an-Array │ └── 1464. Maximum Product of Two Elements in an Array.go ├── 1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence │ └── 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence.go ├── 0151.Reverse-Words-in-a-String │ └── 151. Reverse Words in a String.go ├── 0386.Lexicographical-Numbers │ ├── 386. Lexicographical Numbers.go │ └── README.md ├── 1047.Remove-All-Adjacent-Duplicates-In-String │ └── 1047. Remove All Adjacent Duplicates In String.go ├── 0203.Remove-Linked-List-Elements │ └── README.md ├── 0942.DI-String-Match │ └── 942. DI String Match.go ├── 0204.Count-Primes │ ├── 204. Count Primes.go │ └── README.md ├── 0035.Search-Insert-Position │ └── 35. Search Insert Position.go ├── 0561.Array-Partition-I │ └── 561. Array Partition I.go ├── 1299.Replace-Elements-with-Greatest-Element-on-Right-Side │ └── 1299. Replace Elements with Greatest Element on Right Side.go ├── 0331.Verify-Preorder-Serialization-of-a-Binary-Tree │ └── 331. Verify Preorder Serialization of a Binary Tree.go ├── 0745.Prefix-and-Suffix-Search │ └── 745. Prefix and Suffix Search_test.go ├── 0448.Find-All-Numbers-Disappeared-in-an-Array │ └── 448. Find All Numbers Disappeared in an Array.go ├── 0118.Pascals-Triangle │ └── 118. Pascal's Triangle.go ├── 0343.Integer-Break │ └── 343. Integer Break.go ├── 0724.Find-Pivot-Index │ └── 724. Find Pivot Index.go ├── 1005.Maximize-Sum-Of-Array-After-K-Negations │ └── 1005. Maximize Sum Of Array After K Negations.go ├── 0021.Merge-Two-Sorted-Lists │ ├── README.md │ └── 21. Merge Two Sorted Lists.go ├── 0055.Jump-Game │ └── 55. Jump Game.go ├── 0062.Unique-Paths │ └── 62. Unique Paths.go ├── 0066.Plus-One │ └── 66. Plus One.go ├── 0720.Longest-Word-in-Dictionary │ └── 720. Longest Word in Dictionary.go ├── 1078.Occurrences-After-Bigram │ └── 1078. Occurrences After Bigram.go ├── 0884.Uncommon-Words-from-Two-Sentences │ └── 884. Uncommon Words from Two Sentences.go ├── 0973.K-Closest-Points-to-Origin │ └── 973. K Closest Points to Origin.go ├── 0981.Time-Based-Key-Value-Store │ └── 981. Time Based Key-Value Store_test.go ├── 0201.Bitwise-AND-of-Numbers-Range │ └── 201. Bitwise AND of Numbers Range.go ├── 0922.Sort-Array-By-Parity-II │ └── 922. Sort Array By Parity II.go ├── 0470.Implement-Rand10-Using-Rand7 │ └── 470. Implement Rand10() Using Rand7().go ├── 0706.Design-HashMap │ └── 706. Design HashMap_test.go ├── 0881.Boats-to-Save-People │ └── 881. Boats to Save People.go ├── 1260.Shift-2D-Grid │ └── 1260. Shift 2D Grid.go ├── 0011.Container-With-Most-Water │ └── 11. Container With Most Water.go ├── 0154.Find-Minimum-in-Rotated-Sorted-Array-II │ └── 154. Find Minimum in Rotated Sorted Array II.go ├── 0532.K-diff-Pairs-in-an-Array │ └── 532. K-diff Pairs in an Array.go ├── 0735.Asteroid-Collision │ └── 735. Asteroid Collision.go ├── 0303.Range-Sum-Query---Immutable │ └── 303. Range Sum Query - Immutable_test.go ├── 0414.Third-Maximum-Number │ └── 414. Third Maximum Number.go ├── 0744.Find-Smallest-Letter-Greater-Than-Target │ └── 744. Find Smallest Letter Greater Than Target.go ├── 0762.Prime-Number-of-Set-Bits-in-Binary-Representation │ └── 762. Prime Number of Set Bits in Binary Representation.go ├── 0841.Keys-and-Rooms │ └── 841. Keys and Rooms.go ├── 0023.Merge-k-Sorted-Lists │ └── README.md ├── 0219.Contains-Duplicate-II │ └── 219. Contains Duplicate II.go ├── 0985.Sum-of-Even-Numbers-After-Queries │ └── 985. Sum of Even Numbers After Queries.go ├── 1157.Online-Majority-Element-In-Subarray │ └── 1157. Online Majority Element In Subarray_test.go ├── 0024.Swap-Nodes-in-Pairs │ └── README.md ├── 0930.Binary-Subarrays-With-Sum │ └── 930. Binary Subarrays With Sum.go ├── 1385.Find-the-Distance-Value-Between-Two-Arrays │ └── 1385. Find the Distance Value Between Two Arrays.go ├── 0412.Fizz-Buzz │ └── 412. Fizz Buzz.go ├── 0921.Minimum-Add-to-Make-Parentheses-Valid │ └── 921. Minimum Add to Make Parentheses Valid.go ├── 1232.Check-If-It-Is-a-Straight-Line │ └── 1232. Check If It Is a Straight Line.go ├── 0074.Search-a-2D-Matrix │ └── 74. Search a 2D Matrix.go ├── 0075.Sort-Colors │ └── 75. Sort Colors.go ├── 0093.Restore-IP-Addresses │ └── README.md ├── 0202.Happy-Number │ └── 202. Happy Number.go ├── 0260.Single-Number-III │ └── 260. Single Number III.go ├── 0914.X-of-a-Kind-in-a-Deck-of-Cards │ └── 914. X of a Kind in a Deck of Cards.go ├── 1266.Minimum-Time-Visiting-All-Points │ └── 1266. Minimum Time Visiting All Points.go ├── 0933.Number-of-Recent-Calls │ └── 933. Number of Recent Calls_test.go ├── 0920.Number-of-Music-Playlists │ └── 920. Number of Music Playlists.go ├── 0148.Sort-List │ └── README.md ├── 0557.Reverse-Words-in-a-String-III │ └── 557. Reverse Words in a String III.go ├── 0083.Remove-Duplicates-from-Sorted-List │ └── README.md ├── 0404.Sum-of-Left-Leaves │ └── README.md ├── 0416.Partition-Equal-Subset-Sum │ └── 416. Partition Equal Subset Sum.go ├── 0028.Implement-strStr │ └── 28. Implement strStr().go ├── 0046.Permutations │ └── README.md ├── 0599.Minimum-Index-Sum-of-Two-Lists │ └── 599. Minimum Index Sum of Two Lists.go ├── 0077.Combinations │ ├── README.md │ └── 77. Combinations.go ├── 0402.Remove-K-Digits │ └── 402. Remove K Digits.go ├── 0155.Min-Stack │ └── 155. Min Stack_test.go ├── 0594.Longest-Harmonious-Subsequence │ └── 594. Longest Harmonious Subsequence.go ├── 0946.Validate-Stack-Sequences │ └── 946. Validate Stack Sequences.go ├── 1317.Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers │ └── 1317. Convert Integer to the Sum of Two No-Zero Integers.go ├── 0497.Random-Point-in-Non-overlapping-Rectangles │ └── 497. Random Point in Non-overlapping Rectangles_test.go ├── 0524.Longest-Word-in-Dictionary-through-Deleting │ └── 524. Longest Word in Dictionary through Deleting.go ├── 1175.Prime-Arrangements │ └── 1175. Prime Arrangements.go ├── 1184.Distance-Between-Bus-Stops │ └── 1184. Distance Between Bus Stops.go ├── 0447.Number-of-Boomerangs │ └── 447. Number of Boomerangs.go ├── 0676.Implement-Magic-Dictionary │ └── 676. Implement Magic Dictionary_test.go ├── 0844.Backspace-String-Compare │ └── 844. Backspace String Compare.go ├── 0307.Range-Sum-Query---Mutable │ └── 307. Range Sum Query - Mutable_test.go ├── 0507.Perfect-Number │ └── 507. Perfect Number.go ├── 0528.Random-Pick-with-Weight │ └── 528. Random Pick with Weight_test.go ├── 0713.Subarray-Product-Less-Than-K │ └── 713. Subarray Product Less Than K.go ├── 0765.Couples-Holding-Hands │ └── 765. Couples Holding Hands.go ├── 0232.Implement-Queue-using-Stacks │ └── 232. Implement Queue using Stacks_test.go ├── 0476.Number-Complement │ └── 476. Number Complement.go ├── 0345.Reverse-Vowels-of-a-String │ └── README.md ├── 0732.My-Calendar-III │ └── 732. My Calendar III_test.go ├── 0013.Roman-to-Integer │ └── 13. Roman to Integer.go ├── 0131.Palindrome-Partitioning │ └── README.md ├── 0322.Coin-Change │ └── 322. Coin Change.go ├── 0977.Squares-of-a-Sorted-Array │ └── 977. Squares of a Sorted Array.go ├── 0082.Remove-Duplicates-from-Sorted-List-II │ └── README.md ├── 0995.Minimum-Number-of-K-Consecutive-Bit-Flips │ └── 995. Minimum Number of K Consecutive Bit Flips.go ├── 1290.Convert-Binary-Number-in-a-Linked-List-to-Integer │ └── 1290. Convert Binary Number in a Linked List to Integer.go ├── 0042.Trapping-Rain-Water │ └── 42. Trapping Rain Water.go ├── 0771.Jewels-and-Stones │ └── 771. Jewels and Stones.go ├── 1200.Minimum-Absolute-Difference │ └── 1200. Minimum Absolute Difference.go ├── 0424.Longest-Repeating-Character-Replacement │ └── 424. Longest Repeating Character Replacement.go ├── 0463.Island-Perimeter │ └── 463. Island Perimeter.go ├── 0537.Complex-Number-Multiplication │ └── 537. Complex Number Multiplication.go ├── 0144.Binary-Tree-Preorder-Traversal │ └── README.md ├── 0684.Redundant-Connection │ └── 684. Redundant Connection.go ├── 0885.Spiral-Matrix-III │ └── 885. Spiral Matrix III.go ├── 1189.Maximum-Number-of-Balloons │ └── 1189. Maximum Number of Balloons.go ├── 0658.Find-K-Closest-Elements │ └── 658. Find K Closest Elements.go ├── 0094.Binary-Tree-Inorder-Traversal │ └── README.md ├── 0145.Binary-Tree-Postorder-Traversal │ └── README.md ├── 0257.Binary-Tree-Paths │ └── README.md ├── 0047.Permutations-II │ └── README.md ├── 0059.Spiral-Matrix-II │ └── README.md ├── 0091.Decode-Ways │ └── 91. Decode Ways.go ├── 0226.Invert-Binary-Tree │ └── 226. Invert Binary Tree.go ├── 0541.Reverse-String-II │ └── 541. Reverse String II.go ├── 0668.Kth-Smallest-Number-in-Multiplication-Table │ └── 668. Kth Smallest Number in Multiplication Table.go ├── 0896.Monotonic-Array │ └── 896. Monotonic Array.go ├── 9990377.Combination-Sum-IV │ └── 377. Combination Sum IV.go ├── 0125.Valid-Palindrome │ ├── 125. Valid Palindrome.go │ └── README.md ├── 0237.Delete-Node-in-a-Linked-List │ └── 237. Delete Node in a Linked List.go ├── 0500.Keyboard-Row │ └── 500. Keyboard Row.go ├── 0405.Convert-a-Number-to-Hexadecimal │ └── 405. Convert a Number to Hexadecimal.go ├── 0223.Rectangle-Area │ └── 223. Rectangle Area.go ├── 0515.Find-Largest-Value-in-Each-Tree-Row │ └── README.md ├── 0878.Nth-Magical-Number │ └── 878. Nth Magical Number.go ├── 0925.Long-Pressed-Name │ └── 925. Long Pressed Name.go ├── 1004.Max-Consecutive-Ones-III │ └── 1004. Max Consecutive Ones III.go ├── 0225.Implement-Stack-using-Queues │ └── 225. Implement Stack using Queues_test.go ├── 1380.Lucky-Numbers-in-a-Matrix │ └── 1380. Lucky Numbers in a Matrix.go ├── 0022.Generate-Parentheses │ └── 22. Generate Parentheses.go ├── 0152.Maximum-Product-Subarray │ └── 152. Maximum Product Subarray.go ├── 0209.Minimum-Size-Subarray-Sum │ └── 209. Minimum Size Subarray Sum.go ├── 0393.UTF-8-Validation │ └── 393. UTF-8 Validation.go ├── 0088.Merge-Sorted-Array │ ├── README.md │ └── 88. Merge Sorted Array.go ├── 0169.Majority-Element │ └── 169. Majority Element.go ├── 0231.Power-of-Two │ └── README.md ├── 0496.Next-Greater-Element-I │ └── 496. Next Greater Element I.go ├── 0020.Valid-Parentheses │ └── 20. Valid Parentheses.go ├── 0112.Path-Sum │ └── 112. Path Sum.go ├── 1049.Last-Stone-Weight-II │ └── 1049. Last Stone Weight II.go ├── 0207.Course-Schedule │ └── 207. Course Schedule.go ├── 1160.Find-Words-That-Can-Be-Formed-by-Characters │ └── 1160. Find Words That Can Be Formed by Characters.go ├── 0234.Palindrome-Linked-List │ └── README.md ├── 0839.Similar-String-Groups │ └── 839. Similar String Groups.go ├── 0104.Maximum-Depth-of-Binary-Tree │ └── 104. Maximum Depth of Binary Tree.go ├── 1128.Number-of-Equivalent-Domino-Pairs │ └── 1128. Number of Equivalent Domino Pairs.go ├── 0357.Count-Numbers-with-Unique-Digits │ └── 357. Count Numbers with Unique Digits.go ├── 0392.Is-Subsequence │ └── 392. Is Subsequence.go ├── 0048.Rotate-Image │ └── 48. Rotate Image.go ├── 0056.Merge-Intervals │ └── README.md ├── 0474.Ones-and-Zeroes │ └── 474. Ones and Zeroes.go ├── 0108.Convert-Sorted-Array-to-Binary-Search-Tree │ └── 108. Convert Sorted Array to Binary Search Tree.go ├── 0880.Decoded-String-at-Index │ └── 880. Decoded String at Index.go ├── 0019.Remove-Nth-Node-From-End-of-List │ └── README.md └── 0092.Reverse-Linked-List-II │ └── README.md ├── gotest.sh └── structures ├── Queue_test.go ├── Stack_test.go ├── NestedInterger_test.go ├── Point.go ├── Heap.go ├── Heap_test.go ├── Queue.go └── Stack.go /website/content/docs/shortcodes/section/page1.md: -------------------------------------------------------------------------------- 1 | # Page 1 2 | -------------------------------------------------------------------------------- /website/content/docs/shortcodes/section/page2.md: -------------------------------------------------------------------------------- 1 | # Page 2 2 | -------------------------------------------------------------------------------- /website/themes/book/layouts/partials/docs/inject/footer.html: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /website/themes/book/layouts/partials/docs/inject/content-after.html: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /website/themes/book/layouts/partials/docs/inject/content-before.html: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /website/themes/book/layouts/partials/docs/inject/menu-after.html: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /website/themes/book/layouts/partials/docs/inject/menu-before.html: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/logo.png -------------------------------------------------------------------------------- /website/themes/book/layouts/partials/docs/toc.html: -------------------------------------------------------------------------------- 1 | {{ .TableOfContents }} 2 | -------------------------------------------------------------------------------- /topic/Sort.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/topic/Sort.png -------------------------------------------------------------------------------- /topic/Stack.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/topic/Stack.png -------------------------------------------------------------------------------- /website/content/docs/shortcodes/_index.md: -------------------------------------------------------------------------------- 1 | --- 2 | bookFlatSection: true 3 | --- 4 | -------------------------------------------------------------------------------- /website/themes/book/.gitignore: -------------------------------------------------------------------------------- 1 | public/ 2 | exampleSite/public/ 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /website/themes/book/layouts/_default/list.html: -------------------------------------------------------------------------------- 1 | {{ define "dummy" }}{{ end }} 2 | -------------------------------------------------------------------------------- /website/themes/book/layouts/_default/single.html: -------------------------------------------------------------------------------- 1 | {{ define "dummy" }}{{ end }} 2 | -------------------------------------------------------------------------------- /website/themes/book/exampleSite/content/docs/shortcodes/section/page1.md: -------------------------------------------------------------------------------- 1 | # Page 1 2 | -------------------------------------------------------------------------------- /website/themes/book/exampleSite/content/docs/shortcodes/section/page2.md: -------------------------------------------------------------------------------- 1 | # Page 2 2 | -------------------------------------------------------------------------------- /topic/Union_Find.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/topic/Union_Find.png -------------------------------------------------------------------------------- /topic/Backtracking.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/topic/Backtracking.png -------------------------------------------------------------------------------- /topic/Linked_List.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/topic/Linked_List.png -------------------------------------------------------------------------------- /topic/Segment_Tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/topic/Segment_Tree.png -------------------------------------------------------------------------------- /topic/Two_pointers.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/topic/Two_pointers.png -------------------------------------------------------------------------------- /topic/Sliding_Window.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/topic/Sliding_Window.png -------------------------------------------------------------------------------- /website/static/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/static/logo.png -------------------------------------------------------------------------------- /topic/Bit_Manipulation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/topic/Bit_Manipulation.png -------------------------------------------------------------------------------- /website/themes/book/exampleSite/content/docs/shortcodes/_index.md: -------------------------------------------------------------------------------- 1 | --- 2 | bookFlatSection: true 3 | --- 4 | -------------------------------------------------------------------------------- /topic/Binary_Indexed_Tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/topic/Binary_Indexed_Tree.png -------------------------------------------------------------------------------- /website/themes/book/images/tn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/images/tn.png -------------------------------------------------------------------------------- /website/themes/book/archetypes/posts.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "{{ replace .Name "-" " " | title }}" 3 | date: {{ .Date }} 4 | --- 5 | -------------------------------------------------------------------------------- /website/themes/book/assets/_variables.scss: -------------------------------------------------------------------------------- 1 | /* You can override SASS variables here. */ 2 | 3 | // @import "plugins/dark"; 4 | -------------------------------------------------------------------------------- /website/themes/book/static/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/favicon.png -------------------------------------------------------------------------------- /leetcode/1025.Divisor-Game/1025. Divisor Game.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func divisorGame(N int) bool { 4 | return N%2 == 0 5 | } 6 | -------------------------------------------------------------------------------- /website/archetypes/default.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "{{ replace .Name "-" " " | title }}" 3 | date: {{ .Date }} 4 | draft: true 5 | --- 6 | 7 | -------------------------------------------------------------------------------- /website/themes/book/exampleSite/assets/_variables.scss: -------------------------------------------------------------------------------- 1 | /* You can override SASS variables here. */ 2 | 3 | // @import "plugins/dark"; 4 | -------------------------------------------------------------------------------- /website/themes/book/images/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/images/screenshot.png -------------------------------------------------------------------------------- /website/themes/book/static/LeetCode_Icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/LeetCode_Icon.png -------------------------------------------------------------------------------- /website/themes/book/static/LeetCode_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/LeetCode_logo.png -------------------------------------------------------------------------------- /website/themes/book/exampleSite/content/posts/_index.md: -------------------------------------------------------------------------------- 1 | --- 2 | menu: 3 | after: 4 | name: blog 5 | weight: 5 6 | title: Blog 7 | --- 8 | -------------------------------------------------------------------------------- /website/themes/book/layouts/shortcodes/hint.html: -------------------------------------------------------------------------------- 1 |
2 | {{ .Inner | markdownify }} 3 |
4 | -------------------------------------------------------------------------------- /website/themes/book/static/LeetCode_logo2048.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/LeetCode_logo2048.png -------------------------------------------------------------------------------- /leetcode/9990085.Maximal-Rectangle/85. Maximal Rectangle.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func maximalRectangle(matrix [][]byte) int { 4 | return 0 5 | } 6 | -------------------------------------------------------------------------------- /leetcode/0141.Linked-List-Cycle/141. Linked List Cycle_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "testing" 4 | 5 | func Test_Problem141(t *testing.T) { 6 | } 7 | -------------------------------------------------------------------------------- /website/themes/book/static/apple-touch-icon-120x120.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/apple-touch-icon-120x120.png -------------------------------------------------------------------------------- /website/themes/book/static/apple-touch-icon-152x152.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/apple-touch-icon-152x152.png -------------------------------------------------------------------------------- /website/themes/book/static/apple-touch-icon-180x180.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/apple-touch-icon-180x180.png -------------------------------------------------------------------------------- /website/themes/book/static/apple-touch-icon-512x512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/apple-touch-icon-512x512.png -------------------------------------------------------------------------------- /website/themes/book/static/apple-touch-icon-60x60.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/apple-touch-icon-60x60.png -------------------------------------------------------------------------------- /website/themes/book/static/apple-touch-icon-76x76.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/apple-touch-icon-76x76.png -------------------------------------------------------------------------------- /leetcode/0142.Linked-List-Cycle-II/142. Linked List Cycle II_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "testing" 4 | 5 | func Test_Problem142(t *testing.T) { 6 | } 7 | -------------------------------------------------------------------------------- /website/themes/book/static/apple-touch-icon-1024x1024.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/apple-touch-icon-1024x1024.png -------------------------------------------------------------------------------- /leetcode/9990316.Remove-Duplicate-Letters/316. Remove Duplicate Letters.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func removeDuplicateLetters(s string) string { 4 | return "" 5 | } 6 | -------------------------------------------------------------------------------- /website/themes/book/exampleSite/assets/_custom.scss: -------------------------------------------------------------------------------- 1 | /* You can add custom styles here. */ 2 | 3 | // @import "plugins/numbered"; 4 | // @import "plugins/scrollbars"; 5 | -------------------------------------------------------------------------------- /website/themes/book/static/fonts/roboto-v19-latin-700.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/fonts/roboto-v19-latin-700.woff -------------------------------------------------------------------------------- /website/themes/book/static/fonts/roboto-v19-latin-700.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/fonts/roboto-v19-latin-700.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_AMS-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_AMS-Regular.ttf -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Main-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Main-Bold.ttf -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Main-Bold.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Main-Bold.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Main-Bold.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Main-Bold.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Main-Italic.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Main-Italic.ttf -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Math-Italic.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Math-Italic.ttf -------------------------------------------------------------------------------- /website/themes/book/layouts/partials/docs/comments.html: -------------------------------------------------------------------------------- 1 | 2 | {{ template "_internal/disqus.html" . }} 3 | -------------------------------------------------------------------------------- /website/themes/book/static/fonts/roboto-v19-latin-regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/fonts/roboto-v19-latin-regular.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_AMS-Regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_AMS-Regular.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_AMS-Regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_AMS-Regular.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Fraktur-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Fraktur-Bold.ttf -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Fraktur-Bold.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Fraktur-Bold.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Main-Italic.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Main-Italic.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Main-Italic.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Main-Italic.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Main-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Main-Regular.ttf -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Main-Regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Main-Regular.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Math-Italic.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Math-Italic.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Math-Italic.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Math-Italic.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Size1-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Size1-Regular.ttf -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Size2-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Size2-Regular.ttf -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Size3-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Size3-Regular.ttf -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Size4-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Size4-Regular.ttf -------------------------------------------------------------------------------- /website/themes/book/static/fonts/roboto-v19-latin-300italic.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/fonts/roboto-v19-latin-300italic.woff -------------------------------------------------------------------------------- /website/themes/book/static/fonts/roboto-v19-latin-300italic.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/fonts/roboto-v19-latin-300italic.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/fonts/roboto-v19-latin-regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/fonts/roboto-v19-latin-regular.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Caligraphic-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Caligraphic-Bold.ttf -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Fraktur-Bold.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Fraktur-Bold.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Fraktur-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Fraktur-Regular.ttf -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Fraktur-Regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Fraktur-Regular.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Main-BoldItalic.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Main-BoldItalic.ttf -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Main-BoldItalic.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Main-BoldItalic.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Main-Regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Main-Regular.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Math-BoldItalic.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Math-BoldItalic.ttf -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Math-BoldItalic.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Math-BoldItalic.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_SansSerif-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_SansSerif-Bold.ttf -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_SansSerif-Bold.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_SansSerif-Bold.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_SansSerif-Bold.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_SansSerif-Bold.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_SansSerif-Italic.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_SansSerif-Italic.ttf -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Script-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Script-Regular.ttf -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Script-Regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Script-Regular.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Script-Regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Script-Regular.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Size1-Regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Size1-Regular.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Size1-Regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Size1-Regular.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Size2-Regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Size2-Regular.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Size2-Regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Size2-Regular.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Size3-Regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Size3-Regular.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Size3-Regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Size3-Regular.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Size4-Regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Size4-Regular.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Size4-Regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Size4-Regular.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/fonts/roboto-mono-v6-latin-regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/fonts/roboto-mono-v6-latin-regular.woff -------------------------------------------------------------------------------- /website/themes/book/static/fonts/roboto-mono-v6-latin-regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/fonts/roboto-mono-v6-latin-regular.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Caligraphic-Bold.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Caligraphic-Bold.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Caligraphic-Bold.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Caligraphic-Bold.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Fraktur-Regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Fraktur-Regular.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Main-BoldItalic.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Main-BoldItalic.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Math-BoldItalic.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Math-BoldItalic.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_SansSerif-Italic.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_SansSerif-Italic.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_SansSerif-Italic.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_SansSerif-Italic.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_SansSerif-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_SansSerif-Regular.ttf -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_SansSerif-Regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_SansSerif-Regular.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Typewriter-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Typewriter-Regular.ttf -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Caligraphic-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Caligraphic-Regular.ttf -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Caligraphic-Regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Caligraphic-Regular.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Caligraphic-Regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Caligraphic-Regular.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_SansSerif-Regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_SansSerif-Regular.woff2 -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Typewriter-Regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Typewriter-Regular.woff -------------------------------------------------------------------------------- /website/themes/book/static/katex/fonts/KaTeX_Typewriter-Regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/HEAD/website/themes/book/static/katex/fonts/KaTeX_Typewriter-Regular.woff2 -------------------------------------------------------------------------------- /website/content/ChapterTwo/Binary_Indexed_Tree.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Binary Indexed Tree 3 | type: docs 4 | --- 5 | 6 | # Binary Indexed Tree 7 | 8 | ![](https://img.halfrost.com/Leetcode/Binary_Indexed_Tree.png) 9 | -------------------------------------------------------------------------------- /leetcode/0172.Factorial-Trailing-Zeroes/172. Factorial Trailing Zeroes.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func trailingZeroes(n int) int { 4 | if n/5 == 0 { 5 | return 0 6 | } 7 | return n/5 + trailingZeroes(n/5) 8 | } 9 | -------------------------------------------------------------------------------- /leetcode/0344.Reverse-String/344. Reverse String.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func reverseString(s []byte) { 4 | for i, j := 0, len(s)-1; i < j; { 5 | s[i], s[j] = s[j], s[i] 6 | i++ 7 | j-- 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /leetcode/1108.Defanging-an-IP-Address/1108. Defanging an IP Address.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strings" 4 | 5 | func defangIPaddr(address string) string { 6 | return strings.Replace(address, ".", "[.]", -1) 7 | } 8 | -------------------------------------------------------------------------------- /website/themes/book/layouts/_default/_markup/render-heading.html: -------------------------------------------------------------------------------- 1 | 2 | {{ .Text | safeHTML }} 3 | # 4 | 5 | -------------------------------------------------------------------------------- /website/themes/book/static/svg/menu.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /leetcode/0136.Single-Number/136. Single Number.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func singleNumber(nums []int) int { 4 | result := 0 5 | for i := 0; i < len(nums); i++ { 6 | result ^= nums[i] 7 | } 8 | return result 9 | } 10 | -------------------------------------------------------------------------------- /leetcode/0263.Ugly-Number/263. Ugly Number.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isUgly(num int) bool { 4 | for i := 2; i < 6 && num > 0; i++ { 5 | for num%i == 0 { 6 | num /= i 7 | } 8 | } 9 | return num == 1 10 | } 11 | -------------------------------------------------------------------------------- /leetcode/0836.Rectangle-Overlap/836. Rectangle Overlap.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isRectangleOverlap(rec1 []int, rec2 []int) bool { 4 | return rec1[0] < rec2[2] && rec2[0] < rec1[2] && rec1[1] < rec2[3] && rec2[1] < rec1[3] 5 | } 6 | -------------------------------------------------------------------------------- /leetcode/0268.Missing-Number/268. Missing Number.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func missingNumber(nums []int) int { 4 | xor, i := 0, 0 5 | for i = 0; i < len(nums); i++ { 6 | xor = xor ^ i ^ nums[i] 7 | } 8 | return xor ^ i 9 | } 10 | -------------------------------------------------------------------------------- /leetcode/0190.Reverse-Bits/190. Reverse Bits.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func reverseBits(num uint32) uint32 { 4 | var res uint32 5 | for i := 0; i < 32; i++ { 6 | res = res<<1 | num&1 7 | num >>= 1 8 | } 9 | return res 10 | } 11 | -------------------------------------------------------------------------------- /leetcode/0338.Counting-Bits/338. Counting Bits.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func countBits(num int) []int { 4 | bits := make([]int, num+1) 5 | for i := 1; i <= num; i++ { 6 | bits[i] += bits[i&(i-1)] + 1 7 | } 8 | return bits 9 | } 10 | -------------------------------------------------------------------------------- /website/content/ChapterThree/_index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 第三章 3 | type: docs 4 | --- 5 | 6 | # 第三章 一些模板 7 | 8 |

9 | 10 |

11 | 12 | 13 | 这一章会罗列一些整理好的模板。一起来看看吧。 -------------------------------------------------------------------------------- /website/themes/book/archetypes/docs.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "{{ .Name | humanize | title }}" 3 | weight: 1 4 | # bookFlatSection: false 5 | # bookToc: true 6 | # bookHidden: false 7 | # bookCollapseSection: false 8 | # bookComments: true 9 | --- 10 | -------------------------------------------------------------------------------- /website/themes/book/layouts/shortcodes/columns.html: -------------------------------------------------------------------------------- 1 |
2 | {{ range split .Inner "<--->" }} 3 |
4 | {{ . | markdownify }} 5 |
6 | {{ end }} 7 |
8 | -------------------------------------------------------------------------------- /leetcode/0461.Hamming-Distance/461. Hamming Distance.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func hammingDistance(x int, y int) int { 4 | distance := 0 5 | for xor := x ^ y; xor != 0; xor &= (xor - 1) { 6 | distance++ 7 | } 8 | return distance 9 | } 10 | -------------------------------------------------------------------------------- /leetcode/1037.Valid-Boomerang/1037. Valid Boomerang.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isBoomerang(points [][]int) bool { 4 | return (points[0][0]-points[1][0])*(points[0][1]-points[2][1]) != (points[0][0]-points[2][0])*(points[0][1]-points[1][1]) 5 | } 6 | -------------------------------------------------------------------------------- /leetcode/0258.Add-Digits/258. Add Digits.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func addDigits(num int) int { 4 | for num > 9 { 5 | cur := 0 6 | for num != 0 { 7 | cur += num % 10 8 | num /= 10 9 | } 10 | num = cur 11 | } 12 | return num 13 | } 14 | -------------------------------------------------------------------------------- /leetcode/1185.Day-of-the-Week/1185. Day of the Week.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "time" 4 | 5 | func dayOfTheWeek(day int, month int, year int) string { 6 | return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.Local).Weekday().String() 7 | } 8 | -------------------------------------------------------------------------------- /website/themes/book/layouts/partials/docs/menu-bundle.html: -------------------------------------------------------------------------------- 1 | {{ with .Site.GetPage .Site.Params.BookMenuBundle }} 2 | {{- $href := printf "href=\"%s\"" $.RelPermalink -}} 3 | {{- replace .Content $href (print $href "class=active") | safeHTML -}} 4 | {{ end }} 5 | -------------------------------------------------------------------------------- /website/themes/book/static/svg/toc.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /leetcode/0070.Climbing-Stairs/70. Climbing Stairs.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func climbStairs(n int) int { 4 | dp := make([]int, n+1) 5 | dp[0], dp[1] = 1, 1 6 | for i := 2; i <= n; i++ { 7 | dp[i] = dp[i-1] + dp[i-2] 8 | } 9 | return dp[n] 10 | } 11 | -------------------------------------------------------------------------------- /website/resources/_gen/assets/scss/book.scss_50fc8c04e12a2f59027287995557ceff.json: -------------------------------------------------------------------------------- 1 | {"Target":"book.min.6cd8553a6854f4812343f0f0c8baca31271e686434f381fbe3c7226f66639176.css","MediaType":"text/css","Data":{"Integrity":"sha256-bNhVOmhU9IEjQ/DwyLrKMSceaGQ084H748cib2ZjkXY="}} -------------------------------------------------------------------------------- /leetcode/0206.Reverse-Linked-List/README.md: -------------------------------------------------------------------------------- 1 | # [206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/description/) 2 | 3 | ## 题目 4 | 5 | Reverse a singly linked list. 6 | 7 | ## 题目大意 8 | 9 | 翻转单链表 10 | 11 | 12 | ## 解题思路 13 | 14 | 按照题意做即可。 -------------------------------------------------------------------------------- /leetcode/0389.Find-the-Difference/389. Find the Difference.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findTheDifference(s string, t string) byte { 4 | n, ch := len(t), t[len(t)-1] 5 | for i := 0; i < n-1; i++ { 6 | ch ^= s[i] 7 | ch ^= t[i] 8 | } 9 | return ch 10 | } 11 | -------------------------------------------------------------------------------- /website/resources/_gen/assets/scss/leetcode/book.scss_50fc8c04e12a2f59027287995557ceff.json: -------------------------------------------------------------------------------- 1 | {"Target":"book.min.1cf8e09514d081e98d59a10c37d68862354c1b636fbdab1bfc1053e747f15b4c.css","MediaType":"text/css","Data":{"Integrity":"sha256-HPjglRTQgemNWaEMN9aIYjVMG2Nvvasb/BBT50fxW0w="}} -------------------------------------------------------------------------------- /website/themes/book/layouts/partials/docs/date.html: -------------------------------------------------------------------------------- 1 | 5 | {{- $format := default "January 2, 2006" .Format -}} 6 | {{- return (.Date.Format $format) -}} 7 | -------------------------------------------------------------------------------- /website/themes/book/layouts/shortcodes/section.html: -------------------------------------------------------------------------------- 1 |
2 | {{ range .Page.Pages }} 3 |
4 | {{ partial "docs/title" . }} 5 |
6 |
7 | {{ default .Summary .Description }} 8 |
9 | {{ end }} 10 |
11 | -------------------------------------------------------------------------------- /leetcode/0007.Reverse-Integer/7. Reverse Integer.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func reverse7(x int) int { 4 | tmp := 0 5 | for x != 0 { 6 | tmp = tmp*10 + x%10 7 | x = x / 10 8 | } 9 | if tmp > 1<<31-1 || tmp < -(1<<31) { 10 | return 0 11 | } 12 | return tmp 13 | } 14 | -------------------------------------------------------------------------------- /leetcode/0171.Excel-Sheet-Column-Number/171. Excel Sheet Column Number.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func titleToNumber(s string) int { 4 | val, res := 0, 0 5 | for i := 0; i < len(s); i++ { 6 | val = int(s[i] - 'A' + 1) 7 | res = res*26 + val 8 | } 9 | return res 10 | } 11 | -------------------------------------------------------------------------------- /leetcode/1089.Duplicate-Zeros/1089. Duplicate Zeros.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func duplicateZeros(arr []int) { 4 | for i := 0; i < len(arr); i++ { 5 | if arr[i] == 0 && i+1 < len(arr) { 6 | arr = append(arr[:i+1], arr[i:len(arr)-1]...) 7 | i++ 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /website/themes/book/assets/sw-register.js: -------------------------------------------------------------------------------- 1 | {{- $swJS := resources.Get "sw.js" | resources.ExecuteAsTemplate "sw.js" . -}} 2 | if (navigator.serviceWorker) { 3 | navigator.serviceWorker.register( 4 | "{{ $swJS.RelPermalink }}", 5 | { scope: "{{ "/" | relURL }}" } 6 | ); 7 | } 8 | -------------------------------------------------------------------------------- /website/themes/book/static/svg/calendar.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /leetcode/0001.Two-Sum/1. Two Sum.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func twoSum(nums []int, target int) []int { 4 | m := make(map[int]int) 5 | for k, v := range nums { 6 | if idx, ok := m[target-v]; ok { 7 | return []int{idx, k} 8 | } 9 | m[v] = k 10 | } 11 | return nil 12 | } 13 | -------------------------------------------------------------------------------- /leetcode/0717.1-bit-and-2-bit-Characters/717. 1-bit and 2-bit Characters.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isOneBitCharacter(bits []int) bool { 4 | var i int 5 | for i = 0; i < len(bits)-1; i++ { 6 | if bits[i] == 1 { 7 | i++ 8 | } 9 | } 10 | return i == len(bits)-1 11 | } 12 | -------------------------------------------------------------------------------- /website/themes/book/exampleSite/resources/_gen/assets/scss/book.scss_50fc8c04e12a2f59027287995557ceff.json: -------------------------------------------------------------------------------- 1 | {"Target":"book.min.6cd8553a6854f4812343f0f0c8baca31271e686434f381fbe3c7226f66639176.css","MediaType":"text/css","Data":{"Integrity":"sha256-bNhVOmhU9IEjQ/DwyLrKMSceaGQ084H748cib2ZjkXY="}} -------------------------------------------------------------------------------- /website/themes/book/layouts/shortcodes/details.html: -------------------------------------------------------------------------------- 1 |
2 | {{ cond .IsNamedParams (.Get "title") (.Get 0) }} 3 |
4 | {{ .Inner | markdownify }} 5 |
6 |
7 | -------------------------------------------------------------------------------- /website/themes/book/exampleSite/resources/_gen/assets/scss/example/book.scss_50fc8c04e12a2f59027287995557ceff.json: -------------------------------------------------------------------------------- 1 | {"Target":"book.min.284c8fc21ced13c579d9027a9d14893c56b243c6045001180391cebb4cc36ab8.css","MediaType":"text/css","Data":{"Integrity":"sha256-KEyPwhztE8V52QJ6nRSJPFayQ8YEUAEYA5HOu0zDarg="}} -------------------------------------------------------------------------------- /leetcode/0371.Sum-of-Two-Integers/371. Sum of Two Integers.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func getSum(a int, b int) int { 4 | if a == 0 { 5 | return b 6 | } 7 | if b == 0 { 8 | return a 9 | } 10 | // (a & b)<<1 计算的是进位 11 | // a ^ b 计算的是不带进位的加法 12 | return getSum((a&b)<<1, a^b) 13 | } 14 | -------------------------------------------------------------------------------- /leetcode/1470.Shuffle-the-Array/1470. Shuffle the Array.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func shuffle(nums []int, n int) []int { 4 | result := make([]int, 0) 5 | for i := 0; i < n; i++ { 6 | result = append(result, nums[i]) 7 | result = append(result, nums[n+i]) 8 | } 9 | return result 10 | } 11 | -------------------------------------------------------------------------------- /website/content/docs/shortcodes/section/_index.md: -------------------------------------------------------------------------------- 1 | --- 2 | bookCollapseSection: true 3 | --- 4 | 5 | # Section 6 | 7 | Section renders pages in section as definition list, using title and description. 8 | 9 | ## Example 10 | 11 | ```tpl 12 | {{}} 13 | ``` 14 | 15 | {{
}} 16 | -------------------------------------------------------------------------------- /leetcode/1295.Find-Numbers-with-Even-Number-of-Digits/1295. Find Numbers with Even Number of Digits.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strconv" 4 | 5 | func findNumbers(nums []int) int { 6 | res := 0 7 | for _, n := range nums { 8 | res += 1 - len(strconv.Itoa(n))%2 9 | } 10 | return res 11 | } 12 | -------------------------------------------------------------------------------- /website/themes/book/assets/plugins/_dark.scss: -------------------------------------------------------------------------------- 1 | $gray-100: rgba(255, 255, 255, 0.1); 2 | $gray-200: rgba(255, 255, 255, 0.2); 3 | 4 | $body-background: #343a40; 5 | $body-font-color: #e9ecef; 6 | 7 | $color-link: #84b2ff; 8 | $color-visited-link: #b88dff; 9 | 10 | $icon-filter: brightness(0) invert(1); 11 | -------------------------------------------------------------------------------- /leetcode/1137.N-th-Tribonacci-Number/1137. N-th Tribonacci Number.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func tribonacci(n int) int { 4 | if n < 2 { 5 | return n 6 | } 7 | trib, prev, prev2 := 1, 1, 0 8 | for n > 2 { 9 | trib, prev, prev2 = trib+prev+prev2, trib, prev 10 | n-- 11 | } 12 | return trib 13 | } 14 | -------------------------------------------------------------------------------- /website/themes/book/assets/book.scss: -------------------------------------------------------------------------------- 1 | @import "defaults"; 2 | @import "variables"; 3 | 4 | @import "normalize"; 5 | @import "utils"; 6 | @import "main"; 7 | @import "fonts"; 8 | @import "print"; 9 | 10 | @import "markdown"; 11 | @import "shortcodes"; 12 | 13 | // Custom defined styles 14 | @import "custom"; 15 | -------------------------------------------------------------------------------- /website/themes/book/assets/menu-reset.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | var menu = document.querySelector("aside.book-menu nav"); 3 | addEventListener("beforeunload", function(event) { 4 | localStorage.setItem("menu.scrollTop", menu.scrollTop); 5 | }); 6 | menu.scrollTop = localStorage.getItem("menu.scrollTop"); 7 | })(); 8 | -------------------------------------------------------------------------------- /website/themes/book/static/svg/edit.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /leetcode/0096.Unique-Binary-Search-Trees/96. Unique Binary Search Trees.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func numTrees(n int) int { 4 | dp := make([]int, n+1) 5 | dp[0], dp[1] = 1, 1 6 | for i := 2; i <= n; i++ { 7 | for j := 1; j <= i; j++ { 8 | dp[i] += dp[j-1] * dp[i-j] 9 | } 10 | } 11 | return dp[n] 12 | } 13 | -------------------------------------------------------------------------------- /leetcode/0781.Rabbits-in-Forest/781. Rabbits in Forest.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func numRabbits(ans []int) int { 4 | total, m := 0, make(map[int]int) 5 | for _, v := range ans { 6 | if m[v] == 0 { 7 | m[v] += v 8 | total += v + 1 9 | } else { 10 | m[v]-- 11 | } 12 | } 13 | return total 14 | } 15 | -------------------------------------------------------------------------------- /website/themes/book/exampleSite/content/docs/shortcodes/section/_index.md: -------------------------------------------------------------------------------- 1 | --- 2 | bookCollapseSection: true 3 | --- 4 | 5 | # Section 6 | 7 | Section renders pages in section as definition list, using title and description. 8 | 9 | ## Example 10 | 11 | ```tpl 12 | {{}} 13 | ``` 14 | 15 | {{
}} 16 | -------------------------------------------------------------------------------- /website/themes/book/i18n/nb.yaml: -------------------------------------------------------------------------------- 1 | - id: Search 2 | translation: Søk 3 | 4 | - id: Edit this page 5 | translation: Rediger denne siden 6 | 7 | - id: Last modified by 8 | translation: Sist endret av 9 | 10 | - id: Expand 11 | translation: Utvid 12 | 13 | - id: bookSearchConfig 14 | translation: '{ cache: true }' 15 | -------------------------------------------------------------------------------- /leetcode/0217.Contains-Duplicate/217. Contains Duplicate.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func containsDuplicate(nums []int) bool { 4 | record := make(map[int]bool, len(nums)) 5 | for _, n := range nums { 6 | if _, found := record[n]; found { 7 | return true 8 | } 9 | record[n] = true 10 | } 11 | return false 12 | } 13 | -------------------------------------------------------------------------------- /leetcode/1287.Element-Appearing-More-Than-In-Sorted-Array/1287. Element Appearing More Than 25% In Sorted Array.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findSpecialInteger(arr []int) int { 4 | n := len(arr) 5 | for i := 0; i < n-n/4; i++ { 6 | if arr[i] == arr[i+n/4] { 7 | return arr[i] 8 | } 9 | } 10 | return -1 11 | } 12 | -------------------------------------------------------------------------------- /website/themes/book/i18n/en.yaml: -------------------------------------------------------------------------------- 1 | - id: Search 2 | translation: Search 3 | 4 | - id: Edit this page 5 | translation: Edit this page 6 | 7 | - id: Last modified by 8 | translation: Last modified by 9 | 10 | - id: Expand 11 | translation: Expand 12 | 13 | - id: bookSearchConfig 14 | translation: '{ cache: true }' 15 | -------------------------------------------------------------------------------- /leetcode/0122.Best-Time-to-Buy-and-Sell-Stock-II/122. Best Time to Buy and Sell Stock II.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func maxProfit122(prices []int) int { 4 | profit := 0 5 | for i := 0; i < len(prices)-1; i++ { 6 | if prices[i+1] > prices[i] { 7 | profit += prices[i+1] - prices[i] 8 | } 9 | } 10 | return profit 11 | } 12 | -------------------------------------------------------------------------------- /leetcode/0283.Move-Zeroes/283. Move Zeroes.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func moveZeroes(nums []int) { 4 | if len(nums) == 0 { 5 | return 6 | } 7 | j := 0 8 | for i := 0; i < len(nums); i++ { 9 | if nums[i] != 0 { 10 | if i != j { 11 | nums[i], nums[j] = nums[j], nums[i] 12 | } 13 | j++ 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0961.N-Repeated-Element-in-Size-2N-Array/961. N-Repeated Element in Size 2N Array.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func repeatedNTimes(A []int) int { 4 | kv := make(map[int]struct{}) 5 | for _, val := range A { 6 | if _, ok := kv[val]; ok { 7 | return val 8 | } 9 | kv[val] = struct{}{} 10 | } 11 | return 0 12 | } 13 | -------------------------------------------------------------------------------- /leetcode/1281.Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/1281. Subtract the Product and Sum of Digits of an Integer.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func subtractProductAndSum(n int) int { 4 | sum, product := 0, 1 5 | for ; n > 0; n /= 10 { 6 | sum += n % 10 7 | product *= n % 10 8 | } 9 | return product - sum 10 | } 11 | -------------------------------------------------------------------------------- /leetcode/1313.Decompress-Run-Length-Encoded-List/1313. Decompress Run-Length Encoded List.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func decompressRLElist(nums []int) []int { 4 | res := []int{} 5 | for i := 0; i < len(nums); i += 2 { 6 | for j := 0; j < nums[i]; j++ { 7 | res = append(res, nums[i+1]) 8 | } 9 | } 10 | return res 11 | } 12 | -------------------------------------------------------------------------------- /website/themes/book/i18n/de.yaml: -------------------------------------------------------------------------------- 1 | - id: Search 2 | translation: Suche 3 | 4 | - id: Edit this page 5 | translation: Seite bearbeiten 6 | 7 | - id: Last modified by 8 | translation: Zuletzt geändert von 9 | 10 | - id: Expand 11 | translation: Erweitern 12 | 13 | - id: bookSearchConfig 14 | translation: '{ cache: true }' 15 | -------------------------------------------------------------------------------- /website/themes/book/i18n/pt.yaml: -------------------------------------------------------------------------------- 1 | - id: Search 2 | translation: Buscar 3 | 4 | - id: Edit this page 5 | translation: Editar página 6 | 7 | - id: Last modified by 8 | translation: Última modificação por 9 | 10 | - id: Expand 11 | translation: Expandir 12 | 13 | - id: bookSearchConfig 14 | translation: '{ cache: true }' 15 | -------------------------------------------------------------------------------- /website/themes/book/i18n/sv.yaml: -------------------------------------------------------------------------------- 1 | - id: Search 2 | translation: Sök 3 | 4 | - id: Edit this page 5 | translation: Redigera denna sida 6 | 7 | - id: Last modified by 8 | translation: Senast modifierad av 9 | 10 | - id: Expand 11 | translation: Expandera 12 | 13 | - id: bookSearchConfig 14 | translation: '{ cache: true }' 15 | -------------------------------------------------------------------------------- /website/themes/book/i18n/es.yaml: -------------------------------------------------------------------------------- 1 | - id: Search 2 | translation: Buscar 3 | 4 | - id: Edit this page 5 | translation: Editar esta página 6 | 7 | - id: Last modified by 8 | translation: Última modificación por 9 | 10 | - id: Expand 11 | translation: Expand 12 | 13 | - id: bookSearchConfig 14 | translation: '{ cache: true }' 15 | -------------------------------------------------------------------------------- /website/themes/book/i18n/cs.yaml: -------------------------------------------------------------------------------- 1 | - id: Search 2 | translation: Vyhledávat 3 | 4 | - id: Edit this page 5 | translation: Upravit tuto stránku 6 | 7 | - id: Last modified by 8 | translation: Autor poslední změny 9 | 10 | - id: Expand 11 | translation: Rozbalit 12 | 13 | - id: bookSearchConfig 14 | translation: '{ cache: true }' 15 | -------------------------------------------------------------------------------- /gotest.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | echo "" > coverage.txt 5 | 6 | for d in $(go list ./leetcode/... | grep -v vendor); do 7 | echo $d 8 | go test -coverprofile=profile.out -covermode=atomic $d 9 | if [ -f profile.out ]; then 10 | cat profile.out >> coverage.txt 11 | rm profile.out 12 | fi 13 | done 14 | -------------------------------------------------------------------------------- /website/themes/book/i18n/fr.yaml: -------------------------------------------------------------------------------- 1 | - id: Search 2 | translation: Rechercher 3 | 4 | - id: Edit this page 5 | translation: Modifier cette page 6 | 7 | - id: Last modified by 8 | translation: Dernière modification par 9 | 10 | - id: Expand 11 | translation: Développer 12 | 13 | - id: bookSearchConfig 14 | translation: '{ cache: true }' 15 | -------------------------------------------------------------------------------- /leetcode/1306.Jump-Game-III/1306. Jump Game III.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func canReach(arr []int, start int) bool { 4 | if start >= 0 && start < len(arr) && arr[start] < len(arr) { 5 | jump := arr[start] 6 | arr[start] += len(arr) 7 | return jump == 0 || canReach(arr, start+jump) || canReach(arr, start-jump) 8 | } 9 | return false 10 | } 11 | -------------------------------------------------------------------------------- /website/themes/book/i18n/uk.yaml: -------------------------------------------------------------------------------- 1 | - id: Search 2 | translation: Пошук 3 | 4 | - id: Edit this page 5 | translation: Редагувати цю сторінку 6 | 7 | - id: Last modified by 8 | translation: Остання зміна від 9 | 10 | - id: Expand 11 | translation: Розгорнути 12 | 13 | - id: bookSearchConfig 14 | translation: '{ split: /[^a-zа-яё0-9\w]/gi }' 15 | -------------------------------------------------------------------------------- /leetcode/0453.Minimum-Moves-to-Equal-Array-Elements/453. Minimum Moves to Equal Array Elements.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "math" 4 | 5 | func minMoves(nums []int) int { 6 | sum, min, l := 0, math.MaxInt32, len(nums) 7 | for _, v := range nums { 8 | sum += v 9 | if min > v { 10 | min = v 11 | } 12 | } 13 | return sum - min*l 14 | } 15 | -------------------------------------------------------------------------------- /leetcode/0575.Distribute-Candies/575. Distribute Candies.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func distributeCandies(candies []int) int { 4 | n, m := len(candies), make(map[int]struct{}, len(candies)) 5 | for _, candy := range candies { 6 | m[candy] = struct{}{} 7 | } 8 | res := len(m) 9 | if n/2 < res { 10 | return n / 2 11 | } 12 | return res 13 | } 14 | -------------------------------------------------------------------------------- /leetcode/1389.Create-Target-Array-in-the-Given-Order/1389. Create Target Array in the Given Order.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func createTargetArray(nums []int, index []int) []int { 4 | result := make([]int, len(nums)) 5 | for i, pos := range index { 6 | copy(result[pos+1:i+1], result[pos:i]) 7 | result[pos] = nums[i] 8 | } 9 | return result 10 | } 11 | -------------------------------------------------------------------------------- /leetcode/0766.Toeplitz-Matrix/766. Toeplitz Matrix.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isToeplitzMatrix(matrix [][]int) bool { 4 | rows, columns := len(matrix), len(matrix[0]) 5 | for i := 1; i < rows; i++ { 6 | for j := 1; j < columns; j++ { 7 | if matrix[i-1][j-1] != matrix[i][j] { 8 | return false 9 | } 10 | } 11 | } 12 | return true 13 | } 14 | -------------------------------------------------------------------------------- /leetcode/1154.Day-of-the-Year/1154. Day of the Year.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "time" 4 | 5 | func dayOfYear(date string) int { 6 | first := date[:4] + "-01-01" 7 | firstDay, _ := time.Parse("2006-01-02", first) 8 | dateDay, _ := time.Parse("2006-01-02", date) 9 | duration := dateDay.Sub(firstDay) 10 | return int(duration.Hours())/24 + 1 11 | } 12 | -------------------------------------------------------------------------------- /leetcode/1221.Split-a-String-in-Balanced-Strings/1221. Split a String in Balanced Strings.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func balancedStringSplit(s string) int { 4 | count, res := 0, 0 5 | for _, r := range s { 6 | if r == 'R' { 7 | count++ 8 | } else { 9 | count-- 10 | } 11 | if count == 0 { 12 | res++ 13 | } 14 | } 15 | return res 16 | } 17 | -------------------------------------------------------------------------------- /website/themes/book/i18n/ru.yaml: -------------------------------------------------------------------------------- 1 | - id: Search 2 | translation: Поиск 3 | 4 | - id: Edit this page 5 | translation: Редактировать эту страницу 6 | 7 | - id: Last modified by 8 | translation: Последнее изменение от 9 | 10 | - id: Expand 11 | translation: Развернуть 12 | 13 | - id: bookSearchConfig 14 | translation: '{ split: /[^a-zа-яё0-9\w]/gi }' 15 | -------------------------------------------------------------------------------- /leetcode/0275.H-Index-II/275. H-Index II.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func hIndex275(citations []int) int { 4 | low, high := 0, len(citations)-1 5 | for low <= high { 6 | mid := low + (high-low)>>1 7 | if len(citations)-mid > citations[mid] { 8 | low = mid + 1 9 | } else { 10 | high = mid - 1 11 | } 12 | } 13 | return len(citations) - low 14 | } 15 | -------------------------------------------------------------------------------- /leetcode/0397.Integer-Replacement/397. Integer Replacement.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func integerReplacement(n int) int { 4 | res := 0 5 | for n > 1 { 6 | if (n & 1) == 0 { // 判断是否是偶数 7 | n >>= 1 8 | } else if (n+1)%4 == 0 && n != 3 { // 末尾 2 位为 11 9 | n++ 10 | } else { // 末尾 2 位为 01 11 | n-- 12 | } 13 | res++ 14 | } 15 | return res 16 | } 17 | -------------------------------------------------------------------------------- /website/themes/book/layouts/posts/single.html: -------------------------------------------------------------------------------- 1 | {{ define "main" }} 2 |
3 |

4 | {{ .Title }} 5 |

6 | {{ partial "docs/post-meta" . }} 7 |

8 | {{- .Content -}} 9 |

10 |
11 | {{ end }} 12 | 13 | {{ define "toc" }} 14 | {{ partial "docs/toc" . }} 15 | {{ end }} 16 | -------------------------------------------------------------------------------- /leetcode/0891.Sum-of-Subsequence-Widths/891. Sum of Subsequence Widths.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func sumSubseqWidths(A []int) int { 8 | sort.Ints(A) 9 | res, mod, n, p := 0, 1000000007, len(A), 1 10 | for i := 0; i < n; i++ { 11 | res = (res + (A[i]-A[n-1-i])*p) % mod 12 | p = (p << 1) % mod 13 | } 14 | return res 15 | } 16 | -------------------------------------------------------------------------------- /website/themes/book/assets/_print.scss: -------------------------------------------------------------------------------- 1 | @media print { 2 | .book-menu, 3 | .book-footer, 4 | .book-toc { 5 | display: none; 6 | } 7 | 8 | .book-header, 9 | .book-header aside { 10 | display: block; 11 | } 12 | 13 | main { 14 | // Fix for https://bugzilla.mozilla.org/show_bug.cgi?id=939897 15 | display: block !important; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/0191.Number-of-1-Bits/191. Number of 1 Bits.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "math/bits" 4 | 5 | // 解法一 6 | func hammingWeight(num uint32) int { 7 | return bits.OnesCount(uint(num)) 8 | } 9 | 10 | // 解法二 11 | func hammingWeight1(num uint32) int { 12 | count := 0 13 | for num != 0 { 14 | num = num & (num - 1) 15 | count++ 16 | } 17 | return count 18 | } 19 | -------------------------------------------------------------------------------- /leetcode/0387.First-Unique-Character-in-a-String/387. First Unique Character in a String.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func firstUniqChar(s string) int { 4 | result := make([]int, 26) 5 | for i := 0; i < len(s); i++ { 6 | result[s[i]-'a']++ 7 | } 8 | for i := 0; i < len(s); i++ { 9 | if result[s[i]-'a'] == 1 { 10 | return i 11 | } 12 | } 13 | return -1 14 | } 15 | -------------------------------------------------------------------------------- /leetcode/1304.Find-N-Unique-Integers-Sum-up-to-Zero/1304. Find N Unique Integers Sum up to Zero.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func sumZero(n int) []int { 4 | res, left, right, start := make([]int, n), 0, n-1, 1 5 | for left < right { 6 | res[left] = start 7 | res[right] = -start 8 | start++ 9 | left = left + 1 10 | right = right - 1 11 | } 12 | return res 13 | } 14 | -------------------------------------------------------------------------------- /leetcode/0050.Powx-n/50. Pow(x, n).go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | // 时间复杂度 O(log n),空间复杂度 O(1) 4 | func myPow(x float64, n int) float64 { 5 | if n == 0 { 6 | return 1 7 | } 8 | if n == 1 { 9 | return x 10 | } 11 | if n < 0 { 12 | n = -n 13 | x = 1 / x 14 | } 15 | tmp := myPow(x, n/2) 16 | if n%2 == 0 { 17 | return tmp * tmp 18 | } 19 | return tmp * tmp * x 20 | } 21 | -------------------------------------------------------------------------------- /leetcode/0598.Range-Addition-II/598. Range Addition II.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func maxCount(m int, n int, ops [][]int) int { 4 | minM, minN := m, n 5 | for _, op := range ops { 6 | minM = min(minM, op[0]) 7 | minN = min(minN, op[1]) 8 | } 9 | return minM * minN 10 | } 11 | 12 | func min(a, b int) int { 13 | if a < b { 14 | return a 15 | } 16 | return b 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/1051.Height-Checker/1051. Height Checker.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "sort" 4 | 5 | func heightChecker(heights []int) int { 6 | result, checker := 0, []int{} 7 | checker = append(checker, heights...) 8 | sort.Ints(checker) 9 | for i := 0; i < len(heights); i++ { 10 | if heights[i] != checker[i] { 11 | result++ 12 | } 13 | } 14 | return result 15 | } 16 | -------------------------------------------------------------------------------- /leetcode/0350.Intersection-of-Two-Arrays-II/350. Intersection of Two Arrays II.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func intersect(nums1 []int, nums2 []int) []int { 4 | m := map[int]int{} 5 | var res []int 6 | for _, n := range nums1 { 7 | m[n]++ 8 | } 9 | for _, n := range nums2 { 10 | if m[n] > 0 { 11 | res = append(res, n) 12 | m[n]-- 13 | } 14 | } 15 | return res 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0367.Valid-Perfect-Square/367. Valid Perfect Square.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isPerfectSquare(num int) bool { 4 | low, high := 1, num 5 | for low <= high { 6 | mid := low + (high-low)>>1 7 | if mid*mid == num { 8 | return true 9 | } else if mid*mid < num { 10 | low = mid + 1 11 | } else { 12 | high = mid - 1 13 | } 14 | } 15 | return false 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0409.Longest-Palindrome/409. Longest Palindrome.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func longestPalindrome(s string) int { 4 | counter := make(map[rune]int) 5 | for _, r := range s { 6 | counter[r]++ 7 | } 8 | answer := 0 9 | for _, v := range counter { 10 | answer += v / 2 * 2 11 | if answer%2 == 0 && v%2 == 1 { 12 | answer++ 13 | } 14 | } 15 | return answer 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0455.Assign-Cookies/455. Assign Cookies.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "sort" 4 | 5 | func findContentChildren(g []int, s []int) int { 6 | sort.Ints(g) 7 | sort.Ints(s) 8 | gi, si, res := 0, 0, 0 9 | for gi < len(g) && si < len(s) { 10 | if s[si] >= g[gi] { 11 | res++ 12 | si++ 13 | gi++ 14 | } else { 15 | si++ 16 | } 17 | } 18 | return res 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0832.Flipping-an-Image/832. Flipping an Image.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func flipAndInvertImage(A [][]int) [][]int { 4 | for i := 0; i < len(A); i++ { 5 | for a, b := 0, len(A[i])-1; a < b; a, b = a+1, b-1 { 6 | A[i][a], A[i][b] = A[i][b], A[i][a] 7 | } 8 | for a := 0; a < len(A[i]); a++ { 9 | A[i][a] = (A[i][a] + 1) % 2 10 | } 11 | } 12 | return A 13 | } 14 | -------------------------------------------------------------------------------- /leetcode/0349.Intersection-of-Two-Arrays/349. Intersection of Two Arrays.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func intersection(nums1 []int, nums2 []int) []int { 4 | m := map[int]bool{} 5 | var res []int 6 | for _, n := range nums1 { 7 | m[n] = true 8 | } 9 | for _, n := range nums2 { 10 | if m[n] { 11 | delete(m, n) 12 | res = append(res, n) 13 | } 14 | } 15 | return res 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/1217.Play-with-Chips/1217. Play with Chips.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func minCostToMoveChips(chips []int) int { 4 | odd, even := 0, 0 5 | for _, c := range chips { 6 | if c%2 == 0 { 7 | even++ 8 | } else { 9 | odd++ 10 | } 11 | } 12 | return min(odd, even) 13 | } 14 | 15 | func min(a int, b int) int { 16 | if a > b { 17 | return b 18 | } 19 | return a 20 | } 21 | -------------------------------------------------------------------------------- /leetcode/0342.Power-of-Four/342. Power of Four.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | // 解法一 数论 4 | func isPowerOfFour(num int) bool { 5 | return num > 0 && (num&(num-1)) == 0 && (num-1)%3 == 0 6 | } 7 | 8 | // 解法二 循环 9 | func isPowerOfFour1(num int) bool { 10 | for num >= 4 { 11 | if num%4 == 0 { 12 | num = num / 4 13 | } else { 14 | return false 15 | } 16 | } 17 | return num == 1 18 | } 19 | -------------------------------------------------------------------------------- /leetcode/0454.4Sum-II/454. 4Sum II.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func fourSumCount(A []int, B []int, C []int, D []int) int { 4 | m := make(map[int]int, len(A)*len(B)) 5 | for _, a := range A { 6 | for _, b := range B { 7 | m[a+b]++ 8 | } 9 | } 10 | ret := 0 11 | for _, c := range C { 12 | for _, d := range D { 13 | ret += m[0-c-d] 14 | } 15 | } 16 | 17 | return ret 18 | } 19 | -------------------------------------------------------------------------------- /leetcode/0704.Binary-Search/704. Binary Search.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func search704(nums []int, target int) int { 4 | low, high := 0, len(nums)-1 5 | for low <= high { 6 | mid := low + (high-low)>>1 7 | if nums[mid] == target { 8 | return mid 9 | } else if nums[mid] > target { 10 | high = mid - 1 11 | } else { 12 | low = mid + 1 13 | } 14 | } 15 | return -1 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0867.Transpose-Matrix/867. Transpose Matrix.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func transpose(A [][]int) [][]int { 4 | row, col, result := len(A), len(A[0]), make([][]int, len(A[0])) 5 | for i := range result { 6 | result[i] = make([]int, row) 7 | } 8 | for i := 0; i < row; i++ { 9 | for j := 0; j < col; j++ { 10 | result[j][i] = A[i][j] 11 | } 12 | } 13 | return result 14 | } 15 | -------------------------------------------------------------------------------- /leetcode/0027.Remove-Element/27. Remove Element.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func removeElement(nums []int, val int) int { 4 | if len(nums) == 0 { 5 | return 0 6 | } 7 | j := 0 8 | for i := 0; i < len(nums); i++ { 9 | if nums[i] != val { 10 | if i != j { 11 | nums[i], nums[j] = nums[j], nums[i] 12 | j++ 13 | } else { 14 | j++ 15 | } 16 | } 17 | } 18 | return j 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0041.First-Missing-Positive/41. First Missing Positive.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func firstMissingPositive(nums []int) int { 4 | numMap := make(map[int]int, len(nums)) 5 | for _, v := range nums { 6 | numMap[v] = v 7 | } 8 | for index := 1; index < len(nums)+1; index++ { 9 | if _, ok := numMap[index]; !ok { 10 | return index 11 | } 12 | } 13 | return len(nums) + 1 14 | } 15 | -------------------------------------------------------------------------------- /leetcode/0485.Max-Consecutive-Ones/485. Max Consecutive Ones.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findMaxConsecutiveOnes(nums []int) int { 4 | maxCount, currentCount := 0, 0 5 | for _, v := range nums { 6 | if v == 1 { 7 | currentCount++ 8 | } else { 9 | currentCount = 0 10 | } 11 | if currentCount > maxCount { 12 | maxCount = currentCount 13 | } 14 | } 15 | return maxCount 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0633.Sum-of-Square-Numbers/633. Sum of Square Numbers.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "math" 4 | 5 | func judgeSquareSum(c int) bool { 6 | low, high := 0, int(math.Sqrt(float64(c))) 7 | for low <= high { 8 | if low*low+high*high < c { 9 | low++ 10 | } else if low*low+high*high > c { 11 | high-- 12 | } else { 13 | return true 14 | } 15 | } 16 | return false 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/1207.Unique-Number-of-Occurrences/1207. Unique Number of Occurrences.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func uniqueOccurrences(arr []int) bool { 4 | freq, m := map[int]int{}, map[int]bool{} 5 | for _, v := range arr { 6 | freq[v]++ 7 | } 8 | for _, v := range freq { 9 | if _, ok := m[v]; !ok { 10 | m[v] = true 11 | } else { 12 | return false 13 | } 14 | } 15 | return true 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0168.Excel-Sheet-Column-Title/168. Excel Sheet Column Title.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func convertToTitle(n int) string { 4 | result := []byte{} 5 | for n > 0 { 6 | result = append(result, 'A'+byte((n-1)%26)) 7 | n = (n - 1) / 26 8 | } 9 | for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 { 10 | result[i], result[j] = result[j], result[i] 11 | } 12 | return string(result) 13 | } 14 | -------------------------------------------------------------------------------- /leetcode/0009.Palindrome-Number/9. Palindrome Number.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strconv" 4 | 5 | func isPalindrome(x int) bool { 6 | if x < 0 { 7 | return false 8 | } 9 | if x < 10 { 10 | return true 11 | } 12 | s := strconv.Itoa(x) 13 | length := len(s) 14 | for i := 0; i <= length/2; i++ { 15 | if s[i] != s[length-1-i] { 16 | return false 17 | } 18 | } 19 | return true 20 | } 21 | -------------------------------------------------------------------------------- /leetcode/0441.Arranging-Coins/441. Arranging Coins.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "math" 4 | 5 | // 解法一 数学公式 6 | func arrangeCoins(n int) int { 7 | if n <= 0 { 8 | return 0 9 | } 10 | x := math.Sqrt(2*float64(n)+0.25) - 0.5 11 | return int(x) 12 | } 13 | 14 | // 解法二 模拟 15 | func arrangeCoins1(n int) int { 16 | k := 1 17 | for n >= k { 18 | n -= k 19 | k++ 20 | } 21 | return k - 1 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0645.Set-Mismatch/645. Set Mismatch.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findErrorNums(nums []int) []int { 4 | m, res := make([]int, len(nums)), make([]int, 2) 5 | for _, n := range nums { 6 | if m[n-1] == 0 { 7 | m[n-1] = 1 8 | } else { 9 | res[0] = n 10 | } 11 | } 12 | for i := range m { 13 | if m[i] == 0 { 14 | res[1] = i + 1 15 | break 16 | } 17 | } 18 | return res 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/1017.Convert-to-Base--2/1017. Convert to Base -2.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strconv" 4 | 5 | func baseNeg2(N int) string { 6 | if N == 0 { 7 | return "0" 8 | } 9 | res := "" 10 | for N != 0 { 11 | remainder := N % (-2) 12 | N = N / (-2) 13 | if remainder < 0 { 14 | remainder += 2 15 | N++ 16 | } 17 | res = strconv.Itoa(remainder) + res 18 | } 19 | return res 20 | } 21 | -------------------------------------------------------------------------------- /leetcode/1464.Maximum-Product-of-Two-Elements-in-an-Array/1464. Maximum Product of Two Elements in an Array.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func maxProduct(nums []int) int { 4 | max1, max2 := 0, 0 5 | for _, num := range nums { 6 | if num >= max1 { 7 | max2 = max1 8 | max1 = num 9 | } else if num <= max1 && num >= max2 { 10 | max2 = num 11 | } 12 | } 13 | return (max1 - 1) * (max2 - 1) 14 | } 15 | -------------------------------------------------------------------------------- /leetcode/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strings" 4 | 5 | func isPrefixOfWord(sentence string, searchWord string) int { 6 | for i, v := range strings.Split(sentence, " ") { 7 | if strings.HasPrefix(v, searchWord) { 8 | return i + 1 9 | } 10 | } 11 | return -1 12 | } 13 | -------------------------------------------------------------------------------- /leetcode/0151.Reverse-Words-in-a-String/151. Reverse Words in a String.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strings" 4 | 5 | func reverseWords151(s string) string { 6 | ss := strings.Fields(s) 7 | reverse151(&ss, 0, len(ss)-1) 8 | return strings.Join(ss, " ") 9 | } 10 | 11 | func reverse151(m *[]string, i int, j int) { 12 | for i <= j { 13 | (*m)[i], (*m)[j] = (*m)[j], (*m)[i] 14 | i++ 15 | j-- 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/0386.Lexicographical-Numbers/386. Lexicographical Numbers.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func lexicalOrder(n int) []int { 4 | res := make([]int, 0, n) 5 | dfs386(1, n, &res) 6 | return res 7 | } 8 | 9 | func dfs386(x, n int, res *[]int) { 10 | limit := (x + 10) / 10 * 10 11 | for x <= n && x < limit { 12 | *res = append(*res, x) 13 | if x*10 <= n { 14 | dfs386(x*10, n, res) 15 | } 16 | x++ 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /leetcode/1047.Remove-All-Adjacent-Duplicates-In-String/1047. Remove All Adjacent Duplicates In String.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func removeDuplicates1047(S string) string { 4 | stack := []rune{} 5 | for _, s := range S { 6 | if len(stack) == 0 || len(stack) > 0 && stack[len(stack)-1] != s { 7 | stack = append(stack, s) 8 | } else { 9 | stack = stack[:len(stack)-1] 10 | } 11 | } 12 | return string(stack) 13 | } 14 | -------------------------------------------------------------------------------- /leetcode/0203.Remove-Linked-List-Elements/README.md: -------------------------------------------------------------------------------- 1 | # [203. Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/) 2 | 3 | ## 题目 4 | 5 | Remove all elements from a linked list of integers that have value val. 6 | 7 | Example : 8 | 9 | ```c 10 | Input: 1->2->6->3->4->5->6, val = 6 11 | Output: 1->2->3->4->5 12 | ``` 13 | 14 | 15 | ## 题目大意 16 | 17 | 删除链表中所有指定值的结点。 18 | 19 | ## 解题思路 20 | 21 | 按照题意做即可。 -------------------------------------------------------------------------------- /leetcode/0942.DI-String-Match/942. DI String Match.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func diStringMatch(S string) []int { 4 | result, maxNum, minNum, index := make([]int, len(S)+1), len(S), 0, 0 5 | for _, ch := range S { 6 | if ch == 'I' { 7 | result[index] = minNum 8 | minNum++ 9 | } else { 10 | result[index] = maxNum 11 | maxNum-- 12 | } 13 | index++ 14 | } 15 | result[index] = minNum 16 | return result 17 | } 18 | -------------------------------------------------------------------------------- /website/themes/book/layouts/shortcodes/button.html: -------------------------------------------------------------------------------- 1 | {{ $ref := "" }} 2 | {{ $target := "" }} 3 | {{ with .Get "href" }} 4 | {{ $ref = . }} 5 | {{ $target = "_blank" }} 6 | {{ end }} 7 | {{ with .Get "relref" }} 8 | {{ $ref = relref $ . }} 9 | {{ end }} 10 | 11 | {{ $.Inner }} 12 | 13 | -------------------------------------------------------------------------------- /leetcode/0204.Count-Primes/204. Count Primes.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func countPrimes(n int) int { 4 | isNotPrime := make([]bool, n) 5 | for i := 2; i*i < n; i++ { 6 | if isNotPrime[i] { 7 | continue 8 | } 9 | for j := i * i; j < n; j = j + i { 10 | isNotPrime[j] = true 11 | } 12 | } 13 | count := 0 14 | for i := 2; i < n; i++ { 15 | if !isNotPrime[i] { 16 | count++ 17 | } 18 | } 19 | return count 20 | } 21 | -------------------------------------------------------------------------------- /website/content/ChapterFour/_index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 第四章 3 | type: docs 4 | --- 5 | 6 | # 第四章 Leetcode 题解 7 | 8 |

9 | 10 |

11 | 12 | 13 | 这一章就是 LeetCode 的题解了。笔者目前只刷到 608 题,题解这里有 520 题,都已经 runtime beats 100% 了。相差的 88 题是还没有 beats 100% 的,笔者还需要继续优化~ 14 | 15 | 题解慢慢更新中,欢迎大家提出更好的解法。点击页面下方的 edit,会跳转到 github 对应的页面 markdown 中,可以提交你的最优解 PR。 16 | 17 | 让我们在题解的太空遨游吧~ -------------------------------------------------------------------------------- /website/themes/book/i18n/zh.yaml: -------------------------------------------------------------------------------- 1 | - id: Search 2 | translation: 搜索 3 | 4 | - id: Edit this page 5 | translation: 编辑本页 6 | 7 | - id: Last modified by 8 | translation: 最后修改者 9 | 10 | - id: Expand 11 | translation: 展开 12 | 13 | - id: bookSearchConfig 14 | translation: | 15 | { 16 | encode: false, 17 | tokenize: function(str) { 18 | return str.replace(/[\x00-\x7F]/g, '').split(''); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /leetcode/0141.Linked-List-Cycle/README.md: -------------------------------------------------------------------------------- 1 | # [141. Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/description/) 2 | 3 | ## 题目 4 | 5 | Given a linked list, determine if it has a cycle in it. 6 | 7 | Follow up: 8 | Can you solve it without using extra space? 9 | 10 | 11 | 12 | ## 题目大意 13 | 14 | 判断链表是否有环,不能使用额外的空间。 15 | 16 | ## 解题思路 17 | 18 | 给 2 个指针,一个指针是另外一个指针的下一个指针。快指针一次走 2 格,慢指针一次走 1 格。如果存在环,那么前一个指针一定会经过若干圈之后追上慢的指针。 -------------------------------------------------------------------------------- /website/themes/book/i18n/ja.yaml: -------------------------------------------------------------------------------- 1 | - id: Search 2 | translation: 検索 3 | 4 | - id: Edit this page 5 | translation: このページを編集する 6 | 7 | - id: Last modified by 8 | translation: 最終更新者 9 | 10 | - id: Expand 11 | translation: 展開 12 | 13 | - id: bookSearchConfig 14 | translation: | 15 | { 16 | encode: false, 17 | tokenize: function(str) { 18 | return str.replace(/[\x00-\x7F]/g, '').split(''); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /leetcode/0035.Search-Insert-Position/35. Search Insert Position.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func searchInsert(nums []int, target int) int { 4 | low, high := 0, len(nums)-1 5 | for low <= high { 6 | mid := low + (high-low)>>1 7 | if nums[mid] >= target { 8 | high = mid - 1 9 | } else { 10 | if (mid == len(nums)-1) || (nums[mid+1] >= target) { 11 | return mid + 1 12 | } 13 | low = mid + 1 14 | } 15 | } 16 | return 0 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/0561.Array-Partition-I/561. Array Partition I.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func arrayPairSum(nums []int) int { 4 | array := [20001]int{} 5 | for i := 0; i < len(nums); i++ { 6 | array[nums[i]+10000]++ 7 | } 8 | flag, sum := true, 0 9 | for i := 0; i < len(array); i++ { 10 | for array[i] > 0 { 11 | if flag { 12 | sum = sum + i - 10000 13 | } 14 | flag = !flag 15 | array[i]-- 16 | } 17 | } 18 | return sum 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/1299.Replace-Elements-with-Greatest-Element-on-Right-Side/1299. Replace Elements with Greatest Element on Right Side.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func replaceElements(arr []int) []int { 4 | j, temp := -1, 0 5 | for i := len(arr) - 1; i >= 0; i-- { 6 | temp = arr[i] 7 | arr[i] = j 8 | j = max(j, temp) 9 | } 10 | return arr 11 | } 12 | 13 | func max(a int, b int) int { 14 | if a > b { 15 | return a 16 | } 17 | return b 18 | } 19 | -------------------------------------------------------------------------------- /website/themes/book/layouts/taxonomy/list.html: -------------------------------------------------------------------------------- 1 | {{ define "main" }} 2 |
3 |

{{ .Title | title }}

4 | {{ $taxonomies := index .Site.Taxonomies .Page.Type }} 5 | {{ range $taxonomies }} 6 |
{{ .Page.Title }} {{ .Count }}
7 | {{ end }} 8 |
9 | {{ end }} 10 | 11 | {{ define "toc" }} 12 | {{ partial "docs/taxonomy" . }} 13 | {{ end }} 14 | -------------------------------------------------------------------------------- /leetcode/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/331. Verify Preorder Serialization of a Binary Tree.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strings" 4 | 5 | func isValidSerialization(preorder string) bool { 6 | nodes, diff := strings.Split(preorder, ","), 1 7 | for _, node := range nodes { 8 | diff-- 9 | if diff < 0 { 10 | return false 11 | } 12 | if node != "#" { 13 | diff += 2 14 | } 15 | } 16 | return diff == 0 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/0745.Prefix-and-Suffix-Search/745. Prefix and Suffix Search_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem745(t *testing.T) { 9 | obj := Constructor745([]string{"apple"}) 10 | fmt.Printf("obj = %v\n", obj) 11 | param1 := obj.F("a", "e") 12 | fmt.Printf("param_1 = %v obj = %v\n", param1, obj) 13 | param2 := obj.F("b", "") 14 | fmt.Printf("param_2 = %v obj = %v\n", param2, obj) 15 | } 16 | -------------------------------------------------------------------------------- /website/themes/book/layouts/shortcodes/mermaid.html: -------------------------------------------------------------------------------- 1 | {{ if not (.Page.Scratch.Get "mermaid") }} 2 | 3 | 4 | 9 | {{ .Page.Scratch.Set "mermaid" true }} 10 | {{ end }} 11 | 12 |

13 | {{- .Inner -}} 14 |

15 | -------------------------------------------------------------------------------- /website/themes/book/layouts/shortcodes/tab.html: -------------------------------------------------------------------------------- 1 | {{ if .Parent }} 2 | {{ $name := .Get 0 }} 3 | {{ $group := printf "tabs-%s" (.Parent.Get 0) }} 4 | 5 | {{ if not (.Parent.Scratch.Get $group) }} 6 | {{ .Parent.Scratch.Set $group slice }} 7 | {{ end }} 8 | 9 | {{ .Parent.Scratch.Add $group (dict "Name" $name "Content" .Inner) }} 10 | {{ else }} 11 | {{ errorf "%q: 'tab' shortcode must be inside 'tabs' shortcode" .Page.Path }} 12 | {{ end}} 13 | -------------------------------------------------------------------------------- /leetcode/0448.Find-All-Numbers-Disappeared-in-an-Array/448. Find All Numbers Disappeared in an Array.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findDisappearedNumbers(nums []int) []int { 4 | res := []int{} 5 | for _, v := range nums { 6 | if v < 0 { 7 | v = -v 8 | } 9 | if nums[v-1] > 0 { 10 | nums[v-1] = -nums[v-1] 11 | } 12 | } 13 | for i, v := range nums { 14 | if v > 0 { 15 | res = append(res, i+1) 16 | } 17 | } 18 | return res 19 | } 20 | -------------------------------------------------------------------------------- /website/themes/book/static/svg/translate.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /leetcode/0118.Pascals-Triangle/118. Pascal's Triangle.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func generate(numRows int) [][]int { 4 | result := [][]int{} 5 | for i := 0; i < numRows; i++ { 6 | row := []int{} 7 | for j := 0; j < i+1; j++ { 8 | if j == 0 || j == i { 9 | row = append(row, 1) 10 | } else if i > 1 { 11 | row = append(row, result[i-1][j-1]+result[i-1][j]) 12 | } 13 | } 14 | result = append(result, row) 15 | } 16 | return result 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/0343.Integer-Break/343. Integer Break.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func integerBreak(n int) int { 4 | dp := make([]int, n+1) 5 | dp[0], dp[1] = 1, 1 6 | for i := 1; i <= n; i++ { 7 | for j := 1; j < i; j++ { 8 | // dp[i] = max(dp[i], j * (i - j), j*dp[i-j]) 9 | dp[i] = max(dp[i], j*max(dp[i-j], i-j)) 10 | } 11 | } 12 | return dp[n] 13 | } 14 | 15 | func max(a int, b int) int { 16 | if a > b { 17 | return a 18 | } 19 | return b 20 | } 21 | -------------------------------------------------------------------------------- /leetcode/0724.Find-Pivot-Index/724. Find Pivot Index.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | // 2 * leftSum + num[i] = sum 4 | // 时间: O(n) 5 | // 空间: O(1) 6 | func pivotIndex(nums []int) int { 7 | if len(nums) <= 0 { 8 | return -1 9 | } 10 | var sum, leftSum int 11 | for _, num := range nums { 12 | sum += num 13 | } 14 | for index, num := range nums { 15 | if leftSum*2+num == sum { 16 | return index 17 | } 18 | leftSum += num 19 | } 20 | return -1 21 | } 22 | -------------------------------------------------------------------------------- /leetcode/1005.Maximize-Sum-Of-Array-After-K-Negations/1005. Maximize Sum Of Array After K Negations.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func largestSumAfterKNegations(A []int, K int) int { 8 | sort.Ints(A) 9 | minIdx := 0 10 | for i := 0; i < K; i++ { 11 | A[minIdx] = -A[minIdx] 12 | if A[minIdx+1] < A[minIdx] { 13 | minIdx++ 14 | } 15 | } 16 | sum := 0 17 | for _, a := range A { 18 | sum += a 19 | } 20 | return sum 21 | } 22 | -------------------------------------------------------------------------------- /website/themes/book/i18n/ko.yaml: -------------------------------------------------------------------------------- 1 | - id: Search 2 | translation: Search 3 | 4 | - id: Edit this page 5 | translation: Edit this page 6 | 7 | - id: Last modified by 8 | translation: Last modified by 9 | 10 | - id: Expand 11 | translation: Expand 12 | 13 | - id: bookSearchConfig 14 | translation: | 15 | { 16 | encode: false, 17 | tokenize: function(str) { 18 | return str.replace(/[\x00-\x7F]/g, '').split(''); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /leetcode/0021.Merge-Two-Sorted-Lists/README.md: -------------------------------------------------------------------------------- 1 | # [21. Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) 2 | 3 | ## 题目 4 | 5 | Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 6 | 7 | Example : 8 | 9 | ``` 10 | Input: 1->2->4, 1->3->4 11 | Output: 1->1->2->3->4->4 12 | 13 | ``` 14 | 15 | ## 题目大意 16 | 17 | 合并 2 个有序链表 18 | 19 | ## 解题思路 20 | 21 | 按照题意做即可。 -------------------------------------------------------------------------------- /leetcode/0055.Jump-Game/55. Jump Game.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func canJump(nums []int) bool { 4 | n := len(nums) 5 | if n == 0 { 6 | return false 7 | } 8 | if n == 1 { 9 | return true 10 | } 11 | maxJump := 0 12 | for i, v := range nums { 13 | if i > maxJump { 14 | return false 15 | } 16 | maxJump = max(maxJump, i+v) 17 | } 18 | return true 19 | } 20 | 21 | func max(a int, b int) int { 22 | if a > b { 23 | return a 24 | } 25 | return b 26 | } 27 | -------------------------------------------------------------------------------- /leetcode/0062.Unique-Paths/62. Unique Paths.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func uniquePaths(m int, n int) int { 4 | dp := make([][]int, n) 5 | for i := 0; i < n; i++ { 6 | dp[i] = make([]int, m) 7 | } 8 | for i := 0; i < m; i++ { 9 | dp[0][i] = 1 10 | } 11 | for i := 0; i < n; i++ { 12 | dp[i][0] = 1 13 | } 14 | for i := 1; i < n; i++ { 15 | for j := 1; j < m; j++ { 16 | dp[i][j] = dp[i-1][j] + dp[i][j-1] 17 | } 18 | } 19 | return dp[n-1][m-1] 20 | } 21 | -------------------------------------------------------------------------------- /website/themes/book/layouts/partials/docs/title.html: -------------------------------------------------------------------------------- 1 | 5 | {{ $title := "" }} 6 | 7 | {{ if .Title }} 8 | {{ $title = .Title }} 9 | {{ else if and .IsSection .File }} 10 | {{ $title = path.Base .File.Dir | humanize | title }} 11 | {{ else if and .IsPage .File }} 12 | {{ $title = .File.BaseFileName | humanize | title }} 13 | {{ end }} 14 | 15 | {{ return $title }} 16 | -------------------------------------------------------------------------------- /leetcode/0066.Plus-One/66. Plus One.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func plusOne(digits []int) []int { 4 | if len(digits) == 0 { 5 | return []int{} 6 | } 7 | carry := 1 8 | for i := len(digits) - 1; i >= 0; i-- { 9 | if digits[i]+carry > 9 { 10 | digits[i] = 0 11 | carry = 1 12 | } else { 13 | digits[i] += carry 14 | carry = 0 15 | } 16 | } 17 | if digits[0] == 0 && carry == 1 { 18 | digits = append([]int{1}, digits...) 19 | } 20 | return digits 21 | } 22 | -------------------------------------------------------------------------------- /leetcode/0204.Count-Primes/README.md: -------------------------------------------------------------------------------- 1 | # [204. Count Primes](https://leetcode.com/problems/count-primes/) 2 | 3 | 4 | ## 题目 5 | 6 | Count the number of prime numbers less than a non-negative number, **n**. 7 | 8 | **Example:** 9 | 10 | Input: 10 11 | Output: 4 12 | Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7. 13 | 14 | 15 | ## 题目大意 16 | 17 | 统计所有小于非负整数 n 的质数的数量。 18 | 19 | 20 | ## 解题思路 21 | 22 | - 给出一个数字 n,要求输出小于 n 的所有素数的个数总和。简单题。 23 | -------------------------------------------------------------------------------- /leetcode/0720.Longest-Word-in-Dictionary/720. Longest Word in Dictionary.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func longestWord(words []string) string { 8 | sort.Strings(words) 9 | mp := make(map[string]bool) 10 | var res string 11 | for _, word := range words { 12 | size := len(word) 13 | if size == 1 || mp[word[:size-1]] { 14 | if size > len(res) { 15 | res = word 16 | } 17 | mp[word] = true 18 | } 19 | } 20 | return res 21 | } 22 | -------------------------------------------------------------------------------- /leetcode/1078.Occurrences-After-Bigram/1078. Occurrences After Bigram.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strings" 4 | 5 | func findOcurrences(text string, first string, second string) []string { 6 | var res []string 7 | words := strings.Split(text, " ") 8 | if len(words) < 3 { 9 | return []string{} 10 | } 11 | for i := 2; i < len(words); i++ { 12 | if words[i-2] == first && words[i-1] == second { 13 | res = append(res, words[i]) 14 | } 15 | } 16 | return res 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/0884.Uncommon-Words-from-Two-Sentences/884. Uncommon Words from Two Sentences.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strings" 4 | 5 | func uncommonFromSentences(A string, B string) []string { 6 | m, res := map[string]int{}, []string{} 7 | for _, s := range []string{A, B} { 8 | for _, word := range strings.Split(s, " ") { 9 | m[word]++ 10 | } 11 | } 12 | for key := range m { 13 | if m[key] == 1 { 14 | res = append(res, key) 15 | } 16 | } 17 | return res 18 | } 19 | -------------------------------------------------------------------------------- /leetcode/0973.K-Closest-Points-to-Origin/973. K Closest Points to Origin.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "sort" 4 | 5 | // KClosest define 6 | func KClosest(points [][]int, K int) [][]int { 7 | sort.Slice(points, func(i, j int) bool { 8 | return points[i][0]*points[i][0]+points[i][1]*points[i][1] < 9 | points[j][0]*points[j][0]+points[j][1]*points[j][1] 10 | }) 11 | ans := make([][]int, K) 12 | for i := 0; i < K; i++ { 13 | ans[i] = points[i] 14 | } 15 | return ans 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0981.Time-Based-Key-Value-Store/981. Time Based Key-Value Store_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem981(t *testing.T) { 9 | obj := Constructor981() 10 | obj.Set("foo", "bar", 1) 11 | fmt.Printf("Get = %v\n", obj.Get("foo", 1)) 12 | fmt.Printf("Get = %v\n", obj.Get("foo", 3)) 13 | obj.Set("foo", "bar2", 4) 14 | fmt.Printf("Get = %v\n", obj.Get("foo", 4)) 15 | fmt.Printf("Get = %v\n", obj.Get("foo", 5)) 16 | } 17 | -------------------------------------------------------------------------------- /website/themes/book/layouts/partials/docs/post-meta.html: -------------------------------------------------------------------------------- 1 | {{ with .Date}} 2 |
{{ partial "docs/date" (dict "Date" . "Format" $.Site.Params.BookDateFormat) }}
3 | {{ end }} 4 | 5 | {{ range $taxonomy, $_ := .Site.Taxonomies }} 6 | {{ with $terms := $.GetTerms $taxonomy }} 7 |
8 | {{ range $n, $term := $terms }}{{ if $n }}, {{ end }} 9 | {{ $term.Title }} 10 | {{- end }} 11 |
12 | {{ end }} 13 | {{ end }} 14 | -------------------------------------------------------------------------------- /leetcode/0201.Bitwise-AND-of-Numbers-Range/201. Bitwise AND of Numbers Range.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | // 解法一 4 | func rangeBitwiseAnd1(m int, n int) int { 5 | if m == 0 { 6 | return 0 7 | } 8 | moved := 0 9 | for m != n { 10 | m >>= 1 11 | n >>= 1 12 | moved++ 13 | } 14 | return m << uint32(moved) 15 | } 16 | 17 | // 解法二 Brian Kernighan's algorithm 18 | func rangeBitwiseAnd(m int, n int) int { 19 | for n > m { 20 | n &= (n - 1) // 清除最低位的 1 21 | } 22 | return n 23 | } 24 | -------------------------------------------------------------------------------- /leetcode/0922.Sort-Array-By-Parity-II/922. Sort Array By Parity II.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func sortArrayByParityII(A []int) []int { 4 | if len(A) == 0 || len(A)%2 != 0 { 5 | return []int{} 6 | } 7 | res := make([]int, len(A)) 8 | oddIndex := 1 9 | evenIndex := 0 10 | for i := 0; i < len(A); i++ { 11 | if A[i]%2 == 0 { 12 | res[evenIndex] = A[i] 13 | evenIndex += 2 14 | } else { 15 | res[oddIndex] = A[i] 16 | oddIndex += 2 17 | } 18 | } 19 | return res 20 | } 21 | -------------------------------------------------------------------------------- /leetcode/0470.Implement-Rand10-Using-Rand7/470. Implement Rand10() Using Rand7().go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "math/rand" 4 | 5 | func rand10() int { 6 | rand10 := 10 7 | for rand10 >= 10 { 8 | rand10 = (rand7() - 1) + rand7() 9 | } 10 | return rand10%10 + 1 11 | } 12 | 13 | func rand7() int { 14 | return rand.Intn(7) 15 | } 16 | 17 | func rand101() int { 18 | rand40 := 40 19 | for rand40 >= 40 { 20 | rand40 = (rand7()-1)*7 + rand7() - 1 21 | } 22 | return rand40%10 + 1 23 | } 24 | -------------------------------------------------------------------------------- /leetcode/0706.Design-HashMap/706. Design HashMap_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem706(t *testing.T) { 9 | obj := Constructor706() 10 | obj.Put(7, 10) 11 | fmt.Printf("Get 7 = %v\n", obj.Get(7)) 12 | obj.Put(7, 20) 13 | fmt.Printf("Contains 7 = %v\n", obj.Get(7)) 14 | param1 := obj.Get(100) 15 | fmt.Printf("param1 = %v\n", param1) 16 | obj.Remove(7) 17 | param1 = obj.Get(7) 18 | fmt.Printf("param1 = %v\n", param1) 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0881.Boats-to-Save-People/881. Boats to Save People.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func numRescueBoats(people []int, limit int) int { 8 | sort.Ints(people) 9 | left, right, res := 0, len(people)-1, 0 10 | for left <= right { 11 | if left == right { 12 | res++ 13 | return res 14 | } 15 | if people[left]+people[right] <= limit { 16 | left++ 17 | right-- 18 | } else { 19 | right-- 20 | } 21 | res++ 22 | } 23 | return res 24 | } 25 | -------------------------------------------------------------------------------- /leetcode/1260.Shift-2D-Grid/1260. Shift 2D Grid.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func shiftGrid(grid [][]int, k int) [][]int { 4 | x, y := len(grid[0]), len(grid) 5 | newGrid := make([][]int, y) 6 | for i := 0; i < y; i++ { 7 | newGrid[i] = make([]int, x) 8 | } 9 | for i := 0; i < y; i++ { 10 | for j := 0; j < x; j++ { 11 | ny := (k / x) + i 12 | if (j + (k % x)) >= x { 13 | ny++ 14 | } 15 | newGrid[ny%y][(j+(k%x))%x] = grid[i][j] 16 | } 17 | } 18 | return newGrid 19 | } 20 | -------------------------------------------------------------------------------- /website/content/docs/shortcodes/buttons.md: -------------------------------------------------------------------------------- 1 | # Buttons 2 | 3 | Buttons are styled links that can lead to local page or external link. 4 | 5 | ## Example 6 | 7 | ```tpl 8 | {{}}Get Home{{}} 9 | {{}}Contribute{{}} 10 | ``` 11 | 12 | {{< button relref="/" >}}Get Home{{< /button >}} 13 | {{< button href="https://github.com/alex-shpak/hugo-book" >}}Contribute{{< /button >}} 14 | -------------------------------------------------------------------------------- /website/themes/book/i18n/cn.yaml: -------------------------------------------------------------------------------- 1 | # This should be removed in future, 'cn' is moved to `zh' 2 | - id: Search 3 | translation: 搜索 4 | 5 | - id: Edit this page 6 | translation: 编辑本页 7 | 8 | - id: Last modified by 9 | translation: 最后修改者 10 | 11 | - id: Expand 12 | translation: 展开 13 | 14 | - id: bookSearchConfig 15 | translation: | 16 | { 17 | encode: false, 18 | tokenize: function(str) { 19 | return str.replace(/[\x00-\x7F]/g, '').split(''); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /leetcode/0011.Container-With-Most-Water/11. Container With Most Water.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func maxArea(height []int) int { 4 | max, start, end := 0, 0, len(height)-1 5 | for start < end { 6 | width := end - start 7 | high := 0 8 | if height[start] < height[end] { 9 | high = height[start] 10 | start++ 11 | } else { 12 | high = height[end] 13 | end-- 14 | } 15 | 16 | temp := width * high 17 | if temp > max { 18 | max = temp 19 | } 20 | } 21 | return max 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0154.Find-Minimum-in-Rotated-Sorted-Array-II/154. Find Minimum in Rotated Sorted Array II.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findMin154(nums []int) int { 4 | low, high := 0, len(nums)-1 5 | for low < high { 6 | if nums[low] < nums[high] { 7 | return nums[low] 8 | } 9 | mid := low + (high-low)>>1 10 | if nums[mid] > nums[low] { 11 | low = mid + 1 12 | } else if nums[mid] == nums[low] { 13 | low++ 14 | } else { 15 | high = mid 16 | } 17 | } 18 | return nums[low] 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0532.K-diff-Pairs-in-an-Array/532. K-diff Pairs in an Array.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findPairs(nums []int, k int) int { 4 | if k < 0 || len(nums) == 0 { 5 | return 0 6 | } 7 | var count int 8 | m := make(map[int]int, len(nums)) 9 | for _, value := range nums { 10 | m[value]++ 11 | } 12 | for key := range m { 13 | if k == 0 && m[key] > 1 { 14 | count++ 15 | continue 16 | } 17 | if k > 0 && m[key+k] > 0 { 18 | count++ 19 | } 20 | } 21 | return count 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0735.Asteroid-Collision/735. Asteroid Collision.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func asteroidCollision(asteroids []int) []int { 4 | res := []int{} 5 | for _, v := range asteroids { 6 | for len(res) != 0 && res[len(res)-1] > 0 && res[len(res)-1] < -v { 7 | res = res[:len(res)-1] 8 | } 9 | if len(res) == 0 || v > 0 || res[len(res)-1] < 0 { 10 | res = append(res, v) 11 | } else if v < 0 && res[len(res)-1] == -v { 12 | res = res[:len(res)-1] 13 | } 14 | } 15 | return res 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0303.Range-Sum-Query---Immutable/303. Range Sum Query - Immutable_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem303(t *testing.T) { 9 | obj := Constructor303([]int{-2, 0, 3, -5, 2, -1}) 10 | fmt.Printf("obj = %v\n", obj) 11 | fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 2)) // return 1 12 | fmt.Printf("SumRange(2,5) = %v\n", obj.SumRange(2, 5)) // return -1 13 | fmt.Printf("SumRange(0,5) = %v\n", obj.SumRange(0, 5)) // return -3 14 | } 15 | -------------------------------------------------------------------------------- /leetcode/0414.Third-Maximum-Number/414. Third Maximum Number.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | func thirdMax(nums []int) int { 8 | a, b, c := math.MinInt64, math.MinInt64, math.MinInt64 9 | for _, v := range nums { 10 | if v > a { 11 | c = b 12 | b = a 13 | a = v 14 | } else if v < a && v > b { 15 | c = b 16 | b = v 17 | } else if v < b && v > c { 18 | c = v 19 | } 20 | } 21 | if c == math.MinInt64 { 22 | return a 23 | } 24 | return c 25 | } 26 | -------------------------------------------------------------------------------- /leetcode/0744.Find-Smallest-Letter-Greater-Than-Target/744. Find Smallest Letter Greater Than Target.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func nextGreatestLetter(letters []byte, target byte) byte { 4 | low, high := 0, len(letters)-1 5 | for low <= high { 6 | mid := low + (high-low)>>1 7 | if letters[mid] > target { 8 | high = mid - 1 9 | } else { 10 | low = mid + 1 11 | } 12 | } 13 | find := letters[low%len(letters)] 14 | if find <= target { 15 | return letters[0] 16 | } 17 | return find 18 | } 19 | -------------------------------------------------------------------------------- /leetcode/0762.Prime-Number-of-Set-Bits-in-Binary-Representation/762. Prime Number of Set Bits in Binary Representation.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "math/bits" 4 | 5 | func countPrimeSetBits(L int, R int) int { 6 | counter := 0 7 | for i := L; i <= R; i++ { 8 | if isPrime(bits.OnesCount(uint(i))) { 9 | counter++ 10 | } 11 | } 12 | return counter 13 | } 14 | 15 | func isPrime(x int) bool { 16 | return x == 2 || x == 3 || x == 5 || x == 7 || x == 11 || x == 13 || x == 17 || x == 19 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/0841.Keys-and-Rooms/841. Keys and Rooms.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func canVisitAllRooms(rooms [][]int) bool { 4 | visited := make(map[int]bool) 5 | visited[0] = true 6 | dfsVisitAllRooms(rooms, visited, 0) 7 | return len(rooms) == len(visited) 8 | } 9 | 10 | func dfsVisitAllRooms(es [][]int, visited map[int]bool, from int) { 11 | for _, to := range es[from] { 12 | if visited[to] { 13 | continue 14 | } 15 | visited[to] = true 16 | dfsVisitAllRooms(es, visited, to) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /website/themes/book/i18n/jp.yaml: -------------------------------------------------------------------------------- 1 | # This should be removed in future, 'jp' is moved to `ja' 2 | - id: Search 3 | translation: 検索 4 | 5 | - id: Edit this page 6 | translation: このページを編集する 7 | 8 | - id: Last modified by 9 | translation: 最終更新者 10 | 11 | - id: Expand 12 | translation: 展開 13 | 14 | - id: bookSearchConfig 15 | translation: | 16 | { 17 | encode: false, 18 | tokenize: function(str) { 19 | return str.replace(/[\x00-\x7F]/g, '').split(''); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /leetcode/0023.Merge-k-Sorted-Lists/README.md: -------------------------------------------------------------------------------- 1 | # [23. Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/) 2 | 3 | ## 题目 4 | 5 | Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 6 | 7 | 8 | 9 | Example : 10 | 11 | ``` 12 | Input: 13 | [ 14 | 1->4->5, 15 | 1->3->4, 16 | 2->6 17 | ] 18 | Output: 1->1->2->3->4->4->5->6 19 | 20 | ``` 21 | 22 | ## 题目大意 23 | 24 | 合并 K 个有序链表 25 | 26 | ## 解题思路 27 | 28 | 借助分治的思想,把 K 个有序链表两两合并即可。相当于是第 21 题的加强版。 -------------------------------------------------------------------------------- /leetcode/0219.Contains-Duplicate-II/219. Contains Duplicate II.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func containsNearbyDuplicate(nums []int, k int) bool { 4 | if len(nums) <= 1 { 5 | return false 6 | } 7 | if k <= 0 { 8 | return false 9 | } 10 | record := make(map[int]bool, len(nums)) 11 | for i, n := range nums { 12 | if _, found := record[n]; found { 13 | return true 14 | } 15 | record[n] = true 16 | if len(record) == k+1 { 17 | delete(record, nums[i-k]) 18 | } 19 | } 20 | return false 21 | } 22 | -------------------------------------------------------------------------------- /leetcode/0985.Sum-of-Even-Numbers-After-Queries/985. Sum of Even Numbers After Queries.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func sumEvenAfterQueries(A []int, queries [][]int) []int { 4 | cur, res := 0, []int{} 5 | for _, v := range A { 6 | if v%2 == 0 { 7 | cur += v 8 | } 9 | } 10 | for _, q := range queries { 11 | if A[q[1]]%2 == 0 { 12 | cur -= A[q[1]] 13 | } 14 | A[q[1]] += q[0] 15 | if A[q[1]]%2 == 0 { 16 | cur += A[q[1]] 17 | } 18 | res = append(res, cur) 19 | } 20 | return res 21 | } 22 | -------------------------------------------------------------------------------- /leetcode/1157.Online-Majority-Element-In-Subarray/1157. Online Majority Element In Subarray_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem1157(t *testing.T) { 9 | arr := []int{1, 1, 2, 2, 1, 1} 10 | obj := Constructor1157(arr) 11 | fmt.Printf("obj = %v\n", obj) 12 | fmt.Printf("query(0,5,4) = %v\n", obj.Query(0, 5, 4)) //1 13 | fmt.Printf("query(0,3,3) = %v\n", obj.Query(0, 3, 3)) //-1 14 | fmt.Printf("query(2,3,2) = %v\n", obj.Query(2, 3, 2)) //2 15 | } 16 | -------------------------------------------------------------------------------- /leetcode/0024.Swap-Nodes-in-Pairs/README.md: -------------------------------------------------------------------------------- 1 | # [24. Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/description/) 2 | 3 | ## 题目 4 | 5 | Given a linked list, swap every two adjacent nodes and return its head. 6 | 7 | You may not modify the values in the list's nodes, only nodes itself may be changed. 8 | 9 | 10 | 11 | Example: 12 | 13 | ```c 14 | Given 1->2->3->4, you should return the list as 2->1->4->3. 15 | ``` 16 | 17 | ## 题目大意 18 | 19 | 两两相邻的元素,翻转链表 20 | 21 | ## 解题思路 22 | 23 | 按照题意做即可。 -------------------------------------------------------------------------------- /leetcode/0930.Binary-Subarrays-With-Sum/930. Binary Subarrays With Sum.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "fmt" 4 | 5 | func numSubarraysWithSum(A []int, S int) int { 6 | freq, sum, res := make([]int, len(A)+1), 0, 0 7 | freq[0] = 1 8 | for _, v := range A { 9 | t := sum + v - S 10 | if t >= 0 { 11 | // 总和有多余的,需要减去 t,除去的方法有 freq[t] 种 12 | res += freq[t] 13 | } 14 | sum += v 15 | freq[sum]++ 16 | fmt.Printf("freq = %v sum = %v res = %v t = %v\n", freq, sum, res, t) 17 | } 18 | return res 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/1385.Find-the-Distance-Value-Between-Two-Arrays/1385. Find the Distance Value Between Two Arrays.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findTheDistanceValue(arr1 []int, arr2 []int, d int) int { 4 | res := 0 5 | for i := range arr1 { 6 | for j := range arr2 { 7 | if abs(arr1[i]-arr2[j]) <= d { 8 | break 9 | } 10 | if j == len(arr2)-1 { 11 | res++ 12 | } 13 | } 14 | } 15 | return res 16 | } 17 | 18 | func abs(a int) int { 19 | if a < 0 { 20 | return -1 * a 21 | } 22 | return a 23 | } 24 | -------------------------------------------------------------------------------- /leetcode/0412.Fizz-Buzz/412. Fizz Buzz.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strconv" 4 | 5 | func fizzBuzz(n int) []string { 6 | if n < 0 { 7 | return []string{} 8 | } 9 | solution := make([]string, n) 10 | for i := 1; i <= n; i++ { 11 | if i%3 == 0 && i%5 == 0 { 12 | solution[i-1] = "FizzBuzz" 13 | } else if i%3 == 0 { 14 | solution[i-1] = "Fizz" 15 | } else if i%5 == 0 { 16 | solution[i-1] = "Buzz" 17 | } else { 18 | solution[i-1] = strconv.Itoa(i) 19 | } 20 | } 21 | return solution 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0921.Minimum-Add-to-Make-Parentheses-Valid/921. Minimum Add to Make Parentheses Valid.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func minAddToMakeValid(S string) int { 4 | if len(S) == 0 { 5 | return 0 6 | } 7 | stack := make([]rune, 0) 8 | for _, v := range S { 9 | if v == '(' { 10 | stack = append(stack, v) 11 | } else if (v == ')') && len(stack) > 0 && stack[len(stack)-1] == '(' { 12 | stack = stack[:len(stack)-1] 13 | } else { 14 | stack = append(stack, v) 15 | } 16 | } 17 | return len(stack) 18 | } 19 | -------------------------------------------------------------------------------- /leetcode/1232.Check-If-It-Is-a-Straight-Line/1232. Check If It Is a Straight Line.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func checkStraightLine(coordinates [][]int) bool { 4 | dx0 := coordinates[1][0] - coordinates[0][0] 5 | dy0 := coordinates[1][1] - coordinates[0][1] 6 | for i := 1; i < len(coordinates)-1; i++ { 7 | dx := coordinates[i+1][0] - coordinates[i][0] 8 | dy := coordinates[i+1][1] - coordinates[i][1] 9 | if dy*dx0 != dy0*dx { // check cross product 10 | return false 11 | } 12 | } 13 | return true 14 | } 15 | -------------------------------------------------------------------------------- /website/themes/book/exampleSite/content/docs/shortcodes/buttons.md: -------------------------------------------------------------------------------- 1 | # Buttons 2 | 3 | Buttons are styled links that can lead to local page or external link. 4 | 5 | ## Example 6 | 7 | ```tpl 8 | {{}}Get Home{{}} 9 | {{}}Contribute{{}} 10 | ``` 11 | 12 | {{< button relref="/" >}}Get Home{{< /button >}} 13 | {{< button href="https://github.com/alex-shpak/hugo-book" >}}Contribute{{< /button >}} 14 | -------------------------------------------------------------------------------- /leetcode/0074.Search-a-2D-Matrix/74. Search a 2D Matrix.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func searchMatrix(matrix [][]int, target int) bool { 4 | if len(matrix) == 0 { 5 | return false 6 | } 7 | m, low, high := len(matrix[0]), 0, len(matrix[0])*len(matrix)-1 8 | for low <= high { 9 | mid := low + (high-low)>>1 10 | if matrix[mid/m][mid%m] == target { 11 | return true 12 | } else if matrix[mid/m][mid%m] > target { 13 | high = mid - 1 14 | } else { 15 | low = mid + 1 16 | } 17 | } 18 | return false 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0075.Sort-Colors/75. Sort Colors.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func sortColors(nums []int) { 4 | if len(nums) == 0 { 5 | return 6 | } 7 | 8 | r := 0 9 | w := 0 10 | b := 0 // label the end of different colors; 11 | for _, num := range nums { 12 | if num == 0 { 13 | nums[b] = 2 14 | b++ 15 | nums[w] = 1 16 | w++ 17 | nums[r] = 0 18 | r++ 19 | } else if num == 1 { 20 | nums[b] = 2 21 | b++ 22 | nums[w] = 1 23 | w++ 24 | } else if num == 2 { 25 | b++ 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /leetcode/0093.Restore-IP-Addresses/README.md: -------------------------------------------------------------------------------- 1 | # [93. Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses/) 2 | 3 | 4 | ## 题目 5 | 6 | Given a string containing only digits, restore it by returning all possible valid IP address combinations. 7 | 8 | **Example:** 9 | 10 | Input: "25525511135" 11 | Output: ["255.255.11.135", "255.255.111.35"] 12 | 13 | ## 题目大意 14 | 15 | 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。 16 | 17 | ## 解题思路 18 | 19 | - DFS 深搜 20 | - 需要注意的点是 IP 的规则,以 0 开头的数字和超过 255 的数字都为非法的。 21 | 22 | -------------------------------------------------------------------------------- /leetcode/0202.Happy-Number/202. Happy Number.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isHappy(n int) bool { 4 | if n == 0 { 5 | return false 6 | } 7 | res := 0 8 | num := n 9 | record := map[int]int{} 10 | for { 11 | for num != 0 { 12 | res += (num % 10) * (num % 10) 13 | num = num / 10 14 | } 15 | if _, ok := record[res]; !ok { 16 | if res == 1 { 17 | return true 18 | } 19 | record[res] = res 20 | num = res 21 | res = 0 22 | continue 23 | } else { 24 | return false 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/0260.Single-Number-III/260. Single Number III.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func singleNumberIII(nums []int) []int { 4 | diff := 0 5 | for _, num := range nums { 6 | diff ^= num 7 | } 8 | // Get its last set bit (lsb) 9 | diff &= -diff 10 | res := []int{0, 0} // this array stores the two numbers we will return 11 | for _, num := range nums { 12 | if (num & diff) == 0 { // the bit is not set 13 | res[0] ^= num 14 | } else { // the bit is set 15 | res[1] ^= num 16 | } 17 | } 18 | return res 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0914.X-of-a-Kind-in-a-Deck-of-Cards/914. X of a Kind in a Deck of Cards.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func hasGroupsSizeX(deck []int) bool { 4 | if len(deck) < 2 { 5 | return false 6 | } 7 | m, g := map[int]int{}, -1 8 | for _, d := range deck { 9 | m[d]++ 10 | } 11 | for _, v := range m { 12 | if g == -1 { 13 | g = v 14 | } else { 15 | g = gcd(g, v) 16 | } 17 | } 18 | return g >= 2 19 | } 20 | 21 | func gcd(a, b int) int { 22 | if a == 0 { 23 | return b 24 | } 25 | return gcd(b%a, a) 26 | } 27 | -------------------------------------------------------------------------------- /leetcode/1266.Minimum-Time-Visiting-All-Points/1266. Minimum Time Visiting All Points.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func minTimeToVisitAllPoints(points [][]int) int { 4 | res := 0 5 | for i := 1; i < len(points); i++ { 6 | res += max(abs(points[i][0]-points[i-1][0]), abs(points[i][1]-points[i-1][1])) 7 | } 8 | return res 9 | } 10 | 11 | func max(a int, b int) int { 12 | if a > b { 13 | return a 14 | } 15 | return b 16 | } 17 | 18 | func abs(a int) int { 19 | if a > 0 { 20 | return a 21 | } 22 | return -a 23 | } 24 | -------------------------------------------------------------------------------- /leetcode/0933.Number-of-Recent-Calls/933. Number of Recent Calls_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem933(t *testing.T) { 9 | obj := Constructor933() 10 | fmt.Printf("obj = %v\n", obj) 11 | param1 := obj.Ping(1) 12 | fmt.Printf("param = %v\n", param1) 13 | param1 = obj.Ping(100) 14 | fmt.Printf("param = %v\n", param1) 15 | param1 = obj.Ping(3001) 16 | fmt.Printf("param = %v\n", param1) 17 | param1 = obj.Ping(3002) 18 | fmt.Printf("param = %v\n", param1) 19 | } 20 | -------------------------------------------------------------------------------- /website/themes/book/layouts/shortcodes/expand.html: -------------------------------------------------------------------------------- 1 | {{ warnf "Expand shortcode is deprecated. Use 'details' instead." }} 2 |
3 | 13 |
14 | -------------------------------------------------------------------------------- /leetcode/0920.Number-of-Music-Playlists/920. Number of Music Playlists.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func numMusicPlaylists(N int, L int, K int) int { 4 | dp, mod := make([][]int, L+1), 1000000007 5 | for i := 0; i < L+1; i++ { 6 | dp[i] = make([]int, N+1) 7 | } 8 | dp[0][0] = 1 9 | for i := 1; i <= L; i++ { 10 | for j := 1; j <= N; j++ { 11 | dp[i][j] = (dp[i-1][j-1] * (N - (j - 1))) % mod 12 | if j > K { 13 | dp[i][j] = (dp[i][j] + (dp[i-1][j]*(j-K))%mod) % mod 14 | } 15 | } 16 | } 17 | return dp[L][N] 18 | } 19 | -------------------------------------------------------------------------------- /leetcode/0148.Sort-List/README.md: -------------------------------------------------------------------------------- 1 | # [148. Sort List](https://leetcode.com/problems/sort-list/) 2 | 3 | ## 题目 4 | 5 | Sort a linked list in O(n log n) time using constant space complexity. 6 | 7 | Example 1: 8 | 9 | ```c 10 | Input: 4->2->1->3 11 | Output: 1->2->3->4 12 | ``` 13 | 14 | Example 2: 15 | 16 | ```c 17 | Input: -1->5->3->4->0 18 | Output: -1->0->3->4->5 19 | ``` 20 | 21 | ## 题目大意 22 | 23 | 链表的排序,要求时间复杂度必须是 O(n log n),空间复杂度是 O(1) 24 | 25 | ## 解题思路 26 | 27 | 这道题只能用归并排序才能符合要求。归并排序需要的 2 个操作在其他题目已经出现过了,取中间点是第 876 题,合并 2 个有序链表是第 21 题。 -------------------------------------------------------------------------------- /leetcode/0557.Reverse-Words-in-a-String-III/557. Reverse Words in a String III.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "strings" 5 | ) 6 | 7 | func reverseWords(s string) string { 8 | ss := strings.Split(s, " ") 9 | for i, s := range ss { 10 | ss[i] = revers(s) 11 | } 12 | return strings.Join(ss, " ") 13 | } 14 | 15 | func revers(s string) string { 16 | bytes := []byte(s) 17 | i, j := 0, len(bytes)-1 18 | for i < j { 19 | bytes[i], bytes[j] = bytes[j], bytes[i] 20 | i++ 21 | j-- 22 | } 23 | return string(bytes) 24 | } 25 | -------------------------------------------------------------------------------- /website/themes/book/assets/plugins/_scrollbars.scss: -------------------------------------------------------------------------------- 1 | @import "defaults"; 2 | @import "variables"; 3 | 4 | // Webkit 5 | ::-webkit-scrollbar { 6 | width: $padding-8; 7 | } 8 | 9 | ::-webkit-scrollbar-thumb { 10 | background: transparent; 11 | border-radius: $padding-8; 12 | } 13 | 14 | :hover::-webkit-scrollbar-thumb { 15 | background: $gray-500; 16 | } 17 | 18 | // MS 19 | body { 20 | -ms-overflow-style: -ms-autohiding-scrollbar 21 | } 22 | 23 | // Future 24 | .book-menu nav { 25 | scrollbar-color: transparent $gray-500; 26 | } 27 | -------------------------------------------------------------------------------- /website/themes/book/layouts/partials/docs/header.html: -------------------------------------------------------------------------------- 1 |
2 | 5 | 6 | {{ partial "docs/title" . }} 7 | 8 | 13 | 14 |
15 | -------------------------------------------------------------------------------- /leetcode/0083.Remove-Duplicates-from-Sorted-List/README.md: -------------------------------------------------------------------------------- 1 | # [83. Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) 2 | 3 | ## 题目 4 | 5 | Given a sorted linked list, delete all duplicates such that each element appear only once. 6 | 7 | Example 1: 8 | 9 | ``` 10 | Input: 1->1->2 11 | Output: 1->2 12 | ``` 13 | 14 | Example 2: 15 | 16 | ``` 17 | Input: 1->1->2->3->3 18 | Output: 1->2->3 19 | ``` 20 | 21 | ## 题目大意 22 | 23 | 删除链表中重复的结点,以保障每个结点只出现一次。 24 | 25 | 26 | ## 解题思路 27 | 28 | 按照题意做即可。 -------------------------------------------------------------------------------- /leetcode/0404.Sum-of-Left-Leaves/README.md: -------------------------------------------------------------------------------- 1 | # [404. Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) 2 | 3 | 4 | ## 题目 5 | 6 | Find the sum of all left leaves in a given binary tree. 7 | 8 | **Example:** 9 | 10 | 3 11 | / \ 12 | 9 20 13 | / \ 14 | 15 7 15 | 16 | There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 17 | 18 | 19 | ## 题目大意 20 | 21 | 计算给定二叉树的所有左叶子之和。 22 | 23 | 24 | ## 解题思路 25 | 26 | 27 | - 这一题是微软的面试题。递归求解即可 28 | 29 | -------------------------------------------------------------------------------- /leetcode/0416.Partition-Equal-Subset-Sum/416. Partition Equal Subset Sum.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func canPartition(nums []int) bool { 4 | sum := 0 5 | for _, v := range nums { 6 | sum += v 7 | } 8 | if sum%2 != 0 { 9 | return false 10 | } 11 | // C = half sum 12 | n, C, dp := len(nums), sum/2, make([]bool, sum/2+1) 13 | for i := 0; i <= C; i++ { 14 | dp[i] = (nums[0] == i) 15 | } 16 | for i := 1; i < n; i++ { 17 | for j := C; j >= nums[i]; j-- { 18 | dp[j] = dp[j] || dp[j-nums[i]] 19 | } 20 | } 21 | return dp[C] 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0028.Implement-strStr/28. Implement strStr().go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strings" 4 | 5 | // 解法一 6 | func strStr(haystack string, needle string) int { 7 | for i := 0; ; i++ { 8 | for j := 0; ; j++ { 9 | if j == len(needle) { 10 | return i 11 | } 12 | if i+j == len(haystack) { 13 | return -1 14 | } 15 | if needle[j] != haystack[i+j] { 16 | break 17 | } 18 | } 19 | } 20 | } 21 | 22 | // 解法二 23 | func strStr1(haystack string, needle string) int { 24 | return strings.Index(haystack, needle) 25 | } 26 | -------------------------------------------------------------------------------- /leetcode/0046.Permutations/README.md: -------------------------------------------------------------------------------- 1 | # [46. Permutations](https://leetcode.com/problems/permutations/) 2 | 3 | 4 | ## 题目 5 | 6 | Given a collection of **distinct** integers, return all possible permutations. 7 | 8 | **Example:** 9 | 10 | 11 | Input: [1,2,3] 12 | Output: 13 | [ 14 | [1,2,3], 15 | [1,3,2], 16 | [2,1,3], 17 | [2,3,1], 18 | [3,1,2], 19 | [3,2,1] 20 | ] 21 | 22 | 23 | ## 题目大意 24 | 25 | 给定一个没有重复数字的序列,返回其所有可能的全排列。 26 | 27 | 28 | ## 解题思路 29 | 30 | - 求出一个数组的排列组合中的所有排列,用 DFS 深搜即可。 31 | -------------------------------------------------------------------------------- /leetcode/0599.Minimum-Index-Sum-of-Two-Lists/599. Minimum Index Sum of Two Lists.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findRestaurant(list1 []string, list2 []string) []string { 4 | m, ans := make(map[string]int, len(list1)), []string{} 5 | for i, r := range list1 { 6 | m[r] = i 7 | } 8 | for j, r := range list2 { 9 | if _, ok := m[r]; ok { 10 | m[r] += j 11 | if len(ans) == 0 || m[r] == m[ans[0]] { 12 | ans = append(ans, r) 13 | } else if m[r] < m[ans[0]] { 14 | ans = []string{r} 15 | } 16 | } 17 | } 18 | return ans 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0077.Combinations/README.md: -------------------------------------------------------------------------------- 1 | # [77. Combinations](https://leetcode.com/problems/combinations/) 2 | 3 | 4 | ## 题目 5 | 6 | Given two integers *n* and *k*, return all possible combinations of *k* numbers out of 1 ... *n*. 7 | 8 | **Example:** 9 | 10 | Input: n = 4, k = 2 11 | Output: 12 | [ 13 | [2,4], 14 | [3,4], 15 | [2,3], 16 | [1,2], 17 | [1,3], 18 | [1,4], 19 | ] 20 | 21 | ## 题目大意 22 | 23 | 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。 24 | 25 | ## 解题思路 26 | 27 | - 计算排列组合中的组合,用 DFS 深搜即可,注意剪枝 28 | -------------------------------------------------------------------------------- /leetcode/0402.Remove-K-Digits/402. Remove K Digits.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func removeKdigits(num string, k int) string { 4 | if k == len(num) { 5 | return "0" 6 | } 7 | res := []byte{} 8 | for i := 0; i < len(num); i++ { 9 | c := num[i] 10 | for k > 0 && len(res) > 0 && c < res[len(res)-1] { 11 | res = res[:len(res)-1] 12 | k-- 13 | } 14 | res = append(res, c) 15 | } 16 | res = res[:len(res)-k] 17 | 18 | // trim leading zeros 19 | for len(res) > 1 && res[0] == '0' { 20 | res = res[1:] 21 | } 22 | return string(res) 23 | } 24 | -------------------------------------------------------------------------------- /leetcode/0155.Min-Stack/155. Min Stack_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem155(t *testing.T) { 9 | obj1 := Constructor155() 10 | obj1.Push(1) 11 | fmt.Printf("obj1 = %v\n", obj1) 12 | obj1.Push(0) 13 | fmt.Printf("obj1 = %v\n", obj1) 14 | obj1.Push(10) 15 | fmt.Printf("obj1 = %v\n", obj1) 16 | obj1.Pop() 17 | fmt.Printf("obj1 = %v\n", obj1) 18 | param3 := obj1.Top() 19 | fmt.Printf("param_3 = %v\n", param3) 20 | param4 := obj1.GetMin() 21 | fmt.Printf("param_4 = %v\n", param4) 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0594.Longest-Harmonious-Subsequence/594. Longest Harmonious Subsequence.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findLHS(nums []int) int { 4 | if len(nums) < 2 { 5 | return 0 6 | } 7 | res := make(map[int]int, len(nums)) 8 | for _, num := range nums { 9 | if _, exist := res[num]; exist { 10 | res[num]++ 11 | continue 12 | } 13 | res[num] = 1 14 | } 15 | longest := 0 16 | for k, c := range res { 17 | if n, exist := res[k+1]; exist { 18 | if c+n > longest { 19 | longest = c + n 20 | } 21 | } 22 | } 23 | return longest 24 | } 25 | -------------------------------------------------------------------------------- /leetcode/0946.Validate-Stack-Sequences/946. Validate Stack Sequences.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "fmt" 4 | 5 | func validateStackSequences(pushed []int, popped []int) bool { 6 | stack, j, N := []int{}, 0, len(pushed) 7 | for _, x := range pushed { 8 | stack = append(stack, x) 9 | fmt.Printf("stack = %v j = %v\n", stack, j) 10 | for len(stack) != 0 && j < N && stack[len(stack)-1] == popped[j] { 11 | stack = stack[0 : len(stack)-1] 12 | j++ 13 | } 14 | fmt.Printf("*****stack = %v j = %v\n", stack, j) 15 | } 16 | return j == N 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/1317.Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/1317. Convert Integer to the Sum of Two No-Zero Integers.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func getNoZeroIntegers(n int) []int { 4 | noZeroPair := []int{} 5 | for i := 1; i <= n/2; i++ { 6 | if isNoZero(i) && isNoZero(n-i) { 7 | noZeroPair = append(noZeroPair, []int{i, n - i}...) 8 | break 9 | } 10 | } 11 | return noZeroPair 12 | } 13 | 14 | func isNoZero(n int) bool { 15 | for n != 0 { 16 | if n%10 == 0 { 17 | return false 18 | } 19 | n /= 10 20 | } 21 | return true 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0497.Random-Point-in-Non-overlapping-Rectangles/497. Random Point in Non-overlapping Rectangles_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem497(t *testing.T) { 9 | w := [][]int{{1, 1, 5, 5}} 10 | sol := Constructor497(w) 11 | fmt.Printf("1.Pick = %v\n", sol.Pick()) 12 | fmt.Printf("2.Pick = %v\n", sol.Pick()) 13 | fmt.Printf("3.Pick = %v\n", sol.Pick()) 14 | fmt.Printf("4.Pick = %v\n", sol.Pick()) 15 | fmt.Printf("5.Pick = %v\n", sol.Pick()) 16 | fmt.Printf("6.Pick = %v\n", sol.Pick()) 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/0524.Longest-Word-in-Dictionary-through-Deleting/524. Longest Word in Dictionary through Deleting.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findLongestWord(s string, d []string) string { 4 | res := "" 5 | for i := 0; i < len(d); i++ { 6 | pointS := 0 7 | pointD := 0 8 | for pointS < len(s) && pointD < len(d[i]) { 9 | if s[pointS] == d[i][pointD] { 10 | pointD++ 11 | } 12 | pointS++ 13 | } 14 | if pointD == len(d[i]) && (len(res) < len(d[i]) || (len(res) == len(d[i]) && res > d[i])) { 15 | res = d[i] 16 | } 17 | } 18 | return res 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/1175.Prime-Arrangements/1175. Prime Arrangements.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "sort" 4 | 5 | var primes = []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97} 6 | 7 | func numPrimeArrangements(n int) int { 8 | primeCount := sort.Search(25, func(i int) bool { return primes[i] > n }) 9 | return factorial(primeCount) * factorial(n-primeCount) % 1000000007 10 | } 11 | 12 | func factorial(n int) int { 13 | if n == 1 || n == 0 { 14 | return 1 15 | } 16 | return n * factorial(n-1) % 1000000007 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/1184.Distance-Between-Bus-Stops/1184. Distance Between Bus Stops.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func distanceBetweenBusStops(distance []int, start int, destination int) int { 4 | clockwiseDis, counterclockwiseDis, n := 0, 0, len(distance) 5 | for i := start; i != destination; i = (i + 1) % n { 6 | clockwiseDis += distance[i] 7 | } 8 | for i := destination; i != start; i = (i + 1) % n { 9 | counterclockwiseDis += distance[i] 10 | } 11 | if clockwiseDis < counterclockwiseDis { 12 | return clockwiseDis 13 | } 14 | return counterclockwiseDis 15 | } 16 | -------------------------------------------------------------------------------- /leetcode/0447.Number-of-Boomerangs/447. Number of Boomerangs.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func numberOfBoomerangs(points [][]int) int { 4 | res := 0 5 | for i := 0; i < len(points); i++ { 6 | record := make(map[int]int, len(points)) 7 | for j := 0; j < len(points); j++ { 8 | if j != i { 9 | record[dis(points[i], points[j])]++ 10 | } 11 | } 12 | for _, r := range record { 13 | res += r * (r - 1) 14 | } 15 | } 16 | return res 17 | } 18 | 19 | func dis(pa, pb []int) int { 20 | return (pa[0]-pb[0])*(pa[0]-pb[0]) + (pa[1]-pb[1])*(pa[1]-pb[1]) 21 | } 22 | -------------------------------------------------------------------------------- /leetcode/0676.Implement-Magic-Dictionary/676. Implement Magic Dictionary_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem676(t *testing.T) { 9 | dict := []string{"hello", "leetcode"} 10 | obj := Constructor676() 11 | obj.BuildDict(dict) 12 | fmt.Printf("obj = %v\n", obj) 13 | fmt.Println(obj.Search("hello")) 14 | fmt.Println(obj.Search("apple")) 15 | fmt.Println(obj.Search("leetcode")) 16 | fmt.Println(obj.Search("leetcoded")) 17 | fmt.Println(obj.Search("hhllo")) 18 | fmt.Println(obj.Search("hell")) 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0844.Backspace-String-Compare/844. Backspace String Compare.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func backspaceCompare(S string, T string) bool { 4 | s := make([]rune, 0) 5 | for _, c := range S { 6 | if c == '#' { 7 | if len(s) > 0 { 8 | s = s[:len(s)-1] 9 | } 10 | } else { 11 | s = append(s, c) 12 | } 13 | } 14 | s2 := make([]rune, 0) 15 | for _, c := range T { 16 | if c == '#' { 17 | if len(s2) > 0 { 18 | s2 = s2[:len(s2)-1] 19 | } 20 | } else { 21 | s2 = append(s2, c) 22 | } 23 | } 24 | return string(s) == string(s2) 25 | } 26 | -------------------------------------------------------------------------------- /leetcode/0307.Range-Sum-Query---Mutable/307. Range Sum Query - Mutable_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem307(t *testing.T) { 9 | obj := Constructor307([]int{1, 3, 5}) 10 | fmt.Printf("obj = %v\n", obj) 11 | fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 2)) 12 | obj.Update(1, 2) 13 | fmt.Printf("obj = %v\n", obj) 14 | fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 2)) 15 | } 16 | 17 | // SumRange define 18 | func (ma *NumArray) SumRange(i int, j int) int { 19 | return ma.st.Query(i, j) 20 | } 21 | -------------------------------------------------------------------------------- /leetcode/0507.Perfect-Number/507. Perfect Number.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "math" 4 | 5 | // 方法一 6 | func checkPerfectNumber(num int) bool { 7 | if num <= 1 { 8 | return false 9 | } 10 | sum, bound := 1, int(math.Sqrt(float64(num)))+1 11 | for i := 2; i < bound; i++ { 12 | if num%i != 0 { 13 | continue 14 | } 15 | corrDiv := num / i 16 | sum += corrDiv + i 17 | } 18 | return sum == num 19 | } 20 | 21 | // 方法二 打表 22 | func checkPerfectNumber_(num int) bool { 23 | return num == 6 || num == 28 || num == 496 || num == 8128 || num == 33550336 24 | } 25 | -------------------------------------------------------------------------------- /leetcode/0528.Random-Pick-with-Weight/528. Random Pick with Weight_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem528(t *testing.T) { 9 | w := []int{1, 3} 10 | sol := Constructor528(w) 11 | fmt.Printf("1.PickIndex = %v\n", sol.PickIndex()) 12 | fmt.Printf("2.PickIndex = %v\n", sol.PickIndex()) 13 | fmt.Printf("3.PickIndex = %v\n", sol.PickIndex()) 14 | fmt.Printf("4.PickIndex = %v\n", sol.PickIndex()) 15 | fmt.Printf("5.PickIndex = %v\n", sol.PickIndex()) 16 | fmt.Printf("6.PickIndex = %v\n", sol.PickIndex()) 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/0713.Subarray-Product-Less-Than-K/713. Subarray Product Less Than K.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func numSubarrayProductLessThanK(nums []int, k int) int { 4 | if len(nums) == 0 { 5 | return 0 6 | } 7 | res, left, right, prod := 0, 0, 0, 1 8 | for left < len(nums) { 9 | if right < len(nums) && prod*nums[right] < k { 10 | prod = prod * nums[right] 11 | right++ 12 | } else if left == right { 13 | left++ 14 | right++ 15 | } else { 16 | res += right - left 17 | prod = prod / nums[left] 18 | left++ 19 | } 20 | } 21 | return res 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0765.Couples-Holding-Hands/765. Couples Holding Hands.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/template" 5 | ) 6 | 7 | func minSwapsCouples(row []int) int { 8 | if len(row)&1 == 1 { 9 | return 0 10 | } 11 | uf := template.UnionFind{} 12 | uf.Init(len(row)) 13 | for i := 0; i < len(row)-1; i = i + 2 { 14 | uf.Union(i, i+1) 15 | } 16 | for i := 0; i < len(row)-1; i = i + 2 { 17 | if uf.Find(row[i]) != uf.Find(row[i+1]) { 18 | uf.Union(row[i], row[i+1]) 19 | } 20 | } 21 | return len(row)/2 - uf.TotalCount() 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0232.Implement-Queue-using-Stacks/232. Implement Queue using Stacks_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem232(t *testing.T) { 9 | obj := Constructor232() 10 | fmt.Printf("obj = %v\n", obj) 11 | obj.Push(2) 12 | fmt.Printf("obj = %v\n", obj) 13 | obj.Push(10) 14 | fmt.Printf("obj = %v\n", obj) 15 | param2 := obj.Pop() 16 | fmt.Printf("param_2 = %v\n", param2) 17 | param3 := obj.Peek() 18 | fmt.Printf("param_3 = %v\n", param3) 19 | param4 := obj.Empty() 20 | fmt.Printf("param_4 = %v\n", param4) 21 | } 22 | -------------------------------------------------------------------------------- /structures/Queue_test.go: -------------------------------------------------------------------------------- 1 | package structures 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_Queue(t *testing.T) { 10 | ast := assert.New(t) 11 | 12 | q := NewQueue() 13 | ast.True(q.IsEmpty(), "检查新建的 q 是否为空") 14 | 15 | start, end := 0, 100 16 | 17 | for i := start; i < end; i++ { 18 | q.Push(i) 19 | ast.Equal(i-start+1, q.Len(), "Push 后检查 q 的长度。") 20 | } 21 | 22 | for i := start; i < end; i++ { 23 | ast.Equal(i, q.Pop(), "从 q 中 pop 出数来。") 24 | } 25 | 26 | ast.True(q.IsEmpty(), "检查 Pop 完毕后的 q 是否为空") 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/0476.Number-Complement/476. Number Complement.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | // 解法一 4 | func findComplement(num int) int { 5 | xx := ^0 // ^0 = 1111111111111111111111 6 | for xx&num > 0 { 7 | xx <<= 1 // 构造出来的 xx = 1111111…000000,0 的个数就是 num 的长度 8 | } 9 | return ^xx ^ num // xx ^ num,结果是前面的 0 全是 1 的num,再取反即是答案 10 | } 11 | 12 | // 解法二 13 | func findComplement1(num int) int { 14 | temp := 1 15 | for temp <= num { 16 | temp <<= 1 // 构造出来的 temp = 00000……10000,末尾 0 的个数是 num 的长度 17 | } 18 | return (temp - 1) ^ num // temp - 1 即是前面都是 0,num 长度的末尾都是 1 的数,再异或 num 即是最终结果 19 | } 20 | -------------------------------------------------------------------------------- /structures/Stack_test.go: -------------------------------------------------------------------------------- 1 | package structures 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_Stack(t *testing.T) { 10 | ast := assert.New(t) 11 | 12 | s := NewStack() 13 | ast.True(s.IsEmpty(), "检查新建的 s 是否为空") 14 | 15 | start, end := 0, 100 16 | 17 | for i := start; i < end; i++ { 18 | s.Push(i) 19 | ast.Equal(i-start+1, s.Len(), "Push 后检查 q 的长度。") 20 | } 21 | 22 | for i := end - 1; i >= start; i-- { 23 | ast.Equal(i, s.Pop(), "从 s 中 pop 出数来。") 24 | } 25 | 26 | ast.True(s.IsEmpty(), "检查 Pop 完毕后的 s 是否为空") 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/0345.Reverse-Vowels-of-a-String/README.md: -------------------------------------------------------------------------------- 1 | # [345. Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/) 2 | 3 | ## 题目 4 | 5 | Write a function that takes a string as input and reverse only the vowels of a string. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | ```c 12 | Input: "hello" 13 | Output: "holle" 14 | ``` 15 | 16 | Example 2: 17 | 18 | ```c 19 | Input: "leetcode" 20 | Output: "leotcede" 21 | ``` 22 | 23 | ## 题目大意 24 | 25 | 题目要求我们反转字符串中的元音字母。需要注意字母大小写。 26 | 27 | ## 解题思路 28 | 29 | 这一题的解题思路是用 2 个指针,指针对撞的思路,来不断交换首尾元素,即可。这一题和第 344 题思路一样。 30 | 31 | -------------------------------------------------------------------------------- /leetcode/0732.My-Calendar-III/732. My Calendar III_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem732(t *testing.T) { 9 | obj := Constructor732() 10 | fmt.Printf("book = %v\n\n", obj.Book(10, 20)) // returns 1 11 | fmt.Printf("book = %v\n\n", obj.Book(50, 60)) // returns 1 12 | fmt.Printf("book = %v\n\n", obj.Book(10, 40)) // returns 2 13 | fmt.Printf("book = %v\n\n", obj.Book(5, 15)) // returns 3 14 | fmt.Printf("book = %v\n\n", obj.Book(5, 10)) // returns 3 15 | fmt.Printf("book = %v\n\n", obj.Book(25, 55)) // returns 3 16 | } 17 | -------------------------------------------------------------------------------- /structures/NestedInterger_test.go: -------------------------------------------------------------------------------- 1 | package structures 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_NestedInteger(t *testing.T) { 10 | ast := assert.New(t) 11 | 12 | n := NestedInteger{} 13 | 14 | ast.True(n.IsInteger()) 15 | 16 | n.SetInteger(1) 17 | ast.Equal(1, n.GetInteger()) 18 | 19 | elem := NestedInteger{Num: 1} 20 | 21 | expected := NestedInteger{ 22 | Num: 1, 23 | Ns: []*NestedInteger{&elem}, 24 | } 25 | n.Add(elem) 26 | 27 | ast.Equal(expected, n) 28 | 29 | ast.Equal(expected.Ns, n.GetList()) 30 | } 31 | -------------------------------------------------------------------------------- /website/content/docs/shortcodes/details.md: -------------------------------------------------------------------------------- 1 | # Details 2 | 3 | Details shortcode is a helper for `details` html5 element. It is going to replace `expand` shortcode. 4 | 5 | ## Example 6 | ```tpl 7 | {{}} 8 | ## Markdown content 9 | Lorem markdownum insigne... 10 | {{}} 11 | ``` 12 | ```tpl 13 | {{}} 14 | ## Markdown content 15 | Lorem markdownum insigne... 16 | {{}} 17 | ``` 18 | 19 | {{< details "Title" open >}} 20 | ## Markdown content 21 | Lorem markdownum insigne... 22 | {{< /details >}} 23 | -------------------------------------------------------------------------------- /leetcode/0013.Roman-to-Integer/13. Roman to Integer.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | var roman = map[string]int{ 4 | "I": 1, 5 | "V": 5, 6 | "X": 10, 7 | "L": 50, 8 | "C": 100, 9 | "D": 500, 10 | "M": 1000, 11 | } 12 | 13 | func romanToInt(s string) int { 14 | if s == "" { 15 | return 0 16 | } 17 | num, lastint, total := 0, 0, 0 18 | for i := 0; i < len(s); i++ { 19 | char := s[len(s)-(i+1) : len(s)-i] 20 | num = roman[char] 21 | if num < lastint { 22 | total = total - num 23 | } else { 24 | total = total + num 25 | } 26 | lastint = num 27 | } 28 | return total 29 | } 30 | -------------------------------------------------------------------------------- /leetcode/0131.Palindrome-Partitioning/README.md: -------------------------------------------------------------------------------- 1 | # [131. Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/) 2 | 3 | 4 | ## 题目 5 | 6 | Given a string *s*, partition *s* such that every substring of the partition is a palindrome. 7 | 8 | Return all possible palindrome partitioning of *s*. 9 | 10 | **Example:** 11 | 12 | Input: "aab" 13 | Output: 14 | [ 15 | ["aa","b"], 16 | ["a","a","b"] 17 | ] 18 | 19 | ## 题目大意 20 | 21 | 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。返回 s 所有可能的分割方案。 22 | 23 | ## 解题思路 24 | 25 | - 要求输出一个字符串可以被拆成回文串的所有解,DFS 递归求解即可。 26 | -------------------------------------------------------------------------------- /leetcode/0322.Coin-Change/322. Coin Change.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func coinChange(coins []int, amount int) int { 4 | dp := make([]int, amount+1) 5 | dp[0] = 0 6 | for i := 1; i < len(dp); i++ { 7 | dp[i] = amount + 1 8 | } 9 | for i := 1; i <= amount; i++ { 10 | for j := 0; j < len(coins); j++ { 11 | if coins[j] <= i { 12 | dp[i] = min(dp[i], dp[i-coins[j]]+1) 13 | } 14 | } 15 | } 16 | if dp[amount] > amount { 17 | return -1 18 | } 19 | return dp[amount] 20 | } 21 | 22 | func min(a int, b int) int { 23 | if a > b { 24 | return b 25 | } 26 | return a 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/0977.Squares-of-a-Sorted-Array/977. Squares of a Sorted Array.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "sort" 4 | 5 | // 解法一 6 | func sortedSquares(A []int) []int { 7 | ans := make([]int, len(A)) 8 | for i, k, j := 0, len(A)-1, len(ans)-1; i <= j; k-- { 9 | if A[i]*A[i] > A[j]*A[j] { 10 | ans[k] = A[i] * A[i] 11 | i++ 12 | } else { 13 | ans[k] = A[j] * A[j] 14 | j-- 15 | } 16 | } 17 | return ans 18 | } 19 | 20 | // 解法二 21 | func sortedSquares1(A []int) []int { 22 | for i, value := range A { 23 | A[i] = value * value 24 | } 25 | sort.Ints(A) 26 | return A 27 | } 28 | -------------------------------------------------------------------------------- /website/themes/book/layouts/partials/docs/taxonomy.html: -------------------------------------------------------------------------------- 1 | 20 | -------------------------------------------------------------------------------- /leetcode/0082.Remove-Duplicates-from-Sorted-List-II/README.md: -------------------------------------------------------------------------------- 1 | # [82. Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/) 2 | 3 | ## 题目 4 | 5 | Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. 6 | 7 | Example 1: 8 | 9 | ``` 10 | Input: 1->2->3->3->4->4->5 11 | Output: 1->2->5 12 | ``` 13 | 14 | Example 2: 15 | 16 | ``` 17 | Input: 1->1->1->2->3 18 | Output: 2->3 19 | ``` 20 | 21 | ## 题目大意 22 | 23 | 删除链表中重复的结点,只要是有重复过的结点,全部删除。 24 | 25 | ## 解题思路 26 | 27 | 按照题意做即可。 -------------------------------------------------------------------------------- /leetcode/0206.Reverse-Linked-List/206. Reverse Linked List.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/structures" 5 | ) 6 | 7 | // ListNode define 8 | type ListNode = structures.ListNode 9 | 10 | /** 11 | * Definition for singly-linked list. 12 | * type ListNode struct { 13 | * Val int 14 | * Next *ListNode 15 | * } 16 | */ 17 | 18 | func reverseList(head *ListNode) *ListNode { 19 | var behind *ListNode 20 | for head != nil { 21 | next := head.Next 22 | head.Next = behind 23 | behind = head 24 | head = next 25 | } 26 | return behind 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/0995.Minimum-Number-of-K-Consecutive-Bit-Flips/995. Minimum Number of K Consecutive Bit Flips.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func minKBitFlips(A []int, K int) int { 4 | flippedTime, count := 0, 0 5 | for i := 0; i < len(A); i++ { 6 | if i >= K && A[i-K] == 2 { 7 | flippedTime-- 8 | } 9 | // 下面这个判断包含了两种情况: 10 | // 如果 flippedTime 是奇数,且 A[i] == 1 就需要翻转 11 | // 如果 flippedTime 是偶数,且 A[i] == 0 就需要翻转 12 | if flippedTime%2 == A[i] { 13 | if i+K > len(A) { 14 | return -1 15 | } 16 | A[i] = 2 17 | flippedTime++ 18 | count++ 19 | } 20 | } 21 | return count 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/1290.Convert-Binary-Number-in-a-Linked-List-to-Integer/1290. Convert Binary Number in a Linked List to Integer.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/structures" 5 | ) 6 | 7 | // ListNode define 8 | type ListNode = structures.ListNode 9 | 10 | /** 11 | * Definition for singly-linked list. 12 | * type ListNode struct { 13 | * Val int 14 | * Next *ListNode 15 | * } 16 | */ 17 | func getDecimalValue(head *ListNode) int { 18 | sum := 0 19 | for head != nil { 20 | sum = sum*2 + head.Val 21 | head = head.Next 22 | } 23 | return sum 24 | } 25 | -------------------------------------------------------------------------------- /website/themes/book/layouts/shortcodes/tabs.html: -------------------------------------------------------------------------------- 1 | {{ if .Inner }}{{ end }} 2 | {{ $id := .Get 0 }} 3 | {{ $group := printf "tabs-%s" $id }} 4 | 5 |
6 | {{- range $index, $tab := .Scratch.Get $group -}} 7 | 8 | 11 |
12 | {{- .Content | markdownify -}} 13 |
14 | {{- end -}} 15 |
16 | -------------------------------------------------------------------------------- /leetcode/0042.Trapping-Rain-Water/42. Trapping Rain Water.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func trap(height []int) int { 4 | res, left, right, maxLeft, maxRight := 0, 0, len(height)-1, 0, 0 5 | for left <= right { 6 | if height[left] <= height[right] { 7 | if height[left] > maxLeft { 8 | maxLeft = height[left] 9 | } else { 10 | res += maxLeft - height[left] 11 | } 12 | left++ 13 | } else { 14 | if height[right] >= maxRight { 15 | maxRight = height[right] 16 | } else { 17 | res += maxRight - height[right] 18 | } 19 | right-- 20 | } 21 | } 22 | return res 23 | } 24 | -------------------------------------------------------------------------------- /leetcode/0771.Jewels-and-Stones/771. Jewels and Stones.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strings" 4 | 5 | // 解法一 6 | func numJewelsInStones(J string, S string) int { 7 | count := 0 8 | for i := range S { 9 | if strings.Contains(J, string(S[i])) { 10 | count++ 11 | } 12 | } 13 | return count 14 | } 15 | 16 | // 解法二 17 | func numJewelsInStones1(J string, S string) int { 18 | cache, result := make(map[rune]bool), 0 19 | for _, r := range J { 20 | cache[r] = true 21 | } 22 | for _, r := range S { 23 | if _, ok := cache[r]; ok { 24 | result++ 25 | } 26 | } 27 | return result 28 | } 29 | -------------------------------------------------------------------------------- /leetcode/1200.Minimum-Absolute-Difference/1200. Minimum Absolute Difference.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "math" 5 | "sort" 6 | ) 7 | 8 | func minimumAbsDifference(arr []int) [][]int { 9 | minDiff, res := math.MaxInt32, [][]int{} 10 | sort.Ints(arr) 11 | for i := 1; i < len(arr); i++ { 12 | if arr[i]-arr[i-1] < minDiff { 13 | minDiff = arr[i] - arr[i-1] 14 | } 15 | if minDiff == 1 { 16 | break 17 | } 18 | } 19 | for i := 1; i < len(arr); i++ { 20 | if arr[i]-arr[i-1] == minDiff { 21 | res = append(res, []int{arr[i-1], arr[i]}) 22 | } 23 | } 24 | return res 25 | } 26 | -------------------------------------------------------------------------------- /structures/Point.go: -------------------------------------------------------------------------------- 1 | package structures 2 | 3 | // Point 定义了一个二维坐标点 4 | type Point struct { 5 | X, Y int 6 | } 7 | 8 | // Intss2Points 把 [][]int 转换成 []Point 9 | func Intss2Points(points [][]int) []Point { 10 | res := make([]Point, len(points)) 11 | for i, p := range points { 12 | res[i] = Point{ 13 | X: p[0], 14 | Y: p[1], 15 | } 16 | } 17 | return res 18 | } 19 | 20 | // Points2Intss 把 []Point 转换成 [][]int 21 | func Points2Intss(points []Point) [][]int { 22 | res := make([][]int, len(points)) 23 | for i, p := range points { 24 | res[i] = []int{p.X, p.Y} 25 | } 26 | return res 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/0424.Longest-Repeating-Character-Replacement/424. Longest Repeating Character Replacement.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func characterReplacement(s string, k int) int { 4 | res, left, counter, freq := 0, 0, 0, make([]int, 26) 5 | for right := 0; right < len(s); right++ { 6 | freq[s[right]-'A']++ 7 | counter = max(counter, freq[s[right]-'A']) 8 | for right-left+1-counter > k { 9 | freq[s[left]-'A']-- 10 | left++ 11 | } 12 | res = max(res, right-left+1) 13 | } 14 | return res 15 | } 16 | 17 | func max(a int, b int) int { 18 | if a > b { 19 | return a 20 | } 21 | return b 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0463.Island-Perimeter/463. Island Perimeter.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func islandPerimeter(grid [][]int) int { 4 | counter := 0 5 | for i := 0; i < len(grid); i++ { 6 | for j := 0; j < len(grid[0]); j++ { 7 | if grid[i][j] == 1 { 8 | if i-1 < 0 || grid[i-1][j] == 0 { 9 | counter++ 10 | } 11 | if i+1 >= len(grid) || grid[i+1][j] == 0 { 12 | counter++ 13 | } 14 | if j-1 < 0 || grid[i][j-1] == 0 { 15 | counter++ 16 | } 17 | if j+1 >= len(grid[0]) || grid[i][j+1] == 0 { 18 | counter++ 19 | } 20 | } 21 | } 22 | } 23 | return counter 24 | } 25 | -------------------------------------------------------------------------------- /leetcode/0537.Complex-Number-Multiplication/537. Complex Number Multiplication.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "strconv" 5 | "strings" 6 | ) 7 | 8 | func complexNumberMultiply(a string, b string) string { 9 | realA, imagA := parse(a) 10 | realB, imagB := parse(b) 11 | real := realA*realB - imagA*imagB 12 | imag := realA*imagB + realB*imagA 13 | return strconv.Itoa(real) + "+" + strconv.Itoa(imag) + "i" 14 | } 15 | 16 | func parse(s string) (int, int) { 17 | ss := strings.Split(s, "+") 18 | r, _ := strconv.Atoi(ss[0]) 19 | i, _ := strconv.Atoi(ss[1][:len(ss[1])-1]) 20 | return r, i 21 | } 22 | -------------------------------------------------------------------------------- /website/themes/book/layouts/posts/list.html: -------------------------------------------------------------------------------- 1 | {{ define "main" }} 2 | {{ range sort .Paginator.Pages }} 3 |
4 |

5 | {{ .Title }} 6 |

7 | {{ partial "docs/post-meta" . }} 8 |

9 | {{- .Summary -}} 10 | {{ if .Truncated }} 11 | ... 12 | {{ end }} 13 |

14 |
15 | {{ end }} 16 | 17 | {{ template "_internal/pagination.html" . }} 18 | {{ end }} 19 | 20 | {{ define "toc" }} 21 | {{ partial "docs/taxonomy" . }} 22 | {{ end }} 23 | -------------------------------------------------------------------------------- /leetcode/0144.Binary-Tree-Preorder-Traversal/README.md: -------------------------------------------------------------------------------- 1 | # [144. Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) 2 | 3 | ## 题目 4 | 5 | Given a binary tree, return the preorder traversal of its nodes' values. 6 | 7 | 8 | 9 | Example : 10 | 11 | ```c 12 | Input: [1,null,2,3] 13 | 1 14 | \ 15 | 2 16 | / 17 | 3 18 | 19 | Output: [1,2,3] 20 | ``` 21 | 22 | 23 | Follow up: Recursive solution is trivial, could you do it iteratively? 24 | 25 | 26 | 27 | 28 | ## 题目大意 29 | 30 | 先根遍历一颗树。 31 | 32 | ## 解题思路 33 | 34 | 两种递归的实现方法,见代码。 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /leetcode/0684.Redundant-Connection/684. Redundant Connection.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/template" 5 | ) 6 | 7 | func findRedundantConnection(edges [][]int) []int { 8 | if len(edges) == 0 { 9 | return []int{} 10 | } 11 | uf, res := template.UnionFind{}, []int{} 12 | uf.Init(len(edges) + 1) 13 | for i := 0; i < len(edges); i++ { 14 | if uf.Find(edges[i][0]) != uf.Find(edges[i][1]) { 15 | uf.Union(edges[i][0], edges[i][1]) 16 | } else { 17 | res = append(res, edges[i][0]) 18 | res = append(res, edges[i][1]) 19 | } 20 | } 21 | return res 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0885.Spiral-Matrix-III/885. Spiral Matrix III.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func spiralMatrixIII(R int, C int, r0 int, c0 int) [][]int { 4 | res, round, spDir := [][]int{}, 0, [][]int{ 5 | {0, 1}, // 朝右 6 | {1, 0}, // 朝下 7 | {0, -1}, // 朝左 8 | {-1, 0}, // 朝上 9 | } 10 | res = append(res, []int{r0, c0}) 11 | for i := 0; len(res) < R*C; i++ { 12 | for j := 0; j < i/2+1; j++ { 13 | r0 += spDir[round%4][0] 14 | c0 += spDir[round%4][1] 15 | if 0 <= r0 && r0 < R && 0 <= c0 && c0 < C { 16 | res = append(res, []int{r0, c0}) 17 | } 18 | } 19 | round++ 20 | } 21 | return res 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/1189.Maximum-Number-of-Balloons/1189. Maximum Number of Balloons.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func maxNumberOfBalloons(text string) int { 4 | fre := make([]int, 26) 5 | for _, t := range text { 6 | fre[t-'a']++ 7 | } 8 | // 字符 b 的频次是数组下标 1 对应的元素值 9 | // 字符 a 的频次是数组下标 0 对应的元素值 10 | // 字符 l 的频次是数组下标 11 对应的元素值,这里有 2 个 l,所以元素值需要除以 2 11 | // 字符 o 的频次是数组下标 14 对应的元素值,这里有 2 个 o,所以元素值需要除以 2 12 | // 字符 n 的频次是数组下标 13 对应的元素值 13 | return min(fre[1], min(fre[0], min(fre[11]/2, min(fre[14]/2, fre[13])))) 14 | } 15 | 16 | func min(a int, b int) int { 17 | if a > b { 18 | return b 19 | } 20 | return a 21 | } 22 | -------------------------------------------------------------------------------- /website/themes/book/exampleSite/content/docs/shortcodes/details.md: -------------------------------------------------------------------------------- 1 | # Details 2 | 3 | Details shortcode is a helper for `details` html5 element. It is going to replace `expand` shortcode. 4 | 5 | ## Example 6 | ```tpl 7 | {{}} 8 | ## Markdown content 9 | Lorem markdownum insigne... 10 | {{}} 11 | ``` 12 | ```tpl 13 | {{}} 14 | ## Markdown content 15 | Lorem markdownum insigne... 16 | {{}} 17 | ``` 18 | 19 | {{< details "Title" open >}} 20 | ## Markdown content 21 | Lorem markdownum insigne... 22 | {{< /details >}} 23 | -------------------------------------------------------------------------------- /leetcode/0658.Find-K-Closest-Elements/658. Find K Closest Elements.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "sort" 4 | 5 | // 解法一 库函数二分搜索 6 | func findClosestElements(arr []int, k int, x int) []int { 7 | return arr[sort.Search(len(arr)-k, func(i int) bool { return x-arr[i] <= arr[i+k]-x }):][:k] 8 | } 9 | 10 | // 解法二 手撸二分搜索 11 | func findClosestElements1(arr []int, k int, x int) []int { 12 | low, high := 0, len(arr)-k 13 | for low < high { 14 | mid := low + (high-low)>>1 15 | if x-arr[mid] > arr[mid+k]-x { 16 | low = mid + 1 17 | } else { 18 | high = mid 19 | } 20 | } 21 | return arr[low : low+k] 22 | } 23 | -------------------------------------------------------------------------------- /website/themes/book/layouts/taxonomy/taxonomy.html: -------------------------------------------------------------------------------- 1 | {{ define "main" }} 2 | {{ range sort .Paginator.Pages }} 3 |
4 |

5 | {{ .Title }} 6 |

7 | {{ partial "docs/post-meta" . }} 8 |

9 | {{- .Summary -}} 10 | {{ if .Truncated }} 11 | ... 12 | {{ end }} 13 |

14 |
15 | {{ end }} 16 | 17 | {{ template "_internal/pagination.html" . }} 18 | {{ end }} 19 | 20 | {{ define "toc" }} 21 | {{ partial "docs/taxonomy" . }} 22 | {{ end }} 23 | -------------------------------------------------------------------------------- /leetcode/0094.Binary-Tree-Inorder-Traversal/README.md: -------------------------------------------------------------------------------- 1 | # [94. Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) 2 | 3 | ## 题目 4 | 5 | 6 | Given a binary tree, return the inorder traversal of its nodes' values. 7 | 8 | 9 | 10 | Example : 11 | 12 | ```c 13 | Input: [1,null,2,3] 14 | 1 15 | \ 16 | 2 17 | / 18 | 3 19 | 20 | Output: [1,3,2] 21 | ``` 22 | 23 | 24 | Follow up: Recursive solution is trivial, could you do it iteratively? 25 | 26 | 27 | 28 | 29 | 30 | 31 | ## 题目大意 32 | 33 | 中根遍历一颗树。 34 | 35 | ## 解题思路 36 | 37 | 递归的实现方法,见代码。 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /leetcode/0145.Binary-Tree-Postorder-Traversal/README.md: -------------------------------------------------------------------------------- 1 | # [145. Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/) 2 | 3 | ## 题目 4 | 5 | 6 | Given a binary tree, return the postorder traversal of its nodes' values. 7 | 8 | 9 | 10 | Example : 11 | 12 | ```c 13 | Input: [1,null,2,3] 14 | 1 15 | \ 16 | 2 17 | / 18 | 3 19 | 20 | Output: [3,2,1] 21 | ``` 22 | 23 | 24 | Follow up: Recursive solution is trivial, could you do it iteratively? 25 | 26 | 27 | 28 | 29 | ## 题目大意 30 | 31 | 后根遍历一颗树。 32 | 33 | ## 解题思路 34 | 35 | 递归的实现方法,见代码。 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /leetcode/0257.Binary-Tree-Paths/README.md: -------------------------------------------------------------------------------- 1 | # [257. Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/) 2 | 3 | 4 | ## 题目 5 | 6 | Given a binary tree, return all root-to-leaf paths. 7 | 8 | **Note:** A leaf is a node with no children. 9 | 10 | **Example:** 11 | 12 | Input: 13 | 14 | 1 15 | / \ 16 | 2 3 17 | \ 18 | 5 19 | 20 | Output: ["1->2->5", "1->3"] 21 | 22 | Explanation: All root-to-leaf paths are: 1->2->5, 1->3 23 | 24 | ## 题目大意 25 | 26 | 给定一个二叉树,返回所有从根节点到叶子节点的路径。说明: 叶子节点是指没有子节点的节点。 27 | 28 | ## 解题思路 29 | 30 | - Google 的面试题,考察递归 31 | 32 | -------------------------------------------------------------------------------- /structures/Heap.go: -------------------------------------------------------------------------------- 1 | package structures 2 | 3 | // intHeap 实现了 heap 的接口 4 | type intHeap []int 5 | 6 | func (h intHeap) Len() int { 7 | return len(h) 8 | } 9 | 10 | func (h intHeap) Less(i, j int) bool { 11 | return h[i] < h[j] 12 | } 13 | 14 | func (h intHeap) Swap(i, j int) { 15 | h[i], h[j] = h[j], h[i] 16 | } 17 | 18 | func (h *intHeap) Push(x interface{}) { 19 | // Push 使用 *h,是因为 20 | // Push 增加了 h 的长度 21 | *h = append(*h, x.(int)) 22 | } 23 | 24 | func (h *intHeap) Pop() interface{} { 25 | // Pop 使用 *h ,是因为 26 | // Pop 减短了 h 的长度 27 | res := (*h)[len(*h)-1] 28 | *h = (*h)[:len(*h)-1] 29 | return res 30 | } 31 | -------------------------------------------------------------------------------- /leetcode/0047.Permutations-II/README.md: -------------------------------------------------------------------------------- 1 | # [47. Permutations II](https://leetcode.com/problems/permutations-ii/) 2 | 3 | 4 | ## 题目 5 | 6 | Given a collection of numbers that might contain duplicates, return all possible unique permutations. 7 | 8 | **Example:** 9 | 10 | 11 | Input: [1,1,2] 12 | Output: 13 | [ 14 | [1,1,2], 15 | [1,2,1], 16 | [2,1,1] 17 | ] 18 | 19 | 20 | ## 题目大意 21 | 22 | 给定一个可包含重复数字的序列,返回所有不重复的全排列。 23 | 24 | ## 解题思路 25 | 26 | - 这一题是第 46 题的加强版,第 46 题中求数组的排列,数组中元素不重复,但是这一题中,数组元素会重复,所以需要最终排列出来的结果需要去重。 27 | - 去重的方法是经典逻辑,将数组排序以后,判断重复元素再做逻辑判断。 28 | - 其他思路和第 46 题完全一致,DFS 深搜即可。 29 | -------------------------------------------------------------------------------- /leetcode/0059.Spiral-Matrix-II/README.md: -------------------------------------------------------------------------------- 1 | # [59. Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/) 2 | 3 | 4 | ## 题目 5 | 6 | Given a positive integer *n*, generate a square matrix filled with elements from 1 to *n*2 in spiral order. 7 | 8 | **Example:** 9 | 10 | 11 | Input: 3 12 | Output: 13 | [ 14 | [ 1, 2, 3 ], 15 | [ 8, 9, 4 ], 16 | [ 7, 6, 5 ] 17 | ] 18 | 19 | 20 | ## 题目大意 21 | 22 | 给定一个正整数 n,生成一个包含 1 到 n^2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。 23 | 24 | 25 | ## 解题思路 26 | 27 | - 给出一个数组 n,要求输出一个 n * n 的二维数组,里面元素是 1 - n*n,且数组排列顺序是螺旋排列的 28 | - 这一题是第 54 题的加强版,没有需要注意的特殊情况,直接模拟即可。 29 | 30 | -------------------------------------------------------------------------------- /leetcode/0091.Decode-Ways/91. Decode Ways.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "strconv" 5 | ) 6 | 7 | func numDecodings(s string) int { 8 | if len(s) == 0 { 9 | return 0 10 | } 11 | dp := make([]int, len(s)+1) 12 | dp[0] = 1 13 | if s[:1] == "0" { 14 | dp[1] = 0 15 | } else { 16 | dp[1] = 1 17 | } 18 | for i := 2; i <= len(s); i++ { 19 | lastNum, _ := strconv.Atoi(s[i-1 : i]) 20 | if lastNum >= 1 && lastNum <= 9 { 21 | dp[i] += dp[i-1] 22 | } 23 | lastNum, _ = strconv.Atoi(s[i-2 : i]) 24 | if lastNum >= 10 && lastNum <= 26 { 25 | dp[i] += dp[i-2] 26 | } 27 | } 28 | return dp[len(s)] 29 | } 30 | -------------------------------------------------------------------------------- /leetcode/0226.Invert-Binary-Tree/226. Invert Binary Tree.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/structures" 5 | ) 6 | 7 | // TreeNode define 8 | type TreeNode = structures.TreeNode 9 | 10 | /** 11 | * Definition for a binary tree node. 12 | * type TreeNode struct { 13 | * Val int 14 | * Left *TreeNode 15 | * Right *TreeNode 16 | * } 17 | */ 18 | 19 | func invertTree(root *TreeNode) *TreeNode { 20 | if root == nil { 21 | return nil 22 | } 23 | invertTree(root.Left) 24 | invertTree(root.Right) 25 | root.Left, root.Right = root.Right, root.Left 26 | return root 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/0541.Reverse-String-II/541. Reverse String II.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func reverseStr(s string, k int) string { 4 | if k > len(s) { 5 | k = len(s) 6 | } 7 | for i := 0; i < len(s); i = i + 2*k { 8 | if len(s)-i >= k { 9 | ss := revers(s[i : i+k]) 10 | s = s[:i] + ss + s[i+k:] 11 | } else { 12 | ss := revers(s[i:]) 13 | s = s[:i] + ss 14 | } 15 | } 16 | return s 17 | } 18 | 19 | func revers(s string) string { 20 | bytes := []byte(s) 21 | i, j := 0, len(bytes)-1 22 | for i < j { 23 | bytes[i], bytes[j] = bytes[j], bytes[i] 24 | i++ 25 | j-- 26 | } 27 | return string(bytes) 28 | } 29 | -------------------------------------------------------------------------------- /leetcode/0668.Kth-Smallest-Number-in-Multiplication-Table/668. Kth Smallest Number in Multiplication Table.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "math" 4 | 5 | func findKthNumber(m int, n int, k int) int { 6 | low, high := 1, m*n 7 | for low < high { 8 | mid := low + (high-low)>>1 9 | if counterKthNum(m, n, mid) >= k { 10 | high = mid 11 | } else { 12 | low = mid + 1 13 | } 14 | } 15 | return low 16 | } 17 | 18 | func counterKthNum(m, n, mid int) int { 19 | count := 0 20 | for i := 1; i <= m; i++ { 21 | count += int(math.Min(math.Floor(float64(mid)/float64(i)), float64(n))) 22 | } 23 | return count 24 | } 25 | -------------------------------------------------------------------------------- /leetcode/0896.Monotonic-Array/896. Monotonic Array.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isMonotonic(A []int) bool { 4 | if len(A) <= 1 { 5 | return true 6 | } 7 | if A[0] < A[1] { 8 | return inc(A[1:]) 9 | } 10 | if A[0] > A[1] { 11 | return dec(A[1:]) 12 | } 13 | return inc(A[1:]) || dec(A[1:]) 14 | } 15 | 16 | func inc(A []int) bool { 17 | for i := 0; i < len(A)-1; i++ { 18 | if A[i] > A[i+1] { 19 | return false 20 | } 21 | } 22 | return true 23 | } 24 | 25 | func dec(A []int) bool { 26 | for i := 0; i < len(A)-1; i++ { 27 | if A[i] < A[i+1] { 28 | return false 29 | } 30 | } 31 | return true 32 | } 33 | -------------------------------------------------------------------------------- /leetcode/9990377.Combination-Sum-IV/377. Combination Sum IV.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func combinationSum4(nums []int, target int) int { 4 | if len(nums) == 0 { 5 | return 0 6 | } 7 | c, res := []int{}, 0 8 | findcombinationSum4(nums, target, 0, c, &res) 9 | return res 10 | } 11 | 12 | func findcombinationSum4(nums []int, target, index int, c []int, res *int) { 13 | if target <= 0 { 14 | if target == 0 { 15 | *res++ 16 | } 17 | return 18 | } 19 | for i := 0; i < len(nums); i++ { 20 | c = append(c, nums[i]) 21 | findcombinationSum4(nums, target-nums[i], i, c, res) 22 | c = c[:len(c)-1] 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /leetcode/0125.Valid-Palindrome/125. Valid Palindrome.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "strings" 5 | ) 6 | 7 | func isPalindrome(s string) bool { 8 | 9 | s = strings.ToLower(s) 10 | 11 | i, j := 0, len(s)-1 12 | for i < j { 13 | for i < j && !isChar(s[i]) { 14 | i++ 15 | } 16 | for i < j && !isChar(s[j]) { 17 | j-- 18 | } 19 | if s[i] != s[j] { 20 | return false 21 | } 22 | i++ 23 | j-- 24 | } 25 | 26 | return true 27 | } 28 | 29 | // 判断 c 是否是字符或者数字 30 | func isChar(c byte) bool { 31 | if ('a' <= c && c <= 'z') || ('0' <= c && c <= '9') { 32 | return true 33 | } 34 | return false 35 | } 36 | -------------------------------------------------------------------------------- /leetcode/0237.Delete-Node-in-a-Linked-List/237. Delete Node in a Linked List.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/structures" 5 | ) 6 | 7 | // ListNode define 8 | type ListNode = structures.ListNode 9 | 10 | /** 11 | * Definition for singly-linked list. 12 | * type ListNode struct { 13 | * Val int 14 | * Next *ListNode 15 | * } 16 | */ 17 | func deleteNode(node *ListNode) { 18 | if node == nil { 19 | return 20 | } 21 | cur := node 22 | for cur.Next.Next != nil { 23 | cur.Val = cur.Next.Val 24 | cur = cur.Next 25 | } 26 | cur.Val = cur.Next.Val 27 | cur.Next = nil 28 | } 29 | -------------------------------------------------------------------------------- /leetcode/0500.Keyboard-Row/500. Keyboard Row.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strings" 4 | 5 | func findWords500(words []string) []string { 6 | rows := []string{"qwertyuiop", "asdfghjkl", "zxcvbnm"} 7 | output := make([]string, 0) 8 | for _, s := range words { 9 | if len(s) == 0 { 10 | continue 11 | } 12 | lowerS := strings.ToLower(s) 13 | oneRow := false 14 | for _, r := range rows { 15 | if strings.ContainsAny(lowerS, r) { 16 | oneRow = !oneRow 17 | if !oneRow { 18 | break 19 | } 20 | } 21 | } 22 | if oneRow { 23 | output = append(output, s) 24 | } 25 | } 26 | return output 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/0405.Convert-a-Number-to-Hexadecimal/405. Convert a Number to Hexadecimal.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func toHex(num int) string { 4 | if num == 0 { 5 | return "0" 6 | } 7 | if num < 0 { 8 | num += 1 << 32 9 | } 10 | mp := map[int]string{ 11 | 0: "0", 1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9", 12 | 10: "a", 11: "b", 12: "c", 13: "d", 14: "e", 15: "f", 13 | } 14 | var bitArr []string 15 | for num > 0 { 16 | bitArr = append(bitArr, mp[num%16]) 17 | num /= 16 18 | } 19 | str := "" 20 | for i := len(bitArr) - 1; i >= 0; i-- { 21 | str += bitArr[i] 22 | } 23 | return str 24 | } 25 | -------------------------------------------------------------------------------- /leetcode/0223.Rectangle-Area/223. Rectangle Area.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func computeArea(A int, B int, C int, D int, E int, F int, G int, H int) int { 4 | X0, Y0, X1, Y1 := max(A, E), max(B, F), min(C, G), min(D, H) 5 | return area(A, B, C, D) + area(E, F, G, H) - area(X0, Y0, X1, Y1) 6 | } 7 | 8 | func area(x0, y0, x1, y1 int) int { 9 | l, h := x1-x0, y1-y0 10 | if l <= 0 || h <= 0 { 11 | return 0 12 | } 13 | return l * h 14 | } 15 | 16 | func max(a int, b int) int { 17 | if a > b { 18 | return a 19 | } 20 | return b 21 | } 22 | 23 | func min(a int, b int) int { 24 | if a > b { 25 | return b 26 | } 27 | return a 28 | } 29 | -------------------------------------------------------------------------------- /leetcode/0515.Find-Largest-Value-in-Each-Tree-Row/README.md: -------------------------------------------------------------------------------- 1 | # [515. Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/) 2 | 3 | 4 | ## 题目 5 | 6 | You need to find the largest value in each row of a binary tree. 7 | 8 | **Example:** 9 | 10 | Input: 11 | 12 | 1 13 | / \ 14 | 3 2 15 | / \ \ 16 | 5 3 9 17 | 18 | Output: [1, 3, 9] 19 | 20 | 21 | ## 题目大意 22 | 23 | 求在二叉树的每一行中找到最大的值。 24 | 25 | 26 | ## 解题思路 27 | 28 | 29 | - 给出一个二叉树,要求依次输出每行的最大值 30 | - 用 BFS 层序遍历,将每层排序取出最大值。改进的做法是遍历中不断更新每层的最大值。 31 | 32 | -------------------------------------------------------------------------------- /leetcode/0878.Nth-Magical-Number/878. Nth Magical Number.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func nthMagicalNumber(N int, A int, B int) int { 4 | low, high := int64(0), int64(1*1e14) 5 | for low < high { 6 | mid := low + (high-low)>>1 7 | if calNthMagicalCount(mid, int64(A), int64(B)) < int64(N) { 8 | low = mid + 1 9 | } else { 10 | high = mid 11 | } 12 | } 13 | return int(low) % 1000000007 14 | } 15 | 16 | func calNthMagicalCount(num, a, b int64) int64 { 17 | ab := a * b / gcd(a, b) 18 | return num/a + num/b - num/ab 19 | } 20 | 21 | func gcd(a, b int64) int64 { 22 | for b != 0 { 23 | a, b = b, a%b 24 | } 25 | return a 26 | } 27 | -------------------------------------------------------------------------------- /leetcode/0925.Long-Pressed-Name/925. Long Pressed Name.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isLongPressedName(name string, typed string) bool { 4 | if len(name) == 0 && len(typed) == 0 { 5 | return true 6 | } 7 | if (len(name) == 0 && len(typed) != 0) || (len(name) != 0 && len(typed) == 0) { 8 | return false 9 | } 10 | 11 | j := 0 12 | for i := 0; i < len(name); i++ { 13 | if j < len(typed) && name[i] == typed[j] { 14 | j++ 15 | continue 16 | } else { 17 | if i > 0 && j < len(typed) && name[i-1] == typed[j] { 18 | j++ 19 | i-- 20 | } else { 21 | return false 22 | } 23 | } 24 | } 25 | return true 26 | } 27 | -------------------------------------------------------------------------------- /leetcode/1004.Max-Consecutive-Ones-III/1004. Max Consecutive Ones III.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func longestOnes(A []int, K int) int { 4 | res, left, right := 0, 0, 0 5 | for left < len(A) { 6 | if right < len(A) && ((A[right] == 0 && K > 0) || A[right] == 1) { 7 | if A[right] == 0 { 8 | K-- 9 | } 10 | right++ 11 | } else { 12 | if K == 0 || (right == len(A) && K > 0) { 13 | res = max(res, right-left) 14 | } 15 | if A[left] == 0 { 16 | K++ 17 | } 18 | left++ 19 | } 20 | } 21 | return res 22 | } 23 | 24 | func max(a int, b int) int { 25 | if a > b { 26 | return a 27 | } 28 | return b 29 | } 30 | -------------------------------------------------------------------------------- /structures/Heap_test.go: -------------------------------------------------------------------------------- 1 | package structures 2 | 3 | import ( 4 | "container/heap" 5 | "fmt" 6 | "testing" 7 | 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_intHeap(t *testing.T) { 12 | ast := assert.New(t) 13 | 14 | ih := new(intHeap) 15 | heap.Init(ih) 16 | 17 | heap.Push(ih, 1) 18 | heap.Pop(ih) 19 | 20 | begin, end := 0, 10 21 | for i := begin; i < end; i++ { 22 | heap.Push(ih, i) 23 | ast.Equal(0, (*ih)[0], "插入 %d 后的最小值却是 %d,ih=%v", i, (*ih)[0], (*ih)) 24 | } 25 | 26 | for i := begin; i < end; i++ { 27 | fmt.Println(i, *ih) 28 | ast.Equal(i, heap.Pop(ih), "Pop 后 ih=%v", (*ih)) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /website/themes/book/theme.toml: -------------------------------------------------------------------------------- 1 | # theme.toml template for a Hugo theme 2 | # See https://github.com/gohugoio/hugoThemes#themetoml for an example 3 | 4 | name = "Book" 5 | license = "MIT" 6 | licenselink = "https://github.com/alex-shpak/hugo-book/blob/master/LICENSE" 7 | description = "Hugo documentation theme as simple as plain book" 8 | homepage = "https://github.com/alex-shpak/hugo-book" 9 | tags = ["responsive", "clean", "documentation", "docs", "flexbox", "search", "mobile", "multilingual", "disqus"] 10 | features = [] 11 | min_version = "0.68" 12 | 13 | [author] 14 | name = "Alex Shpak" 15 | homepage = "https://github.com/alex-shpak/" 16 | -------------------------------------------------------------------------------- /leetcode/0225.Implement-Stack-using-Queues/225. Implement Stack using Queues_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem225(t *testing.T) { 9 | obj := Constructor225() 10 | fmt.Printf("obj = %v\n", obj) 11 | param5 := obj.Empty() 12 | fmt.Printf("param_5 = %v\n", param5) 13 | obj.Push(2) 14 | fmt.Printf("obj = %v\n", obj) 15 | obj.Push(10) 16 | fmt.Printf("obj = %v\n", obj) 17 | param2 := obj.Pop() 18 | fmt.Printf("param_2 = %v\n", param2) 19 | param3 := obj.Top() 20 | fmt.Printf("param_3 = %v\n", param3) 21 | param4 := obj.Empty() 22 | fmt.Printf("param_4 = %v\n", param4) 23 | } 24 | -------------------------------------------------------------------------------- /leetcode/1380.Lucky-Numbers-in-a-Matrix/1380. Lucky Numbers in a Matrix.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func luckyNumbers(matrix [][]int) []int { 4 | t, r, res := make([]int, len(matrix[0])), make([]int, len(matrix[0])), []int{} 5 | for _, val := range matrix { 6 | m, k := val[0], 0 7 | for j := 0; j < len(matrix[0]); j++ { 8 | if val[j] < m { 9 | m = val[j] 10 | k = j 11 | } 12 | if t[j] < val[j] { 13 | t[j] = val[j] 14 | } 15 | } 16 | 17 | if t[k] == m { 18 | r[k] = m 19 | } 20 | } 21 | for k, v := range r { 22 | if v > 0 && v == t[k] { 23 | res = append(res, v) 24 | } 25 | } 26 | return res 27 | } 28 | -------------------------------------------------------------------------------- /website/content/docs/example/collapsed/3rd-level/4th-level.md: -------------------------------------------------------------------------------- 1 | # 4th Level of Menu 2 | 3 | ## Caesorum illa tu sentit micat vestes papyriferi 4 | 5 | Inde aderam facti; Theseus vis de tauri illa peream. Oculos **uberaque** non 6 | regisque vobis cursuque, opus venit quam vulnera. Et maiora necemque, lege modo; 7 | gestanda nitidi, vero? Dum ne pectoraque testantur. 8 | 9 | Venasque repulsa Samos qui, exspectatum eram animosque hinc, [aut 10 | manes](http://www.creveratnon.net/apricaaetheriis), Assyrii. Cupiens auctoribus 11 | pariter rubet, profana magni super nocens. Vos ius sibilat inpar turba visae 12 | iusto! Sedes ante dum superest **extrema**. 13 | -------------------------------------------------------------------------------- /leetcode/0022.Generate-Parentheses/22. Generate Parentheses.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func generateParenthesis(n int) []string { 4 | if n == 0 { 5 | return []string{} 6 | } 7 | res := []string{} 8 | findGenerateParenthesis(n, n, "", &res) 9 | return res 10 | } 11 | 12 | func findGenerateParenthesis(lindex, rindex int, str string, res *[]string) { 13 | if lindex == 0 && rindex == 0 { 14 | *res = append(*res, str) 15 | return 16 | } 17 | if lindex > 0 { 18 | findGenerateParenthesis(lindex-1, rindex, str+"(", res) 19 | } 20 | if rindex > 0 && lindex < rindex { 21 | findGenerateParenthesis(lindex, rindex-1, str+")", res) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /leetcode/0152.Maximum-Product-Subarray/152. Maximum Product Subarray.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func maxProduct(nums []int) int { 4 | minimum, maximum, res := nums[0], nums[0], nums[0] 5 | for i := 1; i < len(nums); i++ { 6 | if nums[i] < 0 { 7 | maximum, minimum = minimum, maximum 8 | } 9 | maximum = max(nums[i], maximum*nums[i]) 10 | minimum = min(nums[i], minimum*nums[i]) 11 | res = max(res, maximum) 12 | } 13 | return res 14 | } 15 | 16 | func max(a int, b int) int { 17 | if a > b { 18 | return a 19 | } 20 | return b 21 | } 22 | 23 | func min(a int, b int) int { 24 | if a > b { 25 | return b 26 | } 27 | return a 28 | } 29 | -------------------------------------------------------------------------------- /leetcode/0209.Minimum-Size-Subarray-Sum/209. Minimum Size Subarray Sum.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func minSubArrayLen(s int, nums []int) int { 4 | n := len(nums) 5 | if n == 0 { 6 | return 0 7 | } 8 | left, right, res, sum := 0, -1, n+1, 0 9 | for left < n { 10 | if (right+1) < n && sum < s { 11 | right++ 12 | sum += nums[right] 13 | } else { 14 | sum -= nums[left] 15 | left++ 16 | } 17 | if sum >= s { 18 | res = min(res, right-left+1) 19 | } 20 | } 21 | if res == n+1 { 22 | return 0 23 | } 24 | return res 25 | } 26 | 27 | func min(a int, b int) int { 28 | if a > b { 29 | return b 30 | } 31 | return a 32 | } 33 | -------------------------------------------------------------------------------- /leetcode/0387.First-Unique-Character-in-a-String/README.md: -------------------------------------------------------------------------------- 1 | # [387. First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) 2 | 3 | ## 题目 4 | 5 | Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. 6 | 7 | **Examples:** 8 | 9 | s = "leetcode" 10 | return 0. 11 | 12 | s = "loveleetcode", 13 | return 2. 14 | 15 | **Note:** You may assume the string contain only lowercase letters. 16 | 17 | 18 | 19 | ## 题目大意 20 | 21 | 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。 22 | 23 | 24 | ## 解题思路 25 | 26 | - 简单题,要求输出第一个没有重复的字符。 27 | -------------------------------------------------------------------------------- /leetcode/0393.UTF-8-Validation/393. UTF-8 Validation.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func validUtf8(data []int) bool { 4 | count := 0 5 | for _, d := range data { 6 | if count == 0 { 7 | if d >= 248 { // 11111000 = 248 8 | return false 9 | } else if d >= 240 { // 11110000 = 240 10 | count = 3 11 | } else if d >= 224 { // 11100000 = 224 12 | count = 2 13 | } else if d >= 192 { // 11000000 = 192 14 | count = 1 15 | } else if d > 127 { // 01111111 = 127 16 | return false 17 | } 18 | } else { 19 | if d <= 127 || d >= 192 { 20 | return false 21 | } 22 | count-- 23 | } 24 | } 25 | return count == 0 26 | } 27 | -------------------------------------------------------------------------------- /structures/Queue.go: -------------------------------------------------------------------------------- 1 | package structures 2 | 3 | // Queue 是用于存放 int 的队列 4 | type Queue struct { 5 | nums []int 6 | } 7 | 8 | // NewQueue 返回 *kit.Queue 9 | func NewQueue() *Queue { 10 | return &Queue{nums: []int{}} 11 | } 12 | 13 | // Push 把 n 放入队列 14 | func (q *Queue) Push(n int) { 15 | q.nums = append(q.nums, n) 16 | } 17 | 18 | // Pop 从 q 中取出最先进入队列的值 19 | func (q *Queue) Pop() int { 20 | res := q.nums[0] 21 | q.nums = q.nums[1:] 22 | return res 23 | } 24 | 25 | // Len 返回 q 的长度 26 | func (q *Queue) Len() int { 27 | return len(q.nums) 28 | } 29 | 30 | // IsEmpty 反馈 q 是否为空 31 | func (q *Queue) IsEmpty() bool { 32 | return q.Len() == 0 33 | } 34 | -------------------------------------------------------------------------------- /leetcode/0088.Merge-Sorted-Array/README.md: -------------------------------------------------------------------------------- 1 | # [88. Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/description/) 2 | 3 | ## 题目 4 | 5 | Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. 6 | 7 | Note: 8 | 9 | You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively. 10 | 11 | ## 题目大意 12 | 13 | 合并两个已经有序的数组,结果放在第一个数组中,第一个数组假设空间足够大。要求算法时间复杂度足够低。 14 | 15 | ## 解题思路 16 | 17 | 为了不大量移动元素,就要从2个数组长度之和的最后一个位置开始,依次选取两个数组中大的数,从第一个数组的尾巴开始往头放,只要循环一次以后,就生成了合并以后的数组了。 -------------------------------------------------------------------------------- /leetcode/0169.Majority-Element/169. Majority Element.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | // 解法一 时间复杂度 O(n) 空间复杂度 O(1) 4 | func majorityElement(nums []int) int { 5 | res, count := nums[0], 0 6 | for i := 0; i < len(nums); i++ { 7 | if count == 0 { 8 | res, count = nums[i], 1 9 | } else { 10 | if nums[i] == res { 11 | count++ 12 | } else { 13 | count-- 14 | } 15 | } 16 | } 17 | return res 18 | } 19 | 20 | // 解法二 时间复杂度 O(n) 空间复杂度 O(n) 21 | func majorityElement1(nums []int) int { 22 | m := make(map[int]int) 23 | for _, v := range nums { 24 | m[v]++ 25 | if m[v] > len(nums)/2 { 26 | return v 27 | } 28 | } 29 | return 0 30 | } 31 | -------------------------------------------------------------------------------- /leetcode/0231.Power-of-Two/README.md: -------------------------------------------------------------------------------- 1 | # [231. Power of Two](https://leetcode.com/problems/power-of-two/) 2 | 3 | ## 题目 4 | 5 | Given an integer, write a function to determine if it is a power of two. 6 | 7 | **Example 1:** 8 | 9 | Input: 1 10 | Output: true 11 | Explanation: 2^0 = 1 12 | 13 | **Example 2:** 14 | 15 | Input: 16 16 | Output: true 17 | Explanation: 2^4 = 16 18 | 19 | **Example 3:** 20 | 21 | Input: 218 22 | Output: false 23 | 24 | ## 题目大意 25 | 26 | 给定一个整数,编写一个函数来判断它是否是 2 的幂次方。 27 | 28 | 29 | ## 解题思路 30 | 31 | - 判断一个数是不是 2 的 n 次方。 32 | - 这一题最简单的思路是循环,可以通过。但是题目要求不循环就要判断,这就需要用到数论的知识了。这一题和第 326 题是一样的思路。 33 | -------------------------------------------------------------------------------- /leetcode/0496.Next-Greater-Element-I/496. Next Greater Element I.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func nextGreaterElement(nums1 []int, nums2 []int) []int { 4 | if len(nums1) == 0 || len(nums2) == 0 { 5 | return []int{} 6 | } 7 | res, reocrd := []int{}, map[int]int{} 8 | for i, v := range nums2 { 9 | reocrd[v] = i 10 | } 11 | for i := 0; i < len(nums1); i++ { 12 | flag := false 13 | for j := reocrd[nums1[i]]; j < len(nums2); j++ { 14 | if nums2[j] > nums1[i] { 15 | res = append(res, nums2[j]) 16 | flag = true 17 | break 18 | } 19 | } 20 | if flag == false { 21 | res = append(res, -1) 22 | } 23 | } 24 | return res 25 | } 26 | -------------------------------------------------------------------------------- /leetcode/0020.Valid-Parentheses/20. Valid Parentheses.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isValid(s string) bool { 4 | // 空字符串直接返回 true 5 | if len(s) == 0 { 6 | return true 7 | } 8 | stack := make([]rune, 0) 9 | for _, v := range s { 10 | if (v == '[') || (v == '(') || (v == '{') { 11 | stack = append(stack, v) 12 | } else if ((v == ']') && len(stack) > 0 && stack[len(stack)-1] == '[') || 13 | ((v == ')') && len(stack) > 0 && stack[len(stack)-1] == '(') || 14 | ((v == '}') && len(stack) > 0 && stack[len(stack)-1] == '{') { 15 | stack = stack[:len(stack)-1] 16 | } else { 17 | return false 18 | } 19 | } 20 | return len(stack) == 0 21 | } 22 | -------------------------------------------------------------------------------- /leetcode/0112.Path-Sum/112. Path Sum.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/structures" 5 | ) 6 | 7 | // TreeNode define 8 | type TreeNode = structures.TreeNode 9 | 10 | /** 11 | * Definition for a binary tree node. 12 | * type TreeNode struct { 13 | * Val int 14 | * Left *TreeNode 15 | * Right *TreeNode 16 | * } 17 | */ 18 | 19 | func hasPathSum(root *TreeNode, sum int) bool { 20 | if root == nil { 21 | return false 22 | } 23 | if root.Left == nil && root.Right == nil { 24 | return sum == root.Val 25 | } 26 | return hasPathSum(root.Left, sum-root.Val) || hasPathSum(root.Right, sum-root.Val) 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/1049.Last-Stone-Weight-II/1049. Last Stone Weight II.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func lastStoneWeightII(stones []int) int { 4 | sum := 0 5 | for _, v := range stones { 6 | sum += v 7 | } 8 | n, C, dp := len(stones), sum/2, make([]int, sum/2+1) 9 | for i := 0; i <= C; i++ { 10 | if stones[0] <= i { 11 | dp[i] = stones[0] 12 | } else { 13 | dp[i] = 0 14 | } 15 | } 16 | for i := 1; i < n; i++ { 17 | for j := C; j >= stones[i]; j-- { 18 | dp[j] = max(dp[j], dp[j-stones[i]]+stones[i]) 19 | } 20 | } 21 | return sum - 2*dp[C] 22 | } 23 | 24 | func max(a int, b int) int { 25 | if a > b { 26 | return a 27 | } 28 | return b 29 | } 30 | -------------------------------------------------------------------------------- /leetcode/0088.Merge-Sorted-Array/88. Merge Sorted Array.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func merge(nums1 []int, m int, nums2 []int, n int) { 4 | if m == 0 { 5 | copy(nums1, nums2) 6 | return 7 | } 8 | // 这里不需要,因为测试数据考虑到了第一个数组的空间问题 9 | // for index := 0; index < n; index++ { 10 | // nums1 = append(nums1, nums2[index]) 11 | // } 12 | i := m - 1 13 | j := n - 1 14 | k := m + n - 1 15 | // 从后面往前放,只需要循环一次即可 16 | for ; i >= 0 && j >= 0; k-- { 17 | if nums1[i] > nums2[j] { 18 | nums1[k] = nums1[i] 19 | i-- 20 | } else { 21 | nums1[k] = nums2[j] 22 | j-- 23 | } 24 | } 25 | for ; j >= 0; k-- { 26 | nums1[k] = nums2[j] 27 | j-- 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /leetcode/0207.Course-Schedule/207. Course Schedule.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | // AOV 网的拓扑排序 4 | func canFinish(n int, pre [][]int) bool { 5 | in := make([]int, n) 6 | frees := make([][]int, n) 7 | next := make([]int, 0, n) 8 | for _, v := range pre { 9 | in[v[0]]++ 10 | frees[v[1]] = append(frees[v[1]], v[0]) 11 | } 12 | for i := 0; i < n; i++ { 13 | if in[i] == 0 { 14 | next = append(next, i) 15 | } 16 | } 17 | for i := 0; i != len(next); i++ { 18 | c := next[i] 19 | v := frees[c] 20 | for _, vv := range v { 21 | in[vv]-- 22 | if in[vv] == 0 { 23 | next = append(next, vv) 24 | } 25 | } 26 | } 27 | return len(next) == n 28 | } 29 | -------------------------------------------------------------------------------- /leetcode/1160.Find-Words-That-Can-Be-Formed-by-Characters/1160. Find Words That Can Be Formed by Characters.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func countCharacters(words []string, chars string) int { 4 | count, res := make([]int, 26), 0 5 | for i := 0; i < len(chars); i++ { 6 | count[chars[i]-'a']++ 7 | } 8 | for _, w := range words { 9 | if canBeFormed(w, count) { 10 | res += len(w) 11 | } 12 | } 13 | return res 14 | } 15 | func canBeFormed(w string, c []int) bool { 16 | count := make([]int, 26) 17 | for i := 0; i < len(w); i++ { 18 | count[w[i]-'a']++ 19 | if count[w[i]-'a'] > c[w[i]-'a'] { 20 | return false 21 | } 22 | } 23 | return true 24 | } 25 | -------------------------------------------------------------------------------- /website/themes/book/layouts/shortcodes/katex.html: -------------------------------------------------------------------------------- 1 | {{- if not (.Page.Scratch.Get "katex") -}} 2 | 3 | 4 | 5 | 6 | {{- .Page.Scratch.Set "katex" true -}} 7 | {{- end -}} 8 | 9 | 10 | {{ cond (in .Params "display") "\\[" "\\(" -}} 11 | {{- trim .Inner "\n" -}} 12 | {{- cond (in .Params "display") "\\]" "\\)" }} 13 | 14 | -------------------------------------------------------------------------------- /leetcode/0234.Palindrome-Linked-List/README.md: -------------------------------------------------------------------------------- 1 | # [234. Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/) 2 | 3 | ## 题目 4 | 5 | Given a singly linked list, determine if it is a palindrome. 6 | 7 | Example 1: 8 | 9 | ```c 10 | Input: 1->2 11 | Output: false 12 | ``` 13 | 14 | Example 2: 15 | 16 | ```c 17 | Input: 1->2->2->1 18 | Output: true 19 | ``` 20 | 21 | Follow up: 22 | 23 | Could you do it in O(n) time and O(1) space? 24 | 25 | ## 题目大意 26 | 27 | 判断一个链表是否是回文链表。要求时间复杂度 O(n),空间复杂度 O(1)。 28 | 29 | ## 解题思路 30 | 31 | 这道题只需要在第 143 题上面改改就可以了。思路是完全一致的。先找到中间结点,然后反转中间结点后面到结尾的所有结点。最后一一判断头结点开始的结点和中间结点往后开始的结点是否相等。如果一直相等,就是回文链表,如果有不相等的,直接返回不是回文链表。 -------------------------------------------------------------------------------- /leetcode/0386.Lexicographical-Numbers/README.md: -------------------------------------------------------------------------------- 1 | # [386. Lexicographical Numbers](https://leetcode.com/problems/lexicographical-numbers/) 2 | 3 | 4 | ## 题目 5 | 6 | Given an integer n, return 1 - n in lexicographical order. 7 | 8 | For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9]. 9 | 10 | Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000. 11 | 12 | 13 | ## 题目大意 14 | 15 | 给定一个整数 n, 返回从 1 到 n 的字典顺序。例如,给定 n =13,返回 [1,10,11,12,13,2,3,4,5,6,7,8,9] 。 16 | 17 | 请尽可能的优化算法的时间复杂度和空间复杂度。 输入的数据 n 小于等于 5,000,000。 18 | 19 | 20 | 21 | ## 解题思路 22 | 23 | 24 | - 给出一个数字 n ,要求按照字典序对 1-n 这 n 个数排序。 25 | - DFS 暴力求解即可。 26 | -------------------------------------------------------------------------------- /leetcode/0839.Similar-String-Groups/839. Similar String Groups.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/template" 5 | ) 6 | 7 | func numSimilarGroups(A []string) int { 8 | uf := template.UnionFind{} 9 | uf.Init(len(A)) 10 | for i := 0; i < len(A); i++ { 11 | for j := i + 1; j < len(A); j++ { 12 | if isSimilar(A[i], A[j]) { 13 | uf.Union(i, j) 14 | } 15 | } 16 | } 17 | return uf.TotalCount() 18 | } 19 | 20 | func isSimilar(a, b string) bool { 21 | var n int 22 | for i := 0; i < len(a); i++ { 23 | if a[i] != b[i] { 24 | n++ 25 | if n > 2 { 26 | return false 27 | } 28 | } 29 | } 30 | return true 31 | } 32 | -------------------------------------------------------------------------------- /website/themes/book/exampleSite/content/docs/example/collapsed/3rd-level/4th-level.md: -------------------------------------------------------------------------------- 1 | # 4th Level of Menu 2 | 3 | ## Caesorum illa tu sentit micat vestes papyriferi 4 | 5 | Inde aderam facti; Theseus vis de tauri illa peream. Oculos **uberaque** non 6 | regisque vobis cursuque, opus venit quam vulnera. Et maiora necemque, lege modo; 7 | gestanda nitidi, vero? Dum ne pectoraque testantur. 8 | 9 | Venasque repulsa Samos qui, exspectatum eram animosque hinc, [aut 10 | manes](http://www.creveratnon.net/apricaaetheriis), Assyrii. Cupiens auctoribus 11 | pariter rubet, profana magni super nocens. Vos ius sibilat inpar turba visae 12 | iusto! Sedes ante dum superest **extrema**. 13 | -------------------------------------------------------------------------------- /leetcode/0104.Maximum-Depth-of-Binary-Tree/104. Maximum Depth of Binary Tree.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/structures" 5 | ) 6 | 7 | // TreeNode define 8 | type TreeNode = structures.TreeNode 9 | 10 | /** 11 | * Definition for a binary tree node. 12 | * type TreeNode struct { 13 | * Val int 14 | * Left *TreeNode 15 | * Right *TreeNode 16 | * } 17 | */ 18 | 19 | func maxDepth(root *TreeNode) int { 20 | if root == nil { 21 | return 0 22 | } 23 | return max(maxDepth(root.Left), maxDepth(root.Right)) + 1 24 | } 25 | 26 | func max(a int, b int) int { 27 | if a > b { 28 | return a 29 | } 30 | return b 31 | } 32 | -------------------------------------------------------------------------------- /leetcode/1128.Number-of-Equivalent-Domino-Pairs/1128. Number of Equivalent Domino Pairs.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func numEquivDominoPairs(dominoes [][]int) int { 4 | if dominoes == nil || len(dominoes) == 0 { 5 | return 0 6 | } 7 | result, buckets := 0, [100]int{} 8 | for _, dominoe := range dominoes { 9 | key, rotatedKey := dominoe[0]*10+dominoe[1], dominoe[1]*10+dominoe[0] 10 | if dominoe[0] != dominoe[1] { 11 | if buckets[rotatedKey] > 0 { 12 | result += buckets[rotatedKey] 13 | } 14 | } 15 | if buckets[key] > 0 { 16 | result += buckets[key] 17 | buckets[key]++ 18 | } else { 19 | buckets[key]++ 20 | } 21 | } 22 | return result 23 | } 24 | -------------------------------------------------------------------------------- /structures/Stack.go: -------------------------------------------------------------------------------- 1 | package structures 2 | 3 | // Stack 是用于存放 int 的 栈 4 | type Stack struct { 5 | nums []int 6 | } 7 | 8 | // NewStack 返回 *kit.Stack 9 | func NewStack() *Stack { 10 | return &Stack{nums: []int{}} 11 | } 12 | 13 | // Push 把 n 放入 栈 14 | func (s *Stack) Push(n int) { 15 | s.nums = append(s.nums, n) 16 | } 17 | 18 | // Pop 从 s 中取出最后放入 栈 的值 19 | func (s *Stack) Pop() int { 20 | res := s.nums[len(s.nums)-1] 21 | s.nums = s.nums[:len(s.nums)-1] 22 | return res 23 | } 24 | 25 | // Len 返回 s 的长度 26 | func (s *Stack) Len() int { 27 | return len(s.nums) 28 | } 29 | 30 | // IsEmpty 反馈 s 是否为空 31 | func (s *Stack) IsEmpty() bool { 32 | return s.Len() == 0 33 | } 34 | -------------------------------------------------------------------------------- /leetcode/0357.Count-Numbers-with-Unique-Digits/357. Count Numbers with Unique Digits.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | // 暴力打表法 4 | func countNumbersWithUniqueDigits1(n int) int { 5 | res := []int{1, 10, 91, 739, 5275, 32491, 168571, 712891, 2345851, 5611771, 8877691} 6 | if n >= 10 { 7 | return res[10] 8 | } 9 | return res[n] 10 | } 11 | 12 | // 打表方法 13 | func countNumbersWithUniqueDigits(n int) int { 14 | if n == 0 { 15 | return 1 16 | } 17 | res, uniqueDigits, availableNumber := 10, 9, 9 18 | for n > 1 && availableNumber > 0 { 19 | uniqueDigits = uniqueDigits * availableNumber 20 | res += uniqueDigits 21 | availableNumber-- 22 | n-- 23 | } 24 | return res 25 | } 26 | -------------------------------------------------------------------------------- /leetcode/0392.Is-Subsequence/392. Is Subsequence.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | // 解法一 O(n^2) 4 | func isSubsequence(s string, t string) bool { 5 | index := 0 6 | for i := 0; i < len(s); i++ { 7 | flag := false 8 | for ; index < len(t); index++ { 9 | if s[i] == t[index] { 10 | flag = true 11 | break 12 | } 13 | } 14 | if flag == true { 15 | index++ 16 | continue 17 | } else { 18 | return false 19 | } 20 | } 21 | return true 22 | } 23 | 24 | // 解法二 O(n) 25 | func isSubsequence1(s string, t string) bool { 26 | for len(s) > 0 && len(t) > 0 { 27 | if s[0] == t[0] { 28 | s = s[1:] 29 | } 30 | t = t[1:] 31 | } 32 | return len(s) == 0 33 | } 34 | -------------------------------------------------------------------------------- /leetcode/0021.Merge-Two-Sorted-Lists/21. Merge Two Sorted Lists.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/structures" 5 | ) 6 | 7 | // ListNode define 8 | type ListNode = structures.ListNode 9 | 10 | /** 11 | * Definition for singly-linked list. 12 | * type ListNode struct { 13 | * Val int 14 | * Next *ListNode 15 | * } 16 | */ 17 | func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { 18 | if l1 == nil { 19 | return l2 20 | } 21 | if l2 == nil { 22 | return l1 23 | } 24 | if l1.Val < l2.Val { 25 | l1.Next = mergeTwoLists(l1.Next, l2) 26 | return l1 27 | } 28 | l2.Next = mergeTwoLists(l1, l2.Next) 29 | return l2 30 | } 31 | -------------------------------------------------------------------------------- /leetcode/0048.Rotate-Image/48. Rotate Image.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func rotate(matrix [][]int) { 4 | row := len(matrix) 5 | if row <= 0 { 6 | return 7 | } 8 | column := len(matrix[0]) 9 | // rotate by diagonal 对角线变换 10 | for i := 0; i < row; i++ { 11 | for j := i + 1; j < column; j++ { 12 | tmp := matrix[i][j] 13 | matrix[i][j] = matrix[j][i] 14 | matrix[j][i] = tmp 15 | } 16 | } 17 | // rotate by vertical centerline 竖直轴对称翻转 18 | halfColumn := column / 2 19 | for i := 0; i < row; i++ { 20 | for j := 0; j < halfColumn; j++ { 21 | tmp := matrix[i][j] 22 | matrix[i][j] = matrix[i][column-j-1] 23 | matrix[i][column-j-1] = tmp 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /leetcode/0056.Merge-Intervals/README.md: -------------------------------------------------------------------------------- 1 | # [56. Merge Intervals](https://leetcode.com/problems/merge-intervals/) 2 | 3 | ## 题目 4 | 5 | Given a collection of intervals, merge all overlapping intervals. 6 | 7 | Example 1: 8 | 9 | ``` 10 | Input: [[1,3],[2,6],[8,10],[15,18]] 11 | Output: [[1,6],[8,10],[15,18]] 12 | Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6]. 13 | ``` 14 | 15 | Example 2: 16 | 17 | ``` 18 | Input: [[1,4],[4,5]] 19 | Output: [[1,5]] 20 | Explanation: Intervals [1,4] and [4,5] are considered overlapping. 21 | ``` 22 | 23 | ## 题目大意 24 | 25 | 合并给的多个区间,区间有重叠的要进行区间合并。 26 | 27 | 28 | ## 解题思路 29 | 30 | 先按照区间起点进行排序。然后从区间起点小的开始扫描,依次合并每个有重叠的区间。 -------------------------------------------------------------------------------- /leetcode/0474.Ones-and-Zeroes/474. Ones and Zeroes.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strings" 4 | 5 | func findMaxForm(strs []string, m int, n int) int { 6 | dp := make([][]int, m+1) 7 | for i := 0; i < m+1; i++ { 8 | dp[i] = make([]int, n+1) 9 | } 10 | for _, s := range strs { 11 | zero := strings.Count(s, "0") 12 | one := len(s) - zero 13 | if zero > m || one > n { 14 | continue 15 | } 16 | for i := m; i >= zero; i-- { 17 | for j := n; j >= one; j-- { 18 | dp[i][j] = max(dp[i][j], 1+dp[i-zero][j-one]) 19 | } 20 | } 21 | } 22 | return dp[m][n] 23 | } 24 | 25 | func max(a int, b int) int { 26 | if a > b { 27 | return a 28 | } 29 | return b 30 | } 31 | -------------------------------------------------------------------------------- /leetcode/0077.Combinations/77. Combinations.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func combine(n int, k int) [][]int { 4 | if n <= 0 || k <= 0 || k > n { 5 | return [][]int{} 6 | } 7 | c, res := []int{}, [][]int{} 8 | generateCombinations(n, k, 1, c, &res) 9 | return res 10 | } 11 | 12 | func generateCombinations(n, k, start int, c []int, res *[][]int) { 13 | if len(c) == k { 14 | b := make([]int, len(c)) 15 | copy(b, c) 16 | *res = append(*res, b) 17 | return 18 | } 19 | // i will at most be n - (k - c.size()) + 1 20 | for i := start; i <= n-(k-len(c))+1; i++ { 21 | c = append(c, i) 22 | generateCombinations(n, k, i+1, c, res) 23 | c = c[:len(c)-1] 24 | } 25 | return 26 | } 27 | -------------------------------------------------------------------------------- /leetcode/0108.Convert-Sorted-Array-to-Binary-Search-Tree/108. Convert Sorted Array to Binary Search Tree.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/structures" 5 | ) 6 | 7 | // TreeNode define 8 | type TreeNode = structures.TreeNode 9 | 10 | /** 11 | * Definition for a binary tree node. 12 | * type TreeNode struct { 13 | * Val int 14 | * Left *TreeNode 15 | * Right *TreeNode 16 | * } 17 | */ 18 | 19 | func sortedArrayToBST(nums []int) *TreeNode { 20 | if len(nums) == 0 { 21 | return nil 22 | } 23 | return &TreeNode{Val: nums[len(nums)/2], Left: sortedArrayToBST(nums[:len(nums)/2]), Right: sortedArrayToBST(nums[len(nums)/2+1:])} 24 | } 25 | -------------------------------------------------------------------------------- /leetcode/0283.Move-Zeroes/README.md: -------------------------------------------------------------------------------- 1 | # [283. Move Zeroes](https://leetcode.com/problems/move-zeroes/) 2 | 3 | ## 题目 4 | 5 | Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. 6 | 7 | Example 1: 8 | 9 | ```c 10 | Input: [0,1,0,3,12] 11 | Output: [1,3,12,0,0] 12 | ``` 13 | 14 | Note: 15 | 16 | - You must do this in-place without making a copy of the array. 17 | - Minimize the total number of operations. 18 | 19 | 20 | 21 | ## 题目大意 22 | 23 | 题目要求不能采用额外的辅助空间,将数组中 0 元素都移动到数组的末尾,并且维持所有非 0 元素的相对位置。 24 | 25 | ## 解题思路 26 | 27 | 这一题可以只扫描数组一遍,不断的用 i,j 标记 0 和非 0 的元素,然后相互交换,最终到达题目的目的。与这一题相近的题目有第 26 题,第 27 题,第 80 题。 28 | -------------------------------------------------------------------------------- /leetcode/0880.Decoded-String-at-Index/880. Decoded String at Index.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isLetter(char byte) bool { 4 | if char >= 'a' && char <= 'z' { 5 | return true 6 | } 7 | return false 8 | } 9 | 10 | func decodeAtIndex(S string, K int) string { 11 | length := 0 12 | for i := 0; i < len(S); i++ { 13 | if isLetter(S[i]) { 14 | length++ 15 | if length == K { 16 | return string(S[i]) 17 | } 18 | } else { 19 | if length*int(S[i]-'0') >= K { 20 | if K%length != 0 { 21 | return decodeAtIndex(S[:i], K%length) 22 | } 23 | return decodeAtIndex(S[:i], length) 24 | } 25 | length *= int(S[i] - '0') 26 | } 27 | } 28 | return "" 29 | } 30 | -------------------------------------------------------------------------------- /leetcode/0125.Valid-Palindrome/README.md: -------------------------------------------------------------------------------- 1 | # [125. Valid Palindrome](https://leetcode.com/problems/valid-palindrome/description/) 2 | 3 | ## 题目 4 | 5 | Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. 6 | 7 | For example, 8 | 9 | ```c 10 | "A man, a plan, a canal: Panama" is a palindrome. 11 | "race a car" is not a palindrome. 12 | ``` 13 | 14 | Note: 15 | 16 | Have you consider that the string might be empty? This is a good question to ask during an interview. 17 | 18 | For the purpose of this problem, we define empty string as valid palindrome. 19 | 20 | ## 题目大意 21 | 22 | 判断所给的字符串是否是有效的回文串。 23 | 24 | ## 解题思路 25 | 26 | 简单题,按照题意做即可。 -------------------------------------------------------------------------------- /leetcode/0141.Linked-List-Cycle/141. Linked List Cycle.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/structures" 5 | ) 6 | 7 | // ListNode define 8 | type ListNode = structures.ListNode 9 | 10 | /** 11 | * Definition for singly-linked list. 12 | * struct ListNode { 13 | * int val; 14 | * ListNode *next; 15 | * ListNode(int x) : val(x), next(NULL) {} 16 | * }; 17 | */ 18 | 19 | func hasCycle(head *ListNode) bool { 20 | fast := head 21 | slow := head 22 | for slow != nil && fast != nil && fast.Next != nil { 23 | fast = fast.Next.Next 24 | slow = slow.Next 25 | if fast == slow { 26 | return true 27 | } 28 | } 29 | return false 30 | } 31 | -------------------------------------------------------------------------------- /leetcode/0019.Remove-Nth-Node-From-End-of-List/README.md: -------------------------------------------------------------------------------- 1 | # [19. Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) 2 | 3 | ## 题目 4 | 5 | Given a linked list, remove the n-th node from the end of list and return its head. 6 | 7 | Example: 8 | 9 | ``` 10 | Given linked list: 1->2->3->4->5, and n = 2. 11 | 12 | After removing the second node from the end, the linked list becomes 1->2->3->5. 13 | ``` 14 | 15 | ## 题目大意 16 | 17 | 删除链表中倒数第 n 个结点。 18 | 19 | ## 解题思路 20 | 21 | 这道题比较简单,先循环一次拿到链表的总长度,然后循环到要删除的结点的前一个结点开始删除操作。需要注意的一个特例是,有可能要删除头结点,要单独处理。 22 | 23 | 这道题有一种特别简单的解法。设置 2 个指针,一个指针距离前一个指针 n 个距离。同时移动 2 个指针,2 个指针都移动相同的距离。当一个指针移动到了终点,那么前一个指针就是倒数第 n 个节点了。 24 | -------------------------------------------------------------------------------- /leetcode/0092.Reverse-Linked-List-II/README.md: -------------------------------------------------------------------------------- 1 | # [92. Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/) 2 | 3 | ## 题目 4 | 5 | Reverse a linked list from position m to n. Do it in one-pass. 6 | 7 | Note: 1 ≤ m ≤ n ≤ length of list. 8 | 9 | Example: 10 | 11 | ``` 12 | Input: 1->2->3->4->5->NULL, m = 2, n = 4 13 | Output: 1->4->3->2->5->NULL 14 | ``` 15 | 16 | 17 | ## 题目大意 18 | 19 | 给定 2 个链表中结点的位置 m, n,反转这个两个位置区间内的所有结点。 20 | 21 | ## 解题思路 22 | 23 | 由于有可能整个链表都被反转,所以构造一个新的头结点指向当前的头。之后的处理方法是:找到第一个需要反转的结点的前一个结点 p,从这个结点开始,依次把后面的结点用“头插”法,插入到 p 结点的后面。循环次数用 n-m 来控制。 24 | 25 | 这一题结点可以原地变化,更改各个结点的 next 指针就可以。不需要游标 p 指针。因为每次逆序以后,原有结点的相对位置就发生了变化,相当于游标指针已经移动了,所以不需要再有游标 p = p.Next 的操作了。 --------------------------------------------------------------------------------