├── .gitignore ├── Algorithms-4th └── Java │ ├── .gitignore │ ├── lib │ └── algs4.jar │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── chapter_01 │ │ ├── section_01 │ │ ├── E_1_1_33.java │ │ └── E_1_1_6.java │ │ ├── section_02 │ │ ├── E_1_2_1.java │ │ ├── E_1_2_10.java │ │ ├── E_1_2_16.java │ │ └── E_1_2_2.java │ │ └── section_03 │ │ ├── Bag.java │ │ ├── E_1_3_10.java │ │ ├── E_1_3_9.java │ │ ├── Queue.java │ │ └── Stack.java │ └── resources │ └── IntNums.txt ├── Codewars └── README.md ├── Coding-Interviews ├── Java │ ├── .gitignore │ ├── 01-Find-in-Partially-Sorted-Matrix │ │ └── src │ │ │ └── Solution.java │ ├── 02-Replace-Blank │ │ └── src │ │ │ └── Solution.java │ ├── 03-Print-List-Reversingly │ │ └── src │ │ │ ├── ListNode.java │ │ │ └── Solution.java │ ├── 04-Construct-Binary-Tree │ │ └── src │ │ │ ├── Solution.java │ │ │ └── TreeNode.java │ ├── 05-Queue-with-Two-Stacks │ │ └── src │ │ │ └── Solution.java │ ├── 06-Min-Number-in-Rotated-Array │ │ └── src │ │ │ └── Solution.java │ ├── 07-Fibonacci │ │ └── src │ │ │ └── Solution.java │ ├── 08-Jump-Floor │ │ └── src │ │ │ └── Solution.java │ ├── 09-Jump-Floor-II │ │ └── src │ │ │ └── Solution.java │ ├── 10-Rect-Cover │ │ └── src │ │ │ └── Solution.java │ ├── 11-Number-of-1-in-Binary │ │ └── src │ │ │ └── Solution.java │ ├── 12-Power │ │ └── src │ │ │ └── Solution.java │ ├── 13-Reorder-Array │ │ └── src │ │ │ └── Solution.java │ ├── 14-Kth-Node-from-End │ │ └── src │ │ │ ├── ListNode.java │ │ │ └── Solution.java │ ├── 15-Reverse-List │ │ └── src │ │ │ ├── ListNode.java │ │ │ └── Solution.java │ ├── 16-Merge-Sorted-Lists │ │ └── src │ │ │ ├── ListNode.java │ │ │ └── Solution.java │ ├── 17-Substructure-in-Tree │ │ └── src │ │ │ ├── Solution.java │ │ │ └── TreeNode.java │ ├── 18-Mirror-of-Binary-Tree │ │ └── src │ │ │ ├── Solution.java │ │ │ └── TreeNode.java │ ├── 19-Print-Matrix │ │ └── src │ │ │ └── Solution.java │ ├── 20-Min-in-Stack │ │ └── src │ │ │ └── Solution.java │ ├── 21-Stack-Push-Pop-Order │ │ └── src │ │ │ └── Solution.java │ ├── 22-Print-BST-from-Top-to-Bottom │ │ └── src │ │ │ ├── Solution.java │ │ │ └── TreeNode.java │ ├── 23-Sequence-of-BST │ │ └── src │ │ │ └── Solution.java │ ├── 24-Path-in-Tree │ │ └── src │ │ │ ├── Solution.java │ │ │ └── TreeNode.java │ ├── 25-Copy-Complex-List │ │ └── src │ │ │ ├── RandomListNode.java │ │ │ └── Solution.java │ ├── 26-Convert-Binary-Search-Tree │ │ └── src │ │ │ ├── Solution.java │ │ │ └── TreeNode.java │ ├── 27-String-Permutation │ │ └── src │ │ │ └── Solution.java │ ├── 28-More-Than-Half-Number │ │ └── src │ │ │ └── Solution.java │ ├── 29-K-Least-Numbers │ │ └── src │ │ │ └── Solution.java │ ├── 30-Greatest-Sum-of-Subarrays │ │ └── src │ │ │ └── Solution.java │ ├── 31-Number-of-1 │ │ └── src │ │ │ └── Solution.java │ ├── 32-Sort-Array-for-Min-Number │ │ └── src │ │ │ └── Solution.java │ ├── 33-Ugly-Number │ │ └── src │ │ │ └── Solution.java │ ├── 34-First-Not-Repeating-Char │ │ └── src │ │ │ └── Solution.java │ ├── 35-Inverse-Pairs │ │ └── src │ │ │ └── Solution.java │ ├── 36-First-Common-Nodes-in-Lists │ │ └── src │ │ │ ├── ListNode.java │ │ │ └── Solution.java │ ├── 37-Number-of-K │ │ └── src │ │ │ └── Solution.java │ ├── 38-Tree-Depth │ │ └── src │ │ │ ├── Solution.java │ │ │ └── TreeNode.java │ ├── 39-Balanced-Binary-Tree │ │ └── src │ │ │ ├── Solution.java │ │ │ └── TreeNode.java │ ├── 40-Numbers-Appear-Once │ │ └── src │ │ │ └── Solution.java │ ├── 41-Continues-Sequence-with-Sum │ │ └── src │ │ │ └── Solution.java │ ├── 42-Two-Numbers-with-Sum │ │ └── src │ │ │ └── Solution.java │ ├── 43-Left-Rotate-String │ │ └── src │ │ │ └── Solution.java │ ├── 44-Reverse-Words-in-Sentence │ │ └── src │ │ │ └── Solution.java │ ├── 45-Continuous-Cards │ │ └── src │ │ │ └── Solution.java │ ├── 46-Last-Number-in-Circle │ │ └── src │ │ │ └── Solution.java │ ├── 47-Accumulate │ │ └── src │ │ │ └── Solution.java │ ├── 48-Add-Two-Numbers │ │ └── src │ │ │ └── Solution.java │ ├── 49-String-to-Int │ │ └── src │ │ │ └── Solution.java │ ├── 50-Duplicate │ │ └── src │ │ │ └── Solution.java │ ├── 52-Regular-Expression-Matching │ │ └── src │ │ │ └── Solution.java │ ├── 54-First-Appearing-Once │ │ └── src │ │ │ └── Solution.java │ ├── 55-Entry-Node-of-Loop │ │ └── src │ │ │ ├── ListNode.java │ │ │ └── Solution.java │ └── 62-Kth-Node │ │ └── src │ │ ├── Solution.java │ │ └── TreeNode.java └── README.md ├── LICENSE ├── LeetCode ├── CPP │ ├── .gitignore │ ├── 100301 Count Pairs That Form a Complete Day II │ │ ├── CMakeLists.txt │ │ └── main.cpp │ ├── 100304 Count Pairs That Form a Complete Day I │ │ ├── CMakeLists.txt │ │ └── main.cpp │ ├── 100316 Maximum Total Damage With Spell Casting │ │ ├── CMakeLists.txt │ │ └── main.cpp │ ├── 169 Majority Element │ │ ├── CMakeLists.txt │ │ └── main.cpp │ ├── 2288 Apply Discount to Prices │ │ ├── CMakeLists.txt │ │ └── main.cpp │ ├── 26 Remove Duplicates from Sorted Array │ │ ├── CMakeLists.txt │ │ └── main.cpp │ ├── 2663 Lexicographically Smallest Beautiful String │ │ ├── CMakeLists.txt │ │ └── main.cpp │ ├── 27 Remove Element │ │ ├── CMakeLists.txt │ │ └── main.cpp │ ├── 2713 Maximum Strictly Increasing Cells in a Matrix │ │ ├── CMakeLists.txt │ │ └── main.cpp │ ├── 2748 Number of Beautiful Pairs │ │ ├── CMakeLists.txt │ │ └── main.cpp │ ├── 2779 Maximum Beauty of an Array After Applying Operation │ │ ├── CMakeLists.txt │ │ └── Solution.cpp │ ├── 2786 Visit Array Positions to Maximize Score │ │ ├── CMakeLists.txt │ │ └── Solution.cpp │ ├── 2806 Account Balance After Rounded Purchase │ │ ├── CMakeLists.txt │ │ └── Solution.cpp │ ├── 2813 Maximum Elegance of a K-Length Subsequence │ │ ├── CMakeLists.txt │ │ └── Solution.cpp │ ├── 312 Burst Balloons │ │ ├── CMakeLists.txt │ │ └── Solution.cpp │ ├── 419 Battleships in a Board │ │ ├── CMakeLists.txt │ │ └── Solution.cpp │ ├── 503 Next Greater Element II │ │ ├── CMakeLists.txt │ │ └── main.cpp │ ├── 520 Detect Capital │ │ ├── CMakeLists.txt │ │ └── main.cpp │ ├── 521 Longest Uncommon Subsequence I │ │ ├── CMakeLists.txt │ │ └── Solution.cpp │ ├── 522 Longest Uncommon Subsequence II │ │ ├── CMakeLists.txt │ │ └── main.cpp │ ├── 80 Remove Duplicates from Sorted Array II │ │ ├── CMakeLists.txt │ │ └── main.cpp │ ├── 88 Merge Sorted Array │ │ ├── CMakeLists.txt │ │ └── main.cpp │ ├── 881 Boats to Save People │ │ ├── CMakeLists.txt │ │ └── Solution.cpp │ └── LCP61 │ │ ├── CMakeLists.txt │ │ └── main.cpp ├── Java │ ├── .gitignore │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ ├── JZ_Offer_II_001 │ │ └── Solution.java │ │ ├── JZ_Offer_II_002 │ │ └── Solution.java │ │ ├── JZ_Offer_II_003 │ │ └── Solution.java │ │ ├── JZ_Offer_II_004 │ │ └── Solution.java │ │ ├── JZ_Offer_II_005 │ │ └── Solution.java │ │ ├── JZ_Offer_II_006 │ │ └── Solution.java │ │ ├── JZ_Offer_II_007 │ │ └── Solution.java │ │ ├── JZ_Offer_II_008 │ │ └── Solution.java │ │ ├── JZ_Offer_II_009 │ │ └── Solution.java │ │ ├── JZ_Offer_II_010 │ │ └── Solution.java │ │ ├── JZ_Offer_II_011 │ │ └── Solution.java │ │ ├── JZ_Offer_II_012 │ │ └── Solution.java │ │ ├── JZ_Offer_II_013 │ │ └── Solution.java │ │ ├── JZ_Offer_II_014 │ │ └── Solution.java │ │ ├── JZ_Offer_II_015 │ │ └── Solution.java │ │ ├── JZ_Offer_II_016 │ │ └── Solution.java │ │ ├── JZ_Offer_II_017 │ │ ├── Solution.java │ │ └── Solution2.java │ │ ├── JZ_Offer_II_018 │ │ └── Solution.java │ │ ├── JZ_Offer_II_019 │ │ └── Solution.java │ │ ├── JZ_Offer_II_020 │ │ └── Solution.java │ │ ├── JZ_Offer_II_021 │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── JZ_Offer_II_022 │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── JZ_Offer_II_023 │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── JZ_Offer_II_024 │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── JZ_Offer_II_025 │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── JZ_Offer_II_026 │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── JZ_Offer_II_027 │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── JZ_Offer_II_028 │ │ └── Solution.java │ │ ├── JZ_Offer_II_029 │ │ └── Solution.java │ │ ├── JZ_Offer_II_030 │ │ └── RandomizedSet.java │ │ ├── JZ_Offer_II_031 │ │ └── LRUCache.java │ │ ├── JZ_Offer_II_032 │ │ └── Solution.java │ │ ├── JZ_Offer_II_033 │ │ └── Solution.java │ │ ├── JZ_Offer_II_034 │ │ └── Solution.java │ │ ├── JZ_Offer_II_035 │ │ └── Solution.java │ │ ├── JZ_Offer_II_036 │ │ └── Solution.java │ │ ├── JZ_Offer_II_037 │ │ └── Solution.java │ │ ├── JZ_Offer_II_038 │ │ └── Solution.java │ │ ├── JZ_Offer_II_039 │ │ └── Solution.kt │ │ ├── JZ_Offer_II_059 │ │ └── KthLargest.java │ │ ├── JZ_Offer_II_062 │ │ └── Trie.java │ │ ├── JZ_Offer_II_063 │ │ └── Solution.java │ │ ├── JZ_Offer_II_064 │ │ ├── MagicDictionary.java │ │ └── MagicDictionary2.java │ │ ├── JZ_Offer_II_065 │ │ └── Solution.java │ │ ├── JZ_Offer_II_066 │ │ └── MapSum.java │ │ ├── JZ_Offer_II_067 │ │ └── Solution.java │ │ ├── JZ_Offer_II_068 │ │ └── Solution.java │ │ ├── JZ_Offer_II_088 │ │ └── Solution.java │ │ ├── JZ_Offer_II_089 │ │ └── Solution.java │ │ ├── JZ_Offer_II_090 │ │ └── Solution.java │ │ ├── JZ_Offer_II_105 │ │ └── Solution.java │ │ ├── LCP_019 │ │ └── Solution.java │ │ ├── LCP_022 │ │ └── Solution.java │ │ ├── MST_0808_Permutation_II_LCCI │ │ └── Solution.java │ │ ├── MST_1705_Find_Longest_Subarray_LCCI │ │ └── Solution.java │ │ ├── NO_0001_Two_Sum │ │ └── Solution.java │ │ ├── NO_0002_Add_Two_Numbers │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0003_Longest_Substring_Without_Repeating_Characters │ │ └── Solution.java │ │ ├── NO_0004_Median_of_Two_Sorted_Arrays │ │ └── Solution.java │ │ ├── NO_0005_Longest_Palindromic_Substring │ │ └── Solution.java │ │ ├── NO_0007_Reverse_Integer │ │ └── Solution.java │ │ ├── NO_0008_String_to_Integer_atoi │ │ └── Solution.java │ │ ├── NO_0009_Palindrome_Number │ │ └── Solution.java │ │ ├── NO_0010_Regular_Expression_Matching │ │ └── Solution.java │ │ ├── NO_0011_Container_With_Most_Water │ │ └── Solution.java │ │ ├── NO_0012_Integer_to_Roman │ │ └── Solution.java │ │ ├── NO_0013_Roman_to_Integer │ │ └── Solution.java │ │ ├── NO_0014_Longest_Common_Prefix │ │ └── Solution.java │ │ ├── NO_0015_3_Sum │ │ └── Solution.java │ │ ├── NO_0016_3_Sum_Closest │ │ └── Solution.java │ │ ├── NO_0017_Letter_Combinations_of_a_Phone_Number │ │ └── Solution.java │ │ ├── NO_0018_4_Sum │ │ └── Solution.java │ │ ├── NO_0019_Remove_Nth_Node_From_End_of_List │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0020_Valid_Parentheses │ │ └── Solution.java │ │ ├── NO_0021_Merge_Two_Sorted_Lists │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0023_Merge_k_Sorted_Lists │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0024_Swap_Nodes_in_Pairs │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0025_Reverse_Nodes_in_k_Group │ │ ├── ListNode.java │ │ ├── Solution.java │ │ └── Solution2.java │ │ ├── NO_0026_Remove_Duplicates_from_Sorted_Array │ │ └── Solution.java │ │ ├── NO_0027_Remove_Element │ │ └── Solution.java │ │ ├── NO_0028_Find_the_Index_of_the_First_Occurrence_in_a_String │ │ ├── Solution.java │ │ └── SolutionKMP.java │ │ ├── NO_0030_Substring_with_Concatenation_of_All_Words │ │ └── Solution.java │ │ ├── NO_0031_Next_Permutation │ │ └── Solution.java │ │ ├── NO_0033_Search_in_Rotated_Sorted_Array │ │ └── Solution.java │ │ ├── NO_0034_Find_First_and_Last_Position_of_Element_in_Sorted_Array │ │ └── Solution.java │ │ ├── NO_0035_Search_Insert_Position │ │ └── Solution.java │ │ ├── NO_0036_Valid_Sudoku │ │ └── Solution.java │ │ ├── NO_0037_Sudoku_Solver │ │ ├── Solution.java │ │ └── Solution2.java │ │ ├── NO_0038_Count_and_Say │ │ └── Solution.java │ │ ├── NO_0039_Combination_Sum │ │ └── Solution.java │ │ ├── NO_0040_Combination_Sum_II │ │ └── Solution.java │ │ ├── NO_0042_Trapping_Rain_Water │ │ └── Solution.java │ │ ├── NO_0045_Jump_Game_II │ │ └── Solution.java │ │ ├── NO_0046_Permutations │ │ └── Solution.java │ │ ├── NO_0047_Permutations_II │ │ └── Solution.java │ │ ├── NO_0048_Rotate_Image │ │ └── Solution.java │ │ ├── NO_0049_Group_Anagrams │ │ └── Solution.java │ │ ├── NO_0050_Pow_x_n │ │ └── Solution.java │ │ ├── NO_0051_N_Queens │ │ └── Solution.java │ │ ├── NO_0052_N_Queens_II │ │ └── Solution.java │ │ ├── NO_0053_Maximum_Subarray │ │ └── Solution.java │ │ ├── NO_0054_Spiral_Matrix │ │ └── Solution.java │ │ ├── NO_0055_Jump_Game │ │ └── Solution.java │ │ ├── NO_0056_Merge_Intervals │ │ └── Solution.java │ │ ├── NO_0057_Insert_Interval │ │ └── Solution.java │ │ ├── NO_0058_Length_of_Last_Word │ │ └── Solution.java │ │ ├── NO_0061_Rotate_List │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0062_Unique_Paths │ │ └── Solution.java │ │ ├── NO_0063_Unique_Paths_II │ │ └── Solution.java │ │ ├── NO_0064_Minimum_Path_Sum │ │ └── Solution.java │ │ ├── NO_0066_Plus_One │ │ └── Solution.java │ │ ├── NO_0067_Add_Binary │ │ └── Solution.java │ │ ├── NO_0069_Sqrt_x │ │ └── Solution.java │ │ ├── NO_0070_Climbing_Stairs │ │ └── Solution.java │ │ ├── NO_0071_Simplify_Path │ │ └── Solution.java │ │ ├── NO_0073_Set_Matrix_Zeroes │ │ └── Solution.java │ │ ├── NO_0074_Search_a_2D_Matrix │ │ └── Solution.java │ │ ├── NO_0075_Sort_Colors │ │ └── Solution.java │ │ ├── NO_0076_Minimum_Window_Substring │ │ └── Solution.java │ │ ├── NO_0077_Combinations │ │ └── Solution.java │ │ ├── NO_0078_Subsets │ │ └── Solution.java │ │ ├── NO_0079_Word_Search │ │ └── Solution.java │ │ ├── NO_0080_Remove_Duplicates_from_Sorted_Array_II │ │ └── Solution.java │ │ ├── NO_0082_Remove_Duplicates_from_Sorted_List_II │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0083_Remove_Duplicates_from_Sorted_List │ │ ├── ListNode.java │ │ ├── NodeUtil.java │ │ └── Solution.java │ │ ├── NO_0086_Partition_List │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0088_Merge_Sorted_Array │ │ └── Solution.java │ │ ├── NO_0090_Subsets_II │ │ └── Solution.java │ │ ├── NO_0091_Decode_Ways │ │ └── Solution.java │ │ ├── NO_0092_Reverse_Linked_List_II │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0093_Restore_IP_Addresses │ │ └── Solution.java │ │ ├── NO_0094_Binary_Tree_Inorder_Traversal │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0098_Validate_Binary_Search_Tree │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0100_Same_Tree │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0101_Symmetric_Tree │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0102_Binary_Tree_Level_Order_Traversal │ │ ├── Pair.java │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0103_Binary_Tree_Zigzag_Level_Order_Traversal │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0104_Maximum_Depth_of_Binary_Tree │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0105_Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0106_Construct_Binary_Tree_from_Inorder_and_Postorder_Traversal │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0107_Binary_Tree_Level_Order_Traversal_II │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0108_Convert_Sorted_Array_to_Binary_Search_Tree │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0110_Balanced_Binary_Tree │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0111_Minimum_Depth_of_Binary_Tree │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0112_Path_Sum │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0113_Path_Sum_II │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0116_Populating_Next_Right_Pointers_in_Each_Node │ │ ├── Node.java │ │ └── Solution.java │ │ ├── NO_0117_Populating_Next_Right_Pointers_in_Each_Node_II │ │ ├── Node.java │ │ └── Solution.java │ │ ├── NO_0118_Pascals_Triangle │ │ └── Solution.java │ │ ├── NO_0119_Pascals_Triangle_II │ │ └── Solution.java │ │ ├── NO_0120_Triangle │ │ └── Solution.java │ │ ├── NO_0121_Best_Time_to_Buy_and_Sell_Stock │ │ └── Solution.java │ │ ├── NO_0122_Best_Time_to_Buy_and_Sell_Stock_II │ │ └── Solution.java │ │ ├── NO_0125_Valid_Palindrome │ │ └── Solution.java │ │ ├── NO_0126_Word_Ladder_II │ │ └── Solution.java │ │ ├── NO_0127_Word_Ladder │ │ └── Solution.java │ │ ├── NO_0129_Sum_Root_to_Leaf_Numbers │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0130_Surrounded_Regions │ │ ├── Pair.java │ │ └── Solution.java │ │ ├── NO_0131_Palindrome_Partitioning │ │ └── Solution.java │ │ ├── NO_0133_Clone_Graph │ │ ├── Solution.java │ │ └── UndirectedGraphNode.java │ │ ├── NO_0134_Gas_Station │ │ └── Solution.java │ │ ├── NO_0136_Single_Number │ │ └── Solution.java │ │ ├── NO_0138_Copy_List_with_Random_Pointer │ │ ├── Node.java │ │ └── Solution.java │ │ ├── NO_0139_Word_Break │ │ └── Solution.java │ │ ├── NO_0140_Word_Break_II │ │ └── Solution.java │ │ ├── NO_0141_Linked_List_Cycle │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0142_Linked_List_Cycle_II │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0143_Reorder_List │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0144_Binary_Tree_Preorder_Traversal │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0145_Binary_Tree_Postorder_Traversal │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0146_LRU_Cache │ │ └── LRUCache.java │ │ ├── NO_0147_Insertion_Sort_List │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0148_Sort_List │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0149_Max_Points_on_a_Line │ │ ├── Point.java │ │ └── Solution.java │ │ ├── NO_0150_Evaluate_Reverse_Polish_Notation │ │ └── Solution.java │ │ ├── NO_0151_Reverse_Words_in_a_String │ │ └── Solution.java │ │ ├── NO_0153_Find_Minimum_in_Rotated_Sorted_Array │ │ └── Solution.java │ │ ├── NO_0154_Find_Minimum_in_Rotated_Sorted_Array_II │ │ └── Solution.java │ │ ├── NO_0155_Min_Stack │ │ ├── S1 │ │ │ ├── MinStack.java │ │ │ └── Pair.java │ │ ├── S2 │ │ │ └── MinStack.java │ │ └── S3 │ │ │ └── MinStack.java │ │ ├── NO_0157_Read_N_Characters_Given_Read4 │ │ ├── Reader4.java │ │ └── Solution.java │ │ ├── NO_0160_Intersection_of_Two_Linked_Lists │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0162_Find_Peak_Element │ │ └── Solution.java │ │ ├── NO_0167_Two_Sum_II_Input_array_is_sorted │ │ └── Solution.java │ │ ├── NO_0168_Excel_Sheet_Column_Title │ │ └── Solution.java │ │ ├── NO_0169_Majority_Element │ │ └── Solution.java │ │ ├── NO_0171_Excel_Sheet_Column_Number │ │ └── Solution.java │ │ ├── NO_0172_Factorial_Trailing_Zeroes │ │ └── Solution.java │ │ ├── NO_0173_Binary_Search_Tree_Iterator │ │ ├── BSTIterator.java │ │ └── TreeNode.java │ │ ├── NO_0189_Rotate_Array │ │ └── Solution.java │ │ ├── NO_0190_Reverse_Bits │ │ └── Solution.java │ │ ├── NO_0191_Number_of_1_Bits │ │ └── Solution.java │ │ ├── NO_0198_House_Robber │ │ └── Solution.java │ │ ├── NO_0199_Binary_Tree_Right_Side_View │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0200_Number_of_Islands │ │ └── Solution.java │ │ ├── NO_0202_Happy_Number │ │ └── Solution.java │ │ ├── NO_0203_Remove_Linked_List_Elements │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0204_Count_Primes │ │ └── Solution.java │ │ ├── NO_0205_Isomorphic_Strings │ │ └── Solution.java │ │ ├── NO_0206_Reverse_Linked_List │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0208_Implement_Trie_Prefix_Tree │ │ ├── one │ │ │ └── Trie.java │ │ ├── three │ │ │ └── Trie.java │ │ └── two │ │ │ └── Trie.java │ │ ├── NO_0209_Minimum_Size_Subarray_Sum │ │ ├── Solution.java │ │ └── Solution2.java │ │ ├── NO_0210_Course_Schedule_II │ │ └── Solution.kt │ │ ├── NO_0211_Add_and_Search_Word_Data_structure_design │ │ └── WordDictionary.java │ │ ├── NO_0213_House_Robber_II │ │ └── Solution.java │ │ ├── NO_0215_Kth_Largest_Element_in_an_Array │ │ └── Solution.java │ │ ├── NO_0216_Combination_Sum_III │ │ └── Solution.java │ │ ├── NO_0217_Contains_Duplicate │ │ └── Solution.java │ │ ├── NO_0219_Contains_Duplicate_II │ │ └── Solution.java │ │ ├── NO_0220_Contains_Duplicate_III │ │ └── Solution.java │ │ ├── NO_0222_Count_Complete_Tree_Nodes │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0225_Implement_Stack_using_Queues │ │ └── MyStack.java │ │ ├── NO_0226_Invert_Binary_Tree │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0230_Kth_Smallest_Element_in_a_BST │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0231_Power_of_Two │ │ └── Solution.java │ │ ├── NO_0232_Implement_Queue_using_Stacks │ │ └── MyQueue.java │ │ ├── NO_0234_Palindrome_Linked_List │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0235_Lowest_Common_Ancestor_of_a_Binary_Search_Tree │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0236_Lowest_Common_Ancestor_of_a_Binary_Tree │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0237_Delete_Node_in_a_Linked_List │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0240_Search_a_2D_Matrix_II │ │ └── Solution.java │ │ ├── NO_0242_Valid_Anagram │ │ └── Solution.java │ │ ├── NO_0257_Binary_Tree_Paths │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0258_Add_Digits │ │ └── Solution.java │ │ ├── NO_0263_Ugly_Number │ │ └── Solution.java │ │ ├── NO_0268_Missing_Number │ │ └── Solution.java │ │ ├── NO_0278_First_Bad_Version │ │ ├── Solution.java │ │ └── VersionControl.java │ │ ├── NO_0279_Perfect_Squares │ │ ├── Pair.java │ │ └── Solution.java │ │ ├── NO_0283_Move_Zeroes │ │ └── Solution.java │ │ ├── NO_0287_Find_the_Duplicate_Number │ │ └── Solution.java │ │ ├── NO_0290_Word_Pattern │ │ └── Solution.java │ │ ├── NO_0292_Nim_Game │ │ └── Solution.java │ │ ├── NO_0297_Serialize_and_Deserialize_Binary_Tree │ │ ├── Codec.java │ │ └── TreeNode.java │ │ ├── NO_0300_Longest_Increasing_Subsequence │ │ └── Solution.java │ │ ├── NO_0303_Range_Sum_Query_Immutable │ │ ├── NumArray1.java │ │ └── NumArray2.java │ │ ├── NO_0307_Range_Sum_Query_Mutable │ │ └── NumArray.java │ │ ├── NO_0309_Best_Time_to_Buy_and_Sell_Stock_with_Cooldown │ │ └── Solution.java │ │ ├── NO_0322_Coin_Change │ │ ├── Solution.java │ │ └── Solution2.java │ │ ├── NO_0326_Power_of_Three │ │ └── Solution.java │ │ ├── NO_0327_Count_of_Range_Sum │ │ └── Solution.java │ │ ├── NO_0328_Odd_Even_Linked_List │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0337_House_Robber_III │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0341_Flatten_Nested_List_Iterator │ │ ├── NestedInteger.java │ │ └── NestedIterator.java │ │ ├── NO_0342_Power_of_Four │ │ └── Solution.java │ │ ├── NO_0343_Integer_Break │ │ └── Solution.java │ │ ├── NO_0344_Reverse_String │ │ └── Solution.java │ │ ├── NO_0345_Reverse_Vowels_of_a_String │ │ └── Solution.java │ │ ├── NO_0347_Top_K_Frequent_Elements │ │ └── Solution.java │ │ ├── NO_0349_Intersection_of_Two_Arrays │ │ └── Solution.java │ │ ├── NO_0350_Intersection_of_Two_Arrays_II │ │ └── Solution.java │ │ ├── NO_0367_Valid_Perfect_Square │ │ └── Solution.java │ │ ├── NO_0371_Sum_of_Two_Integers │ │ └── Solution.java │ │ ├── NO_0374_Guess_Number_Higher_or_Lower │ │ ├── GuessGame.java │ │ └── Solution.java │ │ ├── NO_0376_Wiggle_Subsequence │ │ └── Solution.java │ │ ├── NO_0377_Combination_Sum_IV │ │ └── Solution.java │ │ ├── NO_0380_Insert_Delete_GetRandom_O_1 │ │ └── RandomizedSet.java │ │ ├── NO_0381_Insert_Delete_GetRandom_O1_Duplicates_allowed │ │ └── RandomizedCollection.java │ │ ├── NO_0384_Shuffle_an_Array │ │ └── Solution.java │ │ ├── NO_0387_First_Unique_Character_in_a_String │ │ └── Solution.java │ │ ├── NO_0392_Is_Subsequence │ │ └── Solution.java │ │ ├── NO_0394_Decode_String │ │ └── Solution.java │ │ ├── NO_0401_Binary_Watch │ │ └── Solution.java │ │ ├── NO_0404_Sum_of_Left_Leaves │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0406_Queue_Reconstruction_by_Height │ │ └── Solution.java │ │ ├── NO_0412_Fizz_Buzz │ │ └── Solution.java │ │ ├── NO_0416_Partition_Equal_Subset_Sum │ │ └── Solution.java │ │ ├── NO_0417_Pacific_Atlantic_Water_Flow │ │ └── Solution.java │ │ ├── NO_0429_N_ary_Tree_Level_Order_Traversal │ │ ├── Node.java │ │ └── Solution.java │ │ ├── NO_0430_Flatten_a_Multilevel_Doubly_Linked_List │ │ ├── Node.java │ │ └── Solution.java │ │ ├── NO_0435_Non_overlapping_Intervals │ │ ├── Interval.java │ │ └── Solution.java │ │ ├── NO_0437_Path_Sum_III │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0438_Find_All_Anagrams_in_a_String │ │ └── Solution.java │ │ ├── NO_0445_Add_Two_Numbers_II │ │ ├── ListNode.java │ │ └── Solution.java │ │ ├── NO_0447_Number_of_Boomerangs │ │ └── Solution.java │ │ ├── NO_0450_Delete_Node_in_a_BST │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0451_Sort_Characters_By_Frequency │ │ └── Solution.java │ │ ├── NO_0454_4_Sum_II │ │ └── Solution.java │ │ ├── NO_0455_Assign_Cookies │ │ └── Solution.java │ │ ├── NO_0460_LFU_Cache │ │ ├── LFUCache.java │ │ └── Node.java │ │ ├── NO_0461_Hamming_Distance │ │ └── Solution.java │ │ ├── NO_0463_Island_Perimeter │ │ └── Solution.java │ │ ├── NO_0474_Ones_and_Zeroes │ │ └── Solution.java │ │ ├── NO_0485_Max_Consecutive_Ones │ │ └── Solution.java │ │ ├── NO_0494_Target_Sum │ │ └── Solution.java │ │ ├── NO_0495_Teemo_Attacking │ │ └── Solution.java │ │ ├── NO_0498_Diagonal_Traverse │ │ └── Solution.java │ │ ├── NO_0509_Fibonacci_Number │ │ └── Solution.java │ │ ├── NO_0514_Freedom_Trail │ │ └── Solution.java │ │ ├── NO_0519_Random_Flip_Matrix │ │ └── Solution.java │ │ ├── NO_0542_01_Matrix │ │ └── Solution.java │ │ ├── NO_0547_Number_of_Provinces │ │ └── Solution.java │ │ ├── NO_0553_Optimal_Division │ │ └── Solution.java │ │ ├── NO_0554_Brick_Wall │ │ └── Solution.java │ │ ├── NO_0557_Reverse_Words_in_a_String_III │ │ └── Solution.java │ │ ├── NO_0559_Maximum_Depth_of_N_ary_Tree │ │ ├── Node.java │ │ └── Solution.java │ │ ├── NO_0561_Array_Partition │ │ └── Solution.java │ │ ├── NO_0589_N_ary_Tree_Preorder_Traversal │ │ ├── Node.java │ │ └── Solution.java │ │ ├── NO_0590_N_ary_Tree_Postorder_Traversal │ │ ├── Node.java │ │ └── Solution.java │ │ ├── NO_0599_Minimum_Index_Sum_of_Two_Lists │ │ └── Solution.java │ │ ├── NO_0617_Merge_Two_Binary_Trees │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0622_Design_Circular_Queue │ │ └── MyCircularQueue.java │ │ ├── NO_0641_Design_Circular_Deque │ │ └── MyCircularDeque.java │ │ ├── NO_0648_Replace_Words │ │ ├── one │ │ │ └── Solution.java │ │ └── two │ │ │ └── Solution.java │ │ ├── NO_0652_Find_Duplicate_Subtrees │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0658_Find_K_Closest_Elements │ │ └── Solution.java │ │ ├── NO_0670_Maximum_Swap │ │ └── Solution.java │ │ ├── NO_0674_Longest_Continuous_Increasing_Subsequence │ │ └── Solution.java │ │ ├── NO_0677_Map_Sum_Pairs │ │ └── MapSum.java │ │ ├── NO_0680_Valid_Palindrome_II │ │ └── Solution.java │ │ ├── NO_0700_Search_in_a_Binary_Search_Tree │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0701_Insert_into_a_Binary_Search_Tree │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0703_Kth_Largest_Element_in_a_Stream │ │ └── KthLargest.java │ │ ├── NO_0704_Binary_Search │ │ └── Solution.java │ │ ├── NO_0705_Design_HashSet │ │ └── MyHashSet.java │ │ ├── NO_0706_Design_HashMap │ │ └── MyHashMap.java │ │ ├── NO_0707_Design_Linked_List │ │ ├── MyLinkedList.java │ │ └── MyLinkedList1.java │ │ ├── NO_0724_Find_Pivot_Index │ │ └── Solution.java │ │ ├── NO_0729_My_Calendar_I │ │ └── MyCalendar.java │ │ ├── NO_0732_My_Calendar_III │ │ ├── MyCalendarThree.java │ │ └── MyCalendarThree1.java │ │ ├── NO_0733_Flood_Fill │ │ └── Solution.java │ │ ├── NO_0739_Daily_Temperatures │ │ └── Solution.java │ │ ├── NO_0740_Delete_and_Earn │ │ └── Solution.java │ │ ├── NO_0744_Find_Smallest_Letter_Greater_Than_Target │ │ └── Solution.java │ │ ├── NO_0746_Min_Cost_Climbing_Stairs │ │ └── Solution.java │ │ ├── NO_0747_Largest_Number_at_Least_Twice_of_Others │ │ └── Solution.java │ │ ├── NO_0752_Open_the_Lock │ │ ├── Solution.java │ │ └── Solution2.java │ │ ├── NO_0754_Reach_a_Number │ │ └── Solution.java │ │ ├── NO_0771_Jewels_and_Stones │ │ └── Solution.java │ │ ├── NO_0795_Number_of_Subarrays_with_Bounded_Maximum │ │ └── Solution.java │ │ ├── NO_0804_Unique_Morse_Code_Words │ │ └── Solution.java │ │ ├── NO_0827_Making_A_Large_Island │ │ └── Solution.java │ │ ├── NO_0831_Masking_Personal_Information │ │ └── Solution.java │ │ ├── NO_0841_Keys_and_Rooms │ │ └── Solution.java │ │ ├── NO_0843_Guess_the_Word │ │ ├── Master.java │ │ ├── Solution.java │ │ └── Solution2.java │ │ ├── NO_0846_Hand_of_Straights │ │ └── Solution.java │ │ ├── NO_0887_Super_Egg_Drop │ │ └── Solution.java │ │ ├── NO_0895_Maximum_Frequency_Stack │ │ └── FreqStack.java │ │ ├── NO_0899_Orderly_Queue │ │ └── Solution.java │ │ ├── NO_0912_Sort_an_Array │ │ └── Solution.java │ │ ├── NO_0922_Sort_Array_By_Parity_II │ │ └── Solution.java │ │ ├── NO_0929_Unique_Email_Addresses │ │ └── Solution.java │ │ ├── NO_0930_Binary_Subarrays_With_Sum │ │ └── Solution.java │ │ ├── NO_0933_Number_of_Recent_Calls │ │ └── RecentCounter.java │ │ ├── NO_0941_Valid_Mountain_Array │ │ └── Solution.java │ │ ├── NO_0946_Validate_Stack_Sequences │ │ └── Solution.java │ │ ├── NO_0957_Prison_Cells_After_N_Days │ │ └── Solution.java │ │ ├── NO_0958_Check_Completeness_of_a_Binary_Tree │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0959_Regions_Cut_By_Slashes │ │ └── Solution.java │ │ ├── NO_0960_Delete_Columns_to_Make_Sorted_III │ │ └── Solution.java │ │ ├── NO_0961_N_Repeated_Element_in_Size_2N_Array │ │ └── Solution.java │ │ ├── NO_0962_Maximum_Width_Ramp │ │ └── Solution.java │ │ ├── NO_0963_Minimum_Area_Rectangle_II │ │ └── Solution.java │ │ ├── NO_0965_Univalued_Binary_Tree │ │ ├── Solution.java │ │ └── TreeNode.java │ │ ├── NO_0966_Vowel_Spellchecker │ │ └── Solution.java │ │ ├── NO_0967_Numbers_With_Same_Consecutive_Differences │ │ └── Solution.java │ │ ├── NO_0973_K_Closest_Points_to_Origin │ │ └── Solution.java │ │ ├── NO_0974_Subarray_Sums_Divisible_by_K │ │ └── Solution.java │ │ ├── NO_0976_Largest_Perimeter_Triangle │ │ └── Solution.java │ │ ├── NO_1017_Convert_to_Base_2 │ │ └── Solution.java │ │ ├── NO_1030_Matrix_Cells_in_Distance_Order │ │ └── Solution.java │ │ ├── NO_1032_Stream_of_Characters │ │ └── StreamChecker.java │ │ ├── NO_1039_Minimum_Score_Triangulation_of_Polygon │ │ └── Solution.java │ │ ├── NO_1078_Occurrences_After_Bigram │ │ └── Solution.java │ │ ├── NO_1091_Shortest_Path_in_Binary_Matrix │ │ └── Solution.java │ │ ├── NO_1092_Shortest_Common_Supersequence │ │ └── Solution.java │ │ ├── NO_1114_Print_in_Order │ │ └── Foo.java │ │ ├── NO_1115_Print_FooBar_Alternately │ │ └── FooBar.java │ │ ├── NO_1116_Print_Zero_Even_Odd │ │ └── ZeroEvenOdd.java │ │ ├── NO_1117_Building_H2O │ │ └── H2O.java │ │ ├── NO_1122_Relative_Sort_Array │ │ └── Solution.java │ │ ├── NO_1129_Shortest_Path_with_Alternating_Colors │ │ └── Solution.java │ │ ├── NO_1137_Nth_Tribonacci_Number │ │ └── Solution.java │ │ ├── NO_1195_Fizz_Buzz_Multithreaded │ │ └── FizzBuzz.java │ │ ├── NO_1206_Design_Skiplist │ │ └── Skiplist.java │ │ ├── NO_1207_Unique_Number_of_Occurrences │ │ └── Solution.java │ │ ├── NO_1226_The_Dining_Philosophers │ │ └── DiningPhilosophers.java │ │ ├── NO_1254_Number_of_Closed_Islands │ │ └── Solution.java │ │ ├── NO_1356_Sort_Integers_by_The_Number_of_1_Bits │ │ └── Solution.java │ │ ├── NO_135_Candy │ │ └── Solution.java │ │ ├── NO_1370_Increasing_Decreasing_String │ │ └── Solution.java │ │ ├── NO_1507_Reformat_Date │ │ └── Solution.java │ │ ├── NO_1539_Kth_Missing_Positive_Number │ │ └── Solution.java │ │ ├── NO_1603_Design_Parking_System │ │ └── ParkingSystem.java │ │ ├── NO_1614_Maximum_Nesting_Depth_of_the_Parentheses │ │ └── Solution.java │ │ ├── NO_1615_Maximal_Network_Rank │ │ └── Solution.java │ │ ├── NO_1616_Split_Two_Strings_to_Make_Palindrome │ │ └── Solution.java │ │ ├── NO_1625_Lexicographically_Smallest_String_After_Applying_Operations │ │ └── Solution.java │ │ ├── NO_1626_Best_Team_With_No_Conflicts │ │ └── Solution.java │ │ ├── NO_1630_Arithmetic_Subarrays │ │ └── Solution.java │ │ ├── NO_1637_Widest_Vertical_Area_Between_Two_Points_Containing_No_Points │ │ └── Solution.java │ │ ├── NO_1638_Count_Substrings_That_Differ_by_One_Character │ │ └── Solution.java │ │ ├── NO_1991_Find_the_Middle_Index_in_Array │ │ └── Solution.java │ │ ├── NO_2389_Longest_Subsequence_With_Limited_Sum │ │ └── Solution.java │ │ ├── NO_2469_Convert_the_Temperature │ │ └── Solution.java │ │ ├── NO_2488_Count_Subarrays_With_Median_K │ │ └── Solution.java │ │ ├── NO_6315_Count_the_Number_of_Vowel_Strings_in_Range │ │ └── Solution.java │ │ ├── NO_6316_Rearrange_Array_to_Maximize_Prefix_Score │ │ └── Solution.java │ │ ├── NO_6317_Count_the_Number_of_Beautiful_Subarrays │ │ └── Solution.java │ │ ├── NO_6318_Minimum_Time_to_Complete_All_Tasks │ │ └── Solution.java │ │ └── NO_856_Score_of_Parentheses │ │ └── Solution.java ├── Kotlin │ ├── .gitignore │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ └── kotlin │ │ └── NO_0746_Min_Cost_Climbing_Stairs │ │ └── Solution.kt └── README.md ├── Nowcoder ├── .gitignore ├── pom.xml └── src │ └── main │ └── java │ ├── HJ001 │ └── Main.java │ ├── HJ002 │ └── Main.java │ ├── HJ003 │ └── Main.java │ ├── HJ004 │ └── Main.java │ ├── HJ005 │ └── Main.java │ ├── HJ008 │ └── Main.java │ ├── HJ014 │ └── Main.java │ ├── HJ017 │ └── Main.java │ ├── HJ020 │ └── Main.java │ ├── HJ023 │ └── Main.java │ ├── HJ027 │ └── Main.java │ ├── HJ033 │ └── Main.java │ ├── HJ068 │ └── Main.java │ ├── HJ101 │ └── Main.java │ ├── HJ106 │ └── Main.java │ ├── NC028 │ └── Solution.java │ ├── NC037 │ ├── Interval.java │ └── Solution.java │ ├── NC052 │ └── Solution.java │ ├── OR026 │ └── Solution.java │ └── STR_TO_IP │ └── Solution.java ├── Programmer-Interview-Guide └── Kotlin │ ├── .gitignore │ ├── build.gradle.kts │ ├── gradle.properties │ ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── settings.gradle.kts │ └── src │ └── main │ └── kotlin │ ├── chapter01 │ ├── CatDogQueue.kt │ ├── GetMinStack.kt │ ├── ReverseStack.kt │ └── TwoStackQueue.kt │ ├── chapter02 │ └── Todo.kt │ ├── chapter03 │ └── Todo.kt │ ├── chapter04 │ └── Todo.kt │ ├── chapter05 │ └── Todo.kt │ ├── chapter06 │ └── Todo.kt │ ├── chapter07 │ └── Todo.kt │ ├── chapter08 │ └── Todo.kt │ └── chapter09 │ └── Todo.kt ├── Python ├── .gitignore └── 001 Two Sum │ └── Solution.py ├── README.md └── assets └── Algorithm-in-Action-logo.png /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea/ 2 | -------------------------------------------------------------------------------- /Algorithms-4th/Java/.gitignore: -------------------------------------------------------------------------------- 1 | ### Maven template 2 | target/ 3 | pom.xml.tag 4 | pom.xml.releaseBackup 5 | pom.xml.versionsBackup 6 | pom.xml.next 7 | release.properties 8 | dependency-reduced-pom.xml 9 | buildNumber.properties 10 | .mvn/timing.properties 11 | # https://github.com/takari/maven-wrapper#usage-without-binary-jar 12 | .mvn/wrapper/maven-wrapper.jar 13 | 14 | ### Example user template template 15 | ### Example user template 16 | 17 | # IntelliJ project files 18 | .idea 19 | *.iml 20 | out 21 | gen 22 | ### Java template 23 | # Compiled class file 24 | *.class 25 | 26 | # Log file 27 | *.log 28 | 29 | # BlueJ files 30 | *.ctxt 31 | 32 | # Mobile Tools for Java (J2ME) 33 | .mtj.tmp/ 34 | 35 | # Package Files # 36 | *.jar 37 | *.war 38 | *.nar 39 | *.ear 40 | *.zip 41 | *.tar.gz 42 | *.rar 43 | 44 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 45 | hs_err_pid* 46 | 47 | -------------------------------------------------------------------------------- /Algorithms-4th/Java/lib/algs4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angus-Liu/Algorithm-in-Action/5aaf7421412556782716054f09cdba82b9321d48/Algorithms-4th/Java/lib/algs4.jar -------------------------------------------------------------------------------- /Algorithms-4th/Java/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.angus 8 | algorithms-4th-java 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 8 13 | 8 14 | 15 | 16 | 17 | 18 | edu.princeton.cs 19 | algs4 20 | 1.0 21 | system 22 | ${basedir}/lib/algs4.jar 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Algorithms-4th/Java/src/main/java/chapter_01/section_01/E_1_1_6.java: -------------------------------------------------------------------------------- 1 | package chapter_01.section_01; 2 | 3 | import edu.princeton.cs.algs4.StdOut; 4 | 5 | public class E_1_1_6 { 6 | public static void main(String[] args) { 7 | int f = 0; 8 | int g = 1; 9 | for (int i = 0; i <= 15; i++) { 10 | // 输出为斐波那契数 11 | StdOut.println(f); 12 | f = f + g; 13 | g = f - g; 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Algorithms-4th/Java/src/main/java/chapter_01/section_03/Bag.java: -------------------------------------------------------------------------------- 1 | package chapter_01.section_03; 2 | 3 | import java.util.Iterator; 4 | 5 | public class Bag implements Iterable { 6 | 7 | public Bag() { 8 | } 9 | 10 | public void add(Item item) { 11 | } 12 | 13 | public boolean isEmpty() { 14 | return false; 15 | } 16 | 17 | public int size() { 18 | return 0; 19 | } 20 | 21 | @Override 22 | public Iterator iterator() { 23 | return null; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Algorithms-4th/Java/src/main/java/chapter_01/section_03/Queue.java: -------------------------------------------------------------------------------- 1 | package chapter_01.section_03; 2 | 3 | import java.util.Iterator; 4 | 5 | public class Queue implements Iterable { 6 | 7 | public Queue() { 8 | } 9 | 10 | public void enqueue(Item item) { 11 | } 12 | 13 | public Item dequeue() { 14 | return null; 15 | } 16 | 17 | public boolean isEmpty() { 18 | return false; 19 | } 20 | 21 | public int size() { 22 | return 0; 23 | } 24 | 25 | @Override 26 | public Iterator iterator() { 27 | return null; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Algorithms-4th/Java/src/main/resources/IntNums.txt: -------------------------------------------------------------------------------- 1 | 12 2 0 1 9 8 88 35 -------------------------------------------------------------------------------- /Codewars/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angus-Liu/Algorithm-in-Action/5aaf7421412556782716054f09cdba82b9321d48/Codewars/README.md -------------------------------------------------------------------------------- /Coding-Interviews/Java/.gitignore: -------------------------------------------------------------------------------- 1 | ### IntelliJ IDEA ### 2 | out 3 | .idea 4 | *.iws 5 | *.iml 6 | *.ipr 7 | 8 | ### VSCode ### 9 | .vscode 10 | -------------------------------------------------------------------------------- /Coding-Interviews/Java/01-Find-in-Partially-Sorted-Matrix/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean Find(int target, int[][] array) { 3 | int row = 0, col = array[0].length - 1; 4 | while (row < array.length && col >= 0) { 5 | if (target > array[row][col]) { 6 | row++; 7 | } else if (target < array[row][col]) { 8 | col--; 9 | } else { 10 | return true; 11 | } 12 | } 13 | return false; 14 | } 15 | 16 | public static void main(String[] args) { 17 | int[][] array = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}}; 18 | System.out.println(new Solution().Find(5, array)); 19 | } 20 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/02-Replace-Blank/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public String replaceSpace(StringBuffer str) { 3 | StringBuilder res = new StringBuilder(); 4 | for (char c : str.toString().toCharArray()) { 5 | if (' ' == c) { 6 | res.append("%20"); 7 | } else { 8 | res.append(c); 9 | } 10 | } 11 | return res.toString(); 12 | } 13 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/03-Print-List-Reversingly/src/ListNode.java: -------------------------------------------------------------------------------- 1 | public class ListNode { 2 | int val; 3 | ListNode next = null; 4 | 5 | ListNode(int val) { 6 | this.val = val; 7 | } 8 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/03-Print-List-Reversingly/src/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | 4 | public class Solution { 5 | public ArrayList printListFromTailToHead(ListNode listNode) { 6 | ArrayList res = new ArrayList<>(); 7 | while (listNode != null) { 8 | res.add(listNode.val); 9 | listNode = listNode.next; 10 | } 11 | Collections.reverse(res); 12 | return res; 13 | } 14 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/04-Construct-Binary-Tree/src/TreeNode.java: -------------------------------------------------------------------------------- 1 | public class TreeNode { 2 | int val; 3 | TreeNode left; 4 | TreeNode right; 5 | 6 | TreeNode(int x) { 7 | val = x; 8 | } 9 | 10 | @Override 11 | public String toString() { 12 | StringBuilder res = new StringBuilder(); 13 | if (this.left != null) { 14 | res.append(left); 15 | } 16 | res.append(val).append(" "); 17 | if (this.right != null) { 18 | res.append(right); 19 | } 20 | return res.toString(); 21 | } 22 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/05-Queue-with-Two-Stacks/src/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Stack; 2 | 3 | public class Solution { 4 | Stack stack1 = new Stack<>(); 5 | Stack stack2 = new Stack<>(); 6 | 7 | public void push(int node) { 8 | while (!stack2.isEmpty()) { 9 | stack1.push(stack2.pop()); 10 | } 11 | stack2.push(node); 12 | while (!stack1.isEmpty()) { 13 | stack2.push(stack1.pop()); 14 | } 15 | } 16 | 17 | public int pop() { 18 | return stack2.pop(); 19 | } 20 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/06-Min-Number-in-Rotated-Array/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int minNumberInRotateArray(int[] array) { 3 | return binarySearch(array, 0, array.length - 1); 4 | } 5 | 6 | private int binarySearch(int[] array, int left, int right) { 7 | int mid = left + (right - left) / 2; 8 | if (array[mid] >= array[left] && array[mid] <= array[right]) { 9 | return array[left]; 10 | } else if (array[mid] < array[left]) { 11 | return binarySearch(array, left, mid); 12 | } else { 13 | // array[mid] > array[right] 14 | return binarySearch(array, mid + 1, right); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/07-Fibonacci/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int Fibonacci(int n) { 3 | if (n < 2) { 4 | return n; 5 | } 6 | int res = 0, one = 0, two = 1; 7 | for (int i = 2; i <= n; i++) { 8 | res = one + two; 9 | one = two; 10 | two = res; 11 | } 12 | return res; 13 | } 14 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/08-Jump-Floor/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * 斐波那契数列的应用 4 | * 5 | * @param target 6 | * @return 7 | */ 8 | public int JumpFloor(int target) { 9 | if (target <= 2) { 10 | return target; 11 | } 12 | int res = 0, one = 1, two = 2; 13 | for (int i = 3; i <= target; i++) { 14 | res = one + two; 15 | one = two; 16 | two = res; 17 | } 18 | return res; 19 | } 20 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/09-Jump-Floor-II/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int JumpFloorII(int target) { 3 | if (target <= 2) { 4 | return target; 5 | } 6 | int res = 0, one = 1, two = 2; 7 | for (int i = 3; i <= target; i++) { 8 | res = one + one + two; 9 | one = two; 10 | two = res; 11 | } 12 | return res; 13 | } 14 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/10-Rect-Cover/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * 斐波拉切数列的应用 4 | * 5 | * @param target 6 | * @return 7 | */ 8 | public int RectCover(int target) { 9 | if (target <= 2) { 10 | return target; 11 | } 12 | int res = 0, one = 1, two = 2; 13 | for (int i = 3; i <= target; i++) { 14 | res = one + two; 15 | one = two; 16 | two = res; 17 | } 18 | return res; 19 | } 20 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/11-Number-of-1-in-Binary/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int NumberOf1(int n) { 3 | int count = 0; 4 | int flag = 1; 5 | while (flag != 0) { 6 | if ((n & flag) != 0) { 7 | count++; 8 | } 9 | flag <<= 1; 10 | } 11 | return count; 12 | } 13 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/12-Power/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public double Power(double base, int exponent) { 3 | return Math.pow(base, exponent); 4 | } 5 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/14-Kth-Node-from-End/src/ListNode.java: -------------------------------------------------------------------------------- 1 | public class ListNode { 2 | int val; 3 | ListNode next = null; 4 | 5 | ListNode(int val) { 6 | this.val = val; 7 | } 8 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/14-Kth-Node-from-End/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public ListNode FindKthToTail(ListNode head, int k) { 3 | if (head == null || k == 0) { 4 | return null; 5 | } 6 | ListNode start = head, end = head; 7 | for (int i = 0; i < k - 1; i++) { 8 | if (end.next != null) { 9 | end = end.next; 10 | } else { 11 | return null; 12 | } 13 | } 14 | while (end.next != null) { 15 | start = start.next; 16 | end = end.next; 17 | } 18 | return start; 19 | } 20 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/15-Reverse-List/src/ListNode.java: -------------------------------------------------------------------------------- 1 | public class ListNode { 2 | int val; 3 | ListNode next = null; 4 | 5 | ListNode(int val) { 6 | this.val = val; 7 | } 8 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/15-Reverse-List/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public ListNode ReverseList(ListNode head) { 3 | ListNode pre = null; 4 | ListNode cur = head; 5 | ListNode next = null; 6 | while (cur != null) { 7 | next = cur.next; 8 | cur.next = pre; 9 | pre = cur; 10 | cur = next; 11 | } 12 | return pre; 13 | } 14 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/16-Merge-Sorted-Lists/src/ListNode.java: -------------------------------------------------------------------------------- 1 | public class ListNode { 2 | int val; 3 | ListNode next = null; 4 | 5 | ListNode(int val) { 6 | this.val = val; 7 | } 8 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/16-Merge-Sorted-Lists/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public ListNode Merge(ListNode list1, ListNode list2) { 3 | ListNode dummyHead = new ListNode(0); 4 | ListNode cur = dummyHead; 5 | while (list1 != null && list2 != null) { 6 | if (list1.val < list2.val) { 7 | cur.next = list1; 8 | cur = cur.next; 9 | list1 = list1.next; 10 | } else { 11 | cur.next = list2; 12 | cur = cur.next; 13 | list2 = list2.next; 14 | } 15 | } 16 | if (list1 == null) { 17 | cur.next = list2; 18 | } else { 19 | cur.next = list1; 20 | } 21 | return dummyHead.next; 22 | } 23 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/17-Substructure-in-Tree/src/TreeNode.java: -------------------------------------------------------------------------------- 1 | public class TreeNode { 2 | int val; 3 | TreeNode left; 4 | TreeNode right; 5 | 6 | TreeNode(int x) { 7 | val = x; 8 | } 9 | 10 | @Override 11 | public String toString() { 12 | StringBuilder res = new StringBuilder(); 13 | if (this.left != null) { 14 | res.append(left); 15 | } 16 | res.append(val).append(" "); 17 | if (this.right != null) { 18 | res.append(right); 19 | } 20 | return res.toString(); 21 | } 22 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/18-Mirror-of-Binary-Tree/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public void Mirror(TreeNode root) { 3 | if (root == null) { 4 | return; 5 | } 6 | if (root.left != null) { 7 | Mirror(root.left); 8 | } 9 | if (root.right != null) { 10 | Mirror(root.right); 11 | } 12 | TreeNode temp = root.left; 13 | root.left = root.right; 14 | root.right = temp; 15 | } 16 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/18-Mirror-of-Binary-Tree/src/TreeNode.java: -------------------------------------------------------------------------------- 1 | public class TreeNode { 2 | int val; 3 | TreeNode left; 4 | TreeNode right; 5 | 6 | TreeNode(int x) { 7 | val = x; 8 | } 9 | 10 | @Override 11 | public String toString() { 12 | StringBuilder res = new StringBuilder(); 13 | if (this.left != null) { 14 | res.append(left); 15 | } 16 | res.append(val).append(" "); 17 | if (this.right != null) { 18 | res.append(right); 19 | } 20 | return res.toString(); 21 | } 22 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/20-Min-in-Stack/src/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Stack; 2 | 3 | public class Solution { 4 | 5 | Stack stack = new Stack<>(); 6 | Stack minStack = new Stack<>(); 7 | 8 | public void push(int node) { 9 | stack.push(node); 10 | if (minStack.isEmpty() || minStack.peek() > node) { 11 | minStack.push(node); 12 | } else { 13 | minStack.push(minStack.peek()); 14 | } 15 | } 16 | 17 | public void pop() { 18 | stack.pop(); 19 | minStack.pop(); 20 | } 21 | 22 | public int top() { 23 | return stack.peek(); 24 | } 25 | 26 | public int min() { 27 | return minStack.peek(); 28 | } 29 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/21-Stack-Push-Pop-Order/src/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | 3 | public class Solution { 4 | public boolean IsPopOrder(int[] pushA, int[] popA) { 5 | // 借助一个辅助栈,把输入的第一个序列中的数字依次压入该辅助栈 6 | // 并按照第二个序列的顺序依次从该栈中弹出数字 7 | LinkedList stack = new LinkedList<>(); 8 | int pushIndex = 0, popIndex = 0; 9 | stack.push(pushA[pushIndex++]); 10 | while (popIndex < popA.length) { 11 | // 若栈顶不是待弹出数字,说明还需要从压入序列中压入数字 12 | while (stack.peek() != popA[popIndex]) { 13 | if (pushIndex < pushA.length) { 14 | stack.push(pushA[pushIndex++]); 15 | } else { 16 | return false; 17 | } 18 | } 19 | stack.pop(); 20 | popIndex++; 21 | } 22 | return true; 23 | } 24 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/22-Print-BST-from-Top-to-Bottom/src/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.LinkedList; 3 | import java.util.Queue; 4 | 5 | public class Solution { 6 | public ArrayList PrintFromTopToBottom(TreeNode root) { 7 | ArrayList res = new ArrayList<>(); 8 | if (root != null) { 9 | Queue queue = new LinkedList<>(); 10 | queue.add(root); 11 | while (!queue.isEmpty()) { 12 | TreeNode node = queue.remove(); 13 | res.add(node.val); 14 | if (node.left != null) { 15 | queue.add(node.left); 16 | } 17 | if (node.right != null) { 18 | queue.add(node.right); 19 | } 20 | } 21 | } 22 | return res; 23 | } 24 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/22-Print-BST-from-Top-to-Bottom/src/TreeNode.java: -------------------------------------------------------------------------------- 1 | public class TreeNode { 2 | int val; 3 | TreeNode left; 4 | TreeNode right; 5 | 6 | TreeNode(int x) { 7 | val = x; 8 | } 9 | 10 | @Override 11 | public String toString() { 12 | StringBuilder res = new StringBuilder(); 13 | if (this.left != null) { 14 | res.append(left); 15 | } 16 | res.append(val).append(" "); 17 | if (this.right != null) { 18 | res.append(right); 19 | } 20 | return res.toString(); 21 | } 22 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/24-Path-in-Tree/src/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class Solution { 4 | 5 | private ArrayList> res = new ArrayList<>(); 6 | private ArrayList path = new ArrayList<>(); 7 | 8 | public ArrayList> FindPath(TreeNode root, int target) { 9 | findInPath(root, target, 0); 10 | return res; 11 | } 12 | 13 | private void findInPath(TreeNode node, int target, int sum) { 14 | if (node == null) { 15 | return; 16 | } 17 | sum += node.val; 18 | path.add(node.val); 19 | // 必须保证是叶子节点 20 | if (sum == target && node.left == null && node.right == null) { 21 | res.add(new ArrayList<>(path)); 22 | } else if (sum < target) { 23 | findInPath(node.left, target, sum); 24 | findInPath(node.right, target, sum); 25 | } 26 | path.remove(path.size() - 1); 27 | } 28 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/24-Path-in-Tree/src/TreeNode.java: -------------------------------------------------------------------------------- 1 | public class TreeNode { 2 | int val; 3 | TreeNode left; 4 | TreeNode right; 5 | 6 | TreeNode(int x) { 7 | val = x; 8 | } 9 | 10 | @Override 11 | public String toString() { 12 | StringBuilder res = new StringBuilder(); 13 | if (this.left != null) { 14 | res.append(left); 15 | } 16 | res.append(val).append(" "); 17 | if (this.right != null) { 18 | res.append(right); 19 | } 20 | return res.toString(); 21 | } 22 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/25-Copy-Complex-List/src/RandomListNode.java: -------------------------------------------------------------------------------- 1 | public class RandomListNode { 2 | int label; 3 | RandomListNode next = null; 4 | RandomListNode random = null; 5 | 6 | RandomListNode(int label) { 7 | this.label = label; 8 | } 9 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/26-Convert-Binary-Search-Tree/src/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class Solution { 5 | 6 | private List nodes = new ArrayList<>(); 7 | 8 | public TreeNode Convert(TreeNode pRootOfTree) { 9 | if (pRootOfTree == null) return null; 10 | inOrderTraverse(pRootOfTree); 11 | for (int i = 0; i < nodes.size(); i++) { 12 | if (i - 1 >= 0) 13 | nodes.get(i).left = nodes.get(i - 1); 14 | if (i + 1 < nodes.size()) 15 | nodes.get(i).right = nodes.get(i + 1); 16 | } 17 | return nodes.get(0); 18 | } 19 | 20 | private void inOrderTraverse(TreeNode node) { 21 | if (node == null) return; 22 | inOrderTraverse(node.left); 23 | nodes.add(node); 24 | inOrderTraverse(node.right); 25 | } 26 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/26-Convert-Binary-Search-Tree/src/TreeNode.java: -------------------------------------------------------------------------------- 1 | public class TreeNode { 2 | int val; 3 | TreeNode left; 4 | TreeNode right; 5 | 6 | TreeNode(int x) { 7 | val = x; 8 | } 9 | 10 | @Override 11 | public String toString() { 12 | StringBuilder res = new StringBuilder(); 13 | if (this.left != null) { 14 | res.append(left); 15 | } 16 | res.append(val).append(" "); 17 | if (this.right != null) { 18 | res.append(right); 19 | } 20 | return res.toString(); 21 | } 22 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/28-More-Than-Half-Number/src/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Map; 3 | 4 | public class Solution { 5 | public int MoreThanHalfNum_Solution(int[] array) { 6 | if (array == null || array.length == 0) { 7 | return 0; 8 | } 9 | Map map = new HashMap<>(); 10 | for (int i : array) { 11 | int time = map.getOrDefault(i, 0) + 1; 12 | if (time > array.length / 2) return i; 13 | map.put(i, time); 14 | } 15 | return 0; 16 | } 17 | 18 | public static void main(String[] args) { 19 | int[] array = {1, 2, 3, 2, 2, 2, 5, 4, 2}; 20 | System.out.println(new Solution().MoreThanHalfNum_Solution(array)); 21 | } 22 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/29-K-Least-Numbers/src/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Comparator; 3 | import java.util.PriorityQueue; 4 | 5 | public class Solution { 6 | public ArrayList GetLeastNumbers_Solution(int[] input, int k) { 7 | if (k == 0 || k > input.length) { 8 | return new ArrayList<>(); 9 | } 10 | // 先使用最大堆保存 k 个数,此后,每次和堆顶比较,若比堆顶小,移除堆顶,新数入堆 11 | PriorityQueue maxHeap = new PriorityQueue<>(k, Comparator.reverseOrder()); 12 | for (int i = 0; i < input.length; i++) { 13 | if (maxHeap.size() != k) { 14 | maxHeap.add(input[i]); 15 | } else if (input[i] <= maxHeap.peek()) { 16 | maxHeap.remove(); 17 | maxHeap.add(input[i]); 18 | } 19 | } 20 | return new ArrayList<>(maxHeap); 21 | } 22 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/30-Greatest-Sum-of-Subarrays/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int FindGreatestSumOfSubArray(int[] array) { 3 | int res = array[0]; 4 | int sum = 0; 5 | for (int num : array) { 6 | // 正值继续传递,负值及时终止 7 | if (sum > 0) { 8 | sum += num; 9 | } else { 10 | sum = num; 11 | } 12 | res = Math.max(res, sum); 13 | } 14 | return res; 15 | } 16 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/31-Number-of-1/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int NumberOf1Between1AndN_Solution(int n) { 3 | return 0; 4 | } 5 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/32-Sort-Array-for-Min-Number/src/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class Solution { 4 | public String PrintMinNumber(int[] numbers) { 5 | if (numbers == null || numbers.length == 0) { 6 | return ""; 7 | } 8 | // 根据新的排序规则进行排序 9 | return Arrays.stream(numbers) 10 | .boxed() 11 | .map(String::valueOf) 12 | .sorted((str1, str2) -> (str1 + str2).compareTo(str2 + str1)) 13 | .reduce((str1, str2) -> str1 + str2) 14 | .get(); 15 | } 16 | 17 | public static void main(String[] args) { 18 | int[] numbers = {3, 32, 321, 2, 11}; 19 | String res = new Solution().PrintMinNumber(numbers); 20 | System.out.println(res); 21 | } 22 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/35-Inverse-Pairs/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int InversePairs(int [] array) { 3 | return 0; 4 | } 5 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/36-First-Common-Nodes-in-Lists/src/ListNode.java: -------------------------------------------------------------------------------- 1 | public class ListNode { 2 | int val; 3 | ListNode next = null; 4 | 5 | ListNode(int val) { 6 | this.val = val; 7 | } 8 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/36-First-Common-Nodes-in-Lists/src/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.HashSet; 2 | import java.util.Set; 3 | 4 | public class Solution { 5 | public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) { 6 | Set nodes = new HashSet<>(); 7 | while (pHead1 != null) { 8 | nodes.add(pHead1); 9 | pHead1 = pHead1.next; 10 | } 11 | ListNode res = null; 12 | while (pHead2 != null) { 13 | if (nodes.contains(pHead2)) { 14 | res = pHead2; 15 | break; 16 | } 17 | pHead2 = pHead2.next; 18 | } 19 | return res; 20 | } 21 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/38-Tree-Depth/src/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | import java.util.Queue; 3 | 4 | public class Solution { 5 | public int TreeDepth_1(TreeNode root) { 6 | return root == null ? 0 : Math.max(TreeDepth(root.left), TreeDepth(root.right)) + 1; 7 | } 8 | 9 | /** 10 | * 层次遍历 11 | * 12 | * @param root 13 | * @return 14 | */ 15 | public int TreeDepth(TreeNode root) { 16 | if (root == null) return 0; 17 | Queue queue = new LinkedList<>(); 18 | queue.add(root); 19 | int level = 0; 20 | while (!queue.isEmpty()) { 21 | level++; 22 | int size = queue.size(); 23 | while (size-- > 0) { 24 | TreeNode n = queue.remove(); 25 | if (n.left != null) queue.add(n.left); 26 | if (n.right != null) queue.add(n.right); 27 | } 28 | } 29 | return level; 30 | } 31 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/38-Tree-Depth/src/TreeNode.java: -------------------------------------------------------------------------------- 1 | public class TreeNode { 2 | int val; 3 | TreeNode left; 4 | TreeNode right; 5 | 6 | TreeNode(int x) { 7 | val = x; 8 | } 9 | 10 | @Override 11 | public String toString() { 12 | StringBuilder res = new StringBuilder(); 13 | if (this.left != null) { 14 | res.append(left); 15 | } 16 | res.append(val).append(" "); 17 | if (this.right != null) { 18 | res.append(right); 19 | } 20 | return res.toString(); 21 | } 22 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/39-Balanced-Binary-Tree/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | private boolean isBalanced = true; 4 | 5 | public boolean IsBalanced_Solution(TreeNode root) { 6 | getHigh(root); 7 | return isBalanced; 8 | } 9 | 10 | /** 11 | * 借助于后序遍历,每个节点只需遍历一次 12 | * 13 | * @param node 14 | * @return 15 | */ 16 | private int getHigh(TreeNode node) { 17 | if (node == null) { 18 | return 0; 19 | } 20 | int leftHigh = getHigh(node.left); 21 | int rightHigh = getHigh(node.right); 22 | if (Math.abs(leftHigh - rightHigh) > 1) { 23 | isBalanced = false; 24 | } 25 | return Math.max(leftHigh, rightHigh) + 1; 26 | } 27 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/39-Balanced-Binary-Tree/src/TreeNode.java: -------------------------------------------------------------------------------- 1 | public class TreeNode { 2 | int val; 3 | TreeNode left; 4 | TreeNode right; 5 | 6 | TreeNode(int x) { 7 | val = x; 8 | } 9 | 10 | @Override 11 | public String toString() { 12 | StringBuilder res = new StringBuilder(); 13 | if (this.left != null) { 14 | res.append(left); 15 | } 16 | res.append(val).append(" "); 17 | if (this.right != null) { 18 | res.append(right); 19 | } 20 | return res.toString(); 21 | } 22 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/43-Left-Rotate-String/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public String LeftRotateString(String str, int n) { 3 | if (str == null || str.length() == 0) return str; 4 | char[] chArr = str.toCharArray(); 5 | n %= chArr.length; 6 | reverse(chArr, 0, chArr.length - 1); 7 | reverse(chArr, 0, chArr.length - n - 1); 8 | reverse(chArr, chArr.length - n, chArr.length - 1); 9 | return new String(chArr); 10 | } 11 | 12 | private void reverse(char[] chArr, int start, int end) { 13 | while (start < end) { 14 | char ch = chArr[start]; 15 | chArr[start++] = chArr[end]; 16 | chArr[end--] = ch; 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/44-Reverse-Words-in-Sentence/src/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class Solution { 4 | public String ReverseSentence(String str) { 5 | if (str == null || str.length() == 0) return str; 6 | String[] words = str.split(" "); 7 | if (words.length <= 1) return str; 8 | int start = 0, end = words.length - 1; 9 | while (start < end) { 10 | String temp = words[start]; 11 | words[start++] = words[end]; 12 | words[end--] = temp; 13 | } 14 | return Arrays.stream(words) 15 | .reduce((str1, str2) -> str1 + " " + str2) 16 | .get(); 17 | } 18 | 19 | public static void main(String[] args) { 20 | String res = new Solution().ReverseSentence(" "); 21 | System.out.println("res = " + res); 22 | } 23 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/45-Continuous-Cards/src/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class Solution { 4 | public boolean isContinuous(int[] numbers) { 5 | if (numbers == null || numbers.length == 0) return false; 6 | Arrays.sort(numbers); 7 | // 记录空缺数 8 | int vacancyCount = 0; 9 | // 记录大小王的个数 10 | int kingCount = 0; 11 | for (int i = 0; i < numbers.length; i++) { 12 | if (numbers[i] == 0) kingCount++; 13 | else if (i + 1 < numbers.length) { 14 | // 两数相等,不可能是顺子 15 | if (numbers[i] == numbers[i + 1]) return false; 16 | vacancyCount += numbers[i + 1] - numbers[i] - 1; 17 | } 18 | } 19 | return kingCount >= vacancyCount; 20 | } 21 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/46-Last-Number-in-Circle/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int LastRemaining_Solution(int n, int m) { 3 | if (n < 1 || m < 1) { 4 | return -1; 5 | } 6 | int last = 0; 7 | for (int i = 2; i <= n; i++) { 8 | last = (last + m) % i; 9 | } 10 | return last; 11 | } 12 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/48-Add-Two-Numbers/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int Add(int num1, int num2) { 3 | int sum; 4 | while (num2 != 0) { 5 | // 异或即是不进位下的相加 6 | sum = num1 ^ num2; 7 | // 位与结果左移一位,得到进位结果 8 | num2 = (num1 & num2) << 1; 9 | num1 = sum; 10 | } 11 | return num1; 12 | } 13 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/55-Entry-Node-of-Loop/src/ListNode.java: -------------------------------------------------------------------------------- 1 | public class ListNode { 2 | int val; 3 | ListNode next = null; 4 | 5 | ListNode(int val) { 6 | this.val = val; 7 | } 8 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/55-Entry-Node-of-Loop/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public ListNode EntryNodeOfLoop(ListNode pHead) { 3 | boolean hasLoop = false; 4 | ListNode slow = pHead; 5 | ListNode fast = pHead; 6 | // 先判断是否有循环 7 | while (fast != null && fast.next != null) { 8 | slow = slow.next; 9 | fast = fast.next.next; 10 | if (slow == fast) { 11 | hasLoop = true; 12 | break; 13 | } 14 | } 15 | if (hasLoop) { 16 | while (pHead != slow) { 17 | pHead = pHead.next; 18 | slow = slow.next; 19 | } 20 | return pHead; 21 | } 22 | return null; 23 | } 24 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/62-Kth-Node/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | private int k; 4 | 5 | TreeNode KthNode(TreeNode pRoot, int k) { 6 | this.k = k; 7 | return kthNode(pRoot); 8 | } 9 | 10 | TreeNode kthNode(TreeNode pRoot) { 11 | if (pRoot == null || k == 0) return null; 12 | // 遍历到最左节点 13 | if (pRoot.left != null) { 14 | TreeNode left = KthNode(pRoot.left, k); 15 | if (left != null) { 16 | return left; 17 | } 18 | } 19 | // 遍历pRoot 20 | k--; 21 | // 遍历右子树 22 | if (pRoot.right != null) { 23 | TreeNode right = KthNode(pRoot.right, k); 24 | if (right != null) { 25 | return right; 26 | } 27 | } 28 | return k == 0 ? pRoot : null; 29 | } 30 | } -------------------------------------------------------------------------------- /Coding-Interviews/Java/62-Kth-Node/src/TreeNode.java: -------------------------------------------------------------------------------- 1 | public class TreeNode { 2 | int val = 0; 3 | TreeNode left = null; 4 | TreeNode right = null; 5 | 6 | public TreeNode(int val) { 7 | this.val = val; 8 | 9 | } 10 | } -------------------------------------------------------------------------------- /LeetCode/CPP/.gitignore: -------------------------------------------------------------------------------- 1 | # IntelliJ project files 2 | **/.idea 3 | **/*.iml 4 | **/out 5 | **/gen 6 | **/cmake-build-debug/ 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/100301 Count Pairs That Form a Complete Day II/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.28) 2 | project(100301_Count_Pairs_That_Form_a_Complete_Day_II) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | add_executable(100301_Count_Pairs_That_Form_a_Complete_Day_II main.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/100304 Count Pairs That Form a Complete Day I/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.28) 2 | project(100304_Count_Pairs_That_Form_a_Complete_Day_I) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | add_executable(100304_Count_Pairs_That_Form_a_Complete_Day_I main.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/100304 Count Pairs That Form a Complete Day I/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | int countCompleteDayPairs(vector &hours) { 9 | int res = 0, n = hours.size(); 10 | for (int i = 0; i < n - 1; i++) { 11 | for (int j = i + 1; j < n; j++) { 12 | if ((hours[i] + hours[j]) % 24 == 0) res++; 13 | } 14 | } 15 | return res; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode/CPP/100316 Maximum Total Damage With Spell Casting/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.28) 2 | project(100316_Maximum_Total_Damage_With_Spell_Casting) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | add_executable(100316_Maximum_Total_Damage_With_Spell_Casting main.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/169 Majority Element/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.30) 2 | project(169_Majority_Element) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | add_executable(169_Majority_Element main.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/169 Majority Element/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | int majorityElement(vector& nums) { 9 | int res = 0; 10 | int count = 0; 11 | for (int num : nums) { 12 | if (count == 0) { 13 | res = num; 14 | count++; 15 | } else { 16 | if (num == res) { 17 | count++; 18 | } else { 19 | count--; 20 | } 21 | } 22 | } 23 | return res; 24 | } 25 | }; -------------------------------------------------------------------------------- /LeetCode/CPP/2288 Apply Discount to Prices/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.28) 2 | project(2288_Apply_Discount_to_Prices) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | add_executable(2288_Apply_Discount_to_Prices main.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/26 Remove Duplicates from Sorted Array/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.30) 2 | project(26_Remove_Duplicates_from_Sorted_Array) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | add_executable(26_Remove_Duplicates_from_Sorted_Array main.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/26 Remove Duplicates from Sorted Array/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | int removeDuplicates(vector& nums) { 9 | int i = 0; 10 | for (int num: nums) { 11 | if (num != nums[i]) nums[++i] = num; 12 | } 13 | return i + 1; 14 | } 15 | }; 16 | 17 | int main() { 18 | vector nums = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4}; 19 | 20 | Solution solution; 21 | solution.removeDuplicates(nums); 22 | 23 | for (int num : nums) { 24 | cout << num << " "; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode/CPP/2663 Lexicographically Smallest Beautiful String/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.28) 2 | project(2663_Lexicographically_Smallest_Beautiful_String) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | add_executable(2663_Lexicographically_Smallest_Beautiful_String main.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/2663 Lexicographically Smallest Beautiful String/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | string smallestBeautifulString(string s, int k) { 8 | k += 'a'; 9 | int n = s.length(); 10 | int i = n - 1; 11 | s[i]++; 12 | while (i < n) { 13 | if (s[i] == k) { 14 | if (i == 0) { 15 | return ""; 16 | } 17 | s[i] = 'a'; 18 | s[--i]++; 19 | } else if (i && s[i] == s[i - 1] || i > 1 && s[i] == s[i - 2]) { 20 | s[i]++; 21 | } else { 22 | i++; 23 | } 24 | } 25 | return s; 26 | } 27 | }; 28 | 29 | int main() { 30 | std::cout << "Hello, World!" << std::endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /LeetCode/CPP/27 Remove Element/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.30) 2 | project(27_Remove_Element) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | add_executable(27_Remove_Element main.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/27 Remove Element/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | int removeElement(vector& nums, int val) { 9 | int i = 0; 10 | for (int num : nums) { 11 | if (num != val) nums[i++] = num; 12 | } 13 | return i; 14 | } 15 | }; 16 | 17 | int main() { 18 | Solution solution; 19 | vector v = {0,1,2,2,3,0,4,2}; 20 | int res = solution.removeElement(v, 2); 21 | cout << "res: " << res << endl; 22 | for (int num : v) { 23 | cout << num << " "; 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/CPP/2713 Maximum Strictly Increasing Cells in a Matrix/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.28) 2 | project(2713_Maximum_Strictly_Increasing_Cells_in_a_Matrix) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | add_executable(2713_Maximum_Strictly_Increasing_Cells_in_a_Matrix main.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/2748 Number of Beautiful Pairs/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.28) 2 | project(2748_Number_of_Beautiful_Pairs) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | add_executable(2748_Number_of_Beautiful_Pairs main.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/2748 Number of Beautiful Pairs/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include <__numeric/gcd_lcm.h> 4 | 5 | using namespace std; 6 | 7 | class Solution { 8 | public: 9 | int countBeautifulPairs(vector& nums) { 10 | int res = 0, n = nums.size(); 11 | for (int i = 0; i < n - 1; ++i) { 12 | while (nums[i] >= 10) { 13 | nums[i] /= 10; 14 | } 15 | for (int j = i + 1; j < n; ++j) { 16 | if (gcd(nums[i], nums[j] % 10) == 1) { 17 | res++; 18 | } 19 | } 20 | } 21 | return res; 22 | } 23 | }; 24 | 25 | int main() { 26 | std::cout << "Hello, World!" << std::endl; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /LeetCode/CPP/2779 Maximum Beauty of an Array After Applying Operation/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.28) 2 | project(2779_Maximum_Beauty_of_an_Array_After_Applying_Operation) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | add_executable(2779_Maximum_Beauty_of_an_Array_After_Applying_Operation Solution.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/2779 Maximum Beauty of an Array After Applying Operation/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | int maximumBeauty(vector &nums, int k) { 9 | ranges::sort(nums); 10 | int res = 0, span = 2 * k; 11 | for (int l = 0, r = 0; r < nums.size(); r++) { 12 | if (nums[r] - nums[l] <= span) { 13 | res = max(res, r - l + 1); 14 | } else { 15 | l++; 16 | } 17 | } 18 | return res; 19 | } 20 | }; 21 | 22 | 23 | int main(int argc, char *argv[]) { 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/CPP/2786 Visit Array Positions to Maximize Score/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.28) 2 | project(2786_Visit_Array_Positions_to_Maximize_Score) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | add_executable(2786_Visit_Array_Positions_to_Maximize_Score Solution.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/2806 Account Balance After Rounded Purchase/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.28) 2 | project(2806_Account_Balance_After_Rounded_Purchase) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | add_executable(2806_Account_Balance_After_Rounded_Purchase Solution.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/2806 Account Balance After Rounded Purchase/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int accountBalanceAfterPurchase1(int purchaseAmount) { 6 | int remainder = purchaseAmount % 10; 7 | return 100 - (remainder < 5 ? purchaseAmount - remainder : purchaseAmount - remainder + 10); 8 | } 9 | 10 | int accountBalanceAfterPurchase(int purchaseAmount) { 11 | return 100 - (purchaseAmount + 5) / 10 * 10; 12 | } 13 | }; 14 | 15 | int main() { 16 | Solution solution; 17 | int res = solution.accountBalanceAfterPurchase(9); 18 | std::cout << res << std::endl; 19 | res = solution.accountBalanceAfterPurchase(15); 20 | std::cout << res << std::endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode/CPP/2813 Maximum Elegance of a K-Length Subsequence/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.28) 2 | project(2813_Maximum_Elegance_of_a_K_Length_Subsequence) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | add_executable(2813_Maximum_Elegance_of_a_K_Length_Subsequence Solution.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/312 Burst Balloons/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.28) 2 | project(0312_Burst_Balloons) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | add_executable(0312_Burst_Balloons Solution.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/312 Burst Balloons/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int maxCoins(vector& nums) { 8 | 9 | } 10 | }; 11 | 12 | int main() { 13 | std::cout << "Hello, World!" << std::endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode/CPP/419 Battleships in a Board/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.28) 2 | project(0419_Battleships_in_a_Board) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | add_executable(0419_Battleships_in_a_Board Solution.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/503 Next Greater Element II/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.28) 2 | project(503_Next_Greater_Element_II) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | add_executable(503_Next_Greater_Element_II main.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/520 Detect Capital/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.28) 2 | project(520_Detect_Capital) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | add_executable(520_Detect_Capital main.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/520 Detect Capital/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | bool detectCapitalUse(string word) { 8 | int upperCnt = ranges::count_if(word, [](char c) { return isupper(c); }); 9 | return upperCnt == 0 || (upperCnt == 1 && isupper(word[0])) || upperCnt == word.size(); 10 | } 11 | }; 12 | 13 | int main() { 14 | std::cout << "Hello, World!" << std::endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode/CPP/521 Longest Uncommon Subsequence I/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.28) 2 | project(0521_Longest_Uncommon_Subsequence_I) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | add_executable(0521_Longest_Uncommon_Subsequence_I Solution.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/521 Longest Uncommon Subsequence I/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int findLUSlength(string a, string b) { 8 | if (a == b) return -1; 9 | return max(a.size(), b.size()); 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LeetCode/CPP/522 Longest Uncommon Subsequence II/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.28) 2 | project(522_Longest_Uncommon_Subsequence_II) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | add_executable(522_Longest_Uncommon_Subsequence_II main.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/80 Remove Duplicates from Sorted Array II/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.30) 2 | project(80_Remove_Duplicates_from_Sorted_Array_II) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | add_executable(80_Remove_Duplicates_from_Sorted_Array_II main.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/80 Remove Duplicates from Sorted Array II/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | int removeDuplicates_1(vector& nums) { 9 | bool hasTwoSame = false; 10 | int l = 0; 11 | for (int r = 1; r < nums.size(); r++) { 12 | if (nums[r] != nums[l]) { 13 | hasTwoSame = false; 14 | nums[++l] = nums[r]; 15 | } else if (!hasTwoSame) { 16 | hasTwoSame = true; 17 | nums[++l] = nums[r]; 18 | } 19 | } 20 | return l + 1; 21 | } 22 | 23 | int removeDuplicates(vector& nums) { 24 | if (nums.size() <= 2) return nums.size(); 25 | int l = 1; 26 | for (int r = 2; r < nums.size(); r++) { 27 | if (nums[r] != nums[l - 1]) { 28 | nums[++l] = nums[r]; 29 | } 30 | } 31 | return l + 1; 32 | } 33 | }; -------------------------------------------------------------------------------- /LeetCode/CPP/88 Merge Sorted Array/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.30) 2 | project(88_Merge_Sorted_Array) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | add_executable(88_Merge_Sorted_Array main.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/88 Merge Sorted Array/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | void merge(vector& nums1, int m, vector& nums2, int n) { 9 | m--; 10 | n--; 11 | for (int i = m + n + 1; i >= 0 && n >= 0; i--) { 12 | if (m >= 0 && nums1[m] > nums2[n]) { 13 | nums1[i] = nums1[m--]; 14 | } else { 15 | nums1[i] = nums2[n--]; 16 | } 17 | } 18 | } 19 | }; 20 | 21 | int main() { 22 | Solution solution; 23 | std::vector nums1 = {1, 2, 3, 0, 0, 0}; 24 | std::vector nums2 = {2, 5, 6}; 25 | solution.merge(nums1, 3, nums2, 3); 26 | 27 | for (int num: nums1) { 28 | std::cout << num << ' '; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /LeetCode/CPP/881 Boats to Save People/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.28) 2 | project(0881_Boats_to_Save_People) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | add_executable(0881_Boats_to_Save_People Solution.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/881 Boats to Save People/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | int numRescueBoats(vector &people, int limit) { 9 | ranges::sort(people); 10 | const int n = static_cast(people.size()); 11 | int res = 0; 12 | for (int i = 0, j = n - 1; i <= j; res++, j--) { 13 | if (people[i] + people[j] <= limit) { 14 | i++; 15 | } 16 | } 17 | return res; 18 | } 19 | }; 20 | 21 | int main(int argc, char *argv[]) { 22 | Solution solution; 23 | vector people{3, 5, 3, 4}; 24 | int num = solution.numRescueBoats(people, 5); 25 | std::cout << num; 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode/CPP/LCP61/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.28) 2 | project(LCP61) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | add_executable(LCP61 main.cpp) 7 | -------------------------------------------------------------------------------- /LeetCode/CPP/LCP61/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | int temperatureTrend(vector& temperatureA, vector& temperatureB) { 9 | auto cmp = [](const int a, const int b) -> int { 10 | return a > b ? 1 : a < b ? -1 : 0; 11 | }; 12 | 13 | int res = 0, sameTrend = 0; 14 | for (int i = 1; i < temperatureA.size(); ++i) { 15 | if (cmp(temperatureA[i], temperatureA[i - 1]) == cmp(temperatureB[i], temperatureB[i - 1])) { 16 | res = max(res, ++sameTrend); 17 | } else { 18 | sameTrend = 0; 19 | } 20 | } 21 | return res; 22 | } 23 | }; 24 | 25 | int main() { 26 | std::cout << "Hello, World!" << std::endl; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_002/Solution.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_002; 2 | 3 | public class Solution { 4 | public String addBinary(String a, String b) { 5 | int n = Math.max(a.length(), b.length()); 6 | char[] sum = new char[n + 1]; // 考虑到进位情况,sum 长度为 n + 1 7 | int carry = 0; // 进位 8 | for (int i = a.length() - 1, j = b.length() - 1, k = n; k >= 0; i--, j--, k--) { 9 | if (i >= 0 && a.charAt(i) == '1') carry++; 10 | if (j >= 0 && b.charAt(j) == '1') carry++; 11 | sum[k] = (char) (carry % 2 + '0'); 12 | carry = carry >= 2 ? 1 : 0; 13 | } 14 | // 头部为 0 时需要去掉 15 | return sum[0] == '1' ? String.valueOf(sum) : String.valueOf(sum, 1, n); 16 | } 17 | 18 | public static void main(String[] args) { 19 | Solution solution = new Solution(); 20 | String res = solution.addBinary("1011", "1011"); 21 | System.out.println("res = " + res); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_006/Solution.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_006; 2 | 3 | public class Solution { 4 | public int[] twoSum(int[] numbers, int target) { 5 | int lo = 0, hi = numbers.length - 1; 6 | while (lo < hi) { 7 | int sum = numbers[lo] + numbers[hi]; 8 | if (sum < target) { 9 | lo++; 10 | } else if (sum > target) { 11 | hi--; 12 | } else { 13 | break; 14 | } 15 | } 16 | return new int[]{lo, hi}; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_008/Solution.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_008; 2 | 3 | public class Solution { 4 | // 滑动窗口 5 | public int minSubArrayLen(int target, int[] nums) { 6 | int res = Integer.MAX_VALUE; 7 | int sum = 0; 8 | for (int l = 0, r = 0; r < nums.length; r++) { 9 | sum += nums[r]; 10 | while (sum >= target) { 11 | res = Math.min(res, r - l + 1); 12 | sum -= nums[l++]; 13 | } 14 | } 15 | return res == Integer.MAX_VALUE ? 0 : res; 16 | } 17 | 18 | public static void main(String[] args) { 19 | Solution solution = new Solution(); 20 | int res = solution.minSubArrayLen(7, new int[]{2, 3, 1, 2, 4, 3}); 21 | System.out.println("res = " + res); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_009/Solution.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_009; 2 | 3 | public class Solution { 4 | 5 | /** 6 | * 滑动窗口法 7 | */ 8 | public int numSubarrayProductLessThanK(int[] nums, int k) { 9 | int res = 0; 10 | int prod = 1; 11 | for (int l = 0, r = 0; r < nums.length; r++) { 12 | prod *= nums[r]; 13 | while (l <= r && prod >= k) { 14 | prod /= nums[l++]; 15 | } 16 | // r >= l 表明 prod < k,此时 l 和 r 之间有多少个数字,就有多少个符合要求的子数组 17 | res += r - l + 1; 18 | } 19 | return res; 20 | } 21 | 22 | public static void main(String[] args) { 23 | Solution solution = new Solution(); 24 | int res = solution.numSubarrayProductLessThanK(new int[]{10, 5, 2, 6}, 100); 25 | System.out.println("res = " + res); 26 | } 27 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_016/Solution.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_016; 2 | 3 | class Solution { 4 | 5 | 6 | /** 7 | * 滑动窗口算法 8 | */ 9 | public int lengthOfLongestSubstring(String s) { 10 | boolean[] win = new boolean[128]; 11 | int maxLen = 0; 12 | for (int l = 0, r = 0; r < s.length(); r++) { 13 | char c = s.charAt(r); 14 | while (win[c]) { 15 | win[s.charAt(l++)] = false; 16 | } 17 | win[c] = true; 18 | maxLen = Math.max(maxLen, r - l + 1); 19 | } 20 | return maxLen; 21 | } 22 | 23 | public static void main(String[] args) { 24 | Solution solution = new Solution(); 25 | int res = solution.lengthOfLongestSubstring("pwwkewa"); 26 | System.out.println("res = " + res); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_018/Solution.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_018; 2 | 3 | class Solution { 4 | public boolean isPalindrome(String s) { 5 | for (int l = 0, r = s.length() - 1; l < r; ) { 6 | char lc = s.charAt(l); 7 | char rc = s.charAt(r); 8 | if (!Character.isLetterOrDigit(lc)) { 9 | l++; 10 | } else if (!Character.isLetterOrDigit(rc)) { 11 | r--; 12 | } else if (Character.toLowerCase(lc) == Character.toLowerCase(rc)) { 13 | l++; 14 | r--; 15 | } else { 16 | return false; 17 | } 18 | } 19 | return true; 20 | } 21 | 22 | public static void main(String[] args) { 23 | Solution solution = new Solution(); 24 | boolean res = solution.isPalindrome("A man, a plan, a canal: Panama"); 25 | System.out.println("res = " + res); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_019/Solution.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_019; 2 | 3 | class Solution { 4 | public boolean validPalindrome(String s) { 5 | int l = 0, r = s.length() - 1; 6 | while (l < r && s.charAt(l) == s.charAt(r)) { 7 | l++; 8 | r--; 9 | } 10 | return l >= r || isPalindrome(s, l + 1, r) || isPalindrome(s, l, r - 1); 11 | } 12 | 13 | private boolean isPalindrome(String s, int l, int r) { 14 | while (l < r) { 15 | if (s.charAt(l++) != s.charAt(r--)) return false; 16 | } 17 | return true; 18 | } 19 | 20 | public static void main(String[] args) { 21 | Solution solution = new Solution(); 22 | boolean res = solution.validPalindrome("abcac"); 23 | System.out.println("res = " + res); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_020/Solution.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_020; 2 | 3 | class Solution { 4 | /** 5 | * 中心拓展 6 | */ 7 | public int countSubstrings(String s) { 8 | int cnt = 0; 9 | char[] chars = s.toCharArray(); 10 | for (int i = 0; i < s.length(); i++) { 11 | cnt += countPalindrome(chars, i, i); 12 | cnt += countPalindrome(chars, i, i + 1); 13 | } 14 | return cnt; 15 | } 16 | 17 | private int countPalindrome(char[] chars, int l, int r) { 18 | int cnt = 0; 19 | while (l >= 0 && r < chars.length && chars[l--] == chars[r++]) { 20 | cnt++; 21 | } 22 | return cnt; 23 | } 24 | 25 | public static void main(String[] args) { 26 | Solution solution = new Solution(); 27 | int res = solution.countSubstrings("aaa"); 28 | System.out.println("res = " + res); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_021/ListNode.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_021; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | 7 | ListNode() { 8 | } 9 | 10 | ListNode(int val) { 11 | this.val = val; 12 | } 13 | 14 | ListNode(int val, ListNode next) { 15 | this.val = val; 16 | this.next = next; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_021/Solution.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_021; 2 | 3 | class Solution { 4 | public ListNode removeNthFromEnd(ListNode head, int n) { 5 | ListNode dummy = new ListNode(0); 6 | dummy.next = head; 7 | ListNode front = dummy; 8 | while (n-- > 0) { 9 | front = front.next; 10 | } 11 | ListNode rear = dummy; 12 | while (front.next != null) { 13 | front = front.next; 14 | rear = rear.next; 15 | } 16 | rear.next = rear.next.next; 17 | return dummy.next; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_022/ListNode.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_022; 2 | 3 | class ListNode { 4 | int val; 5 | ListNode next; 6 | 7 | ListNode(int x) { 8 | val = x; 9 | next = null; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_023/ListNode.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_023; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | 7 | ListNode(int x) { 8 | val = x; 9 | next = null; 10 | } 11 | 12 | @Override 13 | public String toString() { 14 | return "ListNode{" + 15 | "val=" + val + 16 | ", next=" + next + 17 | '}'; 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_023/Solution.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_023; 2 | 3 | class Solution { 4 | /** 5 | * 指针追逐 6 | */ 7 | public ListNode getIntersectionNode(ListNode headA, ListNode headB) { 8 | if (headA == null || headB == null) return null; 9 | // 定义两个指针, 第一轮让两个到达末尾的节点指向另一个链表的头部, 10 | // 最后如果相遇则为交点(在第一轮移动中恰好抹除了长度差) 11 | // 两个指针等于移动了相同的距离, 有交点就返回, 无交点就是各走了两条指针的长度 12 | ListNode curA = headA, curB = headB; 13 | // 在这里第一轮体现在 curA 和 curB 第一次到达尾部会移向另一链表的表头, 14 | // 而第二轮体现在如果 curA 或 curB 相交就返回交点, 不相交最后就是 null == null 15 | while (curA != curB) { 16 | curA = curA == null ? headB : curA.next; 17 | curB = curB == null ? headA : curB.next; 18 | } 19 | return curA; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_024/ListNode.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_024; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | 7 | ListNode() { 8 | } 9 | 10 | ListNode(int val) { 11 | this.val = val; 12 | } 13 | 14 | ListNode(int val, ListNode next) { 15 | this.val = val; 16 | this.next = next; 17 | } 18 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_024/Solution.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_024; 2 | 3 | class Solution { 4 | public ListNode reverseList(ListNode head) { 5 | if (head == null || head.next == null) return head; 6 | ListNode pre = null; 7 | ListNode cur = head; 8 | ListNode next; 9 | while (cur != null) { 10 | next = cur.next; 11 | cur.next = pre; 12 | pre = cur; 13 | cur = next; 14 | } 15 | return pre; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_025/ListNode.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_025; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | 7 | ListNode() { 8 | } 9 | 10 | ListNode(int val) { 11 | this.val = val; 12 | } 13 | 14 | ListNode(int val, ListNode next) { 15 | this.val = val; 16 | this.next = next; 17 | } 18 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_026/ListNode.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_026; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | 7 | ListNode() { 8 | } 9 | 10 | ListNode(int val) { 11 | this.val = val; 12 | } 13 | 14 | ListNode(int val, ListNode next) { 15 | this.val = val; 16 | this.next = next; 17 | } 18 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_027/ListNode.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_027; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | 7 | ListNode() { 8 | } 9 | 10 | ListNode(int val) { 11 | this.val = val; 12 | } 13 | 14 | ListNode(int val, ListNode next) { 15 | this.val = val; 16 | this.next = next; 17 | } 18 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_032/Solution.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_032; 2 | 3 | public class Solution { 4 | public boolean isAnagram(String s, String t) { 5 | if (s.length() != t.length() || s.equals(t)) return false; 6 | int[] counts = new int[26]; 7 | for (int i = 0; i < s.length(); i++) { 8 | counts[s.charAt(i) - 'a']++; 9 | counts[t.charAt(i) - 'a']--; 10 | } 11 | for (int c : counts) { 12 | if (c != 0) return false; 13 | } 14 | return true; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_038/Solution.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_038; 2 | 3 | import java.util.Stack; 4 | 5 | class Solution { 6 | public int[] dailyTemperatures(int[] temperatures) { 7 | int n = temperatures.length; 8 | int[] res = new int[n]; 9 | Stack stack = new Stack<>(); 10 | for (int i = 0; i < n; i++) { 11 | while (!stack.empty() && temperatures[i] > temperatures[stack.peek()]) { 12 | res[stack.peek()] = i - stack.pop(); 13 | } 14 | stack.push(i); 15 | } 16 | return res; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_039/Solution.kt: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_039 2 | 3 | import java.util.* 4 | 5 | class Solution { 6 | fun largestRectangleArea(heights: IntArray): Int { 7 | val stack = Stack(); 8 | stack.push(-1); 9 | var maxArea = 0; 10 | for ((i, value) in heights.withIndex()) { 11 | while (stack.peek() != -1 && heights[stack.peek()] >= heights[i]) { 12 | val height = heights[stack.pop()]; 13 | val width = i - stack.peek() - 1; 14 | maxArea = maxArea.coerceAtLeast(height * width); 15 | } 16 | stack.push(i); 17 | } 18 | while (stack.peek() != -1) { 19 | val height = heights[stack.pop()]; 20 | val width = heights.size - stack.peek() - 1; 21 | maxArea = maxArea.coerceAtLeast(height * width); 22 | } 23 | return maxArea; 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_059/KthLargest.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_059; 2 | 3 | import java.util.PriorityQueue; 4 | 5 | class KthLargest { 6 | 7 | private final PriorityQueue pq; 8 | private final int k; 9 | 10 | public KthLargest(int k, int[] nums) { 11 | pq = new PriorityQueue<>(); 12 | this.k = k; 13 | for (int num : nums) { 14 | add(num); 15 | } 16 | } 17 | 18 | public int add(int val) { 19 | pq.add(val); 20 | if (pq.size() > k) { 21 | pq.remove(); 22 | } 23 | assert !pq.isEmpty(); 24 | return pq.peek(); 25 | } 26 | } 27 | 28 | /** 29 | * Your KthLargest object will be instantiated and called as such: 30 | * KthLargest obj = new KthLargest(k, nums); 31 | * int param_1 = obj.add(val); 32 | */ -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_068/Solution.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_068; 2 | 3 | class Solution { 4 | public int searchInsert(int[] nums, int target) { 5 | int l = 0; 6 | int r = nums.length - 1; 7 | while (l <= r) { 8 | int m = l + (r - l) / 2; 9 | if (nums[m] == target) { 10 | return m; 11 | } else if (nums[m] < target) { 12 | l = m + 1; 13 | } else { 14 | r = m - 1; 15 | } 16 | } 17 | return r + 1; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_088/Solution.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_088; 2 | 3 | class Solution { 4 | public int minCostClimbingStairs(int[] cost) { 5 | int n = cost.length; 6 | for (int i = 2; i < n; i++) { 7 | cost[i] += Math.min(cost[i - 1], cost[i - 2]); 8 | } 9 | return Math.min(cost[n - 1], cost[n - 2]); 10 | } 11 | 12 | public static void main(String[] args) { 13 | int[] cost = {1, 100, 1, 1, 1, 100, 1, 1, 100, 1}; 14 | Solution solution = new Solution(); 15 | int res = solution.minCostClimbingStairs(cost); 16 | System.out.println("res = " + res); 17 | } 18 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_089/Solution.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_089; 2 | 3 | class Solution { 4 | /** 5 | * 进一步降低空间复杂度 6 | */ 7 | public int rob(int[] nums) { 8 | int n = nums.length; 9 | if (n == 1) return nums[0]; 10 | nums[1] = Math.max(nums[0], nums[1]); 11 | if (n == 2) return nums[1]; 12 | for (int i = 2; i < n; i++) { 13 | nums[i] = Math.max(nums[i - 1], nums[i] + nums[i - 2]); 14 | } 15 | return nums[n - 1]; 16 | } 17 | 18 | public static void main(String[] args) { 19 | int[] nums = {2, 1, 1, 2}; 20 | Solution solution = new Solution(); 21 | int res = solution.rob(nums); 22 | System.out.println("res = " + res); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/JZ_Offer_II_090/Solution.java: -------------------------------------------------------------------------------- 1 | package JZ_Offer_II_090; 2 | 3 | class Solution { 4 | /** 5 | * 动态规划,在 198 号问题基础上修改 6 | */ 7 | public int rob(int[] nums) { 8 | int n = nums.length; 9 | return switch (n) { 10 | case 0 -> 0; 11 | case 1 -> nums[0]; 12 | case 2 -> Math.max(nums[0], nums[1]); 13 | // 考虑不抢劫 n-1 号房间和不抢劫 0 号房间的情况 14 | default -> Math.max(rob(nums, 0, n - 2), rob(nums, 1, n - 1)); 15 | }; 16 | } 17 | 18 | private int rob(int[] nums, int start, int end) { 19 | int pre = nums[start]; 20 | int cur = Math.max(pre, nums[start + 1]); 21 | for (int i = start + 2; i <= end; i++) { 22 | int tmp = cur; 23 | cur = Math.max((pre + nums[i]), cur); 24 | pre = tmp; 25 | } 26 | return cur; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/LCP_019/Solution.java: -------------------------------------------------------------------------------- 1 | package LCP_019; 2 | 3 | /** 4 | * 秋叶收藏集 5 | * https://leetcode-cn.com/problems/UlBDOe/ 6 | */ 7 | class Solution { 8 | public int minimumOperations(String leaves) { 9 | // TODO: angus on 2020/10/29 10 | return 0; 11 | } 12 | 13 | public static void main(String[] args) { 14 | Solution s = new Solution(); 15 | int res = s.minimumOperations("ryyryyyrryyyyyryyyrrryyyryryyyyryyrrryryyyryrryrrrryyyrrrrryryyrrrrryyyryyryrryryyryyyyryyrryrryryy"); 16 | // 41 17 | System.out.println(res); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0001_Two_Sum/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0001_Two_Sum; 2 | 3 | import java.util.Arrays; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | class Solution { 8 | /** 9 | * 查找表法,将所有元素放入查找表,之后对于每一个元素 a,查找 target - a 是否存在 10 | */ 11 | public int[] twoSum(int[] nums, int target) { 12 | Map map = new HashMap<>(); 13 | for (int i = 0; i < nums.length; i++) { 14 | int diff = target - nums[i]; 15 | if (map.containsKey(diff)) { 16 | return new int[]{map.get(diff), i}; 17 | } 18 | map.put(nums[i], i); 19 | } 20 | throw new IllegalArgumentException("No solution"); 21 | } 22 | 23 | public static void main(String[] args) { 24 | Solution solution = new Solution(); 25 | int[] twoSum = solution.twoSum(new int[]{3,2,4}, 6); 26 | System.out.println(Arrays.toString(twoSum)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0009_Palindrome_Number/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0009_Palindrome_Number; 2 | 3 | class Solution { 4 | public boolean isPalindrome(int x) { 5 | if (x < 0 || (x != 0 && x % 10 == 0)) { 6 | return false; 7 | } 8 | int o = x; 9 | int res = 0; 10 | while (o > 0) { 11 | res = res * 10 + o % 10; 12 | o /= 10; 13 | } 14 | return x == res; 15 | } 16 | 17 | public boolean isPalindrome2(int x) { 18 | String o = String.valueOf(x); 19 | String d = new StringBuilder(o).reverse().toString(); 20 | return o.equals(d); 21 | } 22 | 23 | public static void main(String[] args) { 24 | System.out.println(new Solution().isPalindrome2(121)); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0012_Integer_to_Roman/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0012_Integer_to_Roman; 2 | 3 | class Solution { 4 | private static final int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; 5 | private static final String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; 6 | 7 | public String intToRoman(int num) { 8 | StringBuilder sb = new StringBuilder(); 9 | for (int i = 0; i < values.length && num > 0; i++) { 10 | while (num >= values[i]) { 11 | num -= values[i]; 12 | sb.append(symbols[i]); 13 | } 14 | } 15 | return sb.toString(); 16 | } 17 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0014_Longest_Common_Prefix/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0014_Longest_Common_Prefix; 2 | 3 | class Solution { 4 | public String longestCommonPrefix(String[] strs) { 5 | if (strs == null || strs.length == 0) { 6 | return ""; 7 | } 8 | for (int i = 0; i < strs[0].length(); i++) { 9 | char pre = strs[0].charAt(i); 10 | for (int j = 1; j < strs.length; j++) { 11 | if (i >= strs[j].length() || strs[j].charAt(i) != pre) { 12 | return strs[0].substring(0, i); 13 | } 14 | } 15 | } 16 | return strs[0]; 17 | } 18 | 19 | public static void main(String[] args) { 20 | String[] strs = new String[]{"dog","racecar","car"}; 21 | System.out.println(new Solution().longestCommonPrefix(strs)); 22 | } 23 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0024_Swap_Nodes_in_Pairs/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0024_Swap_Nodes_in_Pairs; 2 | 3 | class Solution { 4 | public ListNode swapPairs(ListNode head) { 5 | // 借助虚拟头结点简化算法 6 | ListNode dummyHead = new ListNode(0); 7 | dummyHead.next = head; 8 | 9 | ListNode p = dummyHead; 10 | while (p.next != null && p.next.next != null) { 11 | ListNode node1 = p.next; 12 | // p.next.next 13 | ListNode node2 = node1.next; 14 | 15 | // 交换位置 16 | node1.next = node2.next; 17 | node2.next = node1; 18 | p.next = node2; 19 | // p 指向下一轮交换的前一个节点 20 | p = node1; 21 | } 22 | return dummyHead.next; 23 | } 24 | 25 | public static void main(String[] args) { 26 | ListNode head = new ListNode(new int[]{1, 2, 3, 4, 5, 6, 7}); 27 | System.out.println(new Solution().swapPairs(head)); 28 | } 29 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0026_Remove_Duplicates_from_Sorted_Array/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0026_Remove_Duplicates_from_Sorted_Array; 2 | 3 | import java.util.Arrays; 4 | 5 | class Solution { 6 | public int removeDuplicates(int[] nums) { 7 | // 双指针 8 | int j = 0; 9 | for (int i = 0; i < nums.length; i++) { 10 | if (nums[i] != nums[j]) { 11 | nums[++j] = nums[i]; 12 | } 13 | } 14 | return j + 1; 15 | } 16 | 17 | public static void main(String[] args) { 18 | Solution solution = new Solution(); 19 | 20 | int[] nums = new int[]{0, 0, 1, 1, 1, 2, 2, 3, 3, 4}; 21 | System.out.println(solution.removeDuplicates(nums)); 22 | System.out.println(Arrays.toString(nums)); 23 | 24 | nums = new int[]{1, 1, 2}; 25 | System.out.println(solution.removeDuplicates(nums)); 26 | System.out.println(Arrays.toString(nums)); 27 | } 28 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0028_Find_the_Index_of_the_First_Occurrence_in_a_String/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0028_Find_the_Index_of_the_First_Occurrence_in_a_String; 2 | 3 | class Solution { 4 | public int strStr(String haystack, String needle) { 5 | // 虽然用库函数很简单,不过应该看看 KMP 6 | // 参考:https://www.zhihu.com/question/21923021/answer/281346746 7 | return haystack.indexOf(needle); 8 | } 9 | 10 | public static void main(String[] args) { 11 | System.out.println(new Solution().strStr("hello", "ello")); 12 | } 13 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0045_Jump_Game_II/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0045_Jump_Game_II; 2 | 3 | class Solution { 4 | public int jump(int[] nums) { 5 | int n = nums.length; 6 | int end = 0; 7 | int maxPos = 0, steps = 0; 8 | for (int i = 0; i < n - 1; i++) { 9 | maxPos = Math.max(maxPos, i + nums[i]); 10 | if (i == end) { 11 | end = maxPos; 12 | steps++; 13 | } 14 | } 15 | return steps; 16 | } 17 | 18 | public static void main(String[] args) { 19 | Solution solution = new Solution(); 20 | int res = solution.jump(new int[]{2, 3, 1, 1, 4}); 21 | System.out.println("res = " + res); 22 | 23 | res = solution.jump(new int[]{2, 1, 1, 1, 4}); 24 | System.out.println("res = " + res); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0049_Group_Anagrams/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0049_Group_Anagrams; 2 | 3 | import java.util.*; 4 | 5 | class Solution { 6 | /** 7 | * 查找表法 8 | */ 9 | public List> groupAnagrams(String[] strs) { 10 | Map> map = new HashMap<>(); 11 | for (String str : strs) { 12 | char[] chars = str.toCharArray(); 13 | Arrays.sort(chars); 14 | // 将每个字符串对应字符数组经排序后得到的字符串作为该字符串的分类标志 15 | String key = new String(chars); 16 | map.computeIfAbsent(key, k -> new ArrayList<>()).add(str); 17 | } 18 | return new ArrayList<>(map.values()); 19 | } 20 | 21 | public static void main(String[] args) { 22 | Solution solution = new Solution(); 23 | List> res = solution.groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"}); 24 | System.out.println(res); 25 | } 26 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0050_Pow_x_n/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0050_Pow_x_n; 2 | 3 | class Solution { 4 | public double myPow_1(double x, int n) { 5 | return Math.pow(x, n); 6 | } 7 | 8 | public double myPow(double x, int n) { 9 | if (n == 0) { 10 | return 1; 11 | } 12 | if (n == 1) { 13 | return x; 14 | } 15 | double res = 1; 16 | long power = Math.abs((long)n); 17 | while (power != 0) { 18 | if ((power & 1) == 1) { 19 | res *= x; 20 | } 21 | x *= x; 22 | power >>= 1; 23 | } 24 | return n < 0 ? 1 / res : res; 25 | } 26 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0053_Maximum_Subarray/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0053_Maximum_Subarray; 2 | 3 | class Solution { 4 | public int maxSubArray(int[] nums) { 5 | int res = nums[0]; 6 | int sum = 0; 7 | for (int num : nums) { 8 | // 正值继续传递,负值及时终止 9 | if (sum > 0) { 10 | sum += num; 11 | } else { 12 | sum = num; 13 | } 14 | res = Math.max(res, sum); 15 | } 16 | return res; 17 | } 18 | 19 | public static void main(String[] args) { 20 | int[] nums = new int[]{1, 2}; 21 | System.out.println(new Solution().maxSubArray(nums)); 22 | } 23 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0058_Length_of_Last_Word/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0058_Length_of_Last_Word; 2 | 3 | class Solution { 4 | public int lengthOfLastWord(String s) { 5 | if (s == null || s.length() == 0) 6 | return 0; 7 | int res = 0; 8 | for (int i = s.length() - 1; i >= 0; i--) { 9 | if (s.charAt(i) != ' ') 10 | res++; 11 | else if (res != 0) 12 | return res; 13 | } 14 | return res; 15 | } 16 | 17 | public static void main(String[] args) { 18 | Solution solution = new Solution(); 19 | System.out.println(solution.lengthOfLastWord("Hello World")); 20 | System.out.println(solution.lengthOfLastWord(" ")); 21 | System.out.println(solution.lengthOfLastWord(" a ")); 22 | } 23 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0062_Unique_Paths/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0062_Unique_Paths; 2 | 3 | class Solution { 4 | public int uniquePaths(int m, int n) { 5 | // 动态规划问题 6 | int[][] memo = new int[n][m]; 7 | for (int i = 0; i < m; i++) { 8 | memo[0][i] = 1; 9 | } 10 | for (int i = 0; i < n; i++) { 11 | memo[i][0] = 1; 12 | } 13 | for (int i = 1; i < n; i++) { 14 | for (int j = 1; j < m; j++) { 15 | memo[i][j] = memo[i - 1][j] + memo[i][j - 1]; 16 | } 17 | } 18 | return memo[n - 1][m - 1]; 19 | } 20 | 21 | public static void main(String[] args) { 22 | System.out.println(new Solution().uniquePaths(7, 3)); 23 | } 24 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0064_Minimum_Path_Sum/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0064_Minimum_Path_Sum; 2 | 3 | class Solution { 4 | public int minPathSum(int[][] grid) { 5 | int n = grid.length; 6 | int m = grid[0].length; 7 | for (int j = 1; j < m; j++) { 8 | grid[0][j] += grid[0][j - 1]; 9 | } 10 | for (int i = 1; i < n; i++) { 11 | grid[i][0] += grid[i - 1][0]; 12 | } 13 | for (int i = 1; i < n; i++) { 14 | for (int j = 1; j < m; j++) { 15 | grid[i][j] += Math.min(grid[i - 1][j], grid[i][j - 1]); 16 | } 17 | } 18 | return grid[n - 1][m - 1]; 19 | } 20 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0066_Plus_One/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0066_Plus_One; 2 | 3 | import java.util.Arrays; 4 | 5 | class Solution { 6 | public int[] plusOne(int[] digits) { 7 | int carry = 1; 8 | for (int i = digits.length - 1; i >= 0; i--) { 9 | digits[i] += carry; 10 | if (digits[i] < 10) { 11 | return digits; 12 | } else { 13 | digits[i] = 0; 14 | } 15 | } 16 | int[] res = new int[digits.length + 1]; 17 | res[0] = 1; 18 | System.arraycopy(digits, 0, res, 1, digits.length); 19 | return res; 20 | } 21 | 22 | public static void main(String[] args) { 23 | int[] digits = new int[]{9, 9, 9, 9, 9, 9, 9, 9}; 24 | Solution solution = new Solution(); 25 | int[] res = solution.plusOne(digits); 26 | System.out.println(Arrays.toString(res)); 27 | } 28 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0067_Add_Binary/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0067_Add_Binary; 2 | 3 | class Solution { 4 | public String addBinary(String a, String b) { 5 | char[] charsA, charsB; 6 | if (a.length() > b.length()) { 7 | charsA = a.toCharArray(); 8 | charsB = b.toCharArray(); 9 | } else { 10 | charsA = b.toCharArray(); 11 | charsB = a.toCharArray(); 12 | } 13 | int carry = 0; 14 | int sum; 15 | for (int i = charsA.length - 1, j = charsB.length - 1; i >= 0; i--, j--) { 16 | if (j>= 0) { 17 | sum = charsA[i] - '0' + charsB[j] - '0' + carry; 18 | } else { 19 | sum = charsA[i] - '0' + carry; 20 | } 21 | charsA[i] = (char) (sum % 2 + '0'); 22 | carry = sum / 2; 23 | } 24 | if (carry != 0) { 25 | return 1 + new String(charsA); 26 | } 27 | return new String(charsA); 28 | } 29 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0069_Sqrt_x/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0069_Sqrt_x; 2 | 3 | class Solution { 4 | public int mySqrt(int x) { 5 | int res = x; 6 | int low = 1; 7 | int high = x / 2; 8 | while (low <= high) { 9 | int mid = low + (high - low) / 2; 10 | // 防止乘法溢出 11 | if (mid < x / mid) { 12 | // res 并不是一次就得到的,而是慢慢逼近 13 | res = mid; 14 | low = mid + 1; 15 | } else if (mid > x / mid ) { 16 | high = mid - 1; 17 | } else { 18 | return mid; 19 | } 20 | } 21 | return res; 22 | } 23 | 24 | public static void main(String[] args) { 25 | int i = new Solution().mySqrt(2147395599); 26 | System.out.println(i); 27 | } 28 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0074_Search_a_2D_Matrix/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0074_Search_a_2D_Matrix; 2 | 3 | class Solution { 4 | public boolean searchMatrix(int[][] matrix, int target) { 5 | if (matrix == null || matrix.length == 0) return false; 6 | int m = 0; 7 | int n = matrix[0].length - 1; 8 | while (m < matrix.length && n >= 0) { 9 | if (matrix[m][n] == target) { 10 | return true; 11 | } else if (matrix[m][n] > target) { 12 | n--; 13 | } else { 14 | m++; 15 | } 16 | } 17 | return false; 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0078_Subsets/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0078_Subsets; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | class Solution { 7 | private List> res = new ArrayList<>(); 8 | 9 | public List> subsets(int[] nums) { 10 | List subset = new ArrayList<>(); 11 | generateSubsets(nums, 0, subset); 12 | return res; 13 | } 14 | 15 | private void generateSubsets(int[] nums, int start, List subset) { 16 | res.add(new ArrayList<>(subset)); 17 | for (int i = start; i < nums.length; i++) { 18 | subset.add(nums[i]); 19 | generateSubsets(nums, i + 1, subset); 20 | subset.remove(subset.size() - 1); 21 | } 22 | } 23 | 24 | public static void main(String[] args) { 25 | System.out.println(new Solution().subsets(new int[]{1, 2, 3})); 26 | } 27 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0083_Remove_Duplicates_from_Sorted_List/ListNode.java: -------------------------------------------------------------------------------- 1 | package NO_0083_Remove_Duplicates_from_Sorted_List; 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | */ 6 | public class ListNode { 7 | int val; 8 | ListNode next; 9 | 10 | ListNode(int x) { 11 | val = x; 12 | } 13 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0094_Binary_Tree_Inorder_Traversal/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0094_Binary_Tree_Inorder_Traversal; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0098_Validate_Binary_Search_Tree/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0098_Validate_Binary_Search_Tree; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0100_Same_Tree/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0100_Same_Tree; 2 | 3 | 4 | 5 | class Solution { 6 | public boolean isSameTree(TreeNode p, TreeNode q) { 7 | if (p == null && q == null) { 8 | return true; 9 | } else if (p == null || q == null) { 10 | // p == null && q != null || p != null && q == null 11 | return false; 12 | } else if (p.val != q.val) { 13 | // p != null && q != null && p.val != q.val 14 | return false; 15 | } 16 | // p != null && q != null && p.val == q.val 17 | return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0100_Same_Tree/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0100_Same_Tree; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0101_Symmetric_Tree/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0101_Symmetric_Tree; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0102_Binary_Tree_Level_Order_Traversal/Pair.java: -------------------------------------------------------------------------------- 1 | package NO_0102_Binary_Tree_Level_Order_Traversal; 2 | 3 | public class Pair { 4 | private K key; 5 | private V value; 6 | 7 | public Pair(K key, V value) { 8 | this.key = key; 9 | this.value = value; 10 | } 11 | 12 | public K getKey() { 13 | return key; 14 | } 15 | 16 | public V getValue() { 17 | return value; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0102_Binary_Tree_Level_Order_Traversal/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0102_Binary_Tree_Level_Order_Traversal; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0103_Binary_Tree_Zigzag_Level_Order_Traversal/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0103_Binary_Tree_Zigzag_Level_Order_Traversal; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0104_Maximum_Depth_of_Binary_Tree/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0104_Maximum_Depth_of_Binary_Tree; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0105_Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0105_Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0106_Construct_Binary_Tree_from_Inorder_and_Postorder_Traversal/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0106_Construct_Binary_Tree_from_Inorder_and_Postorder_Traversal; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0107_Binary_Tree_Level_Order_Traversal_II/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0107_Binary_Tree_Level_Order_Traversal_II; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0108_Convert_Sorted_Array_to_Binary_Search_Tree/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0108_Convert_Sorted_Array_to_Binary_Search_Tree; 2 | 3 | 4 | 5 | class Solution { 6 | public TreeNode sortedArrayToBST(int[] nums) { 7 | // 左右等分建立左右子树,中间节点作为子树根节点,递归该过程 8 | return nums == null ? null : buildTree(nums, 0, nums.length - 1); 9 | } 10 | 11 | private TreeNode buildTree(int[] nums, int l, int r) { 12 | if (l > r) { 13 | return null; 14 | } 15 | int m = l + (r - l) / 2; 16 | TreeNode root = new TreeNode(nums[m]); 17 | root.left = buildTree(nums, l, m - 1); 18 | root.right = buildTree(nums, m + 1, r); 19 | return root; 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0108_Convert_Sorted_Array_to_Binary_Search_Tree/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0108_Convert_Sorted_Array_to_Binary_Search_Tree; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0110_Balanced_Binary_Tree/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0110_Balanced_Binary_Tree; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0111_Minimum_Depth_of_Binary_Tree/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0111_Minimum_Depth_of_Binary_Tree; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0112_Path_Sum/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0112_Path_Sum; 2 | 3 | class Solution { 4 | public boolean hasPathSum(TreeNode root, int sum) { 5 | // 该题的重点在于递归终止条件 6 | if (root == null) { 7 | return false; 8 | } 9 | // 确保该点为叶子节点 10 | if (root.left == null && root.right == null) { 11 | return root.val == sum; 12 | } 13 | return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val); 14 | } 15 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0112_Path_Sum/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0112_Path_Sum; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0113_Path_Sum_II/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0113_Path_Sum_II; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0116_Populating_Next_Right_Pointers_in_Each_Node/Node.java: -------------------------------------------------------------------------------- 1 | package NO_0116_Populating_Next_Right_Pointers_in_Each_Node; 2 | 3 | class Node { 4 | public int val; 5 | public Node left; 6 | public Node right; 7 | public Node next; 8 | 9 | public Node() {} 10 | 11 | public Node(int _val,Node _left,Node _right,Node _next) { 12 | val = _val; 13 | left = _left; 14 | right = _right; 15 | next = _next; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0117_Populating_Next_Right_Pointers_in_Each_Node_II/Node.java: -------------------------------------------------------------------------------- 1 | package NO_0117_Populating_Next_Right_Pointers_in_Each_Node_II; 2 | 3 | class Node { 4 | public int val; 5 | public Node left; 6 | public Node right; 7 | public Node next; 8 | 9 | public Node() {} 10 | 11 | public Node(int _val, Node _left, Node _right, Node _next) { 12 | val = _val; 13 | left = _left; 14 | right = _right; 15 | next = _next; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0118_Pascals_Triangle/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0118_Pascals_Triangle; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | class Solution { 7 | public List> generate(int numRows) { 8 | List> res = new ArrayList<>(); 9 | if (numRows == 0) return res; 10 | res.add(List.of(1)); 11 | for (int i = 0; i < numRows - 1; i++) { 12 | List pre = res.get(i); 13 | List cur = new ArrayList<>(); 14 | cur.add(pre.get(0)); 15 | for (int j = 1; j <= i; j++) { 16 | cur.add(pre.get(j - 1) + pre.get(j)); 17 | } 18 | cur.add(pre.get(i)); 19 | res.add(cur); 20 | } 21 | return res; 22 | } 23 | 24 | public static void main(String[] args) { 25 | List> res = new Solution().generate(10); 26 | res.forEach(System.out::println); 27 | } 28 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0121_Best_Time_to_Buy_and_Sell_Stock/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0121_Best_Time_to_Buy_and_Sell_Stock; 2 | 3 | class Solution { 4 | public int maxProfit(int[] prices) { 5 | int maxProfit = 0; 6 | if (prices == null || prices.length < 2) { 7 | return maxProfit; 8 | } 9 | int minCost = prices[0]; 10 | for (int i = 1; i < prices.length; i++) { 11 | maxProfit = Math.max(maxProfit, prices[i] - minCost); 12 | minCost = Math.min(minCost, prices[i]); 13 | } 14 | return maxProfit; 15 | } 16 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0129_Sum_Root_to_Leaf_Numbers/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0129_Sum_Root_to_Leaf_Numbers; 2 | 3 | class Solution { 4 | public int sumNumbers(TreeNode root) { 5 | // 利用深搜解决 6 | return dfs(root, 0); 7 | } 8 | 9 | private int dfs(TreeNode node, int sum) { 10 | // 重要:非叶子节点,返回 0 11 | if (node == null) { 12 | return 0; 13 | } 14 | sum = sum * 10 + node.val; 15 | if (node.left == null && node.right == null) { 16 | return sum; 17 | } else { 18 | return dfs(node.left, sum) + dfs(node.right, sum); 19 | } 20 | } 21 | 22 | public static void main(String[] args) { 23 | TreeNode root = new TreeNode(1); 24 | root.left = new TreeNode(2); 25 | TreeNode right = new TreeNode(3); 26 | root.right = right; 27 | right.left = new TreeNode(4); 28 | right.right = new TreeNode(5); 29 | System.out.println(new Solution().sumNumbers(root)); 30 | } 31 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0129_Sum_Root_to_Leaf_Numbers/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0129_Sum_Root_to_Leaf_Numbers; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0130_Surrounded_Regions/Pair.java: -------------------------------------------------------------------------------- 1 | package NO_0130_Surrounded_Regions; 2 | 3 | public class Pair { 4 | private final K key; 5 | private final V value; 6 | 7 | public Pair(K key, V value) { 8 | this.key = key; 9 | this.value = value; 10 | } 11 | 12 | public K getKey() { 13 | return key; 14 | } 15 | 16 | public V getValue() { 17 | return value; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0133_Clone_Graph/UndirectedGraphNode.java: -------------------------------------------------------------------------------- 1 | package NO_0133_Clone_Graph; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | class UndirectedGraphNode { 7 | int label; 8 | List neighbors; 9 | 10 | UndirectedGraphNode(int x) { 11 | label = x; 12 | neighbors = new ArrayList(); 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0134_Gas_Station/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0134_Gas_Station; 2 | 3 | /** 4 | * 加油站 5 | * https://leetcode-cn.com/problems/gas-station/ 6 | */ 7 | class Solution { 8 | public int canCompleteCircuit(int[] gas, int[] cost) { 9 | // 走完全程剩余汽油 10 | int restGas = 0; 11 | // 当前油箱汽油 12 | int curGas = 0; 13 | int start = 0; 14 | for (int i = 0; i < gas.length; i++) { 15 | restGas += gas[i] - cost[i]; 16 | curGas += gas[i] - cost[i]; 17 | if (curGas < 0) { 18 | start = i + 1; 19 | curGas = 0; 20 | } 21 | } 22 | return restGas >= 0? start : -1; 23 | } 24 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0138_Copy_List_with_Random_Pointer/Node.java: -------------------------------------------------------------------------------- 1 | package NO_0138_Copy_List_with_Random_Pointer; 2 | 3 | class Node { 4 | public int val; 5 | public Node next; 6 | public Node random; 7 | 8 | public Node() {} 9 | 10 | public Node(int _val,Node _next,Node _random) { 11 | val = _val; 12 | next = _next; 13 | random = _random; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0138_Copy_List_with_Random_Pointer/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0138_Copy_List_with_Random_Pointer; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | class Solution { 7 | public Node copyRandomList(Node head) { 8 | if (head == null) return null; 9 | Node cur = head; 10 | Map map = new HashMap<>(); 11 | while (cur != null) { 12 | map.put(cur, new Node(cur.val, null, null)); 13 | cur = cur.next; 14 | } 15 | map.forEach((k, v) -> { 16 | v.next = map.get(k.next); 17 | v.random = map.get(k.random); 18 | }); 19 | return map.get(head); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0144_Binary_Tree_Preorder_Traversal/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0144_Binary_Tree_Preorder_Traversal; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0145_Binary_Tree_Postorder_Traversal/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0145_Binary_Tree_Postorder_Traversal; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0147_Insertion_Sort_List/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0147_Insertion_Sort_List; 2 | 3 | class Solution { 4 | public ListNode insertionSortList(ListNode head) { 5 | ListNode dummyHead = new ListNode(0); 6 | 7 | while (head != null) { 8 | ListNode node = dummyHead; 9 | while (node.next != null && node.next.val < head.val) { 10 | node = node.next; 11 | } 12 | ListNode temp = head.next; 13 | head.next = node.next; 14 | node.next = head; 15 | head = temp; 16 | } 17 | return dummyHead.next; 18 | } 19 | 20 | public static void main(String[] args) { 21 | ListNode list = new ListNode(new int[]{2, 1, 23, 4, 5, 6, -1, 0}); 22 | System.out.println(new Solution().insertionSortList(list)); 23 | } 24 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0149_Max_Points_on_a_Line/Point.java: -------------------------------------------------------------------------------- 1 | package NO_0149_Max_Points_on_a_Line; 2 | 3 | class Point { 4 | int x; 5 | int y; 6 | 7 | Point() { 8 | x = 0; 9 | y = 0; 10 | } 11 | 12 | Point(int a, int b) { 13 | x = a; 14 | y = b; 15 | } 16 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0151_Reverse_Words_in_a_String/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0151_Reverse_Words_in_a_String; 2 | 3 | public class Solution { 4 | public String reverseWords(String s) { 5 | // 表示一个或多个空格的正则表达式 6 | String[] words = s.trim().split("\\s+"); 7 | StringBuilder res = new StringBuilder(); 8 | for (int i = words.length - 1; i >= 0; i--) { 9 | res.append(words[i]); 10 | if (i != 0) { 11 | res.append(" "); 12 | } 13 | } 14 | return res.toString(); 15 | } 16 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0153_Find_Minimum_in_Rotated_Sorted_Array/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0153_Find_Minimum_in_Rotated_Sorted_Array; 2 | 3 | class Solution { 4 | public int findMin(int[] nums) { 5 | int l = 0, r = nums.length - 1; 6 | while (l < r && nums[l] >= nums[r]) { 7 | int m = (l + r) / 2; 8 | if (nums[m] < nums[r]) { 9 | r = m; // 注意,不能直接赋值为 m + 1,因原 m 可能为最终答案 10 | } else { 11 | l = m + 1; 12 | } 13 | System.out.printf("l = %d, r = %d, m = %d\n", l, r, m); 14 | } 15 | return nums[l]; 16 | } 17 | 18 | public static void main(String[] args) { 19 | Solution solution = new Solution(); 20 | int[] nums = {15, 16, 17, 11, 12, 13}; 21 | int min = solution.findMin(nums); 22 | System.out.println("min = " + min); 23 | } 24 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0154_Find_Minimum_in_Rotated_Sorted_Array_II/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0154_Find_Minimum_in_Rotated_Sorted_Array_II; 2 | 3 | class Solution { 4 | public int findMin(int[] nums) { 5 | int l = 0, r = nums.length - 1; 6 | while (l < r && nums[l] >= nums[r]) { 7 | int m = l + (r - l) / 2; 8 | if (nums[m] < nums[r]) { 9 | r = m; 10 | } else if (nums[m] > nums[r]) { 11 | l = m + 1; 12 | } else { 13 | // nums[m] == nums[r],排除重复值 14 | r -= 1; 15 | } 16 | } 17 | return nums[l]; 18 | } 19 | 20 | public static void main(String[] args) { 21 | Solution solution = new Solution(); 22 | int[] nums = {3, 1, 3, 3}; 23 | int min = solution.findMin(nums); 24 | System.out.println("min = " + min); 25 | } 26 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0155_Min_Stack/S1/Pair.java: -------------------------------------------------------------------------------- 1 | package NO_0155_Min_Stack.S1; 2 | 3 | public class Pair { 4 | private K key; 5 | private V value; 6 | 7 | public Pair(K key, V value) { 8 | this.key = key; 9 | this.value = value; 10 | } 11 | 12 | public K getKey() { 13 | return key; 14 | } 15 | 16 | public V getValue() { 17 | return value; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0157_Read_N_Characters_Given_Read4/Reader4.java: -------------------------------------------------------------------------------- 1 | package NO_0157_Read_N_Characters_Given_Read4; 2 | 3 | public class Reader4 { 4 | int read4(char[] buf4) { 5 | return 0; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0157_Read_N_Characters_Given_Read4/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0157_Read_N_Characters_Given_Read4; 2 | 3 | public class Solution extends Reader4 { 4 | /** 5 | * @param buf Destination buffer 6 | * @param n Number of characters to read 7 | * @return The number of actual characters read 8 | */ 9 | public int read(char[] buf, int n) { 10 | int idx = 0; 11 | char[] buf4 = new char[4]; 12 | int size; 13 | while ((size = read4(buf4)) > 0 && idx < n) { 14 | for (int i = 0; i < size && idx < n; i++, idx++) { 15 | buf[idx++] = buf4[i]; 16 | } 17 | } 18 | return idx; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0160_Intersection_of_Two_Linked_Lists/ListNode.java: -------------------------------------------------------------------------------- 1 | package NO_0160_Intersection_of_Two_Linked_Lists; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | 7 | ListNode(int x) { 8 | val = x; 9 | next = null; 10 | } 11 | 12 | @Override 13 | public String toString() { 14 | return "ListNode{" + 15 | "val=" + val + 16 | ", next=" + next + 17 | '}'; 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0162_Find_Peak_Element/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0162_Find_Peak_Element; 2 | 3 | class Solution { 4 | public int findPeakElement(int[] nums) { 5 | if (nums == null || nums.length == 0) { 6 | return -1; 7 | } 8 | int left = 0, right = nums.length - 1; 9 | while (left <= right) { 10 | int mid = left + (right - left) / 2; 11 | if ((mid == left || nums[mid] > nums[mid - 1]) 12 | && (mid == right || nums[mid] > nums[mid + 1])) { 13 | return mid; 14 | } else if (mid == left || nums[mid] > nums[mid - 1]) { 15 | left = mid + 1; 16 | } else { 17 | right = mid - 1; 18 | } 19 | } 20 | return -1; 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0169_Majority_Element/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0169_Majority_Element; 2 | 3 | import java.util.Arrays; 4 | 5 | class Solution { 6 | public int majorityElement_1(int[] nums) { 7 | Arrays.sort(nums); 8 | return nums[nums.length / 2]; 9 | } 10 | 11 | /** 12 | * 摩尔投票算法 13 | * 14 | * @param nums 15 | * @return 16 | */ 17 | public int majorityElement(int[] nums) { 18 | int res = 0, count = 0; 19 | for (int num : nums) { 20 | if (count == 0) { 21 | res = num; 22 | count++; 23 | } else { 24 | if (num == res) { 25 | count++; 26 | } else { 27 | count--; 28 | } 29 | } 30 | } 31 | return res; 32 | } 33 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0171_Excel_Sheet_Column_Number/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0171_Excel_Sheet_Column_Number; 2 | 3 | class Solution { 4 | public int titleToNumber(String s) { 5 | if (s == null) return 0; 6 | int res = 0; 7 | for (char c : s.toCharArray()) { 8 | res = res * 26 + (c - 'A') + 1; 9 | } 10 | return res; 11 | } 12 | 13 | public static void main(String[] args) { 14 | Solution solution = new Solution(); 15 | String[] strArr = {"A", "B", "C", "Z", "AA", "AB", "ZY"}; 16 | for (String s : strArr) { 17 | System.out.println(solution.titleToNumber(s)); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0172_Factorial_Trailing_Zeroes/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0172_Factorial_Trailing_Zeroes; 2 | 3 | class Solution { 4 | // 只有 2 * 5 才会使末尾为 0,而阶乘中 2 的个数远远大于 5 的个数, 5 | // 比如 20 = 4 * 5,所以只需要计算 5 的个数即可 6 | public int trailingZeroes(int n) { 7 | if(n < 5) return 0; 8 | int t = n / 5; 9 | return t + trailingZeroes(t); 10 | } 11 | 12 | public static void main(String[] args) { 13 | Solution s = new Solution(); 14 | int[] nums = {0, 3, 5, 6, 10, 20, 100}; 15 | for (int n : nums) { 16 | System.out.println(s.trailingZeroes(n)); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0173_Binary_Search_Tree_Iterator/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0173_Binary_Search_Tree_Iterator; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0190_Reverse_Bits/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0190_Reverse_Bits; 2 | 3 | public class Solution { 4 | // you need treat n as an unsigned value 5 | public int reverseBits(int n) { 6 | return Integer.reverse(n); 7 | } 8 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0199_Binary_Tree_Right_Side_View/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0199_Binary_Tree_Right_Side_View; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0202_Happy_Number/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0202_Happy_Number; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | class Solution { 7 | public boolean isHappy(int n) { 8 | Set set = new HashSet<>(); 9 | while (n != 1) { 10 | n = toSumOfSquares(n); 11 | if (set.contains(n)) { 12 | return false; 13 | } else { 14 | set.add(n); 15 | } 16 | } 17 | return true; 18 | } 19 | 20 | private int toSumOfSquares(int n) { 21 | int sum = 0; 22 | do { 23 | int num = n % 10; 24 | sum += num * num; 25 | n = n / 10; 26 | } while (n > 0); 27 | 28 | return sum; 29 | } 30 | 31 | public static void main(String[] args) { 32 | Solution solution = new Solution(); 33 | boolean happy = solution.isHappy(19); 34 | System.out.println(happy); 35 | } 36 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0206_Reverse_Linked_List/ListNode.java: -------------------------------------------------------------------------------- 1 | package NO_0206_Reverse_Linked_List; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | 7 | ListNode(int x) { 8 | val = x; 9 | } 10 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0210_Course_Schedule_II/Solution.kt: -------------------------------------------------------------------------------- 1 | package NO_0210_Course_Schedule_II 2 | 3 | class Solution { 4 | fun findOrder(numCourses: Int, prerequisites: Array): IntArray { 5 | val preMap = HashMap>() 6 | prerequisites.forEach { 7 | preMap.getOrPut(it[0]) { ArrayList() }.add(it[1]) 8 | } 9 | val visited = BooleanArray(numCourses) 10 | val res = IntArray(numCourses) 11 | var ri = 0 12 | for (i in 0.. set = new HashSet<>(); 9 | for (int i = 0; i < nums.length; i++) { 10 | if (set.contains(nums[i])) { 11 | return true; 12 | } 13 | set.add(nums[i]); 14 | } 15 | return false; 16 | } 17 | 18 | public static void main(String[] args) { 19 | Solution solution = new Solution(); 20 | System.out.println(solution.containsDuplicate(new int[]{1, 1, 1, 3, 3, 4, 3, 2, 4, 2})); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0222_Count_Complete_Tree_Nodes/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0222_Count_Complete_Tree_Nodes; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0225_Implement_Stack_using_Queues/MyStack.java: -------------------------------------------------------------------------------- 1 | package NO_0225_Implement_Stack_using_Queues; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | 6 | class MyStack { 7 | 8 | private Queue queue; 9 | 10 | public MyStack() { 11 | queue = new LinkedList<>(); 12 | } 13 | 14 | public void push(int x) { 15 | queue.add(x); 16 | for (int i = 0; i < queue.size() - 1; i++) { 17 | queue.add(queue.poll()); 18 | } 19 | } 20 | 21 | public int pop() { 22 | return queue.poll(); 23 | } 24 | 25 | public int top() { 26 | return queue.peek(); 27 | } 28 | 29 | public boolean empty() { 30 | return queue.isEmpty(); 31 | } 32 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0226_Invert_Binary_Tree/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0226_Invert_Binary_Tree; 2 | 3 | 4 | 5 | class Solution { 6 | public TreeNode invertTree(TreeNode root) { 7 | if (root == null) { 8 | return null; 9 | } 10 | // 递归反转左右子树 11 | invertTree(root.left); 12 | invertTree(root.right); 13 | 14 | // 反转左右子树 15 | TreeNode tempNode = root.left; 16 | root.left = root.right; 17 | root.right = tempNode; 18 | 19 | return root; 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0226_Invert_Binary_Tree/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0226_Invert_Binary_Tree; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0230_Kth_Smallest_Element_in_a_BST/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0230_Kth_Smallest_Element_in_a_BST; 2 | 3 | 4 | 5 | class Solution { 6 | 7 | private int index; 8 | 9 | public int kthSmallest(TreeNode root, int k) { 10 | // 二分查找,中序遍历 11 | int res = 0; 12 | if (root == null) { 13 | return res; 14 | } 15 | res = kthSmallest(root.left, k); 16 | if (index == k) { 17 | return res; 18 | } 19 | if (++index == k) { 20 | return root.val; 21 | } 22 | return kthSmallest(root.right, k); 23 | } 24 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0230_Kth_Smallest_Element_in_a_BST/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0230_Kth_Smallest_Element_in_a_BST; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0232_Implement_Queue_using_Stacks/MyQueue.java: -------------------------------------------------------------------------------- 1 | package NO_0232_Implement_Queue_using_Stacks; 2 | 3 | import java.util.Stack; 4 | 5 | class MyQueue { 6 | 7 | private Stack stack; 8 | private Stack reverseStack; 9 | 10 | public MyQueue() { 11 | stack = new Stack<>(); 12 | reverseStack = new Stack<>(); 13 | } 14 | 15 | public void push(int x) { 16 | while (!stack.isEmpty()) { 17 | reverseStack.add(stack.pop()); 18 | } 19 | reverseStack.add(x); 20 | while (!reverseStack.isEmpty()) { 21 | stack.add(reverseStack.pop()); 22 | } 23 | } 24 | 25 | public int pop() { 26 | return stack.pop(); 27 | } 28 | 29 | public int peek() { 30 | return stack.peek(); 31 | } 32 | 33 | public boolean empty() { 34 | return stack.isEmpty(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0235_Lowest_Common_Ancestor_of_a_Binary_Search_Tree/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0235_Lowest_Common_Ancestor_of_a_Binary_Search_Tree; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0236_Lowest_Common_Ancestor_of_a_Binary_Tree/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0236_Lowest_Common_Ancestor_of_a_Binary_Tree; 2 | 3 | class Solution { 4 | public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { 5 | // LCA 问题,查阅相关资料 6 | if (root == null) { 7 | return root; 8 | } 9 | if (root == p || root == q) { 10 | return root; 11 | } 12 | TreeNode left = lowestCommonAncestor(root.left, p, q); 13 | TreeNode right = lowestCommonAncestor(root.right, p, q); 14 | if (left != null && right != null) { 15 | return root; 16 | } else if (left != null) { 17 | return left; 18 | } else if (right != null) { 19 | return right; 20 | } 21 | return null; 22 | } 23 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0236_Lowest_Common_Ancestor_of_a_Binary_Tree/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0236_Lowest_Common_Ancestor_of_a_Binary_Tree; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0237_Delete_Node_in_a_Linked_List/ListNode.java: -------------------------------------------------------------------------------- 1 | package NO_0237_Delete_Node_in_a_Linked_List; 2 | 3 | /** 4 | * @author Angus 5 | * @date 2018/10/9 6 | */ 7 | public class ListNode { 8 | int val; 9 | ListNode next; 10 | 11 | ListNode(int x) { 12 | val = x; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0237_Delete_Node_in_a_Linked_List/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0237_Delete_Node_in_a_Linked_List; 2 | 3 | 4 | 5 | /** 6 | * Definition for singly-linked list. 7 | * public class ListNode { 8 | * int val; 9 | * ListNode next; 10 | * ListNode(int x) { val = x; } 11 | * } 12 | */ 13 | class Solution { 14 | public void deleteNode(ListNode node) { 15 | // 将待删除节点赋值为下一个节点 16 | node.val = node.next.val; 17 | node.next = node.next.next; 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0240_Search_a_2D_Matrix_II/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0240_Search_a_2D_Matrix_II; 2 | 3 | class Solution { 4 | public boolean searchMatrix(int[][] matrix, int target) { 5 | if (matrix == null || matrix.length == 0) return false; 6 | int m = 0; 7 | int n = matrix[0].length - 1; 8 | while (m < matrix.length && n >= 0) { 9 | if (matrix[m][n] == target) { 10 | return true; 11 | } else if (matrix[m][n] > target) { 12 | n--; 13 | } else { 14 | m++; 15 | } 16 | } 17 | return false; 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0257_Binary_Tree_Paths/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0257_Binary_Tree_Paths; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0258_Add_Digits/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0258_Add_Digits; 2 | 3 | /** 4 | * 各位相加 5 | * https://leetcode-cn.com/problems/add-digits/ 6 | */ 7 | class Solution { 8 | public int addDigits(int num) { 9 | // X = 100*a + 10*b + c = 99*a + 9*b + (a+b+c) 10 | // 对 9 取余(99*a + 9*b 被除掉)即可得到各位之和 11 | if (num <= 9) return num; 12 | int n = num % 9; 13 | return n == 0 ? 9 : n; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0263_Ugly_Number/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0263_Ugly_Number; 2 | 3 | /** 4 | * 丑数 5 | * https://leetcode-cn.com/problems/ugly-number/ 6 | */ 7 | public class Solution { 8 | public boolean isUgly(int num) { 9 | if (num == 0) return false; 10 | if (num == 1) return true; 11 | if (num % 2 == 0) return isUgly(num / 2); 12 | if (num % 3 == 0) return isUgly(num / 3); 13 | if (num % 5 == 0) return isUgly(num / 5); 14 | return false; 15 | } 16 | 17 | public static void main(String[] args) { 18 | Solution s = new Solution(); 19 | int[] nums = {0, 1, 6, 8, 14, Integer.MIN_VALUE, Integer.MAX_VALUE}; 20 | for (int num : nums) { 21 | System.out.println(s.isUgly(num)); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0268_Missing_Number/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0268_Missing_Number; 2 | 3 | import java.util.Arrays; 4 | import java.util.stream.IntStream; 5 | 6 | class Solution { 7 | public int missingNumber_1(int[] nums) { 8 | int n = nums.length; 9 | int sum = 0; 10 | for (int num : nums) { 11 | sum += num; 12 | } 13 | return n * (n + 1) / 2 - sum; 14 | } 15 | 16 | public int missingNumber_2(int[] nums) { 17 | return IntStream.rangeClosed(1,nums.length).sum() - IntStream.of(nums).sum(); 18 | } 19 | 20 | public int missingNumber(int[] nums) { 21 | Arrays.sort(nums); 22 | int res = nums.length; 23 | for (int i = 0; i < nums.length; i++) { 24 | if (i != nums[i]) { 25 | res = i; 26 | break; 27 | } 28 | } 29 | return res; 30 | } 31 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0278_First_Bad_Version/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0278_First_Bad_Version; 2 | 3 | public class Solution extends VersionControl { 4 | public int firstBadVersion(int n) { 5 | // 二分查找的应用 6 | int l = 1; 7 | int r = n; 8 | while (l < r) { 9 | int mid = l + (r - l) / 2; 10 | if (isBadVersion(mid)) { 11 | // 没用 mid - 1,是因为 mid - 1 可能不是 BadVersion,因此需要保留 mid 12 | r = mid; 13 | } else { 14 | l = mid + 1; 15 | } 16 | } 17 | return r; 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0278_First_Bad_Version/VersionControl.java: -------------------------------------------------------------------------------- 1 | package NO_0278_First_Bad_Version; 2 | 3 | /** 4 | * @author Angus 5 | * @date 2018/12/21 6 | */ 7 | public class VersionControl { 8 | boolean isBadVersion(int version){ 9 | return true; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0279_Perfect_Squares/Pair.java: -------------------------------------------------------------------------------- 1 | package NO_0279_Perfect_Squares; 2 | 3 | public class Pair { 4 | private K key; 5 | private V value; 6 | 7 | public Pair(K key, V value) { 8 | this.key = key; 9 | this.value = value; 10 | } 11 | 12 | public K getKey() { 13 | return key; 14 | } 15 | 16 | public V getValue() { 17 | return value; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0292_Nim_Game/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0292_Nim_Game; 2 | 3 | /** 4 | * Nim 游戏 5 | * https://leetcode-cn.com/problems/nim-game/ 6 | */ 7 | class Solution { 8 | public boolean canWinNim(int n) { 9 | // 巴什博奕 10 | return n % 4 != 0; 11 | } 12 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0297_Serialize_and_Deserialize_Binary_Tree/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0297_Serialize_and_Deserialize_Binary_Tree; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0303_Range_Sum_Query_Immutable/NumArray2.java: -------------------------------------------------------------------------------- 1 | package NO_0303_Range_Sum_Query_Immutable; 2 | 3 | /** 4 | * @author Angus 5 | * @date 2018/10/20 6 | */ 7 | public class NumArray2 { 8 | 9 | private int[] nums; 10 | 11 | public NumArray2(int[] nums) { 12 | this.nums = nums; 13 | // 使得的数组的第 i 位存储的是 0-i 位的和 14 | for (int i = 1; i < nums.length; i++) { 15 | nums[i] += nums[i - 1]; 16 | } 17 | } 18 | 19 | public int sumRange(int i, int j) { 20 | return i == 0 ? nums[j] : nums[j] - nums[i - 1]; 21 | } 22 | 23 | public static void main(String[] args) { 24 | NumArray2 obj = new NumArray2(new int[]{-2, 0, 3, -5, 2, -1}); 25 | System.out.println(obj.sumRange(0, 2)); 26 | System.out.println(obj.sumRange(2, 5)); 27 | System.out.println(obj.sumRange(0, 5)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0322_Coin_Change/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0322_Coin_Change; 2 | 3 | import java.util.Arrays; 4 | 5 | class Solution { 6 | 7 | public int coinChange(int[] coins, int amount) { 8 | // 动态规划 9 | int[] memo = new int[amount + 1]; 10 | Arrays.fill(memo, amount + 1); 11 | memo[0] = 0; 12 | for (int i = 1; i <= amount; i++) { 13 | for (int coin : coins) { 14 | if (i - coin >= 0) { 15 | memo[i] = Math.min(memo[i], memo[i - coin] + 1); 16 | } 17 | } 18 | } 19 | return memo[amount] == amount + 1 ? -1 : memo[amount]; 20 | } 21 | 22 | public static void main(String[] args) { 23 | System.out.println(new Solution() 24 | .coinChange(new int[]{1, 2, 5}, 11)); 25 | System.out.println(new Solution() 26 | .coinChange(new int[]{186, 419, 83, 408}, 6249)); 27 | } 28 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0326_Power_of_Three/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0326_Power_of_Three; 2 | 3 | class Solution { 4 | public boolean isPowerOfThree_1(int n) { 5 | if (n <= 0) { 6 | return false; 7 | } 8 | for (int i = n; i > 1; i /= 3) { 9 | if (i % 3 != 0) { 10 | return false; 11 | } 12 | } 13 | return true; 14 | } 15 | 16 | public boolean isPowerOfThree(int n) { 17 | // 1162261467是3的19次幂,是整数范围内最大的3的幂次 18 | return n > 0 && 1162261467 % n == 0; 19 | } 20 | 21 | public static void main(String[] args) { 22 | System.out.println(new Solution().isPowerOfThree(27)); 23 | System.out.println(new Solution().isPowerOfThree(45)); 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0327_Count_of_Range_Sum/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0327_Count_of_Range_Sum; 2 | 3 | /** 4 | * 区间和的个数 5 | * https://leetcode-cn.com/problems/count-of-range-sum/ 6 | */ 7 | class Solution { 8 | // TODO: angus on 2020/11/7 题目要求优化算法复杂度 9 | public int countRangeSum(int[] nums, int lower, int upper) { 10 | int res = 0; 11 | for (int i = 0; i < nums.length; i++) { 12 | long sum = 0; 13 | for (int j = i; j < nums.length; j++) { 14 | sum += nums[j]; 15 | if (sum >= lower && sum <= upper) res++; 16 | } 17 | } 18 | return res; 19 | } 20 | 21 | public static void main(String[] args) { 22 | Solution s = new Solution(); 23 | int[] nums = new int[]{-2, 5, -1}; 24 | int res = s.countRangeSum(nums, -2, 10); 25 | System.out.println("res = " + res); 26 | } 27 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0328_Odd_Even_Linked_List/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0328_Odd_Even_Linked_List; 2 | 3 | class Solution { 4 | public ListNode oddEvenList(ListNode head) { 5 | if (head == null || head.next == null) { 6 | return head; 7 | } 8 | ListNode o = head; 9 | ListNode p = head.next; 10 | ListNode e = p; 11 | while (o.next != null && e.next != null) { 12 | o.next = e.next; 13 | o = o.next; 14 | e.next = o.next; 15 | e = e.next; 16 | } 17 | o.next = p; 18 | return head; 19 | } 20 | 21 | public static void main(String[] args) { 22 | ListNode list = new ListNode(new int[]{1, 2, 3, 4, 5}); 23 | System.out.println(new Solution().oddEvenList(list)); 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0337_House_Robber_III/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0337_House_Robber_III; 2 | 3 | import javax.swing.tree.TreeNode; 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * public class TreeNode { 8 | * int val; 9 | * TreeNode left; 10 | * TreeNode right; 11 | * TreeNode(int x) { val = x; } 12 | * } 13 | */ 14 | class Solution { 15 | public int rob(TreeNode root) { 16 | return 0; 17 | } 18 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0337_House_Robber_III/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0337_House_Robber_III; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0341_Flatten_Nested_List_Iterator/NestedInteger.java: -------------------------------------------------------------------------------- 1 | package NO_0341_Flatten_Nested_List_Iterator; 2 | 3 | import java.util.List; 4 | 5 | // This is the interface that allows for creating nested lists. 6 | // You should not implement it, or speculate about its implementation 7 | public interface NestedInteger { 8 | 9 | // @return true if this NestedInteger holds a single integer, rather than a nested list. 10 | public boolean isInteger(); 11 | 12 | // @return the single integer that this NestedInteger holds, if it holds a single integer 13 | // Return null if this NestedInteger holds a nested list 14 | public Integer getInteger(); 15 | 16 | // @return the nested list that this NestedInteger holds, if it holds a nested list 17 | // Return null if this NestedInteger holds a single integer 18 | public List getList(); 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0343_Integer_Break/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0343_Integer_Break; 2 | 3 | class Solution { 4 | public int integerBreak(int n) { 5 | // memo[i] 表示将数字 n 分割(至少是分割成两部分)后得到的最大乘积 6 | int[] memo = new int[n + 1]; 7 | memo[1] = 1; 8 | for (int i = 2; i <= n; i++) { 9 | for (int j = 1; j < i; j++) { 10 | memo[i] = Math.max(memo[i], Math.max(j * (i - j), j * memo[i - j])); 11 | } 12 | } 13 | return memo[n]; 14 | } 15 | 16 | public static void main(String[] args) { 17 | System.out.println(new Solution().integerBreak(2)); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0367_Valid_Perfect_Square/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0367_Valid_Perfect_Square; 2 | 3 | class Solution { 4 | public boolean isPerfectSquare_1(int num) { 5 | for (int i = 1; i <= 46340; i++) { 6 | if (i * i == num) { 7 | return true; 8 | } 9 | } 10 | return false; 11 | } 12 | 13 | public boolean isPerfectSquare(int num) { 14 | int left = 0; 15 | // 直接用 num 会发生溢出,所以要么声明为 long 型,要么限定右边界为 46340(√(2147483647)) 16 | int right = num < 46340 ? num : 46340; 17 | while (left <= right) { 18 | int mid = left + (right - left) / 2; 19 | int product = mid * mid; 20 | if (product == num) { 21 | return true; 22 | } else if (product < num) { 23 | left = mid + 1; 24 | } else { 25 | right = mid - 1; 26 | } 27 | } 28 | return false; 29 | } 30 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0371_Sum_of_Two_Integers/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0371_Sum_of_Two_Integers; 2 | 3 | /** 4 | * 两整数之和 5 | * https://leetcode-cn.com/problems/sum-of-two-integers/ 6 | */ 7 | public class Solution { 8 | public int getSum(int a, int b) { 9 | // 异或为无进位的加 10 | int sum = a ^ b; 11 | // 位与加左移得到进位 12 | int carry = (a & b) << 1; 13 | return carry == 0 ? sum : getSum(sum, carry); 14 | } 15 | 16 | public static void main(String[] args) { 17 | Solution s = new Solution(); 18 | System.out.println(s.getSum(10, -10)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0374_Guess_Number_Higher_or_Lower/GuessGame.java: -------------------------------------------------------------------------------- 1 | package NO_0374_Guess_Number_Higher_or_Lower; 2 | 3 | /** 4 | * @author Angus 5 | * @date 2019/1/12 6 | */ 7 | public class GuessGame { 8 | int guess(int num) { 9 | return -1; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0374_Guess_Number_Higher_or_Lower/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0374_Guess_Number_Higher_or_Lower;/* The guess API is defined in the parent class GuessGame. 2 | @param num, your guess 3 | @return -1 if my number is lower, 1 if my number is higher, otherwise return 0 4 | int guess(int num); */ 5 | 6 | public class Solution extends GuessGame { 7 | public int guessNumber(int n) { 8 | int low = 1; 9 | int high = n; 10 | int res = -1; 11 | while (low <= high) { 12 | int mid = low + (high - low) / 2; 13 | if (guess(mid) == 0) { 14 | res = mid; 15 | break; 16 | } else if (guess(mid) == 1) { 17 | low = mid + 1; 18 | } else { 19 | high = mid -1; 20 | } 21 | } 22 | return res; 23 | } 24 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0376_Wiggle_Subsequence/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0376_Wiggle_Subsequence; 2 | 3 | class Solution { 4 | public int wiggleMaxLength(int[] nums) { 5 | int n = nums.length; 6 | if (n < 2) { 7 | return n; 8 | } 9 | int up = 1; 10 | int down = 1; 11 | for (int i = 1; i < n; i++) { 12 | if (nums[i] > nums[i - 1]) { 13 | up = down + 1; 14 | } 15 | if (nums[i] < nums[i - 1]) { 16 | down = up + 1; 17 | } 18 | } 19 | return Math.max(up, down); 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0392_Is_Subsequence/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0392_Is_Subsequence; 2 | 3 | class Solution { 4 | public boolean isSubsequence(String s, String t) { 5 | // 贪心算法 6 | int si = 0; 7 | int ti = 0; 8 | while (si < s.length() && ti < t.length()) { 9 | if (s.charAt(si) == t.charAt(ti)) { 10 | si++; 11 | ti++; 12 | } else { 13 | ti++; 14 | } 15 | } 16 | return si == s.length(); 17 | } 18 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0404_Sum_of_Left_Leaves/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0404_Sum_of_Left_Leaves; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0412_Fizz_Buzz/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0412_Fizz_Buzz; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | class Solution { 7 | public List fizzBuzz(int n) { 8 | List res = new ArrayList<>(); 9 | for (int i = 1; i <= n; i++) { 10 | if (i % 15 == 0) { 11 | res.add("FizzBuzz"); 12 | } else if (i % 3 == 0) { 13 | res.add("Fizz"); 14 | } else if (i % 5 == 0) { 15 | res.add("Buzz"); 16 | } else { 17 | res.add(String.valueOf(i)); 18 | } 19 | } 20 | return res; 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0429_N_ary_Tree_Level_Order_Traversal/Node.java: -------------------------------------------------------------------------------- 1 | package NO_0429_N_ary_Tree_Level_Order_Traversal; 2 | 3 | import java.util.List; 4 | 5 | class Node { 6 | public int val; 7 | public List children; 8 | 9 | public Node() {} 10 | 11 | public Node(int _val,List _children) { 12 | val = _val; 13 | children = _children; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0429_N_ary_Tree_Level_Order_Traversal/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0429_N_ary_Tree_Level_Order_Traversal; 2 | 3 | import java.util.ArrayList; 4 | import java.util.LinkedList; 5 | import java.util.List; 6 | import java.util.Queue; 7 | 8 | class Solution { 9 | public List> levelOrder(Node root) { 10 | List> res = new ArrayList<>(); 11 | if (root == null) return res; 12 | Queue queue = new LinkedList<>(); 13 | queue.add(root); 14 | while (!queue.isEmpty()) { 15 | int size = queue.size(); 16 | List level = new ArrayList<>(); 17 | for (int i = 0; i < size; i++) { 18 | Node node = queue.remove(); 19 | queue.addAll(node.children); 20 | level.add(node.val); 21 | } 22 | res.add(level); 23 | } 24 | return res; 25 | } 26 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0430_Flatten_a_Multilevel_Doubly_Linked_List/Node.java: -------------------------------------------------------------------------------- 1 | package NO_0430_Flatten_a_Multilevel_Doubly_Linked_List; 2 | 3 | class Node { 4 | public int val; 5 | public Node prev; 6 | public Node next; 7 | public Node child; 8 | 9 | public Node() { 10 | } 11 | 12 | public Node(int _val, Node _prev, Node _next, Node _child) { 13 | val = _val; 14 | prev = _prev; 15 | next = _next; 16 | child = _child; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0435_Non_overlapping_Intervals/Interval.java: -------------------------------------------------------------------------------- 1 | package NO_0435_Non_overlapping_Intervals; 2 | 3 | /** 4 | * Definition for an interval. 5 | */ 6 | public class Interval { 7 | int start; 8 | int end; 9 | 10 | Interval() { 11 | start = 0; 12 | end = 0; 13 | } 14 | 15 | Interval(int s, int e) { 16 | start = s; 17 | end = e; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0437_Path_Sum_III/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0437_Path_Sum_III; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0450_Delete_Node_in_a_BST/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0450_Delete_Node_in_a_BST; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0455_Assign_Cookies/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0455_Assign_Cookies; 2 | 3 | import java.util.Arrays; 4 | 5 | class Solution { 6 | public int findContentChildren(int[] g, int[] s) { 7 | // 贪心算法 8 | Arrays.sort(g); 9 | Arrays.sort(s); 10 | 11 | // gi 用来表示当前分配孩子贪心指数的索引,初始时指向最贪心的孩子 12 | int gi = g.length - 1; 13 | // si 用来表示当前分配饼干尺寸大小的索引,初始时指向最大的饼干 14 | int si = s.length - 1; 15 | int res = 0; 16 | while (gi >= 0 && si >= 0) { 17 | if (s[si] >= g[gi]) { 18 | // 将当前饼干分给当前孩子 19 | res++; 20 | gi--; 21 | si--; 22 | } else { 23 | // 查看是否满足次贪心的孩子 24 | gi--; 25 | } 26 | } 27 | return res; 28 | } 29 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0460_LFU_Cache/Node.java: -------------------------------------------------------------------------------- 1 | package NO_0460_LFU_Cache; 2 | 3 | /** 4 | * @author angus 5 | * @date 2019/9/29 6 | */ 7 | public class Node { 8 | int key; 9 | 10 | int value; 11 | 12 | int freq; 13 | 14 | /** 15 | * 前一个节点 16 | */ 17 | Node prev; 18 | 19 | /** 20 | * 后一个节点 21 | */ 22 | Node next; 23 | 24 | /** 25 | * 后继节点(默认值为next) 26 | */ 27 | Node succeed; 28 | 29 | public Node(int key, int value) { 30 | this.key = key; 31 | this.value = value; 32 | this.freq = 1; 33 | } 34 | 35 | public Node(int key, int value, int freq, Node prev, Node next, Node succeed) { 36 | this.key = key; 37 | this.value = value; 38 | this.freq = freq; 39 | this.prev = prev; 40 | this.next = next; 41 | this.succeed = succeed; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0461_Hamming_Distance/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0461_Hamming_Distance; 2 | 3 | class Solution { 4 | public int hammingDistance(int x, int y) { 5 | int dist = x ^ y; 6 | int count = 0; 7 | for (int i = 0; i < 32; i++) { 8 | count += dist & 1; 9 | dist >>>= 1; 10 | } 11 | return count; 12 | } 13 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0474_Ones_and_Zeroes/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0474_Ones_and_Zeroes; 2 | 3 | import java.util.Arrays; 4 | 5 | class Solution { 6 | public int findMaxForm(String[] strs, int m, int n) { 7 | // 动态规划 8 | int[][] dp = new int[m + 1][n + 1]; 9 | for (int[] row : dp) { 10 | Arrays.fill(row, 0); 11 | } 12 | for (int i = 0; i < strs.length; i++) { 13 | int mCost = 0; 14 | int nCost = 0; 15 | for (char c : strs[i].toCharArray()) { 16 | if (c == '0') { 17 | mCost++; 18 | } else { 19 | nCost++; 20 | } 21 | } 22 | for (int u = m; u >= mCost; u--) { 23 | for (int v = n; v >= nCost; v--) { 24 | dp[u][v] = Math.max(dp[u][v], 1 + dp[u - mCost][v - nCost]); 25 | } 26 | } 27 | } 28 | return dp[m][n]; 29 | } 30 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0495_Teemo_Attacking/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0495_Teemo_Attacking; 2 | 3 | class Solution { 4 | public int findPoisonedDuration(int[] timeSeries, int duration) { 5 | int res = 0; 6 | // 记录上次攻击所持续到的时间点 7 | int lastDuration = 0; 8 | // 遍历攻击时间序列 9 | for (int n : timeSeries) { 10 | // 先假设每次攻击都完整有效 11 | res += duration; 12 | // 再比较该次攻击的时间点是否处于上次攻击造成的中毒状态中 13 | if (lastDuration > n) { 14 | // 减去重合部分 15 | res -= (lastDuration - n); 16 | } 17 | // 更新中毒状态持续时间点 18 | lastDuration = n + duration; 19 | } 20 | return res; 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0509_Fibonacci_Number/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0509_Fibonacci_Number; 2 | 3 | class Solution { 4 | public int fib(int n) { 5 | int cur = 0; 6 | int next = 1; 7 | while (n-- > 0) { 8 | next += cur; 9 | cur = next - cur; 10 | } 11 | return cur; 12 | } 13 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0514_Freedom_Trail/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0514_Freedom_Trail; 2 | 3 | /** 4 | * 自由之路 5 | * https://leetcode-cn.com/problems/freedom-trail/ 6 | */ 7 | class Solution { 8 | public int findRotateSteps(String ring, String key) { 9 | throw new RuntimeException("TODO"); 10 | } 11 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0553_Optimal_Division/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0553_Optimal_Division; 2 | 3 | class Solution { 4 | public String optimalDivision(int[] nums) { 5 | return null; 6 | } 7 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0559_Maximum_Depth_of_N_ary_Tree/Node.java: -------------------------------------------------------------------------------- 1 | package NO_0559_Maximum_Depth_of_N_ary_Tree; 2 | 3 | import java.util.List; 4 | 5 | class Node { 6 | public int val; 7 | public List children; 8 | 9 | public Node() { 10 | } 11 | 12 | public Node(int _val, List _children) { 13 | val = _val; 14 | children = _children; 15 | } 16 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0561_Array_Partition/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0561_Array_Partition; 2 | 3 | import java.util.Arrays; 4 | 5 | class Solution { 6 | public int arrayPairSum(int[] nums) { 7 | Arrays.sort(nums); 8 | int res = 0; 9 | for (int i = 0; i < nums.length; i += 2) { 10 | res += nums[i]; 11 | } 12 | return res; 13 | } 14 | 15 | public static void main(String[] args) { 16 | System.out.println(new Solution().arrayPairSum(new int[]{1, 4, 3, 2})); 17 | } 18 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0589_N_ary_Tree_Preorder_Traversal/Node.java: -------------------------------------------------------------------------------- 1 | package NO_0589_N_ary_Tree_Preorder_Traversal; 2 | 3 | import java.util.List; 4 | 5 | class Node { 6 | public int val; 7 | public List children; 8 | 9 | public Node() {} 10 | 11 | public Node(int _val,List _children) { 12 | val = _val; 13 | children = _children; 14 | } 15 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0590_N_ary_Tree_Postorder_Traversal/Node.java: -------------------------------------------------------------------------------- 1 | package NO_0590_N_ary_Tree_Postorder_Traversal; 2 | 3 | import java.util.List; 4 | 5 | class Node { 6 | public int val; 7 | public List children; 8 | 9 | public Node() {} 10 | 11 | public Node(int _val, List _children) { 12 | val = _val; 13 | children = _children; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0617_Merge_Two_Binary_Trees/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0617_Merge_Two_Binary_Trees; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0652_Find_Duplicate_Subtrees/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0652_Find_Duplicate_Subtrees; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0674_Longest_Continuous_Increasing_Subsequence/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0674_Longest_Continuous_Increasing_Subsequence; 2 | 3 | class Solution { 4 | public int findLengthOfLCIS(int[] nums) { 5 | int lcis = 1, cnt = 1; 6 | for (int i = 1; i < nums.length; i++) { 7 | if (nums[i] > nums[i - 1]) { 8 | lcis = Math.max(lcis, ++cnt); 9 | } else { 10 | cnt = 1; 11 | } 12 | } 13 | return lcis; 14 | } 15 | 16 | public static void main(String[] args) { 17 | Solution solution = new Solution(); 18 | int lcis = solution.findLengthOfLCIS(new int[]{1, 3, 5, 4, 7}); 19 | System.out.println("lcis = " + lcis); 20 | lcis = solution.findLengthOfLCIS(new int[]{2, 2, 2, 2, 2}); 21 | System.out.println("lcis = " + lcis); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0700_Search_in_a_Binary_Search_Tree/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0700_Search_in_a_Binary_Search_Tree; 2 | 3 | 4 | class Solution { 5 | /** 6 | * 递归 7 | */ 8 | public TreeNode searchBST_1(TreeNode root, int val) { 9 | if (root == null || root.val == val) return root; 10 | return root.val > val ? searchBST(root.left, val) : searchBST(root.right, val); 11 | } 12 | 13 | /** 14 | * 迭代 15 | */ 16 | public TreeNode searchBST(TreeNode root, int val) { 17 | while (true) { 18 | if (root == null || root.val == val) return root; 19 | root = root.val > val ? root.left : root.right; 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0700_Search_in_a_Binary_Search_Tree/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0700_Search_in_a_Binary_Search_Tree; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0701_Insert_into_a_Binary_Search_Tree/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0701_Insert_into_a_Binary_Search_Tree; 2 | 3 | class Solution { 4 | public TreeNode insertIntoBST(TreeNode root, int val) { 5 | TreeNode cur = root; 6 | while (cur != null) { 7 | if (cur.val > val) { 8 | if (cur.left == null) { 9 | cur.left = new TreeNode(val); 10 | break; 11 | } else { 12 | cur = cur.left; 13 | } 14 | } else if (cur.val < val) { 15 | if (cur.right == null) { 16 | cur.right = new TreeNode(val); 17 | break; 18 | } else { 19 | cur = cur.right; 20 | } 21 | } else { 22 | break; 23 | } 24 | } 25 | return root; 26 | } 27 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0701_Insert_into_a_Binary_Search_Tree/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0701_Insert_into_a_Binary_Search_Tree; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0705_Design_HashSet/MyHashSet.java: -------------------------------------------------------------------------------- 1 | package NO_0705_Design_HashSet; 2 | 3 | class MyHashSet { 4 | 5 | private boolean[] bucket; 6 | 7 | /** 8 | * Initialize your data structure here. 9 | */ 10 | public MyHashSet() { 11 | bucket = new boolean[1000001]; 12 | } 13 | 14 | public void add(int key) { 15 | bucket[key] = true; 16 | } 17 | 18 | public void remove(int key) { 19 | bucket[key] = false; 20 | } 21 | 22 | /** 23 | * Returns true if this set contains the specified element 24 | */ 25 | public boolean contains(int key) { 26 | return bucket[key]; 27 | } 28 | } 29 | 30 | /** 31 | * Your MyHashSet object will be instantiated and called as such: 32 | * MyHashSet obj = new MyHashSet(); 33 | * obj.add(key); 34 | * obj.remove(key); 35 | * boolean param_3 = obj.contains(key); 36 | */ -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0724_Find_Pivot_Index/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0724_Find_Pivot_Index; 2 | 3 | class Solution { 4 | public int pivotIndex(int[] nums) { 5 | int s1 = 0; 6 | for (int num : nums) { 7 | s1 += num; 8 | } 9 | int s2 = s1; 10 | for (int i = 0; i < nums.length; i++) { 11 | s2 -= nums[i]; 12 | if (s2 * 2 == s1 - nums[i]) { 13 | return i; 14 | } 15 | } 16 | return -1; 17 | } 18 | 19 | public static void main(String[] args) { 20 | int[] nums = {1, 7, 3, 6, 5, 6}; 21 | System.out.println(new Solution().pivotIndex(nums)); 22 | int[] nums2 = {1, 0}; 23 | System.out.println(new Solution().pivotIndex(nums2)); 24 | } 25 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0729_My_Calendar_I/MyCalendar.java: -------------------------------------------------------------------------------- 1 | package NO_0729_My_Calendar_I; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | class MyCalendar { 7 | private final List bookings; 8 | 9 | public MyCalendar() { 10 | bookings = new ArrayList<>(); 11 | } 12 | 13 | public boolean book(int start, int end) { 14 | for (int[] b : bookings) { 15 | if (b[1] > start && b[0] < end) { 16 | return false; 17 | } 18 | } 19 | bookings.add(new int[]{start, end}); 20 | return true; 21 | } 22 | } 23 | 24 | /** 25 | * Your MyCalendar object will be instantiated and called as such: 26 | * MyCalendar obj = new MyCalendar(); 27 | * boolean param_1 = obj.book(start,end); 28 | */ 29 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0744_Find_Smallest_Letter_Greater_Than_Target/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0744_Find_Smallest_Letter_Greater_Than_Target; 2 | 3 | class Solution { 4 | public char nextGreatestLetter(char[] letters, char target) { 5 | int left = 0; 6 | int right = letters.length - 1; 7 | 8 | if (letters[right] <= target) { 9 | return letters[0]; 10 | } 11 | 12 | while (left <= right) { 13 | int mid = left + (right - left) / 2; 14 | if (letters[mid] <= target) { 15 | left = mid + 1; 16 | } else { 17 | right = mid - 1; 18 | } 19 | } 20 | return letters[left]; 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0746_Min_Cost_Climbing_Stairs/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0746_Min_Cost_Climbing_Stairs; 2 | 3 | class Solution { 4 | public int minCostClimbingStairs(int[] cost) { 5 | int pre = cost[0], cur = cost[1], next; 6 | for (int i = 2; i < cost.length; i++) { 7 | next = cost[i] + Math.min(pre, cur); 8 | pre = cur; 9 | cur = next; 10 | } 11 | return Math.min(pre, cur); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0747_Largest_Number_at_Least_Twice_of_Others/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0747_Largest_Number_at_Least_Twice_of_Others; 2 | 3 | class Solution { 4 | public int dominantIndex(int[] nums) { 5 | // 找出最大值和次大值 6 | int max = -1, maxIndex = -1, secondMax = 0; 7 | for (int i = 0; i < nums.length; i++) { 8 | if (nums[i] > max) { 9 | secondMax = max; 10 | max = nums[i]; 11 | maxIndex = i; 12 | } else if (nums[i] > secondMax) { 13 | secondMax = nums[i]; 14 | } 15 | } 16 | return max >= secondMax * 2 ? maxIndex : -1; 17 | } 18 | 19 | public static void main(String[] args) { 20 | int[] nums = {0,0,3,2}; 21 | int i = new Solution().dominantIndex(nums); 22 | System.out.println(i); 23 | } 24 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0754_Reach_a_Number/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0754_Reach_a_Number; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 到达终点数字 7 | * https://leetcode-cn.com/problems/reach-a-number/ 8 | */ 9 | class Solution { 10 | 11 | /** 12 | * 一股脑往右走,当发现走过了的时候,如果多走的距离刚好除以2, 13 | * 说明之前走的某一步往左就可以,否者继续往右走,直到满足这个条件 14 | */ 15 | public int reachNumber(int target) { 16 | target = Math.abs(target); 17 | for (int i = 1, sum = 1; ; i++, sum += i) { 18 | if (sum >= target && (sum - target) % 2 == 0) return i; 19 | } 20 | } 21 | 22 | public static void main(String[] args) { 23 | Solution solution = new Solution(); 24 | Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100).forEach(target -> 25 | System.out.println(solution.reachNumber(target)) 26 | ); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0795_Number_of_Subarrays_with_Bounded_Maximum/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0795_Number_of_Subarrays_with_Bounded_Maximum; 2 | 3 | class Solution { 4 | public int numSubarrayBoundedMax(int[] A, int L, int R) { 5 | // 最大元素满足大于等于L小于等于R的子数组个数 = 最大元素小于等于R的子数组个数 - 最大元素小于L的子数组个数 6 | return numSubarrayBoundedMax(A, R) - numSubarrayBoundedMax(A, L - 1); 7 | } 8 | 9 | private int numSubarrayBoundedMax(int[] A, int Max) { 10 | int res = 0; 11 | int numSubarry = 0; 12 | for (int num : A) { 13 | if (num <= Max) { 14 | numSubarry++; 15 | res += numSubarry; 16 | } else { 17 | numSubarry = 0; 18 | } 19 | } 20 | return res; 21 | } 22 | 23 | public static void main(String[] args) { 24 | Solution solution = new Solution(); 25 | System.out.println(solution.numSubarrayBoundedMax(new int[]{2, 1, 4, 3}, 2, 3)); 26 | } 27 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0843_Guess_the_Word/Master.java: -------------------------------------------------------------------------------- 1 | package NO_0843_Guess_the_Word; 2 | 3 | /** 4 | * @author angus on 2020/7/11 5 | * @since 1.0.0 6 | */ 7 | interface Master { 8 | int guess(String word); 9 | } 10 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0887_Super_Egg_Drop/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0887_Super_Egg_Drop; 2 | 3 | class Solution { 4 | public int superEggDrop(int K, int N) { 5 | int[] dp = new int[K + 1]; 6 | int m; 7 | for (m = 0; dp[K] < N; m++) { 8 | for (int i = K; i > 0; i--) { 9 | dp[i] += dp[i - 1] + 1; 10 | } 11 | } 12 | return m; 13 | } 14 | 15 | public static void main(String[] args) { 16 | int res = new Solution().superEggDrop(2, 14); 17 | System.out.println("res = " + res); 18 | } 19 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0912_Sort_an_Array/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0912_Sort_an_Array; 2 | 3 | class Solution { 4 | // 希尔排序 5 | public int[] sortArray(int[] nums) { 6 | int n = nums.length; 7 | // step 为步长 8 | for (int step = n / 2; step >= 1; step /= 2) { 9 | for (int i = step; i < n; i++) { 10 | int t = nums[i]; 11 | int j; 12 | for (j = i; j >= step && t < nums[j - step]; j -= step) { 13 | nums[j] = nums[j - step]; 14 | } 15 | nums[j] = t; 16 | } 17 | } 18 | return nums; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0933_Number_of_Recent_Calls/RecentCounter.java: -------------------------------------------------------------------------------- 1 | package NO_0933_Number_of_Recent_Calls; 2 | 3 | import java.util.LinkedList; 4 | 5 | class RecentCounter { 6 | private LinkedList deque; 7 | 8 | public RecentCounter() { 9 | deque = new LinkedList<>(); 10 | } 11 | 12 | public int ping(int t) { 13 | deque.addLast(t); 14 | // 移除双端队列头部不符合的 ping 15 | while (!deque.isEmpty() && deque.getFirst() < (t - 3000)) { 16 | deque.removeFirst(); 17 | } 18 | 19 | return deque.size(); 20 | } 21 | } 22 | 23 | /** 24 | * Your RecentCounter object will be instantiated and called as such: 25 | * RecentCounter obj = new RecentCounter(); 26 | * int param_1 = obj.ping(t); 27 | */ -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0941_Valid_Mountain_Array/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0941_Valid_Mountain_Array; 2 | 3 | /** 4 | * 有效的山脉数组 5 | * https://leetcode-cn.com/problems/valid-mountain-array/ 6 | */ 7 | class Solution { 8 | public boolean validMountainArray(int[] A) { 9 | if (A == null || A.length < 3) return false; 10 | int i = 0; 11 | int j = A.length - 1; 12 | while (i < j && A[i + 1] > A[i]) i++; 13 | while (j > i && A[j - 1] > A[j]) j--; 14 | return i == j && i > 0 && j < A.length - 1; 15 | } 16 | 17 | public static void main(String[] args) { 18 | Solution s = new Solution(); 19 | int[] A1 = new int[]{2, 1}; 20 | System.out.println(s.validMountainArray(A1)); 21 | int[] A2 = new int[]{3, 5, 5}; 22 | System.out.println(s.validMountainArray(A2)); 23 | int[] A3 = new int[]{0, 3, 2, 1}; 24 | System.out.println(s.validMountainArray(A3)); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0946_Validate_Stack_Sequences/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0946_Validate_Stack_Sequences; 2 | 3 | import java.util.ArrayDeque; 4 | 5 | class Solution { 6 | public boolean validateStackSequences(int[] pushed, int[] popped) { 7 | ArrayDeque stack = new ArrayDeque<>(); 8 | for (int i = 0, j = 0; i < pushed.length; i++) { 9 | stack.push(pushed[i]); 10 | while (!stack.isEmpty() && stack.peek() == popped[j]) { 11 | stack.pop(); 12 | j++; 13 | } 14 | } 15 | return stack.isEmpty(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0958_Check_Completeness_of_a_Binary_Tree/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0958_Check_Completeness_of_a_Binary_Tree; 2 | 3 | import java.util.LinkedList; 4 | 5 | class Solution { 6 | /** 7 | * 广度遍历二叉树,当出现 null 值时停止遍历,如果此时还有没有遍历到的结点,那么就说明该树非完全二叉树。 8 | */ 9 | public boolean isCompleteTree(TreeNode root) { 10 | LinkedList q = new LinkedList<>(); 11 | TreeNode cur; 12 | q.addLast(root); 13 | while ((cur = q.removeFirst()) != null) { 14 | q.addLast(cur.left); 15 | q.addLast(cur.right); 16 | } 17 | while (!q.isEmpty()) { 18 | if (q.removeLast() != null) { 19 | return false; 20 | } 21 | } 22 | return true; 23 | } 24 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0958_Check_Completeness_of_a_Binary_Tree/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0958_Check_Completeness_of_a_Binary_Tree; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0959_Regions_Cut_By_Slashes/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0959_Regions_Cut_By_Slashes; 2 | 3 | class Solution { 4 | public int regionsBySlashes(String[] grid) { 5 | return 0; 6 | } 7 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0960_Delete_Columns_to_Make_Sorted_III/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0960_Delete_Columns_to_Make_Sorted_III; 2 | 3 | class Solution { 4 | public int minDeletionSize(String[] A) { 5 | return 0; 6 | } 7 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0961_N_Repeated_Element_in_Size_2N_Array/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0961_N_Repeated_Element_in_Size_2N_Array; 2 | 3 | import java.util.Arrays; 4 | 5 | class Solution { 6 | public int repeatedNTimes_1(int[] A) { 7 | int res = A[0]; 8 | int[] countArr = new int[10000]; 9 | for (int i : A) { 10 | countArr[i]++; 11 | if (countArr[i] == A.length / 2) { 12 | res = i; 13 | break; 14 | } 15 | } 16 | return res; 17 | } 18 | 19 | public int repeatedNTimes(int[] A) { 20 | Arrays.sort(A); 21 | return A[A.length / 2] == A[A.length - 1] ? A[A.length - 1] : A[A.length / 2 - 1]; 22 | } 23 | 24 | public static void main(String[] args) { 25 | System.out.println(new Solution().repeatedNTimes(new int[]{1, 2, 3, 3})); 26 | } 27 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0965_Univalued_Binary_Tree/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0965_Univalued_Binary_Tree; 2 | 3 | /** 4 | * Definition for a binary tree node. 5 | * public class TreeNode { 6 | * int val; 7 | * TreeNode left; 8 | * TreeNode right; 9 | * TreeNode(int x) { val = x; } 10 | * } 11 | */ 12 | class Solution { 13 | public boolean isUnivalTree(TreeNode root) { 14 | if (root == null) { 15 | return true; 16 | } 17 | return isUnivalTree(root, root.left) && isUnivalTree(root, root.right); 18 | } 19 | 20 | private boolean isUnivalTree(TreeNode root, TreeNode child) { 21 | if (child == null) { 22 | return true; 23 | } 24 | if (root.val != child.val) { 25 | return false; 26 | } 27 | return isUnivalTree(child, child.left) && isUnivalTree(child, child.right); 28 | } 29 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0965_Univalued_Binary_Tree/TreeNode.java: -------------------------------------------------------------------------------- 1 | package NO_0965_Univalued_Binary_Tree; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode(int x) { 9 | val = x; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_0974_Subarray_Sums_Divisible_by_K/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_0974_Subarray_Sums_Divisible_by_K; 2 | 3 | import java.util.*; 4 | 5 | class Solution { 6 | public int subarraysDivByK(int[] A, int K) { 7 | Map map = new HashMap<>(); 8 | Arrays.asList(A); 9 | return 0; 10 | } 11 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_1017_Convert_to_Base_2/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_1017_Convert_to_Base_2; 2 | 3 | class Solution { 4 | public String baseNeg2(int n) { 5 | // TODO 6 | return Integer.toBinaryString(n); 7 | } 8 | 9 | public static void main(String[] args) { 10 | Solution solution = new Solution(); 11 | 12 | for (int i = 0; i < 1; i++) { 13 | solution.baseNeg2(i); 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_1039_Minimum_Score_Triangulation_of_Polygon/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_1039_Minimum_Score_Triangulation_of_Polygon; 2 | 3 | class Solution { 4 | public int minScoreTriangulation(int[] values) { 5 | // TODO: 2023/4/3 6 | return 0; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_1092_Shortest_Common_Supersequence/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_1092_Shortest_Common_Supersequence; 2 | 3 | class Solution { 4 | public String shortestCommonSupersequence(String str1, String str2) { 5 | throw new UnsupportedOperationException("TODO"); 6 | } 7 | 8 | public static void main(String[] args) { 9 | Solution solution = new Solution(); 10 | String res = solution.shortestCommonSupersequence("abac", "aba"); 11 | System.out.println("res = " + res); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_1137_Nth_Tribonacci_Number/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_1137_Nth_Tribonacci_Number; 2 | 3 | class Solution { 4 | public int tribonacci(int n) { 5 | // 0 1 1 2 4 7 13 24 6 | int t0 = 0, t1 = 1, t2 = 1; 7 | while (n-- > 0) { 8 | int tmp = t2; 9 | t2 = t0 + t1 + t2; 10 | t0 = t1; 11 | t1 = tmp; 12 | } 13 | return t0; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_1226_The_Dining_Philosophers/DiningPhilosophers.java: -------------------------------------------------------------------------------- 1 | package NO_1226_The_Dining_Philosophers; 2 | 3 | class DiningPhilosophers { 4 | 5 | public DiningPhilosophers() { 6 | 7 | } 8 | 9 | // call the run() method of any runnable to execute its code 10 | public void wantsToEat(int philosopher, 11 | Runnable pickLeftFork, 12 | Runnable pickRightFork, 13 | Runnable eat, 14 | Runnable putLeftFork, 15 | Runnable putRightFork) throws InterruptedException { 16 | 17 | } 18 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_135_Candy/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_135_Candy; 2 | 3 | class Solution { 4 | public int candy(int[] ratings) { 5 | int n = ratings.length; 6 | int[] leftCandies = new int[n]; 7 | for (int i = 0; i < n; i++) { 8 | if (i > 0 && ratings[i] > ratings[i - 1]) { 9 | leftCandies[i] = leftCandies[i - 1] + 1; 10 | } else { 11 | leftCandies[i] = 1; 12 | } 13 | } 14 | int rightCandy = 0, candySum = 0; 15 | for (int i = n - 1; i >= 0; i--) { 16 | if (i < n - 1 && ratings[i] > ratings[i + 1]) { 17 | rightCandy++; 18 | } else { 19 | rightCandy = 1; 20 | } 21 | candySum += Math.max(leftCandies[i], rightCandy); 22 | } 23 | return candySum; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_1539_Kth_Missing_Positive_Number/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_1539_Kth_Missing_Positive_Number; 2 | 3 | /** 4 | * 第 k 个缺失的正整数 5 | * https://leetcode-cn.com/problems/kth-missing-positive-number/ 6 | */ 7 | class Solution { 8 | public int findKthPositive(int[] arr, int k) { 9 | for (int i = 0; i < arr.length; i++) { 10 | // arr[i] 表示当前这个位置数字大小 11 | // i + 1 表示这个位置没有缺失时数字的大小 12 | // 两者之差表示缺失的正整数的个数 13 | if (arr[i] - (i + 1) >= k) 14 | return i + k; 15 | } 16 | return arr.length + k; 17 | } 18 | 19 | public static void main(String[] args) { 20 | Solution s = new Solution(); 21 | 22 | int[] arr = {2, 3, 4, 7, 11}; 23 | System.out.println(s.findKthPositive(arr, 5)); 24 | 25 | int[] arr2 = {10, 11, 12, 13}; 26 | System.out.println(s.findKthPositive(arr2, 5)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_1603_Design_Parking_System/ParkingSystem.java: -------------------------------------------------------------------------------- 1 | package NO_1603_Design_Parking_System; 2 | 3 | /** 4 | * 设计停车系统 5 | * https://leetcode-cn.com/problems/design-parking-system/ 6 | */ 7 | class ParkingSystem { 8 | 9 | private final int[] parkingSpaces; 10 | 11 | public ParkingSystem(int big, int medium, int small) { 12 | parkingSpaces = new int[]{big, medium, small}; 13 | } 14 | 15 | public boolean addCar(int carType) { 16 | return parkingSpaces[carType - 1]-- > 0; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_1615_Maximal_Network_Rank/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_1615_Maximal_Network_Rank; 2 | 3 | class Solution { 4 | public int maximalNetworkRank(int n, int[][] roads) { 5 | boolean[][] connect = new boolean[n][n]; 6 | int[] ranks = new int[n]; 7 | for (int[] r : roads) { 8 | connect[r[0]][r[1]] = connect[r[1]][r[0]] = true; 9 | ranks[r[0]]++; 10 | ranks[r[1]]++; 11 | } 12 | int maxRank = 0; 13 | for (int i = 0; i < n - 1; i++) { 14 | for (int j = i + 1; j < n; j++) { 15 | maxRank = Math.max(maxRank, ranks[i] + ranks[j] + (connect[i][j] ? -1 : 0)); 16 | } 17 | } 18 | return maxRank; 19 | } 20 | 21 | public static void main(String[] args) { 22 | Solution solution = new Solution(); 23 | int res = solution.maximalNetworkRank(4, new int[][]{{0, 1}, {0, 3}, {1, 2}, {1, 3}}); 24 | System.out.println("res = " + res); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_1625_Lexicographically_Smallest_String_After_Applying_Operations/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_1625_Lexicographically_Smallest_String_After_Applying_Operations; 2 | 3 | class Solution { 4 | public String findLexSmallestString(String s, int a, int b) { 5 | throw new UnsupportedOperationException(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_1626_Best_Team_With_No_Conflicts/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_1626_Best_Team_With_No_Conflicts; 2 | 3 | import java.util.Arrays; 4 | 5 | class Solution { 6 | public int bestTeamScore(int[] scores, int[] ages) { 7 | int n = scores.length; 8 | int[][] people = new int[n][2]; 9 | for (int i = 0; i < n; ++i) { 10 | people[i] = new int[]{scores[i], ages[i]}; 11 | } 12 | Arrays.sort(people, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]); 13 | int[] dp = new int[n]; 14 | int res = 0; 15 | for (int i = 0; i < n; ++i) { 16 | for (int j = i - 1; j >= 0; --j) { 17 | if (people[j][1] <= people[i][1]) { 18 | dp[i] = Math.max(dp[i], dp[j]); 19 | } 20 | } 21 | dp[i] += people[i][0]; 22 | res = Math.max(res, dp[i]); 23 | } 24 | return res; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_1630_Arithmetic_Subarrays/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_1630_Arithmetic_Subarrays; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | class Solution { 8 | public List checkArithmeticSubarrays(int[] nums, int[] l, int[] r) { 9 | List res = new ArrayList<>(); 10 | for (int i = 0; i < l.length; i++) { 11 | res.add(isArithmeticSubarray(nums, l[i], r[i])); 12 | } 13 | return res; 14 | } 15 | 16 | private boolean isArithmeticSubarray(int[] nums, int l, int r) { 17 | if (r == l) return true; 18 | int[] sub = Arrays.copyOfRange(nums, l, r + 1); 19 | Arrays.sort(sub); 20 | int diff = sub[1] - sub[0]; 21 | for (int i = 2; i < sub.length; i++) { 22 | if (sub[i] - sub[i - 1] != diff) { 23 | return false; 24 | } 25 | } 26 | return true; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_1637_Widest_Vertical_Area_Between_Two_Points_Containing_No_Points/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_1637_Widest_Vertical_Area_Between_Two_Points_Containing_No_Points; 2 | 3 | import java.util.Arrays; 4 | import java.util.Comparator; 5 | 6 | class Solution { 7 | public int maxWidthOfVerticalArea(int[][] points) { 8 | return Arrays.stream(points) 9 | .sorted(Comparator.comparingInt(p -> p[0])) 10 | .reduce(new int[]{Integer.MAX_VALUE, 0}, (pre, cur) -> 11 | new int[]{cur[0], Math.max(pre[1], cur[0] - pre[0])} 12 | )[1]; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_1991_Find_the_Middle_Index_in_Array/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_1991_Find_the_Middle_Index_in_Array; 2 | 3 | import java.util.Arrays; 4 | 5 | class Solution { 6 | public int findMiddleIndex(int[] nums) { 7 | int sum = Arrays.stream(nums).sum(); 8 | int leftSum = 0; 9 | for (int i = 0; i < nums.length; i++) { 10 | if (leftSum == sum - nums[i] - leftSum) return i; 11 | leftSum += nums[i]; 12 | } 13 | return -1; 14 | } 15 | 16 | public static void main(String[] args) { 17 | Solution s = new Solution(); 18 | // int[] nums = {1}; 19 | int[] nums = {1, -1, 4}; 20 | System.out.println(s.findMiddleIndex(nums)); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_2389_Longest_Subsequence_With_Limited_Sum/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_2389_Longest_Subsequence_With_Limited_Sum; 2 | 3 | import java.util.Arrays; 4 | 5 | class Solution { 6 | /** 7 | * 贪心算法 8 | */ 9 | public int[] answerQueries(int[] nums, int[] queries) { 10 | Arrays.sort(nums); 11 | int[] answers = new int[queries.length]; 12 | for (int i = 0; i < queries.length; i++) { 13 | for (int j = 0, s = 0; j < nums.length; j++) { 14 | s += nums[j]; 15 | if (s > queries[i]) break; 16 | answers[i]++; 17 | } 18 | } 19 | return answers; 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_2469_Convert_the_Temperature/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_2469_Convert_the_Temperature; 2 | 3 | class Solution { 4 | public double[] convertTemperature(double celsius) { 5 | return new double[]{ 6 | celsius + 273.15, 7 | celsius * 1.80 + 32.00 8 | }; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_6315_Count_the_Number_of_Vowel_Strings_in_Range/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_6315_Count_the_Number_of_Vowel_Strings_in_Range; 2 | 3 | import java.util.Set; 4 | 5 | class Solution { 6 | private final Set vowels = Set.of('a', 'e', 'i', 'o', 'u'); 7 | 8 | public int vowelStrings(String[] words, int left, int right) { 9 | int res = 0; 10 | for (int i = left; i <= right; i++) { 11 | if (vowels.contains(words[i].charAt(0)) 12 | && vowels.contains(words[i].charAt(words[i].length() - 1))) 13 | res++; 14 | } 15 | return res; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_6316_Rearrange_Array_to_Maximize_Prefix_Score/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_6316_Rearrange_Array_to_Maximize_Prefix_Score; 2 | 3 | import java.util.Arrays; 4 | 5 | class Solution { 6 | public int maxScore(int[] nums) { 7 | Arrays.sort(nums); 8 | int res = 0; 9 | long posSum = 0; 10 | for (int i = nums.length - 1; i >= 0; i--) { 11 | posSum += nums[i]; 12 | if (posSum > 0) res++; 13 | } 14 | return res; 15 | } 16 | 17 | public static void main(String[] args) { 18 | Solution solution = new Solution(); 19 | int res = solution.maxScore(new int[]{2, -1, 0, 1, -3, 3, -3}); 20 | System.out.println("res = " + res); 21 | } 22 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_6317_Count_the_Number_of_Beautiful_Subarrays/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_6317_Count_the_Number_of_Beautiful_Subarrays; 2 | 3 | class Solution { 4 | public long beautifulSubarrays(int[] nums) { 5 | return -1; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_6318_Minimum_Time_to_Complete_All_Tasks/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_6318_Minimum_Time_to_Complete_All_Tasks; 2 | 3 | class Solution { 4 | public int findMinimumTime(int[][] tasks) { 5 | return 0; 6 | } 7 | } -------------------------------------------------------------------------------- /LeetCode/Java/src/main/java/NO_856_Score_of_Parentheses/Solution.java: -------------------------------------------------------------------------------- 1 | package NO_856_Score_of_Parentheses; 2 | 3 | class Solution { 4 | 5 | public int scoreOfParentheses(String s) { 6 | int n = s.length(); 7 | int layer = 0; 8 | int[] scores = new int[n / 2 + 1]; 9 | for (int i = 0; i < n; i++) { 10 | if (s.charAt(i) == '(') { 11 | scores[++layer] = 0; 12 | } else { 13 | scores[layer - 1] += Math.max(2 * scores[layer], 1); 14 | layer--; 15 | } 16 | } 17 | return scores[layer]; 18 | } 19 | 20 | public static void main(String[] args) { 21 | String s = "(()(())(()))"; 22 | int res = new Solution().scoreOfParentheses(s); 23 | System.out.println(res); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode/Kotlin/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.idea/ 3 | -------------------------------------------------------------------------------- /LeetCode/Kotlin/README.md: -------------------------------------------------------------------------------- 1 | # LeetCode Kotlin 2 | 3 | // TODO -------------------------------------------------------------------------------- /LeetCode/Kotlin/src/main/kotlin/NO_0746_Min_Cost_Climbing_Stairs/Solution.kt: -------------------------------------------------------------------------------- 1 | package NO_0746_Min_Cost_Climbing_Stairs 2 | 3 | class Solution { 4 | fun minCostClimbingStairs(cost: IntArray): Int { 5 | return 0 6 | } 7 | } 8 | 9 | fun main() { 10 | Solution().minCostClimbingStairs(intArrayOf(1, 2)) 11 | } -------------------------------------------------------------------------------- /Nowcoder/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.angus 8 | nowcoder-java 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 8 13 | 8 14 | UTF-8 15 | 16 | 17 | -------------------------------------------------------------------------------- /Nowcoder/src/main/java/HJ001/Main.java: -------------------------------------------------------------------------------- 1 | package HJ001; 2 | 3 | import java.util.Scanner; 4 | 5 | /** 6 | * HJ1 字符串最后一个单词的长度 7 | * 8 | * @Site 9 | */ 10 | public class Main { 11 | public static void main(String[] args) { 12 | Scanner in = new Scanner(System.in); 13 | // 注意 hasNext 和 hasNextLine 的区别 14 | while (in.hasNextLine()) { // 注意 while 处理多个 case 15 | String str = in.nextLine(); 16 | int idx = str.lastIndexOf(' '); 17 | System.out.println(str.length() - idx - 1); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Nowcoder/src/main/java/HJ002/Main.java: -------------------------------------------------------------------------------- 1 | package HJ002; 2 | 3 | import java.util.Scanner; 4 | 5 | /** 6 | * HJ2 计算某字符出现次数 7 | * 8 | * @Site 9 | */ 10 | public class Main { 11 | private static char lowerCase(char ch) { 12 | // A = 65, a = 97 13 | return ch < 'a' ? (char) (ch + 32) : ch; 14 | } 15 | 16 | public static void main(String[] args) { 17 | Scanner in = new Scanner(System.in); 18 | // 注意 hasNext 和 hasNextLine 的区别 19 | while (in.hasNextLine()) { // 注意 while 处理多个 case 20 | String str = in.nextLine(); 21 | char ch = lowerCase(in.nextLine().charAt(0)); 22 | int count = 0; 23 | for (int i = 0; i < str.length(); i++) { 24 | if (lowerCase(str.charAt(i)) == ch) count++; 25 | } 26 | System.out.println(count); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Nowcoder/src/main/java/HJ003/Main.java: -------------------------------------------------------------------------------- 1 | package HJ003; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Scanner; 6 | 7 | /** 8 | * HJ3 明明的随机数 9 | * 10 | * @Site 11 | */ 12 | public class Main { 13 | public static void main(String[] args) { 14 | Scanner in = new Scanner(System.in); 15 | // 注意 hasNext 和 hasNextLine 的区别 16 | while (in.hasNextInt()) { // 注意 while 处理多个 case 17 | List nums = new ArrayList<>(); 18 | int n = in.nextInt(); 19 | for (int i = 0; i < n; i++) { 20 | nums.add(in.nextInt()); 21 | } 22 | nums.stream().sorted().distinct().forEach(System.out::println); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Nowcoder/src/main/java/HJ005/Main.java: -------------------------------------------------------------------------------- 1 | package HJ005; 2 | 3 | import java.util.Scanner; 4 | 5 | /** 6 | * HJ5 进制转换 7 | * 8 | * @Site 9 | */ 10 | public class Main { 11 | public static void main(String[] args) { 12 | Scanner in = new Scanner(System.in); 13 | // 注意 hasNext 和 hasNextLine 的区别 14 | while (in.hasNextLine()) { // 注意 while 处理多个 case 15 | String hexStr = in.nextLine().substring(2); // 去掉开头的 "0x" 16 | System.out.println(Integer.parseInt(hexStr, 16)); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Nowcoder/src/main/java/HJ014/Main.java: -------------------------------------------------------------------------------- 1 | package HJ014; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | /** 7 | * HJ14 字符串排序 8 | * 9 | * @Site 10 | */ 11 | public class Main { 12 | public static void main(String[] args) { 13 | Scanner in = new Scanner(System.in); 14 | // 注意 hasNext 和 hasNextLine 的区别 15 | while (in.hasNextLine()) { // 注意 while 处理多个 case 16 | int n = Integer.parseInt(in.nextLine()); 17 | String[] arr = new String[n]; 18 | for (int i = 0; i < n; i++) { 19 | arr[i] = in.nextLine(); 20 | } 21 | Arrays.sort(arr); 22 | for (String s : arr) { 23 | System.out.println(s); 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Nowcoder/src/main/java/HJ101/Main.java: -------------------------------------------------------------------------------- 1 | package HJ101; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | /** 7 | * HJ101 输入整型数组和排序标识,对其元素按照升序或降序进行排序 8 | * 9 | * @Site 10 | */ 11 | public class Main { 12 | public static void main(String[] args) { 13 | Scanner in = new Scanner(System.in); 14 | // 注意 hasNext 和 hasNextLine 的区别 15 | while (in.hasNextInt()) { // 注意 while 处理多个 case 16 | int n = in.nextInt(); 17 | Integer[] arr = new Integer[n]; 18 | for (int i = 0; i < n; i++) { 19 | arr[i] = in.nextInt(); 20 | } 21 | int order = in.nextInt(); 22 | Arrays.sort(arr, (a, b) -> order == 0 ? a.compareTo(b) : b.compareTo(a)); 23 | for (Integer num : arr) { 24 | System.out.print(num + " "); 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Nowcoder/src/main/java/HJ106/Main.java: -------------------------------------------------------------------------------- 1 | package HJ106; 2 | 3 | import java.util.Scanner; 4 | 5 | /** 6 | * HJ106 字符逆序 7 | * 8 | * @Site 9 | */ 10 | public class Main { 11 | public static void main(String[] args) { 12 | Scanner in = new Scanner(System.in); 13 | // 注意 hasNext 和 hasNextLine 的区别 14 | while (in.hasNextLine()) { // 注意 while 处理多个 case 15 | String str = in.nextLine(); 16 | char[] chars = str.toCharArray(); 17 | for (int i = 0, j = chars.length - 1; i < j; i++, j--) { 18 | char tmp = chars[i]; 19 | chars[i] = chars[j]; 20 | chars[j] = tmp; 21 | } 22 | System.out.println(new String(chars)); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Nowcoder/src/main/java/NC037/Interval.java: -------------------------------------------------------------------------------- 1 | package NC037; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class Interval { 6 | int start; 7 | int end; 8 | 9 | Interval() { 10 | start = 0; 11 | end = 0; 12 | } 13 | 14 | Interval(int s, int e) { 15 | start = s; 16 | end = e; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return "[" + start + "," + end + "]"; 22 | } 23 | 24 | public static ArrayList of(int[][] intArr) { 25 | ArrayList list = new ArrayList<>(); 26 | for (int[] i : intArr) { 27 | list.add(new Interval(i[0], i[1])); 28 | } 29 | return list; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Programmer-Interview-Guide/Kotlin/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | kotlin("jvm") version "1.5.20-M1" 3 | } 4 | 5 | group = "com.angus" 6 | version = "1.0-SNAPSHOT" 7 | 8 | repositories { 9 | mavenCentral() 10 | } 11 | 12 | dependencies { 13 | implementation(kotlin("stdlib")) 14 | } 15 | -------------------------------------------------------------------------------- /Programmer-Interview-Guide/Kotlin/gradle.properties: -------------------------------------------------------------------------------- 1 | kotlin.code.style=official -------------------------------------------------------------------------------- /Programmer-Interview-Guide/Kotlin/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angus-Liu/Algorithm-in-Action/5aaf7421412556782716054f09cdba82b9321d48/Programmer-Interview-Guide/Kotlin/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Programmer-Interview-Guide/Kotlin/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /Programmer-Interview-Guide/Kotlin/settings.gradle.kts: -------------------------------------------------------------------------------- 1 | rootProject.name = "programmer-interview-guide-kotlin" 2 | 3 | -------------------------------------------------------------------------------- /Programmer-Interview-Guide/Kotlin/src/main/kotlin/chapter01/ReverseStack.kt: -------------------------------------------------------------------------------- 1 | package chapter01 2 | 3 | import java.util.* 4 | 5 | /** 6 | * 如何仅用递归函数和栈操作逆序一个栈 7 | * 题目:一个栈依次压入 1、2、3、4、5,那么从栈顶到栈底分别为 5、4、3、2、1。将这个栈转置后, 8 | * 从栈顶到栈底为 1、2、3、4、5,也就是实现栈中元素的逆序,但是只能用递归函数来实现,不能用其他数据结构。 9 | */ 10 | 11 | /** 12 | * 返回栈底元素 13 | */ 14 | fun popLast(stack: LinkedList): Int { 15 | val num = stack.pop() 16 | if (stack.isEmpty()) return num 17 | val last = popLast(stack) 18 | stack.push(num) 19 | return last 20 | } 21 | 22 | /** 23 | * 逆序一个栈 24 | */ 25 | fun reverse(stack: LinkedList) { 26 | if (stack.isEmpty()) return 27 | // 每次弹出栈底元素 28 | val last = popLast(stack) 29 | // 当栈不为空时,递归处理 30 | reverse(stack) 31 | // 将栈底元素重新压入 32 | stack.push(last) 33 | } 34 | 35 | fun main() { 36 | val stack = LinkedList() 37 | stack.push(1) 38 | stack.push(2) 39 | stack.push(3) 40 | println(stack) 41 | // 逆序 42 | reverse(stack) 43 | println(stack) 44 | } -------------------------------------------------------------------------------- /Programmer-Interview-Guide/Kotlin/src/main/kotlin/chapter02/Todo.kt: -------------------------------------------------------------------------------- 1 | package chapter02 2 | 3 | /** 4 | * TODO: angus on 2021/5/28 添加注释 5 | */ 6 | class Todo { 7 | } -------------------------------------------------------------------------------- /Programmer-Interview-Guide/Kotlin/src/main/kotlin/chapter03/Todo.kt: -------------------------------------------------------------------------------- 1 | package chapter03 2 | 3 | /** 4 | * TODO: angus on 2021/5/28 添加注释 5 | */ 6 | class Todo { 7 | } -------------------------------------------------------------------------------- /Programmer-Interview-Guide/Kotlin/src/main/kotlin/chapter04/Todo.kt: -------------------------------------------------------------------------------- 1 | package chapter04 2 | 3 | /** 4 | * TODO: angus on 2021/5/28 添加注释 5 | */ 6 | class Todo { 7 | } -------------------------------------------------------------------------------- /Programmer-Interview-Guide/Kotlin/src/main/kotlin/chapter05/Todo.kt: -------------------------------------------------------------------------------- 1 | package chapter05 2 | 3 | /** 4 | * TODO: angus on 2021/5/28 添加注释 5 | */ 6 | class Todo { 7 | } -------------------------------------------------------------------------------- /Programmer-Interview-Guide/Kotlin/src/main/kotlin/chapter06/Todo.kt: -------------------------------------------------------------------------------- 1 | package chapter06 2 | 3 | /** 4 | * TODO: angus on 2021/5/28 添加注释 5 | */ 6 | class Todo { 7 | } -------------------------------------------------------------------------------- /Programmer-Interview-Guide/Kotlin/src/main/kotlin/chapter07/Todo.kt: -------------------------------------------------------------------------------- 1 | package chapter07 2 | 3 | /** 4 | * TODO: angus on 2021/5/28 添加注释 5 | */ 6 | class Todo { 7 | } -------------------------------------------------------------------------------- /Programmer-Interview-Guide/Kotlin/src/main/kotlin/chapter08/Todo.kt: -------------------------------------------------------------------------------- 1 | package chapter08 2 | 3 | /** 4 | * TODO: angus on 2021/5/28 添加注释 5 | */ 6 | class Todo { 7 | } -------------------------------------------------------------------------------- /Programmer-Interview-Guide/Kotlin/src/main/kotlin/chapter09/Todo.kt: -------------------------------------------------------------------------------- 1 | package chapter09 2 | 3 | /** 4 | * TODO: angus on 2021/5/28 添加注释 5 | */ 6 | class Todo { 7 | } -------------------------------------------------------------------------------- /Python/.gitignore: -------------------------------------------------------------------------------- 1 | ### Example user template template 2 | ### Example user template 3 | 4 | # IntelliJ project files 5 | .idea 6 | *.iml 7 | out 8 | gen 9 | -------------------------------------------------------------------------------- /Python/001 Two Sum/Solution.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def twoSum(self, nums: List[int], target: int) -> List[int]: 6 | num_dict = {} 7 | for i, num in enumerate(nums): 8 | diff = target - num 9 | if diff in num_dict: 10 | return [i, num_dict[diff]] 11 | else: 12 | num_dict[num] = i 13 | return [] 14 | -------------------------------------------------------------------------------- /assets/Algorithm-in-Action-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angus-Liu/Algorithm-in-Action/5aaf7421412556782716054f09cdba82b9321d48/assets/Algorithm-in-Action-logo.png --------------------------------------------------------------------------------