├── .gitignore ├── LICENSE ├── PULL_REQUEST_TEMPLATE.md ├── README.md ├── README_TEMPLATE.md ├── docs ├── Algorithm_Implementation │ ├── C++ │ │ └── README.md │ ├── Java │ │ ├── Cipher │ │ │ └── README.md │ │ ├── Compression │ │ │ └── README.md │ │ ├── Conversion │ │ │ └── README.md │ │ ├── Data_Structure │ │ │ ├── Graph │ │ │ │ └── README.md │ │ │ ├── HashMap │ │ │ │ └── README.md │ │ │ ├── HashTable │ │ │ │ └── README.md │ │ │ ├── Heqp │ │ │ │ └── README.md │ │ │ ├── LinkedList │ │ │ │ └── README.md │ │ │ ├── Matrix │ │ │ │ └── README.md │ │ │ ├── Queue │ │ │ │ └── README.md │ │ │ ├── README.md │ │ │ ├── Stack │ │ │ │ └── README.md │ │ │ └── Tree │ │ │ │ └── README.md │ │ ├── Dynamic_Programming │ │ │ └── README.md │ │ ├── Hash │ │ │ └── README.md │ │ ├── Others │ │ │ ├── NextNodeInOrderTree.md │ │ │ ├── TopKWords.java │ │ │ └── words.txt │ │ ├── README.md │ │ ├── Search │ │ │ └── README.md │ │ ├── Sort │ │ │ ├── README.md │ │ │ ├── Sort.ipynb │ │ │ ├── insertionSort.py │ │ │ └── 排序稳定性.md │ │ └── Traversals │ │ │ └── README.md │ ├── JavaScript │ │ └── README.md │ └── Python │ │ ├── Cipher │ │ └── README.md │ │ ├── Compression │ │ └── README.md │ │ ├── Conversion │ │ └── README.md │ │ ├── Data_Structure │ │ ├── Graph │ │ │ └── README.md │ │ ├── HashMap │ │ │ └── README.md │ │ ├── HashTable │ │ │ └── README.md │ │ ├── Heqp │ │ │ └── README.md │ │ ├── LinkedList │ │ │ └── README.md │ │ ├── Matrix │ │ │ └── README.md │ │ ├── Queue │ │ │ └── README.md │ │ ├── README.md │ │ ├── Stack │ │ │ └── README.md │ │ └── Tree │ │ │ └── README.md │ │ ├── Dynamic_Programming │ │ └── README.md │ │ ├── Hash │ │ └── README.md │ │ ├── Others │ │ ├── NextNodeInOrderTree.md │ │ ├── TopKWords.java │ │ └── words.txt │ │ ├── README.md │ │ ├── Search │ │ └── README.md │ │ ├── Sort │ │ ├── README.md │ │ ├── Sort.ipynb │ │ ├── insertionSort.py │ │ └── 排序稳定性.md │ │ └── Traversals │ │ └── README.md ├── Leetcode_Solutions │ ├── C++ │ │ ├── 0001._two_sum.md │ │ ├── 0002._Add_Two_Numbers.md │ │ ├── 0003._Longest_Substring_Without_Repeating_Characters.md │ │ ├── 0004. _Median_of_Two_Sorted_Arrays.md │ │ ├── 0005._Longest_Palindromic_Substring.md │ │ ├── 0006._ZigZag _Conversion.md │ │ ├── 0007._Reverse_Integer.md │ │ ├── 0008._String_to_Integer_(atoi).md │ │ ├── 0009._Palindrome_Number.md │ │ ├── 0010._Regular_Expression_Matching.md │ │ ├── 0011._container_with_most_water.md │ │ ├── 0012._Integer_to_Roman.md │ │ ├── 0014._Longest_Common_Prefix.md │ │ ├── 0015._3sum.md │ │ ├── 0016._3Sum_Closest.md │ │ ├── 0017._Letter_Combinations_of_a_Phone_Number.md │ │ ├── 0018._4Sum.md │ │ ├── 0019._Remove_Nth_Node_From_End_of_List.md │ │ ├── 0020._valid_parentheses.md │ │ ├── 0021._Merge _Two _Sorted _Lists.md │ │ ├── 0022._generate_parentheses.md │ │ ├── 0023._merge_k_sorted_lists.md │ │ ├── 0024._Swap_Nodes_in_Pairs.md │ │ ├── 0025._reverse_nodes_in_k_group.md │ │ ├── 0026._Remove_Duplicates_From_Sorted_Array.md │ │ ├── 0027._Remove_Element.md │ │ ├── 0028._implement_strstr.md │ │ ├── 0029._divide_two_integers.md │ │ ├── 0030._substring_with_concatenation_of_all_words.md │ │ ├── 0031._Next_Permutatio.md │ │ ├── 0032._Longest_Valid_Parentheses.md │ │ ├── 0033._Search_in_Rotated_Sorted_Array.md │ │ ├── 0034._Find_First_and_Last_Position_of_Element_in_Sorted_Array.md │ │ ├── 0035._search_insert_position.md │ │ ├── 0036._Valid_Sudoku.md │ │ ├── 0038._Count_and_Say.md │ │ ├── 0039._Combination_Sum.md │ │ ├── 0040._Combination_Sum_II.md │ │ ├── 0041._First_Missing_Positive.md │ │ ├── 0042._Trapping_Rain_Water.md │ │ ├── 0043._Multiply_Strings.md │ │ ├── 0044._Wildcard_Matching.md │ │ ├── 0045._Jump_Game_II.md │ │ ├── 0046._Permutations.md │ │ ├── 0047._Permutations_II.md │ │ ├── 0048._Rotate_Image.md │ │ ├── 0049._Group_Anagrams.md │ │ ├── 0050._powx_n.md │ │ ├── 0051._ N-Queens.md │ │ ├── 0052._N-Queens_II.md │ │ ├── 0053._Maximum_Subarray.md │ │ ├── 0054._Spiral_Matrix.md │ │ ├── 0055._Jump_Game.md │ │ ├── 0056._Merge_Intervals.md │ │ ├── 0057._Insert_Interval.md │ │ ├── 0058._Length_of_Last_Word.md │ │ ├── 0059._Spiral_Matrix_II.md │ │ ├── 0060._Permutation_Sequence.md │ │ ├── 0061._Rotate_List.md │ │ ├── 0062._Unique_Paths.md │ │ ├── 0063._Unique_Paths_II.md │ │ ├── 0064._Minimum_Path_Sum.md │ │ ├── 0065._Valid_Number.md │ │ ├── 0066._Plus_One.md │ │ ├── 0068._Text_Justification.md │ │ ├── 0069._Sqr(x).md │ │ ├── 0072._Edit_Distance.md │ │ ├── 0075._Sort_Colors.md │ │ ├── 0076._Minimum_Window_Substring.md │ │ ├── 0077._combinations.md │ │ ├── 0078._subsets.md │ │ ├── 0081._Search_in_Rotated_Sorted_Array_II.md │ │ ├── 0083._remove_duplicates_from_sorted_lists.md │ │ ├── 0084._Largest_Rectangle_in_Histogram.md │ │ ├── 0085._Maximal_Rectangle.md │ │ ├── 0087._Scramble_String.md │ │ ├── 0088._Merge_Sorted_Array.md │ │ ├── 0090._Subsets_II.md │ │ ├── 0094._binary_tree_inorder_traversal.md │ │ ├── 0096._Unique_Binary_Search_Trees.md │ │ ├── 0097._Interleaving_String.md │ │ ├── 0099._Recover_Binary_Search_Tree.md │ │ ├── 0100._same_tree.md │ │ ├── 0101._Symmetric_Tree.md │ │ ├── 0102._Binary_Tree_Level_Order_Traversal.md │ │ ├── 0104._Maximum_Depth_of_Binary_Tree.md │ │ ├── 0105._Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.md │ │ ├── 0106._Construct_Binary_Tree_from_Inorder_and_Postorder_Traversal.md │ │ ├── 0107._Binary_Tree Level_Order_Traversal_II.md │ │ ├── 0108._Convert_Sorted_Array_to_Binary_Search_Tree.md │ │ ├── 0109._Convert_Sorted_List_to_Binary_Search_Tree.md │ │ ├── 0110._Balanced_Binary_Tree.md │ │ ├── 0111._minimum_depth_of_binary_tree.md │ │ ├── 0112._Path_Sum.md │ │ ├── 0114._Flatten_Binary_Tree_to_Linked_List.md │ │ ├── 0115._Distinct_Subsequences.md │ │ ├── 0118._Pascals_Triangle.md │ │ ├── 0119._Pascals_Triangle-II.md │ │ ├── 0120._Triangle.md │ │ ├── 0121._Best_Tim_ to_Buy_and_Sell_Stock.md │ │ ├── 0122._Best_Time_to_Buy_and_Sell_Stock_II.md │ │ ├── 0123._Best_Time_to_Buy _and_Sell_Stock_III.md │ │ ├── 0124._Binary_Tree_Maximum_Path_Sum.md │ │ ├── 0127._Word_Ladde.md │ │ ├── 0128._Longest_Consecutive_Sequence.md │ │ ├── 0129._Sum_Root_to_Leaf_Numbers.md │ │ ├── 0131._Palindrome_Partitioning.md │ │ ├── 0136._Single_Numbe.md │ │ ├── 0137._single_number_II.md │ │ ├── 0141._linked_list_cycle.md │ │ ├── 0142._linked_list_cycle II.md │ │ ├── 0144._Binary_Tree_Preorder_Traversal.md │ │ ├── 0145._Binary_Tree_Postorder_Traversal.md │ │ ├── 0147._Insert_on_Sort_List.md │ │ ├── 0148._Sort _list.md │ │ ├── 0151._Reverse_Words_in_a_String.md │ │ ├── 0153._Find_Minimum_in_Rotated_Sorted_Array.md │ │ ├── 0154._Find_Minimum_in_Rotated_Sorted_Array-II.md │ │ ├── 0160._intersection_of_two_linked_lists.md │ │ ├── 0164._Maximum_Gap.md │ │ ├── 0166._Fraction_to_Recurring_Decimal.md │ │ ├── 0167._Two_Sum_II-Input_array_is_sorted.md │ │ ├── 0199._Binary_Tree_Right_Side_View.md │ │ ├── 0216._Combination_Sum_III.md │ │ ├── 0230._Kth_Smallest_Element_in_a_BST.md │ │ ├── 0260._Single_Number_III.md │ │ ├── 0287._Find_the_Duplicate_Number.md │ │ ├── 0326._power_of_three.md │ │ ├── 0328._Odd_Even_Linked_List.md │ │ ├── 0329._Longest_Increasing_Path_in_a_Matrix.md │ │ ├── 0338._counting_bits.md │ │ ├── 0413._Arithmetic_Slices.md │ │ ├── 0442._Find_All_Duplicates_in_an_Array.md │ │ ├── 0513._Find_Bottom_Left_Tree_Value.md │ │ ├── 0515._Find_Largest_Value_in_Each_Tree_Row.md │ │ ├── 0540._Single_Element_in_a_Sorted_Array.md │ │ ├── 0581._shortest_unsorted_continuous_subarray.md │ │ ├── 0629._K_Inverse_Pairs_Array.md │ │ ├── 0632._Smallest_Range.md │ │ ├── 0654._maximum_binary_tree.md │ │ ├── 0668._Kth_Smallest_Number_in_Multiplication_Table.md │ │ ├── 0701._Insert_into_a_Binary_Search_Tree.md │ │ ├── 0715._Range_Module.md │ │ ├── 0719._Find_K-th_Smallest_Pair_Distance.md │ │ ├── 0739._Daily_Temperatures.md │ │ ├── 0797._all_paths_from_source_to_target.md │ │ ├── 0814._binary_tree_pruning.md │ │ ├── 0841._Keys_and_Rooms.md │ │ ├── 0877._Stone_Game.md │ │ ├── 0945._Minimum_Increment_to_Make_Array_Unique.md │ │ ├── 0946._Validate_Stack_Sequences.md │ │ ├── 0947._Most_Stones_Removed_with_Same_Row_or_Column.md │ │ ├── 0948._Bag_of_Tokens.md │ │ ├── 0949._Largest_Time_for_Given_Digits.md │ │ ├── 0950._Reveal_Cards_In_Increasing_Order.md │ │ ├── 0951._Flip_Equivalent_Binary_Trees.md │ │ ├── 0952._Largest_Component_Size_by_Common_Factor.md │ │ └── README.md │ ├── Java │ │ ├── 0001._Two_Sum.md │ │ ├── 0002._add_two_numbers.md │ │ ├── 0003._Longest_Substring_Without_Repeating_Characters.md │ │ ├── 0004._Median_of_Two_Sorted_Arrays.md │ │ ├── 0005._Longest_Palindromic_Substring.md │ │ ├── 0023._Merge_K_Sorted_Lists.md │ │ ├── 0141._linked_list_cycle.md │ │ ├── 0218._The_Skyline_Problem.md │ │ ├── 0238._product_of_array_except_self.md │ │ ├── 0342._Power_of_Four.md │ │ ├── 0403._Frog_Jump.md │ │ ├── 0757._Set_Intersection_Size_At_Least_Two.md │ │ ├── 0768._Max_Chunks_To_Make_Sorted_II.md │ │ ├── 0780._Reaching_Points.md │ │ ├── 0793._Preimage_Size_of_Factorial_Zeroes_Function.md │ │ ├── 0827._Making_A_Large_Island.md │ │ ├── 0828._Unique_Letter_String.md │ │ ├── 0834._Sum_of_Distances_in_Tree.md │ │ ├── 0843._Guess_the_Word.md │ │ ├── 0847._Shortest_Path_Visiting_All_Nodes.md │ │ ├── 0850._Rectangle_Area_II.md │ │ ├── 0854._K-Similar_Strings.md │ │ ├── 0857._Minimum_Cost_to_Hire_K_Workers.md │ │ ├── 0862._Shortest_Subarray_with_Sum_at_Least_K.md │ │ ├── 0864._Shortest_Path_to_Get_All_Keys.md │ │ ├── 0871._Minimum_Number_of_Refueling_Stops.md │ │ ├── 0878._Nth_Magical_Number.md │ │ ├── 0879._Profitable_Schemes.md │ │ ├── 0882._Reachable_Nodes_In_Subdivided_Graph.md │ │ ├── 0887._Super_Egg_Drop.md │ │ ├── 0891._Sum_of_Subsequence_Widths.md │ │ ├── 0895._Maximum_Frequency_Stack.md │ │ ├── 0899._Orderly_Queue.md │ │ ├── 0902._Numbers_At_Most_N_Given_Digit_Set.md │ │ ├── 0903._Valid_Permutations_for_DI_Sequence.md │ │ ├── 0906._Super_Palindromes.md │ │ ├── 0913._Cat_and_Mouse.md │ │ └── 0920._Number_of_Music_Playlists.md │ ├── JavaScript │ │ ├── 0001._Two_Sum.md │ │ ├── 0002._Add_Two_Numbers.md │ │ ├── 0003._Longest_Substring_without_Repeating_Characters.md │ │ ├── 0007._Reverse_Integer.md │ │ ├── 0008._String_to_Integer.md │ │ ├── 0009._Palindrome_Number.md │ │ ├── 0011._Container_With_Most_Water.md │ │ ├── 0012._Integer_To_Roman.md │ │ ├── 0013._Roman_To_Integer.md │ │ ├── 0014._Longest_Common_Prefix.md │ │ ├── 0015._Three_Sum.md │ │ ├── 0016._3_Sum_Closest.md │ │ ├── 0017._Letter_Combinations_Of_A_Phone_Number.md │ │ ├── 0020._Valid_Parentheses.md │ │ ├── 0021._Merge_Two_Sorted_Lists.md │ │ ├── 0022._Generate_Parentheses.md │ │ ├── 0035._Search_Insert_Position.md │ │ ├── 0058._Length_of_Last_Word.md │ │ ├── 0066._Plus_One.md │ │ ├── 0067._Add_Binary.md │ │ ├── 0074._Search_a_2D_Matrix.md │ │ ├── 0104._Maximum_Depth_of_Binary_Tree.md │ │ ├── 0167._Two_Sum_II_-_Input_array_is_sorted.md │ │ ├── 0167._Two_Sum_II_Input_Array_is_Sorted.md │ │ ├── 0209._Minimum_Size_Subarray_Sum.md │ │ ├── 0258._Add_Digits.md │ │ ├── 0402._Remove_K_Digits.md │ │ ├── 0485._Max_Consecutive_Ones.md │ │ ├── 0539._Minimum_Time_Difference.md │ │ └── 0881._Boats_to_Save_People.md │ ├── Python │ │ ├── 0001._two_sum.md │ │ ├── 0002._add_two_numbers.md │ │ ├── 0003._longest_substring_without_repeating_characters.md │ │ ├── 0004._median_of_two_sorted_arrays.md │ │ ├── 0005._longest_palindromic_substring.md │ │ ├── 0006._ZigZag_Conversion.md │ │ ├── 0007._Reverse_Integer.md │ │ ├── 0008._string_to_integer_(atoi).md │ │ ├── 0009._Palindrome_Number.md │ │ ├── 0010._regular_expression_matching.md │ │ ├── 0011._container_with_most_water.md │ │ ├── 0012._Integer_to_Roman.md │ │ ├── 0013._Roman_to_Integer.md │ │ ├── 0014._longest_common_prefix.md │ │ ├── 0015._3sum.md │ │ ├── 0016._3sum_closest.md │ │ ├── 0017._letter_combinations_of_a_phone_number.md │ │ ├── 0018._4sum.md │ │ ├── 0019._remove_nth_node_from_end_of_list.md │ │ ├── 0020._valid_parentheses.md │ │ ├── 0021._merge_two_sorted_lists.md │ │ ├── 0022._generate_parentheses.md │ │ ├── 0023._merge_k_sorted_lists.md │ │ ├── 0024._swap_nodes_in_pairs.md │ │ ├── 0025._Reverse_Nodes_in_k-Group.md │ │ ├── 0026._Remove_Duplicates_from_Sorted_Array.md │ │ ├── 0027._Remove_Element.md │ │ ├── 0028._implement_strstr().md │ │ ├── 0029._Divide_Two_Integers.md │ │ ├── 0030._Substring_with_Concatenation_of_All_Words.md │ │ ├── 0031._next_permutation.md │ │ ├── 0032._longest_valid_parentheses.md │ │ ├── 0033._search_in_rotated_sorted_array.md │ │ ├── 0034._Search_for_a_Range.md │ │ ├── 0035._search_insert_position.md │ │ ├── 0036._Valid_Sudoku.md │ │ ├── 0037._Sudoku_Solver.md │ │ ├── 0038._Count_and_Say.md │ │ ├── 0039._combination_sum.md │ │ ├── 0040._combination_sum_ii.md │ │ ├── 0041._First_Missing_Positive.md │ │ ├── 0042._trapping_rain_water.md │ │ ├── 0043._multiply_strings.md │ │ ├── 0044._wildcard_matching.md │ │ ├── 0045._Jump_Game_II.md │ │ ├── 0046._permutations.md │ │ ├── 0047._permutations_ii.md │ │ ├── 0048._rotate_image.md │ │ ├── 0049._group_anagrams.md │ │ ├── 0050._pow(x,_n).md │ │ ├── 0051._n-queens.md │ │ ├── 0052._n-queens_ii.md │ │ ├── 0053._maximum_subarray.md │ │ ├── 0054._spiral_matrix.md │ │ ├── 0055._jump_game.md │ │ ├── 0056._Merge_Intervals.md │ │ ├── 0057._Insert_Interval.md │ │ ├── 0058._length_of_last_word.md │ │ ├── 0059._spiral_matrix_ii.md │ │ ├── 0060._permutation_sequence.md │ │ ├── 0061._rotate_list.md │ │ ├── 0062._unique_paths.md │ │ ├── 0063._unique_paths_ii.md │ │ ├── 0064._minimum_path_sum.md │ │ ├── 0065._Valid_Number.md │ │ ├── 0066._plus_one.md │ │ ├── 0067._add_binary.md │ │ ├── 0068._Text_Justification.md │ │ ├── 0069._sqrt(x).md │ │ ├── 0070._Climbing_Stairs.md │ │ ├── 0071._simplify_path.md │ │ ├── 0072._edit_distance.md │ │ ├── 0073._Set_Matrix_Zeroes.md │ │ ├── 0074._search_a_2d_matrix.md │ │ ├── 0075._sort_colors.md │ │ ├── 0076._Minimum_Window_Substring.md │ │ ├── 0077._combinations.md │ │ ├── 0078._Subsets.md │ │ ├── 0079._word_search.md │ │ ├── 0080._remove_duplicates_from_sorted_array_ii.md │ │ ├── 0081._Search_in_Rotated_Sorted_Array_II.md │ │ ├── 0082._remove_duplicates_from_sorted_list_ii.md │ │ ├── 0083._remove_duplicates_from_sorted_list.md │ │ ├── 0084._Largest_Rectangle_in_Histogram.md │ │ ├── 0085._Maximal_Rectangle.md │ │ ├── 0086._partition_list.md │ │ ├── 0087._Scramble_String.md │ │ ├── 0088._merge_sorted_array.md │ │ ├── 0089._gray_code.md │ │ ├── 0090._subsets_ii.md │ │ ├── 0091._decode_ways.md │ │ ├── 0092._reverse_linked_list_ii.md │ │ ├── 0093._restore_ip_addresses.md │ │ ├── 0094._binary_tree_inorder_traversal.md │ │ ├── 0095._Unique_Binary_Search_Trees_II.md │ │ ├── 0096._unique_binary_search_trees.md │ │ ├── 0097._Interleaving_String.md │ │ ├── 0098._validate_binary_search_tree.md │ │ ├── 0099._Recover_Binary_Search_Tree.md │ │ ├── 0100._same_tree.md │ │ ├── 0101._symmetric_tree.md │ │ ├── 0102._binary_tree_level_order_traversal.md │ │ ├── 0103._binary_tree_zigzag_level_order_traversal.md │ │ ├── 0104._maximum_depth_of_binary_tree.md │ │ ├── 0105._construct_binary_tree_from_preorder_and_inorder_traversal.md │ │ ├── 0106._construct_binary_tree_from_inorder_and_postorder_traversal.md │ │ ├── 0107._binary_tree_level_order_traversal_ii.md │ │ ├── 0108._convert_sorted_array_to_binary_search_tree.md │ │ ├── 0109._convert_sorted_list_to_binary_search_tree.md │ │ ├── 0110._balanced_binary_tree.md │ │ ├── 0111._minimum_depth_of_binary_tree.md │ │ ├── 0112._path_sum.md │ │ ├── 0113._path_sum_ii.md │ │ ├── 0114._flatten_binary_tree_to_linked_list.md │ │ ├── 0115._Distinct_Subsequences.md │ │ ├── 0116._populating_next_right_pointers_in_each_node.md │ │ ├── 0117._Populating_Next_Right_Pointers_in_Each_Node_II.md │ │ ├── 0118._pascal's_triangle.md │ │ ├── 0119._Pascal's_Triangle_II.md │ │ ├── 0120._Triangle.md │ │ ├── 0121._Best_Time_to_Buy_and_Sell_Stock.md │ │ ├── 0122._Best_Time_to_Buy_and_Sell_Stock_II.md │ │ ├── 0123._Best_Time_to_Buy_and_Sell_Stock_III.md │ │ ├── 0124._Binary_Tree_Maximum_Path_Sum.md │ │ ├── 0125._valid_palindrome.md │ │ ├── 0126._Word_Ladder_II.md │ │ ├── 0127._word_ladder.md │ │ ├── 0128._Longest_Consecutive_Sequence.md │ │ ├── 0129._sum_root_to_leaf_numbers.md │ │ ├── 0130._surrounded_regions.md │ │ ├── 0131._palindrome_partitioning.md │ │ ├── 0132._Palindrome_Partitioning_II.md │ │ ├── 0133._clone_graph.md │ │ ├── 0134._Gas_Station.md │ │ ├── 0135._Candy.md │ │ ├── 0136._single_number.md │ │ ├── 0137._Single_Number_II.md │ │ ├── 0138._Copy_List_with_Random_Pointer.md │ │ ├── 0139._word_break.md │ │ ├── 0140._word_break_ii.md │ │ ├── 0141._linked_list_cycle.md │ │ ├── 0142_Linked_List_Cycle_II.md │ │ ├── 0143._reorder_list.md │ │ ├── 0144._binary_tree_preorder_traversal.md │ │ ├── 0145._binary_tree_postorder_traversal.md │ │ ├── 0146._LRU_Cache.md │ │ ├── 0147._insertion_sort_list.md │ │ ├── 0148._sort_list.md │ │ ├── 0149._Max_Points_on_a_Line.md │ │ ├── 0150._evaluate_reverse_polish_notation.md │ │ ├── 0151._reverse_words_in_a_string.md │ │ ├── 0152._maximum_product_subarray.md │ │ ├── 0153._find_minimum_in_rotated_sorted_array.md │ │ ├── 0154._Find_Minimum_in_Rotated_Sorted_Array_II.md │ │ ├── 0155._min_stack.md │ │ ├── 0156._Binary_Tree_Upside_Down.md │ │ ├── 0157._Read_N_Characters_Given_Read4.md │ │ ├── 0158._Read_N_Characters_Given_Read4_II_-_Call_multiple_times.md │ │ ├── 0159._Longest_Substring_with_At_Most_Two_Distinct_Characters.md │ │ ├── 0160._intersection_of_two_linked_lists.md │ │ ├── 0161._One_Edit_Distance.md │ │ ├── 0162._find_peak_element.md │ │ ├── 0163._Missing_Ranges.md │ │ ├── 0165._compare_version_numbers.md │ │ ├── 0166._Fraction_to_Recurring_Decimal.md │ │ ├── 0167._two_sum_ii_-_input_array_is_sorted.md │ │ ├── 0168._excel_sheet_column_title.md │ │ ├── 0169._majority_element.md │ │ ├── 0170._Two_Sum_III_-_Data_structure_design.md │ │ ├── 0171._excel_sheet_column_number.md │ │ ├── 0172._Factorial_Trailing_Zeroes.md │ │ ├── 0173._binary_search_tree_iterator.md │ │ ├── 0174._dungeon_game.md │ │ ├── 0176._Second_Highest_Salary.md │ │ ├── 0177._Nth_Highest_Salary.md │ │ ├── 0178._Rank_Scores.md │ │ ├── 0179._Largest_Number.md │ │ ├── 0180._Consecutive_Numbers.md │ │ ├── 0182._duplicate_emails.md │ │ ├── 0183._Customers_Who_Never_Order.md │ │ ├── 0184._Department_Highest_Salary.md │ │ ├── 0186._Reverse_Words_in_a_String_II.md │ │ ├── 0187._Repeated_DNA_Sequences.md │ │ ├── 0188._Best_Time_to_Buy_and_Sell_Stock_IV.md │ │ ├── 0189._rotate_array.md │ │ ├── 0190._Reverse_Bits.md │ │ ├── 0191._number_of_1_bits.md │ │ ├── 0192._Word_Frequency.md │ │ ├── 0194._Transpose_File.md │ │ ├── 0196._Delete_Duplicate_Emails.md │ │ ├── 0198._house_robber.md │ │ ├── 0199._binary_tree_right_side_view.md │ │ ├── 0200._number_of_islands.md │ │ ├── 0201._Bitwise_AND_of_Numbers_Range.md │ │ ├── 0202._Happy_Number.md │ │ ├── 0203._remove_linked_list_elements.md │ │ ├── 0204._count_primes.md │ │ ├── 0205._isomorphic_strings.md │ │ ├── 0206._reverse_linked_list.md │ │ ├── 0207._course_schedule.md │ │ ├── 0208._implement_trie_(prefix_tree).md │ │ ├── 0209._Minimum_Size_Subarray_Sum.md │ │ ├── 0210._course_schedule_ii.md │ │ ├── 0211._Add_and_Search_Word_-_Data_structure_design.md │ │ ├── 0212._Word_Search_II.md │ │ ├── 0213._house_robber_ii.md │ │ ├── 0214._Shortest_Palindrome.md │ │ ├── 0215._Kth_Largest_Element_in_an_Array.md │ │ ├── 0216._combination_sum_iii.md │ │ ├── 0217._contains_duplicate.md │ │ ├── 0218._The_Skyline_Problem.md │ │ ├── 0219._contains_duplicate_ii.md │ │ ├── 0220._Contains_Duplicate_III.md │ │ ├── 0221._maximal_square.md │ │ ├── 0222._count_complete_tree_nodes.md │ │ ├── 0223._rectangle_area.md │ │ ├── 0224._Basic_Calculator.md │ │ ├── 0225._implement_stack_using_queues.md │ │ ├── 0226._invert_binary_tree.md │ │ ├── 0227._basic_calculator_ii.md │ │ ├── 0228._summary_ranges.md │ │ ├── 0229._majority_element_ii.md │ │ ├── 0230._kth_smallest_element_in_a_bst.md │ │ ├── 0231._Power_of_Two.md │ │ ├── 0232._implement_queue_using_stacks.md │ │ ├── 0234._palindrome_linked_list.md │ │ ├── 0235._lowest_common_ancestor_of_a_binary_search_tree.md │ │ ├── 0236._lowest_common_ancestor_of_a_binary_tree.md │ │ ├── 0237._delete_node_in_a_linked_list.md │ │ ├── 0238._product_of_array_except_self.md │ │ ├── 0239._Sliding_Window_Maximum.md │ │ ├── 0240._search_a_2d_matrix_ii.md │ │ ├── 0241._Different_Ways_to_Add_Parentheses.md │ │ ├── 0242._valid_anagram.md │ │ ├── 0243._Shortest_Word_Distance.md │ │ ├── 0244._Shortest_Word_Distance_II.md │ │ ├── 0245._Shortest_Word_Distance_III.md │ │ ├── 0247._Strobogrammatic_Number_II.md │ │ ├── 0249._Group_Shifted_Strings.md │ │ ├── 0250._Count_Univalue_Subtrees.md │ │ ├── 0251._Flatten_2D_Vector.md │ │ ├── 0252._Meeting_Rooms.md │ │ ├── 0253._Meeting_Rooms_II.md │ │ ├── 0254._Factor_Combinations.md │ │ ├── 0255._Verify_Preorder_Sequence_in_Binary_Search_Tree.md │ │ ├── 0256._Paint_House.md │ │ ├── 0257._binary_tree_paths.md │ │ ├── 0258._Add_Digits.md │ │ ├── 0259._3Sum_Smaller.md │ │ ├── 0260._Single_Number_III.md │ │ ├── 0261._Graph_Valid_Tree.md │ │ ├── 0263._ugly_number.md │ │ ├── 0264._ugly_number_ii.md │ │ ├── 0265._Paint_House_II.md │ │ ├── 0266._Palindrome_Permutation.md │ │ ├── 0267._Palindrome_Permutation_II.md │ │ ├── 0268._missing_number.md │ │ ├── 0269._Alien_Dictionary.md │ │ ├── 0270._Closest_Binary_Search_Tree_Value.md │ │ ├── 0274._H_Index.md │ │ ├── 0276._Paint_Fence.md │ │ ├── 0277._Find_the_Celebrity.md │ │ ├── 0278._First_Bad_Version.md │ │ ├── 0279._perfect_squares.md │ │ ├── 0280._Wiggle_Sort.md │ │ ├── 0281._Zigzag_Iterator.md │ │ ├── 0282._Expression_Add_Operators.md │ │ ├── 0283._move_zeroes.md │ │ ├── 0285._inorder_successor_in_bst.md │ │ ├── 0286._Walls_and_Gates.md │ │ ├── 0287._Find_the_Duplicate_Number.md │ │ ├── 0289._game_of_life.md │ │ ├── 0290._word_pattern.md │ │ ├── 0292._nim_game.md │ │ ├── 0293._Flip_Game.md │ │ ├── 0295._Find_Median_from_Data_Stream.md │ │ ├── 0296._Best_Meeting_Point.md │ │ ├── 0297._Serialize_and_Deserialize_Binary_Tree.md │ │ ├── 0298._Binary_Tree_Longest_Consecutive_Sequence.md │ │ ├── 0299._bulls_and_cows.md │ │ ├── 0300._longest_increasing_subsequence.md │ │ ├── 0301._Remove_Invalid_Parentheses.md │ │ ├── 0303._range_sum_query_-_immutable.md │ │ ├── 0305._Number_of_Islands_II.md │ │ ├── 0307._Range_Sum_Query_-_Mutable.md │ │ ├── 0308._Range_Sum_Query_2D_-_Mutable.md │ │ ├── 0309._Best_Time_to_Buy_and_Sell_Stock_with_Cooldown.md │ │ ├── 0310._Minimum_Height_Trees.md │ │ ├── 0311._Sparse_Matrix_Multiplication.md │ │ ├── 0312._Burst_Balloons.md │ │ ├── 0313._Super_Ugly_Number.md │ │ ├── 0315._Count_of_Smaller_Numbers_After_Self.md │ │ ├── 0316._Remove_Duplicate_Letters.md │ │ ├── 0318._Maximum_Product_of_Word_Lengths.md │ │ ├── 0319._Bulb_Switcher.md │ │ ├── 0321._Create_Maximum_Number.md │ │ ├── 0322._Coin_Change.md │ │ ├── 0323._number_of_connected_components_in_an_undirected_graph.md │ │ ├── 0324._Wiggle_Sort_II.md │ │ ├── 0325._Maximum_Size_Subarray_Sum_Equals_k.md │ │ ├── 0326._power_of_three.md │ │ ├── 0328._odd_even_linked_list.md │ │ ├── 0329._Longest_Increasing_Path_in_a_Matrix.md │ │ ├── 0332._Reconstruct_Itinerary.md │ │ ├── 0334._increasing_triplet_subsequence.md │ │ ├── 0336._Palindrome_Pairs.md │ │ ├── 0337._house_robber_iii.md │ │ ├── 0338._Counting_Bits.md │ │ ├── 0339._Nested_List_Weight_Sum.md │ │ ├── 0340._Longest_Substring_with_At_Most_K_Distinct_Characters.md │ │ ├── 0341._Flatten_Nested_List_Iterator.md │ │ ├── 0342._Power_of_Four.md │ │ ├── 0343._Integer_Break.md │ │ ├── 0344._reverse_string.md │ │ ├── 0345._Reverse_Vowels_of_a_String.md │ │ ├── 0346._Moving_Average_from_Data_Stream.md │ │ ├── 0347._Top_K_Frequent_Elements.md │ │ ├── 0348._Design_Tic-Tac-Toe.md │ │ ├── 0349._intersection_of_two_arrays.md │ │ ├── 0350._intersection_of_two_arrays_ii.md │ │ ├── 0353._Design_Snake_Game.md │ │ ├── 0355._Design_Twitter.md │ │ ├── 0359._Logger_Rate_Limiter.md │ │ ├── 0360._Sort_Transformed_Array.md │ │ ├── 0361._Bomb_Enemy.md │ │ ├── 0363._Max_Sum_of_Rectangle_No_Larger_Than_K.md │ │ ├── 0364._Nested_List_Weight_Sum_II.md │ │ ├── 0366._Find_Leaves_of_Binary_Tree.md │ │ ├── 0367._valid_perfect_square.md │ │ ├── 0369._Plus_One_Linked_List.md │ │ ├── 0370._Range_Addition.md │ │ ├── 0371._sum_of_two_integers.md │ │ ├── 0372._Super_Pow.md │ │ ├── 0374._Guess_Number_Higher_or_Lower.md │ │ ├── 0376._Wiggle_Subsequence.md │ │ ├── 0377._combination_sum_iv.md │ │ ├── 0378._kth_smallest_element_in_a_sorted_matrix.md │ │ ├── 0380._Insert_Delete_GetRandom_O(1).md │ │ ├── 0381._Insert_Delete_GetRandom_O(1)_-_Duplicates_allowed.md │ │ ├── 0382._linked_list_random_node.md │ │ ├── 0383._ransom_note.md │ │ ├── 0384._Shuffle_an_Array.md │ │ ├── 0386._Lexicographical_Numbers.md │ │ ├── 0387._first_unique_character_in_a_string.md │ │ ├── 0388._Longest_Absolute_File_Path.md │ │ ├── 0389._find_the_difference.md │ │ ├── 0392._is_subsequence.md │ │ ├── 0394._decode_string.md │ │ ├── 0395._Longest_Substring_with_At_Least_K_Repeating_Characters.md │ │ ├── 0397._Integer_Replacement.md │ │ ├── 0398._Random_Pick_Index.md │ │ ├── 0400._Nth_Digit.md │ │ ├── 0401._binary_watch.md │ │ ├── 0402._Remove_K_Digits.md │ │ ├── 0403._Frog_Jump.md │ │ ├── 0404._sum_of_left_leaves.md │ │ ├── 0405._Convert_a_Number_to_Hexadecimal.md │ │ ├── 0406._Queue_Reconstruction_by_Height.md │ │ ├── 0408._Valid_Word_Abbreviation.md │ │ ├── 0409._Longest_Palindrome.md │ │ ├── 0410._Split_Array_Largest_Sum.md │ │ ├── 0412._fizz_buzz.md │ │ ├── 0413._Arithmetic_Slices.md │ │ ├── 0414._third_maximum_number.md │ │ ├── 0415._add_strings.md │ │ ├── 0416._Partition_Equal_Subset_Sum.md │ │ ├── 0417._Pacific_Atlantic_Water_Flow.md │ │ ├── 0421._Maximum_XOR_of_Two_Numbers_in_an_Array.md │ │ ├── 0422._Valid_Word_Square.md │ │ ├── 0424._Longest_Repeating_Character_Replacement.md │ │ ├── 0427._Construct_Quad_Tree.md │ │ ├── 0429._N-ary_Tree_Level_Order_Traversal.md │ │ ├── 0430._Flatten_a_Multilevel_Doubly_Linked_List.md │ │ ├── 0433._Minimum_Genetic_Mutation.md │ │ ├── 0434._number_of_segments_in_a_string.md │ │ ├── 0435._Non-overlapping_Intervals.md │ │ ├── 0436._Find_Right_Interval.md │ │ ├── 0437._path_sum_iii.md │ │ ├── 0438._Find_All_Anagrams_in_a_String.md │ │ ├── 0439._Ternary_Expression_Parser.md │ │ ├── 0441._arranging_coins.md │ │ ├── 0442._Find_All_Duplicates_in_an_Array.md │ │ ├── 0448._Find_All_Numbers_Disappeared_in_an_Array.md │ │ ├── 0450._Delete_Node_in_a_BST.md │ │ ├── 0452._minimum_number_of_arrows_to_burst_balloons.md │ │ ├── 0453._Minimum_Moves_to_Equal_Array_Elements.md │ │ ├── 0454._4Sum_II.md │ │ ├── 0457._Circular_Array_Loop.md │ │ ├── 0458._Poor_Pigs.md │ │ ├── 0459._Repeated_Substring_Pattern.md │ │ ├── 0461._Hamming_Distance.md │ │ ├── 0463._Island_Perimeter.md │ │ ├── 0467._Unique_Substrings_in_Wraparound_String.md │ │ ├── 0469._Convex_Polygon.md │ │ ├── 0470._Implement_Rand10()_Using_Rand7().md │ │ ├── 0472._Concatenated_Words.md │ │ ├── 0475._Heaters.md │ │ ├── 0476._Number_Complement.md │ │ ├── 0477._Total_Hamming_Distance.md │ │ ├── 0478._Generate_Random_Point_in_a_Circle.md │ │ ├── 0482._License_Key_Formatting.md │ │ ├── 0484._Find_Permutation.md │ │ ├── 0485._Max_Consecutive_Ones.md │ │ ├── 0486._Predict_the_Winner.md │ │ ├── 0490._The_Maze.md │ │ ├── 0492._Construct_the_Rectangle.md │ │ ├── 0494._Target_Sum.md │ │ ├── 0495._Teemo_Attacking.md │ │ ├── 0496._Next_Greater_Element_I.md │ │ ├── 0499._The_Maze_III.md │ │ ├── 0500._Keyboard_Row.md │ │ ├── 0501._Find_Mode_in_Binary_Search_Tree.md │ │ ├── 0504._Base_7.md │ │ ├── 0505._The_Maze_II.md │ │ ├── 0507._Perfect_Number.md │ │ ├── 0509._Fibonacci_Number.md │ │ ├── 0513._Find_Bottom_Left_Tree_Value.md │ │ ├── 0515._Find_Largest_Value_in_Each_Tree_Row.md │ │ ├── 0516._Longest_Palindromic_Subsequence.md │ │ ├── 0525._Contiguous_Array.md │ │ ├── 0526._Beautiful_Arrangement.md │ │ ├── 0528._Random_Pick_with_Weight.md │ │ ├── 0530._Minimum_Absolute_Difference_in_BST.md │ │ ├── 0531._Lonely_Pixel_I.md │ │ ├── 0532._K-diff_Pairs_in_an_Array.md │ │ ├── 0533._Lonely_Pixel_II.md │ │ ├── 0535._Encode_and_Decode_TinyURL.md │ │ ├── 0536._Construct_Binary_Tree_from_String.md │ │ ├── 0538._Convert_BST_to_Greater_Tree.md │ │ ├── 0539._Minimum_Time_Difference.md │ │ ├── 0540._Single_Element_in_a_Sorted_Array.md │ │ ├── 0541._Reverse_String_II.md │ │ ├── 0543._Diameter_of_Binary_Tree.md │ │ ├── 0547._Friend_Circles.md │ │ ├── 0548._Split_Array_with_Equal_Sum.md │ │ ├── 0549._Binary_Tree_Longest_Consecutive_Sequence_II.md │ │ ├── 0551._Student_Attendance_Record_I.md │ │ ├── 0552._Student_Attendance_Record_II.md │ │ ├── 0558._Quad_Tree_Intersection.md │ │ ├── 0559._Maximum_Depth_of_N-ary_Tree.md │ │ ├── 0560._Subarray_Sum_Equals_K.md │ │ ├── 0561._Array_Partition_I.md │ │ ├── 0562._Longest_Line_of_Consecutive_One_in_Matrix.md │ │ ├── 0563._Binary_Tree_Tilt.md │ │ ├── 0564._Find_the_Closest_Palindrome.md │ │ ├── 0565._Array_Nesting.md │ │ ├── 0566._Reshape_the_Matrix.md │ │ ├── 0572._Subtree_of_Another_Tree.md │ │ ├── 0575._Distribute_Candies.md │ │ ├── 0581._Shortest_Unsorted_Continuous_Subarray.md │ │ ├── 0583._Delete_Operation_for_Two_Strings.md │ │ ├── 0587._Erect_the_Fence.md │ │ ├── 0589._N-ary_Tree_Preorder_Traversal.md │ │ ├── 0590._N-ary_Tree_Postorder_Traversal.md │ │ ├── 0592._Fraction_Addition_and_Subtraction.md │ │ ├── 0593._Valid_Square.md │ │ ├── 0594._Longest_Harmonious_Subsequence.md │ │ ├── 0595._Big_Countries.md │ │ ├── 0599._Minimum_Index_Sum_of_Two_Lists.md │ │ ├── 0606._Construct_String_from_Binary_Tree.md │ │ ├── 0609._Find_Duplicate_File_in_System.md │ │ ├── 0610._Triangle_Judgement.md │ │ ├── 0611._Valid_Triangle_Number.md │ │ ├── 0613._Shortest_Distance_in_a_Line.md │ │ ├── 0617._Merge_Two_Binary_Trees.md │ │ ├── 0621._Task_Scheduler.md │ │ ├── 0622._Design_Circular_Queue.md │ │ ├── 0628._Maximum_Product_of_Three_Numbers.md │ │ ├── 0630._Course_Schedule_III.md │ │ ├── 0632._Smallest_Range.md │ │ ├── 0633._Sum_of_Square_Numbers.md │ │ ├── 0638._Shopping_Offers.md │ │ ├── 0640._Solve_the_Equation.md │ │ ├── 0646._Maximum_Length_of_Pair_Chain.md │ │ ├── 0647._Palindromic_Substrings.md │ │ ├── 0653._Two_Sum_IV_-_Input_is_a_BST.md │ │ ├── 0654._Maximum_Binary_Tree.md │ │ ├── 0657._Judge_Route_Circle.md │ │ ├── 0660._Remove_9.md │ │ ├── 0665._Non-decreasing_Array.md │ │ ├── 0667._Beautiful_Arrangement_II.md │ │ ├── 0672._Bulb_Switcher_II.md │ │ ├── 0677._Map_Sum_Pairs.md │ │ ├── 0678._Valid_Parenthesis_String.md │ │ ├── 0680._Valid_Palindrome_II.md │ │ ├── 0681._Next_Closest_Time.md │ │ ├── 0682._Baseball_Game.md │ │ ├── 0684._Redundant_Connection.md │ │ ├── 0685._Redundant_Connection_II.md │ │ ├── 0687._Longest_Univalue_Path.md │ │ ├── 0688._Knight_Probability_in_Chessboard.md │ │ ├── 0692._Top_K_Frequent_Words.md │ │ ├── 0693._Binary_Number_with_Alternating_Bits.md │ │ ├── 0695._Max_Area_of_Island.md │ │ ├── 0697._Degree_of_an_Array.md │ │ ├── 0698._Partition_to_K_Equal_Sum_Subsets.md │ │ ├── 0701._Insert_into_a_Binary_Search_Tree.md │ │ ├── 0703._Kth_Largest_Element_in_a_Stream.md │ │ ├── 0707._Design_Linked_List.md │ │ ├── 0709._To_Lower_Case.md │ │ ├── 0712._Minimum_ASCII_Delete_Sum_for_Two_Strings.md │ │ ├── 0713._Subarray_Product_Less_Than_K.md │ │ ├── 0714._Best_Time_to_Buy_and_Sell_Stock_with_Transaction_Fee.md │ │ ├── 0721._Accounts_Merge.md │ │ ├── 0724._Find_Pivot_Index.md │ │ ├── 0725._Split_Linked_List_in_Parts.md │ │ ├── 0729._My_Calendar_I.md │ │ ├── 0730._Count_Different_Palindromic_Subsequences.md │ │ ├── 0733._Flood_Fill.md │ │ ├── 0735._Asteroid_Collision.md │ │ ├── 0737._Sentence_Similarity_II.md │ │ ├── 0738._Monotone_Increasing_Digits.md │ │ ├── 0739._Daily_Temperatures.md │ │ ├── 0740._delete_and_earn.md │ │ ├── 0741._Cherry_Pickup.md │ │ ├── 0746._Min_Cost_Climbing_Stairs.md │ │ ├── 0752._Open_the_Lock.md │ │ ├── 0754._Reach_a_Number.md │ │ ├── 0760._Find_Anagram_Mappings.md │ │ ├── 0763._Partition_Labels.md │ │ ├── 0765._Couples_Holding_Hands.md │ │ ├── 0766._Toeplitz_Matrix.md │ │ ├── 0767._Reorganize_String.md │ │ ├── 0771._Jewels_and_Stones.md │ │ ├── 0774._Minimize_Max_Distance_to_Gas_Station.md │ │ ├── 0777._Swap_Adjacent_in_LR_String.md │ │ ├── 0778._Swim_in_Rising_Water.md │ │ ├── 0784._Letter_Case_Permutation.md │ │ ├── 0785._Is_Graph_Bipartite.md │ │ ├── 0791._Custom_Sort_String.md │ │ ├── 0792._Number_of_Matching_Subsequences.md │ │ ├── 0796._Rotate_String.md │ │ ├── 0800._Similar_RGB_Color.md │ │ ├── 0801._Minimum_Swaps_To_Make_Sequences_Increasing.md │ │ ├── 0803._Bricks_Falling_When_Hit.md │ │ ├── 0804._unique_morse_code_words.md │ │ ├── 0808._Soup_Servings.md │ │ ├── 0815._Bus_Routes.md │ │ ├── 0819._Most_Common_Word.md │ │ ├── 0820._Short_Encoding_of_Words.md │ │ ├── 0821._Shortest_Distance_to_a_Character.md │ │ ├── 0824._Goat_Latin.md │ │ ├── 0825._Friends_Of_Appropriate_Ages.md │ │ ├── 0826._Most_Profit_Assigning_Work.md │ │ ├── 0829._Consecutive_Numbers_Sum.md │ │ ├── 0832._Flipping_an_Image.md │ │ ├── 0835._Image_Overlap.md │ │ ├── 0836._Rectangle_Overlap.md │ │ ├── 0839._Similar_String_Groups.md │ │ ├── 0843._Guess_the_Word.md │ │ ├── 0844._Backspace_String_Compare.md │ │ ├── 0850._Rectangle_Area_II.md │ │ ├── 0859._Buddy_Strings.md │ │ ├── 0862._Shortest_Subarray_with_Sum_at_Least_K.md │ │ ├── 0865._Smallest_Subtree_with_all_the_Deepest_Nodes.md │ │ ├── 0867._transpose_matrix.md │ │ ├── 0871._Minimum_Number_of_Refueling_Stops.md │ │ ├── 0872._Leaf-Similar_Trees.md │ │ ├── 0873._Length_of_Longest_Fibonacci_Subsequence.md │ │ ├── 0876._middle_of_the_linked_list.md │ │ ├── 0877._Stone_Game.md │ │ ├── 0878._Nth_Magical_Number.md │ │ ├── 0890._Find_and_Replace_Pattern.md │ │ ├── 0891._Sum_of_Subsequence_Widths.md │ │ ├── 0896._monotonic_array.md │ │ ├── 0900._RLE_Iterator.md │ │ ├── 0901._Online_Stock_Span.md │ │ ├── 0902._Numbers_At_Most_N_Given_Digit_Set.md │ │ ├── 0904._Fruit_Into_Baskets.md │ │ ├── 0905._Sort_Array_By_Parity.md │ │ ├── 0906._Super_Palindromes.md │ │ ├── 0907._Sum_of_Subarray_Minimums.md │ │ ├── 0908._Smallest_Range.md │ │ ├── 0910._Smallest_Range_II.md │ │ ├── 0913._Cat_and_Mouse.md │ │ ├── 0914._X_of_a_Kind_in_a_Deck_of_Cards.md │ │ ├── 0915._Partition_Array_into_Disjoint_Intervals.md │ │ ├── 0916._Word_Subsets.md │ │ ├── 0917._Reverse_Only_Letters.md │ │ ├── 0918._Maximum_Sum_Circular_Subarray.md │ │ ├── 0919._Complete_Binary_Tree_Inserter.md │ │ ├── 0920._Number_of_Music_Playlists.md │ │ ├── 0921._Minimum_Add_to_Make_Parentheses_Valid.md │ │ ├── 0922._Sort_Array_By_Parity_II.md │ │ ├── 0923._3Sum_With_Multiplicity.md │ │ ├── 0924._Minimize_Malware_Spread.md │ │ ├── 0925._Long_Pressed_Name.md │ │ ├── 0926._Flip_String_to_Monotone_Increasing.md │ │ ├── 0927._Three_Equal_Parts.md │ │ ├── 0928._Minimize_Malware_Spread_II.md │ │ ├── 0929._Unique_Email_Addresses.md │ │ ├── 0930._Binary_Subarrays_With_Sum.md │ │ ├── 0931._Minimum_Falling_Path_Sum.md │ │ ├── 0932._Beautiful_Array.md │ │ ├── 0933._Number_of_Recent_Calls.md │ │ ├── 0934._Shortest_Bridge.md │ │ ├── 0935._Knight_Dialer.md │ │ ├── 0936._Stamping_The_Sequence.md │ │ ├── 0937._Reorder_Log_Files.md │ │ ├── 0938._Range_Sum_of_BST.md │ │ ├── 0939._Minimum_Area_Rectangle.md │ │ ├── 0940._Distinct_Subsequences_II.md │ │ ├── 0941._Valid_Mountain_Array.md │ │ ├── 0942._DI_String_Match.md │ │ ├── 0943._Find_the_Shortest_Superstring.md │ │ ├── 0944._Delete_Columns_to_Make_Sorted.md │ │ ├── 0945._Minimum_Increment_to_Make_Array_Unique.md │ │ ├── 0946._Validate_Stack_Sequences.md │ │ ├── 0947._Most_Stones_Removed_with_Same_Row_or_Column.md │ │ ├── 0948._Bag_of_Tokens.md │ │ ├── 0949._Largest_Time_for_Given_Digits.md │ │ ├── 0950._Reveal_Cards_In_Increasing_Order.md │ │ ├── 0951._Flip_Equivalent_Binary_Trees.md │ │ ├── 0952._Largest_Component_Size_by_Common_Factor.md │ │ ├── 0953._Verifying_an_Alien_Dictionary.md │ │ ├── 0954._Array_of_Doubled_Pairs.md │ │ ├── 0955._Delete_Columns_to_Make_Sorted_II.md │ │ ├── 0956._Tallest_Billboard.md │ │ ├── 0957._Prison_Cells_After_N_Days.md │ │ ├── 0958._Check_Completeness_of_a_Binary_Tree.md │ │ ├── 0959._Regions_Cut_By_Slashes.md │ │ ├── 0960._Delete_Columns_to_Make_Sorted_III.md │ │ ├── 0961._N-Repeated_Element_in_Size_2N_Array.md │ │ ├── 0962._Maximum_Width_Ramp.md │ │ ├── 0963._Minimum_Area_Rectangle_II.md │ │ ├── 0964._Least_Operators_to_Express_Number.md │ │ ├── 0965._Univalued_Binary_Tree.md │ │ ├── 0966._Vowel_Spellchecker.md │ │ ├── 0967._Numbers_With_Same_Consecutive_Differences.md │ │ ├── 0968._Binary_Tree_Cameras.md │ │ ├── 0969._Pancake_Sorting.md │ │ ├── 0970._Powerful_Integers.md │ │ ├── 0971._Flip_Binary_Tree_To_Match_Preorder_Traversal.md │ │ ├── 0972._Equal_Rational_Numbers.md │ │ ├── 0973._K_Closest_Points_to_Origin.md │ │ ├── 0974._Subarray_Sums_Divisible_by_K.md │ │ ├── 0975._Odd_Even_Jump.md │ │ ├── 0976._Largest_Perimeter_Triangle.md │ │ ├── 0977._Squares_of_a_Sorted_Array.md │ │ ├── 0978._Longest_Turbulent_Subarray.md │ │ ├── 0979._Distribute_Coins_in_Binary_Tree.md │ │ ├── 0980._Unique_Paths_III.md │ │ ├── 0981._Time_Based_Key-Value_Store.md │ │ ├── 0982._Triples_with_Bitwise_AND_Equal_To_Zero.md │ │ ├── 0983._Minimum_Cost_For_Tickets.md │ │ ├── 0984._String_Without_AAA_or_BBB.md │ │ ├── 0985._Sum_of_Even_Numbers_After_Queries.md │ │ ├── 0986._Interval_List_Intersections.md │ │ ├── 0987._Vertical_Order_Traversal_of_a_Binary_Tree.md │ │ ├── 0988._Smallest_String_Starting_From_Leaf.md │ │ ├── 0989._Add_to_Array_-_Form_of_Integer.md │ │ ├── 0990._Satisfiability_of_Equality_Equations.md │ │ ├── 0991._Broken_Calculator.md │ │ ├── 0992._Subarrays_with_K_Different_Integers.md │ │ ├── 0993._Cousins_in_Binary_Tree.md │ │ ├── 0994._Rotting_Oranges.md │ │ ├── 0995._Minimum_Number_of_K_Consecutive_Bit_Flips.md │ │ ├── 0996._Number_of_Squareful_Arrays.md │ │ ├── 0997._Find_the_Town_Judge.md │ │ ├── 0998._Maximum_Binary_Tree_II.md │ │ ├── 0999._Available_Captures_for_Rook.md │ │ ├── 1001._Grid_Illumination.md │ │ ├── 1004._Max_Consecutive_Ones_III.md │ │ ├── 1005._Maximize_Sum_Of_Array_After_K_Negations.md │ │ ├── 1006._Clumsy_Factorial.md │ │ ├── 1007._Minimum_Domino_Rotations_For_Equal_Row.md │ │ ├── 1008._Construct_Binary_Search_Tree_from_Preorder_Traversal.md │ │ ├── 1009._Complement_of_Base_10_Integer.md │ │ ├── 1010._Pairs_of_Songs_With_Total_Durations_Divisible_by_60.md │ │ ├── 1011._Capacity_To_Ship_Packages_Within_D_Days.md │ │ ├── 1012._Numbers_With_1_Repeated_Digit.md │ │ └── Summary │ │ │ ├── Bit_Manipulation │ │ │ └── 位运算.md │ │ │ ├── Dynamic Programming.md │ │ │ ├── Graph │ │ │ ├── DFS和BFS.md │ │ │ ├── Iterative Deepening Depth First Search (IDDFS).ipynb │ │ │ ├── README.md │ │ │ ├── graph.png │ │ │ └── 无向图是否有环及是否完全联通(是否为树).md │ │ │ ├── Introduction to String Searching Algorithms – topcoder.pdf │ │ │ ├── LinkedList │ │ │ ├── LinkedList技巧.md │ │ │ └── delete_node_in_a_linked_list问题.md │ │ │ ├── Math │ │ │ ├── 求平方根.md │ │ │ └── 线性筛法求n以内的所有素数.md │ │ │ ├── Maximal Square.pdf │ │ │ ├── Python刷题技巧笔记.md │ │ │ ├── Range Sum Query 2D - Immutable.pdf │ │ │ ├── Recusrion & BackTracking.md │ │ │ ├── Regular_expression │ │ │ └── 正则cheatsheet.md │ │ │ ├── Sort │ │ │ └── 八排序.md │ │ │ ├── String │ │ │ ├── Binary Search.md │ │ │ ├── KMP算法.md │ │ │ ├── longestCommonSubsequence.md │ │ │ ├── slide_windows_template.md │ │ │ └── 回文字符串问题的解决方案.md │ │ │ ├── Tree │ │ │ ├── SegmentTree.md │ │ │ ├── Trie.md │ │ │ └── 二叉树的一些操作.md │ │ │ ├── Union_Find │ │ │ └── union_find.md │ │ │ ├── backtracking思路.md │ │ │ ├── deque.md │ │ │ ├── python_base.md │ │ │ ├── 全排列算法.md │ │ │ ├── 子集合问题.md │ │ │ ├── 总结.md │ │ │ ├── 斐波那契的DP思考.md │ │ │ └── 编程思路总结.md │ └── ipynb │ │ ├── 0001._two_sum.ipynb │ │ ├── 0002._add_two_numbers.ipynb │ │ ├── 0005._longest_palindromic_substring.ipynb │ │ ├── 0007._Reverse_Integer.ipynb │ │ ├── 0009._Palindrome_Number.ipynb │ │ ├── 0010._regular_expression_matching.ipynb │ │ ├── 0013._Roman_to_Integer.ipynb │ │ ├── 0020._valid_parentheses.ipynb │ │ ├── 0032._longest_valid_parentheses.ipynb │ │ ├── 0053._maximum_subarray.ipynb │ │ ├── 0062._unique_paths.ipynb │ │ ├── 0064._minimum_path_sum.ipynb │ │ ├── 0072._edit_distance.ipynb │ │ ├── 0139._word_break.ipynb │ │ ├── 0179._Largest_Number.ipynb │ │ ├── 0242._valid_anagram.ipynb │ │ └── 0287._Find_the_Duplicate_Number.ipynb └── 剑指offer │ ├── C++ │ └── README.md │ ├── Java │ ├── 03_01_DuplicationInArray │ │ ├── README.md │ │ └── Solution.java │ ├── 03_02_DuplicationInArrayNoEdit │ │ ├── README.md │ │ └── Solution.java │ ├── 04_FindInPartiallySortedMatrix │ │ ├── README.md │ │ └── Solution.java │ ├── 05_ReplaceSpaces │ │ ├── README.md │ │ └── Solution.java │ ├── 06_PrintListInReversedOrder │ │ ├── README.md │ │ └── Solution.java │ ├── 07_ConstructBinaryTree │ │ ├── README.md │ │ └── Solution.java │ ├── 08_NextNodeInBinaryTrees │ │ ├── README.md │ │ └── Solution.java │ ├── 09_01_QueueWithTwoStacks │ │ ├── README.md │ │ └── Solution.java │ ├── 09_02_StackWithTwoQueues │ │ ├── README.md │ │ └── Solution.java │ ├── 10_01_Fibonacci │ │ ├── README.md │ │ └── Solution.java │ ├── 10_02_JumpFloor │ │ ├── README.md │ │ └── Solution.java │ ├── 10_03_JumpFloorII │ │ ├── README.md │ │ └── Solution.java │ ├── 10_04_RectCover │ │ ├── README.md │ │ └── Solution.java │ ├── 11_MinNumberInRotatedArray │ │ ├── README.md │ │ └── Solution.java │ ├── 12_StringPathInMatrix │ │ ├── README.md │ │ └── Solution.java │ ├── 13_RobotMove │ │ ├── README.md │ │ └── Solution.java │ ├── 14_CuttingRope │ │ ├── README.md │ │ └── Solution.java │ ├── 15_NumberOf1InBinary │ │ ├── README.md │ │ └── Solution.java │ ├── 16_Power │ │ ├── README.md │ │ └── Solution.java │ ├── 17_Print1ToMaxOfNDigits │ │ ├── README.md │ │ └── Solution.java │ ├── 18_01_DeleteNodeInList │ │ ├── README.md │ │ └── Solution.java │ ├── 18_02_DeleteDuplicatedNode │ │ ├── README.md │ │ └── Solution.java │ ├── 19_RegularExpressionsMatching │ │ ├── README.md │ │ └── Solution.java │ ├── 20_NumericStrings │ │ ├── README.md │ │ ├── Solution.java │ │ └── Solution1.java │ ├── 21_ReorderArray │ │ ├── README.md │ │ └── Solution.java │ ├── 22_KthNodeFromEnd │ │ ├── README.md │ │ └── Solution.java │ ├── 23_EntryNodeInListLoop │ │ ├── README.md │ │ └── Solution.java │ ├── 24_ReverseList │ │ ├── README.md │ │ └── Solution.java │ ├── 25_MergeSortedLists │ │ ├── README.md │ │ └── Solution.java │ ├── 26_SubstructureInTree │ │ ├── README.md │ │ └── Solution.java │ ├── 27_MirrorOfBinaryTree │ │ ├── README.md │ │ └── Solution.java │ ├── 28_SymmetricalBinaryTree │ │ ├── README.md │ │ └── Solution.java │ ├── 29_PrintMatrix │ │ ├── README.md │ │ └── Solution.java │ ├── 30_MinInStack │ │ ├── README.md │ │ └── Solution.java │ ├── 31_StackPushPopOrder │ │ ├── README.md │ │ └── Solution.java │ ├── 32_01_PrintTreeFromTopToBottom │ │ ├── README.md │ │ └── Solution.java │ ├── 32_02_PrintTreesInLines │ │ ├── README.md │ │ └── Solution.java │ ├── 32_03_PrintTreesInZigzag │ │ ├── README.md │ │ └── Solution.java │ ├── 33_SquenceOfBST │ │ ├── README.md │ │ └── Solution.java │ ├── 34_PathInTree │ │ ├── README.md │ │ └── Solution.java │ ├── 35_CopyComplexList │ │ ├── README.md │ │ └── Solution.java │ ├── 36_ConvertBinarySearchTree │ │ ├── README.md │ │ └── Solution.java │ ├── 37_SerializeBinaryTrees │ │ ├── README.md │ │ └── Solution.java │ ├── 38_StringPermutation │ │ ├── README.md │ │ └── Solution.java │ ├── 39_MoreThanHalfNumber │ │ ├── README.md │ │ └── Solution.java │ ├── 40_KLeastNumbers │ │ ├── README.md │ │ └── Solution.java │ ├── 41_StreamMedian │ │ ├── README.md │ │ └── Solution.java │ ├── 42_GreatestSumOfSubarrays │ │ ├── README.md │ │ └── Solution.java │ ├── 43_NumberOf1 │ │ ├── README.md │ │ └── Solution.java │ ├── 44_DigitsInSequence │ │ ├── README.md │ │ └── Solution.java │ ├── 45_SortArrayForMinNumber │ │ ├── README.md │ │ └── Solution.java │ ├── 46_TranslateNumbersToStrings │ │ ├── README.md │ │ └── Solution.java │ ├── 47_MaxValueOfGifts │ │ ├── README.md │ │ └── Solution.java │ ├── 48_LongestSubstringWithoutDup │ │ ├── README.md │ │ └── Solution.java │ ├── 49_UglyNumber │ │ ├── README.md │ │ └── Solution.java │ ├── 50_01_FirstNotRepeatingChar │ │ ├── README.md │ │ └── Solution.java │ ├── 50_02_FristCharacterInStream │ │ ├── README.md │ │ └── Solution.java │ ├── 52_FirstCommonNodesInLists │ │ ├── README.md │ │ └── Solution.java │ ├── 53_01_NumberOfK │ │ ├── README.md │ │ └── Solution.java │ ├── 53_02_MissingNumber │ │ ├── README.md │ │ └── Solution.java │ ├── 53_03_IntegerIdenticalToIndex │ │ ├── README.md │ │ └── Solution.java │ ├── 54_KthNodeInBST │ │ ├── README.md │ │ └── Solution.java │ ├── 55_01_TreeDepth │ │ ├── README.md │ │ └── Solution.java │ ├── 55_02_BalancedBinaryTree │ │ ├── README.md │ │ └── Solution.java │ ├── 56_01_NumbersAppearOnce │ │ ├── README.md │ │ └── Solution.java │ ├── 56_02_NumberAppearingOnce │ │ ├── README.md │ │ └── Solution.java │ ├── 57_01_TwoNumbersWithSum │ │ ├── README.md │ │ └── Solution.java │ ├── 57_02_ContinuousSquenceWithSum │ │ ├── README.md │ │ └── Solution.java │ ├── 58_01_ReverseWordsInSentence │ │ ├── README.md │ │ └── Solution.java │ ├── 58_02_LeftRotateString │ │ ├── README.md │ │ └── Solution.java │ ├── 59_01_MaxInSlidingWindow │ │ ├── README.md │ │ └── Solution.java │ ├── 61_ContinousCards │ │ ├── README.md │ │ └── Solution.java │ └── README.md │ ├── JavaScript │ ├── 01-二维数组中的查找.js │ ├── 02-替换空格.js │ ├── 03-从头到尾打印链表.js │ ├── 05-用两个栈实现队列.js │ ├── 06-旋转数组的最小数字.js │ ├── 07-斐波那契数列.js │ ├── 08-跳台阶.js │ ├── 09-变态跳台阶.js │ └── README.md │ └── Python │ ├── 01-二维数组中的查找.py │ ├── 02-替换空格.py │ ├── 03-从尾到头打印链表.py │ ├── 04-重建二叉树.py │ ├── 05-用两个栈实现队列.py │ ├── 06-旋转数组的最小数字.py │ ├── 07-斐波那契数列.py │ ├── 08-跳台阶.py │ ├── 09-变态跳台阶.py │ ├── 10-矩形覆盖.py │ ├── 11-二进制中1的个数.py │ ├── 12-数值的整数次方.py │ ├── 13-调整数组顺序使奇数位于偶数前面.py │ ├── 14-链表中倒数第k个结点.py │ ├── 15-反转链表.py │ ├── 16-合并两个排序的链表.py │ ├── 17-树的子结构.py │ ├── 18-二叉树的镜像.py │ ├── 19-顺时针打印矩阵.py │ ├── 20-包含min函数的栈.py │ ├── 21-栈的压入、弹出序列.py │ ├── 22-从上往下打印二叉树.py │ ├── 23-二叉搜索树的后序遍历序列.py │ ├── 24-二叉树中和为某一值的路径.py │ ├── 25-复杂链表的复制.py │ ├── 26-二叉搜索树与双向链表.py │ ├── 27-字符串的排列.py │ ├── 28-数组中出现次数超过一半的数字.py │ ├── 29-最小的k个数.py │ ├── 30-连续数组的最大和.py │ ├── 31-整数中1出现的次数.py │ ├── 32-把数组排成最小的数.py │ ├── 33-丑数.py │ ├── 34-第一个只出现一次的字符.py │ ├── 35-数组中的逆序对.py │ ├── 36-两个链表的第一个公共节点.py │ ├── 37-数字在排序数组中出现的次数.py │ ├── 38-二叉树的深度.py │ ├── 39-平衡二叉树.py │ ├── 40-数组中只出现一次的数字.py │ ├── 41-和为s的连续整数序列.py │ ├── 42-和为s的两个数字.py │ ├── 43-左旋字符串.py │ ├── 44-翻转单词顺序列.py │ ├── 45-扑克牌顺子.py │ ├── 46-孩子们的游戏(圆圈中最后剩下的数).py │ ├── 47-求1+2+3+...+n.py │ ├── 48-不用加减乘除做加法.py │ ├── 49-把字符串转化成整数.py │ ├── 50-数组中重复的数字.py │ ├── 51-构建乘积数组.py │ ├── 52-正则表达式匹配.py │ ├── 53-表示数值的字符串.py │ ├── 54-字符流中第一个不重复的字符.py │ ├── 55-链表中环的入口.py │ ├── 56-删除链表中重复的结点.py │ ├── 57-二叉树的下一个结点.py │ ├── 58-对称的二叉树.py │ ├── 59-按之字形顺序打印二叉树.py │ ├── 60-把二叉树打印成多行.py │ ├── 61-序列化二叉树.py │ ├── 62-二叉搜索树的第k个结点.py │ ├── 63-数据流中的中位数.py │ ├── 64-滑动窗口的最大值.py │ ├── 65-矩阵中的路径.py │ ├── 66-机器人的运动范围.py │ └── README.md ├── gen_readme_file.py ├── images ├── 84 │ ├── 1.md │ ├── histogram1.png │ └── histogram_area1.png ├── 118 │ └── PascalTriangleAnimated2.gif ├── 218 │ ├── 1.md │ ├── skyline1.jpg │ └── skyline2.jpg ├── 343 │ ├── 1.md │ └── WechatIMG509.jpeg ├── 367 │ ├── 1.md │ └── WechatIMG505.jpeg ├── 371 │ ├── 1.md │ ├── 18C549E8937CF66D42060DCB802661C8.jpg │ └── A4CF94C4A380101180708CF0CF35265B.jpg ├── 463 │ ├── 1.md │ └── island.png ├── 470 │ ├── 1.md │ └── image.png ├── 815 │ ├── 1.md │ └── WechatIMG6057.png ├── 935 │ ├── 1.md │ ├── keypad.png │ └── knight.png ├── 005 │ ├── 1.md │ ├── manacher1.png │ ├── manacher2.png │ └── manacher3.jpg ├── 011 │ ├── 1.md │ └── question_11.jpg ├── 033 │ ├── 1.md │ └── rotated-array12_09_2017.jpg ├── 042 │ ├── 1.md │ └── rainwatertrap.png ├── 065 │ ├── 1.md │ └── image.png ├── MainPage │ └── ApacheCN-group.png ├── N-Queens.png ├── Project cornerstone │ ├── 3221532952133_.pic_hd.jpg │ ├── 3231532952152_.pic_hd.jpg │ └── approve letter.md ├── README.md ├── SortingAlgorithm │ ├── BubbleSort_1.gif │ ├── InsertSort_1.gif │ ├── MergeSort_1.gif │ ├── QuickSort_1.gif │ ├── RadixSort_1.gif │ ├── RadixSort_2.png │ ├── SelectionSort_1.gif │ ├── ShellSort_1.gif │ ├── ShellSort_2.png │ ├── ShellSort_3.png │ ├── ShellSort_4.png │ ├── 八大排序算法性能.png │ ├── 八大排序算法总结.png │ ├── 冒泡排序.gif │ ├── 基数排序.gif │ ├── 希尔排序.png │ ├── 归并排序.gif │ ├── 快速排序.gif │ ├── 构建大顶堆.png │ ├── 直接插入排序.gif │ ├── 简单选择排序.gif │ └── 调整大顶堆.png ├── WechatIMG439.jpeg ├── readme_badges │ ├── 71EC577ABCDF4D4AE8082BED5D204919.jpg │ ├── WechatIMG649.jpeg │ └── organization.jpg ├── sketch1.png ├── union_find8D76B4AFCB73CED67BE37B92B385A55C.jpg └── 线程转换.jpg └── src ├── 51CFCB8475F4ED5B1CAD5F23CF96887A.jpg ├── 58.md ├── 5C3A0153C03FD214FE68B002903E2135.jpg ├── B9A582497F833DDE4E93FA5BAA0BA4EB.jpg ├── WechatIMG437.jpeg ├── WechatIMG438.jpeg ├── aiqiyi.md ├── baicizhanxiaomi.md ├── data_structure.md ├── didi.md ├── glodon.md ├── huaweiliuxuesheng.md ├── indeed_tokyo.md ├── kuaishou.md ├── liulishuo.md ├── pdd.md ├── py2.x ├── TreeRecursionIterator.py └── list2iteration.py ├── py3.x ├── TreeRecursionIterator.py ├── list2iteration.py └── sort │ ├── BubbleSort.py │ ├── InsertionSort.py │ ├── MergeSort.py │ ├── QuickSort.py │ ├── RadixSort.py │ ├── SelectionSort.py │ └── ShellSort.py ├── qqchangE_problem.md ├── shopee.md ├── sohuchangyouweipinhui.md ├── tecent.md ├── times.md ├── vmware.md ├── wangyihuyudierti.java ├── xunlei.md ├── zhaoyincreditcard.md └── zhaoyinwangluokeji.java /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .idea/ 3 | Thumbs.db 4 | db.json 5 | *.log 6 | node_modules/ 7 | public/ 8 | .deploy*/ 9 | .vscode/ 10 | demo.py 11 | test.py 12 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/C++/README.md: -------------------------------------------------------------------------------- 1 | # Some algorithm templates for better understanding! 2 | 3 | > [八大排序算法 集合](/docs/Algorithm/Sort) 4 | 5 | ![](/images/SortingAlgorithm/八大排序算法性能.png) 6 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/Cipher/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Algorithm_Implementation/Java/Cipher/README.md -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/Compression/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/Conversion/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Algorithm_Implementation/Java/Conversion/README.md -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/Data_Structure/Graph/README.md: -------------------------------------------------------------------------------- 1 | # Some algorithm templates for better understanding! 2 | 3 | ## 八大排序算法 4 | 5 | ![](/images/SortingAlgorithm/八大排序算法性能.png) 6 | 7 | | 名称 | 动图 | 代码 | 8 | | --- | --- | --- | 9 | | 冒泡排序 | ![](/images/SortingAlgorithm/冒泡排序.gif) | [BubbleSort.py](/opt/git/LeetCode/docs/Algorithm\ Templates/Sort/BubbleSort.py) | 10 | | 插入排序 | ![](/images/SortingAlgorithm/直接插入排序.gif) | [InsertSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/InsertSort.py) | 11 | | 选择排序 | ![](/images/SortingAlgorithm/简单选择排序.gif) | [SelectionSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/SelectionSort.py) | 12 | | 快速排序 | ![](/images/SortingAlgorithm/快速排序.gif) | [QuickSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/QuickSort.py) | 13 | | 希尔排序 | ![](/images/SortingAlgorithm/希尔排序.png) | [ShellSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/ShellSort.py) | 14 | | 归并排序 | ![](/images/SortingAlgorithm/归并排序.gif) | [MergeSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/MergeSort.py) | 15 | | 基数排序 | ![](/images/SortingAlgorithm/基数排序.gif) | | 16 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/Data_Structure/HashMap/README.md: -------------------------------------------------------------------------------- 1 | # Some algorithm templates for better understanding! 2 | 3 | ## 八大排序算法 4 | 5 | ![](/images/SortingAlgorithm/八大排序算法性能.png) 6 | 7 | | 名称 | 动图 | 代码 | 8 | | --- | --- | --- | 9 | | 冒泡排序 | ![](/images/SortingAlgorithm/冒泡排序.gif) | [BubbleSort.py](/opt/git/LeetCode/docs/Algorithm\ Templates/Sort/BubbleSort.py) | 10 | | 插入排序 | ![](/images/SortingAlgorithm/直接插入排序.gif) | [InsertSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/InsertSort.py) | 11 | | 选择排序 | ![](/images/SortingAlgorithm/简单选择排序.gif) | [SelectionSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/SelectionSort.py) | 12 | | 快速排序 | ![](/images/SortingAlgorithm/快速排序.gif) | [QuickSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/QuickSort.py) | 13 | | 希尔排序 | ![](/images/SortingAlgorithm/希尔排序.png) | [ShellSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/ShellSort.py) | 14 | | 归并排序 | ![](/images/SortingAlgorithm/归并排序.gif) | [MergeSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/MergeSort.py) | 15 | | 基数排序 | ![](/images/SortingAlgorithm/基数排序.gif) | | 16 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/Data_Structure/HashTable/README.md: -------------------------------------------------------------------------------- 1 | # Some algorithm templates for better understanding! 2 | 3 | ## 八大排序算法 4 | 5 | ![](/images/SortingAlgorithm/八大排序算法性能.png) 6 | 7 | | 名称 | 动图 | 代码 | 8 | | --- | --- | --- | 9 | | 冒泡排序 | ![](/images/SortingAlgorithm/冒泡排序.gif) | [BubbleSort.py](/opt/git/LeetCode/docs/Algorithm\ Templates/Sort/BubbleSort.py) | 10 | | 插入排序 | ![](/images/SortingAlgorithm/直接插入排序.gif) | [InsertSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/InsertSort.py) | 11 | | 选择排序 | ![](/images/SortingAlgorithm/简单选择排序.gif) | [SelectionSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/SelectionSort.py) | 12 | | 快速排序 | ![](/images/SortingAlgorithm/快速排序.gif) | [QuickSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/QuickSort.py) | 13 | | 希尔排序 | ![](/images/SortingAlgorithm/希尔排序.png) | [ShellSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/ShellSort.py) | 14 | | 归并排序 | ![](/images/SortingAlgorithm/归并排序.gif) | [MergeSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/MergeSort.py) | 15 | | 基数排序 | ![](/images/SortingAlgorithm/基数排序.gif) | | 16 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/Data_Structure/Heqp/README.md: -------------------------------------------------------------------------------- 1 | # Some algorithm templates for better understanding! 2 | 3 | ## 八大排序算法 4 | 5 | ![](/images/SortingAlgorithm/八大排序算法性能.png) 6 | 7 | | 名称 | 动图 | 代码 | 8 | | --- | --- | --- | 9 | | 冒泡排序 | ![](/images/SortingAlgorithm/冒泡排序.gif) | [BubbleSort.py](/opt/git/LeetCode/docs/Algorithm\ Templates/Sort/BubbleSort.py) | 10 | | 插入排序 | ![](/images/SortingAlgorithm/直接插入排序.gif) | [InsertSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/InsertSort.py) | 11 | | 选择排序 | ![](/images/SortingAlgorithm/简单选择排序.gif) | [SelectionSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/SelectionSort.py) | 12 | | 快速排序 | ![](/images/SortingAlgorithm/快速排序.gif) | [QuickSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/QuickSort.py) | 13 | | 希尔排序 | ![](/images/SortingAlgorithm/希尔排序.png) | [ShellSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/ShellSort.py) | 14 | | 归并排序 | ![](/images/SortingAlgorithm/归并排序.gif) | [MergeSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/MergeSort.py) | 15 | | 基数排序 | ![](/images/SortingAlgorithm/基数排序.gif) | | 16 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/Data_Structure/LinkedList/README.md: -------------------------------------------------------------------------------- 1 | # Some algorithm templates for better understanding! 2 | 3 | ## 八大排序算法 4 | 5 | ![](/images/SortingAlgorithm/八大排序算法性能.png) 6 | 7 | | 名称 | 动图 | 代码 | 8 | | --- | --- | --- | 9 | | 冒泡排序 | ![](/images/SortingAlgorithm/冒泡排序.gif) | [BubbleSort.py](/opt/git/LeetCode/docs/Algorithm\ Templates/Sort/BubbleSort.py) | 10 | | 插入排序 | ![](/images/SortingAlgorithm/直接插入排序.gif) | [InsertSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/InsertSort.py) | 11 | | 选择排序 | ![](/images/SortingAlgorithm/简单选择排序.gif) | [SelectionSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/SelectionSort.py) | 12 | | 快速排序 | ![](/images/SortingAlgorithm/快速排序.gif) | [QuickSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/QuickSort.py) | 13 | | 希尔排序 | ![](/images/SortingAlgorithm/希尔排序.png) | [ShellSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/ShellSort.py) | 14 | | 归并排序 | ![](/images/SortingAlgorithm/归并排序.gif) | [MergeSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/MergeSort.py) | 15 | | 基数排序 | ![](/images/SortingAlgorithm/基数排序.gif) | | 16 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/Data_Structure/Matrix/README.md: -------------------------------------------------------------------------------- 1 | # Some algorithm templates for better understanding! 2 | 3 | ## 八大排序算法 4 | 5 | ![](/images/SortingAlgorithm/八大排序算法性能.png) 6 | 7 | | 名称 | 动图 | 代码 | 8 | | --- | --- | --- | 9 | | 冒泡排序 | ![](/images/SortingAlgorithm/冒泡排序.gif) | [BubbleSort.py](/opt/git/LeetCode/docs/Algorithm\ Templates/Sort/BubbleSort.py) | 10 | | 插入排序 | ![](/images/SortingAlgorithm/直接插入排序.gif) | [InsertSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/InsertSort.py) | 11 | | 选择排序 | ![](/images/SortingAlgorithm/简单选择排序.gif) | [SelectionSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/SelectionSort.py) | 12 | | 快速排序 | ![](/images/SortingAlgorithm/快速排序.gif) | [QuickSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/QuickSort.py) | 13 | | 希尔排序 | ![](/images/SortingAlgorithm/希尔排序.png) | [ShellSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/ShellSort.py) | 14 | | 归并排序 | ![](/images/SortingAlgorithm/归并排序.gif) | [MergeSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/MergeSort.py) | 15 | | 基数排序 | ![](/images/SortingAlgorithm/基数排序.gif) | | 16 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/Data_Structure/Queue/README.md: -------------------------------------------------------------------------------- 1 | ## Queue Implementation 2 | 3 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/Data_Structure/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/Data_Structure/Tree/README.md: -------------------------------------------------------------------------------- 1 | # Some algorithm templates for better understanding! 2 | 3 | ## 八大排序算法 4 | 5 | ![](/images/SortingAlgorithm/八大排序算法性能.png) 6 | 7 | | 名称 | 动图 | 代码 | 8 | | --- | --- | --- | 9 | | 冒泡排序 | ![](/images/SortingAlgorithm/冒泡排序.gif) | [BubbleSort.py](/opt/git/LeetCode/docs/Algorithm\ Templates/Sort/BubbleSort.py) | 10 | | 插入排序 | ![](/images/SortingAlgorithm/直接插入排序.gif) | [InsertSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/InsertSort.py) | 11 | | 选择排序 | ![](/images/SortingAlgorithm/简单选择排序.gif) | [SelectionSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/SelectionSort.py) | 12 | | 快速排序 | ![](/images/SortingAlgorithm/快速排序.gif) | [QuickSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/QuickSort.py) | 13 | | 希尔排序 | ![](/images/SortingAlgorithm/希尔排序.png) | [ShellSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/ShellSort.py) | 14 | | 归并排序 | ![](/images/SortingAlgorithm/归并排序.gif) | [MergeSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/MergeSort.py) | 15 | | 基数排序 | ![](/images/SortingAlgorithm/基数排序.gif) | | 16 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/Dynamic_Programming/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Algorithm_Implementation/Java/Dynamic_Programming/README.md -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/Hash/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Algorithm_Implementation/Java/Hash/README.md -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/Others/NextNodeInOrderTree.md: -------------------------------------------------------------------------------- 1 | ``` python 2 | 8 3 | / 4 | 5 5 | / \ 6 | 3 6 7 | / \ \ 8 | 1 4 7 9 | 10 | 11 | class Node { 12 | Node parent, lc, rc; 13 | int val; 14 | } 15 | ``` 16 | 17 | 1. 首先判断其自有无右孩子,若有,则取其右子树的最左节点; 若无,则开始2 18 | 19 | 20 | 2. 它是其父亲节点的左孩子,则其父亲节点 21 | 22 | 23 | 2. 它是其父亲节点的右孩子,则从其父亲开始往上追溯到第一个向右的节点,如果没有这个节点或者说没有父亲节点,则无下一个节点,若有则取之 24 | 25 | 26 | ```python 27 | def nextNode(node): 28 | def leftest(node): 29 | while node.lc: 30 | node = node.lc 31 | return node 32 | if node.rc: 33 | return leftest(node.rc) 34 | if not node.parent: 35 | return None 36 | if node == node.parent.lc: 37 | return node.parent 38 | elif node == node.parent.rc: 39 | while node.parent.parent: 40 | if node.parent != node.parent.parent.lc: 41 | node = node.parent 42 | else: 43 | return node.parent.parent 44 | return None 45 | ``` 46 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/README.md: -------------------------------------------------------------------------------- 1 | # Some algorithm templates for better understanding! 2 | 3 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/Search/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Algorithm_Implementation/Java/Search/README.md -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/Sort/README.md: -------------------------------------------------------------------------------- 1 | # 八大排序算法 2 | 3 | ![](/images/SortingAlgorithm/八大排序算法性能.png) 4 | 5 | | 名称 | 动图 | 代码 | 6 | | --- | --- | --- | 7 | | 冒泡排序 | ![](/images/SortingAlgorithm/冒泡排序.gif) | [BubbleSort.py](/src/py3.x/sort/BubbleSort.py) | 8 | | 插入排序 | ![](/images/SortingAlgorithm/直接插入排序.gif) | [InsertSort.py](/src/py3.x/sort/InsertSort.py) | 9 | | 选择排序 | ![](/images/SortingAlgorithm/简单选择排序.gif) | [SelectionSort.py](/src/py3.x/sort/SelectionSort.py) | 10 | | 快速排序 | ![](/images/SortingAlgorithm/快速排序.gif) | [QuickSort.py](/src/py3.x/sort/QuickSort.py) | 11 | | 希尔排序 | ![](/images/SortingAlgorithm/希尔排序.png) | [ShellSort.py](/src/py3.x/sort/ShellSort.py) | 12 | | 归并排序 | ![](/images/SortingAlgorithm/归并排序.gif) | [MergeSort.py](/src/py3.x/sort/MergeSort.py) | 13 | | 基数排序 | ![](/images/SortingAlgorithm/基数排序.gif) | [RadixSort.py](/src/py3.x/sort/RadixSort.py) | 14 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/Sort/insertionSort.py: -------------------------------------------------------------------------------- 1 | def insertionSort(nums): 2 | if not nums or len(nums) < 2: 3 | return nums 4 | 5 | for i in range(1, len(nums)): 6 | for j in range(i): 7 | if nums[i] < nums[j]: 8 | nums[i], nums[j] = nums[j], nums[i] 9 | return nums 10 | 11 | 12 | if __name__ == "__main__": 13 | nums = [5, 1, 9, 3, 2, 7] 14 | print('input: ', nums) 15 | nums = insertionSort(nums) 16 | print("result: ", nums) 17 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/Sort/排序稳定性.md: -------------------------------------------------------------------------------- 1 | https://baike.baidu.com/item/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95%E7%A8%B3%E5%AE%9A%E6%80%A7 2 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Java/Traversals/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Algorithm_Implementation/Java/Traversals/README.md -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/JavaScript/README.md: -------------------------------------------------------------------------------- 1 | # Some algorithm templates for better understanding! 2 | 3 | 4 | > [八大排序算法 集合](/docs/Algorithm/Sort) 5 | 6 | ![](/images/SortingAlgorithm/八大排序算法性能.png) 7 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/Cipher/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Algorithm_Implementation/Python/Cipher/README.md -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/Compression/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/Conversion/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Algorithm_Implementation/Python/Conversion/README.md -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/Data_Structure/Graph/README.md: -------------------------------------------------------------------------------- 1 | # Some algorithm templates for better understanding! 2 | 3 | ## 八大排序算法 4 | 5 | ![](/images/SortingAlgorithm/八大排序算法性能.png) 6 | 7 | | 名称 | 动图 | 代码 | 8 | | --- | --- | --- | 9 | | 冒泡排序 | ![](/images/SortingAlgorithm/冒泡排序.gif) | [BubbleSort.py](/opt/git/LeetCode/docs/Algorithm\ Templates/Sort/BubbleSort.py) | 10 | | 插入排序 | ![](/images/SortingAlgorithm/直接插入排序.gif) | [InsertSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/InsertSort.py) | 11 | | 选择排序 | ![](/images/SortingAlgorithm/简单选择排序.gif) | [SelectionSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/SelectionSort.py) | 12 | | 快速排序 | ![](/images/SortingAlgorithm/快速排序.gif) | [QuickSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/QuickSort.py) | 13 | | 希尔排序 | ![](/images/SortingAlgorithm/希尔排序.png) | [ShellSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/ShellSort.py) | 14 | | 归并排序 | ![](/images/SortingAlgorithm/归并排序.gif) | [MergeSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/MergeSort.py) | 15 | | 基数排序 | ![](/images/SortingAlgorithm/基数排序.gif) | | 16 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/Data_Structure/HashMap/README.md: -------------------------------------------------------------------------------- 1 | # Some algorithm templates for better understanding! 2 | 3 | ## 八大排序算法 4 | 5 | ![](/images/SortingAlgorithm/八大排序算法性能.png) 6 | 7 | | 名称 | 动图 | 代码 | 8 | | --- | --- | --- | 9 | | 冒泡排序 | ![](/images/SortingAlgorithm/冒泡排序.gif) | [BubbleSort.py](/opt/git/LeetCode/docs/Algorithm\ Templates/Sort/BubbleSort.py) | 10 | | 插入排序 | ![](/images/SortingAlgorithm/直接插入排序.gif) | [InsertSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/InsertSort.py) | 11 | | 选择排序 | ![](/images/SortingAlgorithm/简单选择排序.gif) | [SelectionSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/SelectionSort.py) | 12 | | 快速排序 | ![](/images/SortingAlgorithm/快速排序.gif) | [QuickSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/QuickSort.py) | 13 | | 希尔排序 | ![](/images/SortingAlgorithm/希尔排序.png) | [ShellSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/ShellSort.py) | 14 | | 归并排序 | ![](/images/SortingAlgorithm/归并排序.gif) | [MergeSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/MergeSort.py) | 15 | | 基数排序 | ![](/images/SortingAlgorithm/基数排序.gif) | | 16 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/Data_Structure/HashTable/README.md: -------------------------------------------------------------------------------- 1 | # Some algorithm templates for better understanding! 2 | 3 | ## 八大排序算法 4 | 5 | ![](/images/SortingAlgorithm/八大排序算法性能.png) 6 | 7 | | 名称 | 动图 | 代码 | 8 | | --- | --- | --- | 9 | | 冒泡排序 | ![](/images/SortingAlgorithm/冒泡排序.gif) | [BubbleSort.py](/opt/git/LeetCode/docs/Algorithm\ Templates/Sort/BubbleSort.py) | 10 | | 插入排序 | ![](/images/SortingAlgorithm/直接插入排序.gif) | [InsertSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/InsertSort.py) | 11 | | 选择排序 | ![](/images/SortingAlgorithm/简单选择排序.gif) | [SelectionSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/SelectionSort.py) | 12 | | 快速排序 | ![](/images/SortingAlgorithm/快速排序.gif) | [QuickSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/QuickSort.py) | 13 | | 希尔排序 | ![](/images/SortingAlgorithm/希尔排序.png) | [ShellSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/ShellSort.py) | 14 | | 归并排序 | ![](/images/SortingAlgorithm/归并排序.gif) | [MergeSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/MergeSort.py) | 15 | | 基数排序 | ![](/images/SortingAlgorithm/基数排序.gif) | | 16 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/Data_Structure/Heqp/README.md: -------------------------------------------------------------------------------- 1 | # Some algorithm templates for better understanding! 2 | 3 | ## 八大排序算法 4 | 5 | ![](/images/SortingAlgorithm/八大排序算法性能.png) 6 | 7 | | 名称 | 动图 | 代码 | 8 | | --- | --- | --- | 9 | | 冒泡排序 | ![](/images/SortingAlgorithm/冒泡排序.gif) | [BubbleSort.py](/opt/git/LeetCode/docs/Algorithm\ Templates/Sort/BubbleSort.py) | 10 | | 插入排序 | ![](/images/SortingAlgorithm/直接插入排序.gif) | [InsertSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/InsertSort.py) | 11 | | 选择排序 | ![](/images/SortingAlgorithm/简单选择排序.gif) | [SelectionSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/SelectionSort.py) | 12 | | 快速排序 | ![](/images/SortingAlgorithm/快速排序.gif) | [QuickSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/QuickSort.py) | 13 | | 希尔排序 | ![](/images/SortingAlgorithm/希尔排序.png) | [ShellSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/ShellSort.py) | 14 | | 归并排序 | ![](/images/SortingAlgorithm/归并排序.gif) | [MergeSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/MergeSort.py) | 15 | | 基数排序 | ![](/images/SortingAlgorithm/基数排序.gif) | | 16 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/Data_Structure/LinkedList/README.md: -------------------------------------------------------------------------------- 1 | # Some algorithm templates for better understanding! 2 | 3 | ## 八大排序算法 4 | 5 | ![](/images/SortingAlgorithm/八大排序算法性能.png) 6 | 7 | | 名称 | 动图 | 代码 | 8 | | --- | --- | --- | 9 | | 冒泡排序 | ![](/images/SortingAlgorithm/冒泡排序.gif) | [BubbleSort.py](/opt/git/LeetCode/docs/Algorithm\ Templates/Sort/BubbleSort.py) | 10 | | 插入排序 | ![](/images/SortingAlgorithm/直接插入排序.gif) | [InsertSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/InsertSort.py) | 11 | | 选择排序 | ![](/images/SortingAlgorithm/简单选择排序.gif) | [SelectionSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/SelectionSort.py) | 12 | | 快速排序 | ![](/images/SortingAlgorithm/快速排序.gif) | [QuickSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/QuickSort.py) | 13 | | 希尔排序 | ![](/images/SortingAlgorithm/希尔排序.png) | [ShellSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/ShellSort.py) | 14 | | 归并排序 | ![](/images/SortingAlgorithm/归并排序.gif) | [MergeSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/MergeSort.py) | 15 | | 基数排序 | ![](/images/SortingAlgorithm/基数排序.gif) | | 16 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/Data_Structure/Matrix/README.md: -------------------------------------------------------------------------------- 1 | # Some algorithm templates for better understanding! 2 | 3 | ## 八大排序算法 4 | 5 | ![](/images/SortingAlgorithm/八大排序算法性能.png) 6 | 7 | | 名称 | 动图 | 代码 | 8 | | --- | --- | --- | 9 | | 冒泡排序 | ![](/images/SortingAlgorithm/冒泡排序.gif) | [BubbleSort.py](/opt/git/LeetCode/docs/Algorithm\ Templates/Sort/BubbleSort.py) | 10 | | 插入排序 | ![](/images/SortingAlgorithm/直接插入排序.gif) | [InsertSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/InsertSort.py) | 11 | | 选择排序 | ![](/images/SortingAlgorithm/简单选择排序.gif) | [SelectionSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/SelectionSort.py) | 12 | | 快速排序 | ![](/images/SortingAlgorithm/快速排序.gif) | [QuickSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/QuickSort.py) | 13 | | 希尔排序 | ![](/images/SortingAlgorithm/希尔排序.png) | [ShellSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/ShellSort.py) | 14 | | 归并排序 | ![](/images/SortingAlgorithm/归并排序.gif) | [MergeSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/MergeSort.py) | 15 | | 基数排序 | ![](/images/SortingAlgorithm/基数排序.gif) | | 16 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/Data_Structure/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/Data_Structure/Stack/README.md: -------------------------------------------------------------------------------- 1 | ## Stack Implementation 2 | 3 | ### Stack using Linked List 4 | ```python 5 | class ListNode(object): 6 | def __init__(self, x): 7 | self.val = x 8 | self.next = None 9 | 10 | class Stack(object): 11 | def __init__(self): 12 | self.head = None 13 | self.size = 0 14 | 15 | def push(self, val): 16 | new_node = ListNode(val) 17 | new_node.next = self.head 18 | self.head = new_node 19 | self.size += 1 20 | 21 | def pop(self): 22 | if self.head == None: 23 | return None 24 | pop_val = self.head.val 25 | self.head = self.head.next 26 | self.size -= 1 27 | return pop_val 28 | 29 | def peek(self): 30 | return self.head.val 31 | 32 | def get_size(self): 33 | return self.size 34 | 35 | def is_empty(self): 36 | return self.size == 0 37 | ``` 38 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/Data_Structure/Tree/README.md: -------------------------------------------------------------------------------- 1 | # Some algorithm templates for better understanding! 2 | 3 | ## 八大排序算法 4 | 5 | ![](/images/SortingAlgorithm/八大排序算法性能.png) 6 | 7 | | 名称 | 动图 | 代码 | 8 | | --- | --- | --- | 9 | | 冒泡排序 | ![](/images/SortingAlgorithm/冒泡排序.gif) | [BubbleSort.py](/opt/git/LeetCode/docs/Algorithm\ Templates/Sort/BubbleSort.py) | 10 | | 插入排序 | ![](/images/SortingAlgorithm/直接插入排序.gif) | [InsertSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/InsertSort.py) | 11 | | 选择排序 | ![](/images/SortingAlgorithm/简单选择排序.gif) | [SelectionSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/SelectionSort.py) | 12 | | 快速排序 | ![](/images/SortingAlgorithm/快速排序.gif) | [QuickSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/QuickSort.py) | 13 | | 希尔排序 | ![](/images/SortingAlgorithm/希尔排序.png) | [ShellSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/ShellSort.py) | 14 | | 归并排序 | ![](/images/SortingAlgorithm/归并排序.gif) | [MergeSort.py](/opt/git/LeetCode/docs/src/py2.x/SortingAlgorithm/MergeSort.py) | 15 | | 基数排序 | ![](/images/SortingAlgorithm/基数排序.gif) | | 16 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/Dynamic_Programming/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Algorithm_Implementation/Python/Dynamic_Programming/README.md -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/Hash/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Algorithm_Implementation/Python/Hash/README.md -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/Others/NextNodeInOrderTree.md: -------------------------------------------------------------------------------- 1 | ``` python 2 | 8 3 | / 4 | 5 5 | / \ 6 | 3 6 7 | / \ \ 8 | 1 4 7 9 | 10 | 11 | class Node { 12 | Node parent, lc, rc; 13 | int val; 14 | } 15 | ``` 16 | 17 | 1. 首先判断其自有无右孩子,若有,则取其右子树的最左节点; 若无,则开始2 18 | 19 | 20 | 2. 它是其父亲节点的左孩子,则其父亲节点 21 | 22 | 23 | 2. 它是其父亲节点的右孩子,则从其父亲开始往上追溯到第一个向右的节点,如果没有这个节点或者说没有父亲节点,则无下一个节点,若有则取之 24 | 25 | 26 | ```python 27 | def nextNode(node): 28 | def leftest(node): 29 | while node.lc: 30 | node = node.lc 31 | return node 32 | if node.rc: 33 | return leftest(node.rc) 34 | if not node.parent: 35 | return None 36 | if node == node.parent.lc: 37 | return node.parent 38 | elif node == node.parent.rc: 39 | while node.parent.parent: 40 | if node.parent != node.parent.parent.lc: 41 | node = node.parent 42 | else: 43 | return node.parent.parent 44 | return None 45 | ``` 46 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/README.md: -------------------------------------------------------------------------------- 1 | # Some algorithm templates for better understanding! 2 | 3 | > [八大排序算法 集合](/docs/Algorithm/Sort) 4 | 5 | ![](/images/SortingAlgorithm/八大排序算法性能.png) 6 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/Search/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Algorithm_Implementation/Python/Search/README.md -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/Sort/README.md: -------------------------------------------------------------------------------- 1 | # 八大排序算法 2 | 3 | ![](/images/SortingAlgorithm/八大排序算法性能.png) 4 | 5 | | 名称 | 动图 | 代码 | 6 | | --- | --- | --- | 7 | | 冒泡排序 | ![](/images/SortingAlgorithm/冒泡排序.gif) | [BubbleSort.py](/src/py3.x/sort/BubbleSort.py) | 8 | | 插入排序 | ![](/images/SortingAlgorithm/直接插入排序.gif) | [InsertSort.py](/src/py3.x/sort/InsertSort.py) | 9 | | 选择排序 | ![](/images/SortingAlgorithm/简单选择排序.gif) | [SelectionSort.py](/src/py3.x/sort/SelectionSort.py) | 10 | | 快速排序 | ![](/images/SortingAlgorithm/快速排序.gif) | [QuickSort.py](/src/py3.x/sort/QuickSort.py) | 11 | | 希尔排序 | ![](/images/SortingAlgorithm/希尔排序.png) | [ShellSort.py](/src/py3.x/sort/ShellSort.py) | 12 | | 归并排序 | ![](/images/SortingAlgorithm/归并排序.gif) | [MergeSort.py](/src/py3.x/sort/MergeSort.py) | 13 | | 基数排序 | ![](/images/SortingAlgorithm/基数排序.gif) | [RadixSort.py](/src/py3.x/sort/RadixSort.py) | 14 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/Sort/insertionSort.py: -------------------------------------------------------------------------------- 1 | def insertionSort(nums): 2 | if not nums or len(nums) < 2: 3 | return nums 4 | 5 | for i in range(1, len(nums)): 6 | for j in range(i): 7 | if nums[i] < nums[j]: 8 | nums[i], nums[j] = nums[j], nums[i] 9 | return nums 10 | 11 | 12 | if __name__ == "__main__": 13 | nums = [5, 1, 9, 3, 2, 7] 14 | print('input: ', nums) 15 | nums = insertionSort(nums) 16 | print("result: ", nums) 17 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/Sort/排序稳定性.md: -------------------------------------------------------------------------------- 1 | https://baike.baidu.com/item/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95%E7%A8%B3%E5%AE%9A%E6%80%A7 2 | -------------------------------------------------------------------------------- /docs/Algorithm_Implementation/Python/Traversals/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Algorithm_Implementation/Python/Traversals/README.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0007._Reverse_Integer.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0007._Reverse_Integer.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0008._String_to_Integer_(atoi).md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0008._String_to_Integer_(atoi).md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0012._Integer_to_Roman.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0012._Integer_to_Roman.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0014._Longest_Common_Prefix.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0014._Longest_Common_Prefix.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0017._Letter_Combinations_of_a_Phone_Number.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0017._Letter_Combinations_of_a_Phone_Number.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0019._Remove_Nth_Node_From_End_of_List.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0019._Remove_Nth_Node_From_End_of_List.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0022._generate_parentheses.md: -------------------------------------------------------------------------------- 1 | # 22. Generate Parentheses 2 | 3 | **难度: Medium** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode-cn.com/problems/generate-parentheses/ 10 | 11 | > 内容描述 12 | 13 | ``` 14 | 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。 15 | 16 | 例如,给出 n = 3,生成结果为: 17 | 18 | [ 19 | "((()))", 20 | "(()())", 21 | "(())()", 22 | "()(())", 23 | "()()()" 24 | ] 25 | ``` 26 | 27 | ## 解题方案 28 | > 思路 1 29 | ``` 30 | 回溯法 31 | ``` 32 | 33 | ```cpp 34 | void dfs(int left, int total, string path, vector& ans){ 35 | if(total==0&&left==0){ 36 | ans.push_back(path); 37 | return ; 38 | } 39 | if(left>0) 40 | dfs(left-1, total-1, path+"(", ans); 41 | if(left generateParenthesis(int n) { 45 | vector ans; 46 | string path=""; 47 | dfs(n, n*2, path, ans); 48 | return ans; 49 | } 50 | ``` -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0025._reverse_nodes_in_k_group.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0025._reverse_nodes_in_k_group.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0026._Remove_Duplicates_From_Sorted_Array.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0026._Remove_Duplicates_From_Sorted_Array.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0027._Remove_Element.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0027._Remove_Element.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0028._implement_strstr.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0028._implement_strstr.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0029._divide_two_integers.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0029._divide_two_integers.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0032._Longest_Valid_Parentheses.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0032._Longest_Valid_Parentheses.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0034._Find_First_and_Last_Position_of_Element_in_Sorted_Array.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0034._Find_First_and_Last_Position_of_Element_in_Sorted_Array.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0035._search_insert_position.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0035._search_insert_position.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0036._Valid_Sudoku.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0036._Valid_Sudoku.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0038._Count_and_Say.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0038._Count_and_Say.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0041._First_Missing_Positive.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0041._First_Missing_Positive.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0042._Trapping_Rain_Water.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0042._Trapping_Rain_Water.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0043._Multiply_Strings.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0043._Multiply_Strings.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0044._Wildcard_Matching.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0044._Wildcard_Matching.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0045._Jump_Game_II.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0045._Jump_Game_II.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0046._Permutations.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0046._Permutations.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0047._Permutations_II.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0047._Permutations_II.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0049._Group_Anagrams.md: -------------------------------------------------------------------------------- 1 | # 49. Group Anagrams 2 | 3 | **难度: Medium** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode-cn.com/problems/group-anagrams/submissions/ 10 | 11 | > 内容描述 12 | 13 | ``` 14 | 给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。 15 | 16 | 示例: 17 | 18 | 输入: ["eat", "tea", "tan", "ate", "nat", "bat"], 19 | 输出: 20 | [ 21 | ["ate","eat","tea"], 22 | ["nat","tan"], 23 | ["bat"] 24 | ] 25 | 说明: 26 | 27 | 所有输入均为小写字母。 28 | 不考虑答案输出的顺序。 29 | ``` 30 | 31 | ## 解题方案 32 | > 思路 1 33 | ``` 34 | 不同的组别字符串元素相同,顺序不同,所以排序后相同 35 | 把排序后相同的元素映射到同一个vector 36 | 所有vector的组合就是结果 37 | ``` 38 | 39 | ```cpp 40 | vector> groupAnagrams(vector& strs) { 41 | vector> ans; 42 | map> maps; 43 | for(int i=0;i难度Easy** 4 | 5 | ## 刷题内容 6 | > 原题连接 7 | 8 | * https://leetcode.com/problems/length-of-last-word/ 9 | 10 | > 内容描述 11 | 12 | ``` 13 | Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. 14 | 15 | If the last word does not exist, return 0. 16 | ``` 17 | 18 | > 思路 19 | ******- 时间复杂度: O(n)*****- 空间复杂度: O(1)****** 20 | 21 | 这题我们只要从字符串的末尾开始向前找到第一个单词即可 22 | ```cpp 23 | class Solution { 24 | public: 25 | int lengthOfLastWord(string s) { 26 | int i = s.length() - 1,length = s.length() - 1; 27 | while((s[i] == ' ') &&(i >= 0)) 28 | { 29 | --i; 30 | --length; 31 | } 32 | while((s[i] != ' ') && (i >= 0)) 33 | --i; 34 | return length- i; 35 | } 36 | }; 37 | ``` 38 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0059._Spiral_Matrix_II.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0059._Spiral_Matrix_II.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0060._Permutation_Sequence.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0060._Permutation_Sequence.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0061._Rotate_List.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0061._Rotate_List.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0065._Valid_Number.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0065._Valid_Number.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0066._Plus_One.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0066._Plus_One.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0068._Text_Justification.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0068._Text_Justification.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0069._Sqr(x).md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0069._Sqr(x).md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0076._Minimum_Window_Substring.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0076._Minimum_Window_Substring.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0077._combinations.md: -------------------------------------------------------------------------------- 1 | # 77. Combinations 2 | 3 | **难度: Medium** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode-cn.com/problems/combinations/ 10 | 11 | > 内容描述 12 | ``` 13 | 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。 14 | 15 | 示例: 16 | 17 | 输入: n = 4, k = 2 18 | 输出: 19 | [ 20 | [2,4], 21 | [3,4], 22 | [2,3], 23 | [1,2], 24 | [1,3], 25 | [1,4], 26 | ] 27 | ``` 28 | 29 | ## 解题方案 30 | > 思路 1 31 | ``` 32 | 回溯寻找所有组合。 33 | ``` 34 | 35 | ```cpp 36 | void dfs(int k, int index, int n, vector& path, vector>& ans){ 37 | if(k==0){ 38 | ans.push_back(path); 39 | return ; 40 | } 41 | for(int i=index;i<=n;i++){ 42 | path.push_back(i); 43 | dfs(k-1, i+1, n, path, ans); 44 | path.pop_back(); 45 | } 46 | } 47 | vector> combine(int n, int k) { 48 | vector> ans; 49 | vector path; 50 | dfs(k, 1, n, path, ans); 51 | return ans; 52 | } 53 | ``` -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0078._subsets.md: -------------------------------------------------------------------------------- 1 | # 78. Subsets 2 | 3 | **难度: Medium** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode-cn.com/problems/subsets/ 10 | 11 | > 内容描述 12 | ``` 13 | 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。 14 | 15 | 说明:解集不能包含重复的子集。 16 | 17 | 示例: 18 | 19 | 输入: nums = [1,2,3] 20 | 输出: 21 | [ 22 | [3], 23 | [1], 24 | [2], 25 | [1,2,3], 26 | [1,3], 27 | [2,3], 28 | [1,2], 29 | [] 30 | ] 31 | ``` 32 | 33 | ## 解题方案 34 | > 思路 1 35 | ``` 36 | 每个元素有两种选择,选与不选,可以使用K位二进制数组表示K个元素的选与不选。 37 | ``` 38 | 39 | ```cpp 40 | vector> subsets(vector& nums) { 41 | vector> ans; 42 | for(int i=0;i tmp; 44 | int index = 0; 45 | int j=i; 46 | while(j){ 47 | if(j%2==1) 48 | tmp.push_back(nums[index]); 49 | index++; 50 | j/=2; 51 | } 52 | ans.push_back(tmp); 53 | } 54 | return ans; 55 | } 56 | ``` -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0081._Search_in_Rotated_Sorted_Array_II.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0081._Search_in_Rotated_Sorted_Array_II.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0083._remove_duplicates_from_sorted_lists.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0083._remove_duplicates_from_sorted_lists.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0085._Maximal_Rectangle.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0085._Maximal_Rectangle.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0087._Scramble_String.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0087._Scramble_String.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0088._Merge_Sorted_Array.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0088._Merge_Sorted_Array.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0094._binary_tree_inorder_traversal.md: -------------------------------------------------------------------------------- 1 | # 94. Binary Tree Inorder Traversal 2 | **难度: Medium** 3 | 4 | ## 刷题内容 5 | > 原题连接 6 | 7 | * https://leetcode-cn.com/problems/binary-tree-inorder-traversal/ 8 | 9 | > 内容描述 10 | ``` 11 | 给定一个二叉树,返回它的中序 遍历。 12 | 13 | 示例: 14 | 15 | 输入: [1,null,2,3] 16 | 1 17 | \ 18 | 2 19 | / 20 | 3 21 | 22 | 输出: [1,3,2] 23 | 24 | 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 25 | ``` 26 | 27 | ## 解题方案 28 | > 思路 1 29 | ``` 30 | 递归。 31 | ``` 32 | 33 | ```cpp 34 | void dfs(TreeNode* root, vector& ans){ 35 | if(root==NULL) 36 | return ; 37 | dfs(root->left, ans); 38 | ans.push_back(root->val); 39 | dfs(root->right, ans); 40 | } 41 | vector inorderTraversal(TreeNode* root) { 42 | vector ans; 43 | dfs(root, ans); 44 | return ans; 45 | } 46 | ``` -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0096._Unique_Binary_Search_Trees.md: -------------------------------------------------------------------------------- 1 | # 96. Unique Binary Search Trees 2 | 3 | **难度: Medium** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode-cn.com/problems/unique-binary-search-trees/ 10 | 11 | > 内容描述 12 | 13 | ``` 14 | 给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 15 | 16 | 示例: 17 | 18 | 输入: 3 19 | 输出: 5 20 | 解释: 21 | 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 22 | 23 | 1 3 3 2 1 24 | \ / / / \ \ 25 | 3 2 1 1 3 2 26 | / / \ \ 27 | 2 1 2 3 28 | ``` 29 | 30 | ## 解题方案 31 | > 思路 1 32 | ``` 33 | 假设有n个节点,左右子树有(0,n-1)(1,n-2)...(n-1, 0)等情况 34 | 子结构: 35 | dp[i] = dp[0]*dp[i-1]+...+dp[i-1]*dp[0] 36 | ``` 37 | 38 | ```cpp 39 | int numTrees(int n) { 40 | vector dp(n+1, 0); 41 | dp[0]=1; 42 | dp[1]=1; 43 | for(int i=2;i<=n;i++){ 44 | for(int j=0;j难度: Easy** 3 | ## 刷题内容 4 | > 原题连接 5 | * https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/ 6 | > 内容描述 7 | ``` 8 | Given a binary tree, find its minimum depth. 9 | 10 | The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 11 | 12 | Note: A leaf is a node with no children. 13 | ``` 14 | ## 解题方案 15 | > 思路 1 16 | ``` 17 | 遍历树,找出最小深度的分支 18 | ``` 19 | ```cpp 20 | void dfs(TreeNode* root, int depth, int& ans){ 21 | if(root == NULL) 22 | return ; 23 | if(root->left==NULL && root->right==NULL){ 24 | if(depthleft, depth+1, ans); 29 | dfs(root->right, depth+1, ans); 30 | } 31 | int minDepth(TreeNode* root) { 32 | int ans = 10000; 33 | if(root==NULL) 34 | return 0; 35 | dfs(root, 1, ans); 36 | return ans; 37 | } 38 | 39 | ``` -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0112._Path_Sum.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0112._Path_Sum.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0114._Flatten_Binary_Tree_to_Linked_List.md: -------------------------------------------------------------------------------- 1 | # 114. Flatten Binary Tree to Linked List 2 | 3 | **难度: Middle** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/ 10 | 11 | > 内容描述 12 | 13 | ``` 14 | 给定一个二叉树,原地将它展开为链表。 15 | 16 | 例如,给定二叉树 17 | 18 | 1 19 | / \ 20 | 2 5 21 | / \ \ 22 | 3 4 6 23 | 将其展开为: 24 | 25 | 1 26 | \ 27 | 2 28 | \ 29 | 3 30 | \ 31 | 4 32 | \ 33 | 5 34 | \ 35 | 6 36 | ``` 37 | 38 | ## 解题方案 39 | > 思路 1 40 | ``` 41 | 后续遍历二叉树 42 | ``` 43 | 44 | ```cpp 45 | void dfs(TreeNode* root){ 46 | if(root==NULL) 47 | return ; 48 | dfs(root->left); 49 | dfs(root->right); 50 | TreeNode* tmp=root->right; 51 | root->right=root->left; 52 | root->left=NULL; 53 | while(root->right) 54 | root=root->right; 55 | root->right = tmp; 56 | } 57 | void flatten(TreeNode* root) { 58 | dfs(root); 59 | } 60 | ``` -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0115._Distinct_Subsequences.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0115._Distinct_Subsequences.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0120._Triangle.md: -------------------------------------------------------------------------------- 1 | # 120. Triangle 2 | 3 | **难度: Medium** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode-cn.com/problems/triangle/ 10 | 11 | > 内容描述 12 | 13 | ``` 14 | 给定一个三角形,找出自顶向下的最小路径和。每一步只能移动到下一行中相邻的结点上。 15 | 16 | 例如,给定三角形: 17 | 18 | [ 19 | [2], 20 | [3,4], 21 | [6,5,7], 22 | [4,1,8,3] 23 | ] 24 | 自顶向下的最小路径和为 11(即,2 + 3 + 5 + 1 = 11)。 25 | ``` 26 | 27 | ## 解题方案 28 | > 思路 1 29 | ``` 30 | dfs超时 31 | 采用dp,自底向上更新每个数,每个数加上向下路径的最小和,更新到第一层既为第一层向下路径的最小和。 32 | ``` 33 | 34 | ```cpp 35 | 36 | int minimumTotal(vector>& triangle) { 37 | for (int i = triangle.size() - 2; i >= 0; i--) 38 | for (int j = 0; j < triangle[i].size(); j++) 39 | triangle[i][j] += min(triangle[i + 1][j], triangle[i + 1][j + 1]); 40 | return triangle[0][0]; 41 | } 42 | ``` -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0121._Best_Tim_ to_Buy_and_Sell_Stock.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0121._Best_Tim_ to_Buy_and_Sell_Stock.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0122._Best_Time_to_Buy_and_Sell_Stock_II.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0122._Best_Time_to_Buy_and_Sell_Stock_II.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0123._Best_Time_to_Buy _and_Sell_Stock_III.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0123._Best_Time_to_Buy _and_Sell_Stock_III.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0124._Binary_Tree_Maximum_Path_Sum.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0124._Binary_Tree_Maximum_Path_Sum.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0127._Word_Ladde.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0127._Word_Ladde.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0141._linked_list_cycle.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0141._linked_list_cycle.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0144._Binary_Tree_Preorder_Traversal.md: -------------------------------------------------------------------------------- 1 | # 144. Binary Tree Preorder Traversal 2 | 3 | **难度: Middle** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode-cn.com/problems/binary-tree-preorder-traversal/ 10 | 11 | > 内容描述 12 | 13 | ``` 14 | 给定一个二叉树,返回它的 前序 遍历。 15 | 16 | 示例: 17 | 18 | 输入: [1,null,2,3] 19 | 1 20 | \ 21 | 2 22 | / 23 | 3 24 | 25 | 输出: [1,2,3] 26 | 27 | ``` 28 | 29 | ## 解题方案 30 | > 思路 1 31 | ``` 32 | 递归 33 | ``` 34 | 35 | ```cpp 36 | void dfs(TreeNode* root, vector &ans){ 37 | if(root==NULL) 38 | return ; 39 | ans.push_back(root->val); 40 | dfs(root->left, ans); 41 | dfs(root->right, ans); 42 | } 43 | vector preorderTraversal(TreeNode* root) { 44 | vector ans; 45 | dfs(root, ans); 46 | return ans; 47 | } 48 | ``` -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0145._Binary_Tree_Postorder_Traversal.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0145._Binary_Tree_Postorder_Traversal.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0147._Insert_on_Sort_List.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0147._Insert_on_Sort_List.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0148._Sort _list.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0148._Sort _list.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0151._Reverse_Words_in_a_String.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0151._Reverse_Words_in_a_String.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0164._Maximum_Gap.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0164._Maximum_Gap.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0166._Fraction_to_Recurring_Decimal.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0166._Fraction_to_Recurring_Decimal.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0260._Single_Number_III.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/C++/0260._Single_Number_III.md -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0287._Find_the_Duplicate_Number.md: -------------------------------------------------------------------------------- 1 | # 287. Find the Duplicate Number 2 | 3 | **难度: Middle** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode-cn.com/problems/find-the-duplicate-number/ 10 | 11 | > 内容描述 12 | 13 | ``` 14 | 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数。假设只有一个重复的整数,找出这个重复的数。 15 | 16 | 示例 1: 17 | 18 | 输入: [1,3,4,2,2] 19 | 输出: 2 20 | 示例 2: 21 | 22 | 输入: [3,1,3,4,2] 23 | 输出: 3 24 | 说明: 25 | 26 | 不能更改原数组(假设数组是只读的)。 27 | 只能使用额外的 O(1) 的空间。 28 | 时间复杂度小于 O(n2) 。 29 | 数组中只有一个重复的数字,但它可能不止重复出现一次。 30 | ``` 31 | 32 | ## 解题方案 33 | > 思路 1 34 | ``` 35 | 采用下标取反标记,假设下标数字为负,则为ans 36 | ``` 37 | 38 | ```cpp 39 | int findDuplicate(vector& nums) { 40 | for(int i=0;i难度: Easy** 3 | ## 刷题内容 4 | > 原题连接 5 | * https://leetcode-cn.com/problems/power-of-three/ 6 | > 内容描述 7 | ``` 8 | Given an integer, write a function to determine if it is a power of three. 9 | 10 | ###example 11 | 1. Input: 27 -> Output: true 12 | 13 | 2. Input: 0 -> Output: false 14 | 15 | 3. Input: 9 -> Output: true 16 | 17 | 4. Input: 45 -> Output: false 18 | ``` 19 | ### 题解方案 20 | ``` 21 | 只要是3的幂次,一定能够被3的最大次方整除 22 | ``` 23 | ### coding 24 | ```cpp 25 | bool isPowerOfThree(int n) { 26 | return n > 0 and 1162261467 % n ==0; 27 | } 28 | ``` -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0338._counting_bits.md: -------------------------------------------------------------------------------- 1 | # 338. Counting Bits 2 | 3 | **难度: Medium** 4 | 5 | ## 刷题内容 6 | > 原题连接 7 | 8 | * https://leetcode-cn.com/problems/counting-bits/ 9 | > 内容描述 10 | ``` 11 | 给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。 12 | 13 | 示例 1: 14 | 15 | 输入: 2 16 | 输出: [0,1,1] 17 | 示例 2: 18 | 19 | 输入: 5 20 | 输出: [0,1,1,2,1,2] 21 | 进阶: 22 | 23 | 给出时间复杂度为O(n*sizeof(integer))的解答非常容易。但你可以在线性时间O(n)内用一趟扫描做到吗? 24 | 要求算法的空间复杂度为O(n)。 25 | 你能进一步完善解法吗?要求在C++或任何其他语言中不使用任何内置函数(如 C++ 中的 __builtin_popcount)来执行此操作。 26 | ``` 27 | 28 | ## 解题方案 29 | > 思路 1 30 | ``` 31 | 第i个数的二进制中数含1的个数等于等于第(i&i-1)个数中二进制数中1的个数加1 32 | ``` 33 | 34 | ```cpp 35 | vector countBits(int num) { 36 | vector res(num+1,0); 37 | for(int i=1;i<=num;i++){ 38 | res[i]=res[i&(i-1)]+1; 39 | } 40 | return res; 41 | } 42 | 43 | ``` -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0442._Find_All_Duplicates_in_an_Array.md: -------------------------------------------------------------------------------- 1 | # 442. Find All Duplicates in an Array 2 | 3 | **难度: Middle** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode-cn.com/problems/find-all-duplicates-in-an-array/ 10 | 11 | > 内容描述 12 | 13 | ``` 14 | 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次。 15 | 16 | 找到所有出现两次的元素。 17 | 18 | 你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗? 19 | 20 | 示例: 21 | 22 | 输入: 23 | [4,3,2,7,8,2,3,1] 24 | 25 | 输出: 26 | [2,3] 27 | ``` 28 | 29 | ## 解题方案 30 | > 思路 1 31 | ``` 32 | 数值在1-n之间,可以对相应数值-1的下标位置取反,如果出现两次,就为正值,否则为负。 33 | ``` 34 | 35 | ```cpp 36 | vector findDuplicates(vector& nums) { 37 | vector ans; 38 | for(int i=0;i难度: Middle** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode-cn.com/problems/single-element-in-a-sorted-array/ 10 | 11 | > 内容描述 12 | 13 | ``` 14 | 给定一个只包含整数的有序数组,每个元素都会出现两次,唯有一个数只会出现一次,找出这个数。 15 | 16 | 示例 1: 17 | 18 | 输入: [1,1,2,3,3,4,4,8,8] 19 | 输出: 2 20 | 示例 2: 21 | 22 | 输入: [3,3,7,7,10,11,11] 23 | 输出: 10 24 | 注意: 您的方案应该在 O(log n)时间复杂度和 O(1)空间复杂度中运行。 25 | ``` 26 | 27 | ## 解题方案 28 | > 思路 1 29 | ``` 30 | 时间复杂度选择二分 31 | 32 | ``` 33 | 34 | ```cpp 35 | int singleNonDuplicate(vector& nums) { 36 | int start=0, end=nums.size()/2; 37 | while(start难度: Medium** 4 | 5 | ## 刷题内容 6 | > 原题连接 7 | * https://leetcode-cn.com/problems/binary-tree-pruning/ 8 | > 内容描述 9 | ``` 10 | 给定二叉树根结点 root ,此外树的每个结点的值要么是 0,要么是 1。 11 | 12 | 返回移除了所有不包含 1 的子树的原二叉树。 13 | 14 | ( 节点 X 的子树为 X 本身,以及所有 X 的后代。) 15 | 16 | 示例1: 17 | 输入: [1,null,0,0,1] 18 | 输出: [1,null,0,null,1] 19 | ``` 20 | 21 | ## 解题方案 22 | > 思路 1 23 | ``` 24 | 遍历二叉树,切除左右子树中无1的子树。 25 | ``` 26 | 27 | ```cpp 28 | bool dfs(TreeNode* root){ 29 | if(root==NULL) 30 | return false; 31 | bool left = dfs(root->left); 32 | bool right = dfs(root->right); 33 | if(left==false) 34 | root->left=NULL; 35 | if(right==false) 36 | root->right=NULL; 37 | return root->val==1||left||right; 38 | } 39 | TreeNode* pruneTree(TreeNode* root) { 40 | dfs(root); 41 | return root; 42 | } 43 | ``` -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0945._Minimum_Increment_to_Make_Array_Unique.md: -------------------------------------------------------------------------------- 1 | ### 945. Minimum Increment to Make Array Unique 2 | 3 | 题目: 4 | https://leetcode.com/problems/minimum-increment-to-make-array-unique/ 5 | 6 | 难度: 7 | Medium 8 | 9 | 题意: 10 | 11 | 1. 给定一个数组 12 | 2. 每次操作,数组中的元素只能+1 13 | 3. 求最少多少次操作,数组中所有元素都不相同 14 | 15 | 思路: 16 | 17 | - 注意看题意,数组中的元素只能+1,所以数组需要先排序,从小到大,因为小的数处理好,大的数不管怎么+1都不会冲突 18 | - 从小到大循环,需要操作的只可能是跟前一个相同的,或者比前一个小(因为前一个也有可能跟前前一个相同),因此我们跟前面一个比较,如果比前面一个大,那么啥事都没,如果跟前一个相同,或者比前一个小,那么我们就把他加到比前面一个数大1即可 19 | 20 | 解法: 21 | 22 | ```c++ 23 | class Solution { 24 | public: 25 | int minIncrementForUnique(vector& A) { 26 | sort(A.begin(), A.end()); 27 | int ret = 0; 28 | for (int i= 1;i < A.size();i++) { 29 | if (A[i] <= A[i - 1]) { 30 | ret += A[i - 1] - A[i] + 1; 31 | A[i] = A[i - 1] + 1; 32 | } 33 | } 34 | return ret; 35 | } 36 | }; 37 | ``` 38 | 39 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/0946._Validate_Stack_Sequences.md: -------------------------------------------------------------------------------- 1 | ### 946. Validate Stack Sequences 2 | 3 | 题目: 4 | https://leetcode.com/problems/validate-stack-sequences/ 5 | 6 | 难度: 7 | Medium 8 | 9 | 题意: 10 | 11 | 1. 两个数组 12 | 2. 问入栈顺序为pushed数组,是否可能出栈顺序为popped数组 13 | 14 | 思路: 15 | 16 | - 模拟即可。我们把新建一个栈,按照pushed数组的顺序一个一个丢进去,并且,检查popped数组前面的数组,如果可以弹出就弹出。到最后判断栈是否为空即可 17 | 18 | 解法: 19 | 20 | ```c++ 21 | class Solution { 22 | public: 23 | int stack[1010]; 24 | bool validateStackSequences(vector& pushed, vector& popped) { 25 | int n = 0, j = 0; 26 | for (int i = 0;i < pushed.size();i++) { 27 | stack[n++] = pushed[i]; 28 | while (n > 0 && stack[n - 1] == popped[j]) { 29 | n--; 30 | j++; 31 | } 32 | } 33 | if (n == 0) { 34 | return true; 35 | } else { 36 | return false; 37 | } 38 | } 39 | }; 40 | ``` 41 | 42 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/C++/README.md: -------------------------------------------------------------------------------- 1 | # Leetcode solutions and summary! 2 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Java/0141._linked_list_cycle.md: -------------------------------------------------------------------------------- 1 | # 141. Linked List Cycle 2 | 3 | **难度: Easy** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode.com/problems/linked-list-cycle/description/ 10 | 11 | > 内容描述 12 | 13 | ``` 14 | Given a linked list, determine if it has a cycle in it. 15 | 16 | Follow up: 17 | Can you solve it without using extra space? 18 | ``` 19 | 20 | ## 解题方案 21 | 22 | 23 | > 思路 1 24 | ******- 时间复杂度: O(N)******- 空间复杂度: O(1)****** 25 | 26 | 27 | 快慢指针 28 | 29 | 30 | 31 | ```java 32 | java 33 | public class Solution { 34 | public boolean hasCycle(ListNode head) { 35 | if (head == null){ 36 | return false; 37 | } 38 | ListNode fast = head; 39 | ListNode slow = head; 40 | while (fast != null && slow != null && fast.next != null){ 41 | fast = fast.next.next; 42 | slow = slow.next; 43 | if (slow == fast){ 44 | return true; 45 | } 46 | } 47 | return false; 48 | } 49 | } 50 | ``` 51 | 52 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Java/0342._Power_of_Four.md: -------------------------------------------------------------------------------- 1 | # 342. Power of Four 2 | 3 | **难度: Easy** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode.com/problems/power-of-four/description/ 10 | 11 | > 内容描述 12 | 13 | ``` 14 | Given an integer (signed 32 bits), write a function to check whether it is a power of 4. 15 | 16 | Example 1: 17 | 18 | Input: 16 19 | Output: true 20 | Example 2: 21 | 22 | Input: 5 23 | Output: false 24 | Follow up: Could you solve it without loops/recursion? 25 | ``` 26 | 27 | ## 解题方案 28 | 29 | > 思路 1 30 | ******- 时间复杂度: O(1)******- 空间复杂度: O(1)****** 31 | 32 | recursive 33 | 34 | ```java 35 | class Solution { 36 | public boolean isPowerOfFour(int num) { 37 | if (num <= 0) 38 | return false; 39 | if (num == 1) 40 | return true; 41 | if (num % 4 == 0) 42 | return isPowerOfFour(num/4); 43 | return false; 44 | } 45 | } 46 | ``` 47 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/JavaScript/0058._Length_of_Last_Word.md: -------------------------------------------------------------------------------- 1 | # 58. Length of Last Word 2 | 3 | **难度: Easy** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode.com/problems/length-of-last-word/description/ 10 | 11 | > 内容描述 12 | 13 | ``` 14 | Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. 15 | 16 | If the last word does not exist, return 0. 17 | 18 | Note: A word is defined as a character sequence consists of non-space characters only. 19 | 20 | Example: 21 | 22 | Input: "Hello World" 23 | 24 | Output: 5 25 | ``` 26 | 27 | ## 解题方案 28 | 29 | > 思路 1 30 | ******- 时间复杂度: O(N)******- 空间复杂度: O(N)****** 31 | 32 | * 将数组以空格分割,找到最后一个字符串输出长度 33 | * 注意以空格结尾以及输入空字符串 34 | 35 | 代码: 36 | 37 | ```javascript 38 | /** 39 | * @param {string} s 40 | * @return {number} 41 | */ 42 | var lengthOfLastWord = function(s) { 43 | var temp = s.split(' ').filter(function (value) { 44 | return value!=''; 45 | }); 46 | return temp.length>0?temp.pop().length:0; 47 | }; 48 | ``` 49 | 50 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/JavaScript/0104._Maximum_Depth_of_Binary_Tree.md: -------------------------------------------------------------------------------- 1 | # 104. Maximum Depth of Binary Tree 2 | 3 | **难度: Easy** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode.com/problems/maximum-depth-of-binary-tree/description/ 10 | 11 | > 内容描述 12 | 13 | ``` 14 | Given a binary tree, find its maximum depth. 15 | 16 | The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 17 | 18 | Note: A leaf is a node with no children. 19 | 20 | Example: 21 | 22 | Given binary tree [3,9,20,null,null,15,7], 23 | 24 | 3 25 | / \ 26 | 9 20 27 | / \ 28 | 15 7 29 | return its depth = 3. 30 | ``` 31 | 32 | ## 解题方案 33 | 34 | > 思路 1 35 | ******- 时间复杂度: O(log2 N)******- 空间复杂度: O(N)****** 36 | 37 | * 这道题使用递归进行解决,因为对于树的每一个儿子的处理方法是一致的。 38 | * 将左儿子和右儿子中最大的数进行返回再加上当前的深度1即可解决。 39 | 40 | 代码: 41 | 42 | ```javascript 43 | var maxDepth = function(root) { 44 | if(root===null||root === undefined) 45 | return 0; 46 | return Math.max(maxDepth(root.left),maxDepth(root.right))+1; 47 | }; 48 | ``` 49 | 50 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0182._duplicate_emails.md: -------------------------------------------------------------------------------- 1 | # 182. duplicate-emails 查找重复的电子邮箱 2 | 3 | **难度: Easy** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode.com/problems/duplicate-emails 10 | * https://leetcode-cn.com/problems/duplicate-emails/description 11 | 12 | > 内容描述 13 | 14 | ``` 15 | 编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。 16 | 17 | 示例: 18 | +----+---------+ 19 | | Id | Email | 20 | +----+---------+ 21 | | 1 | a@b.com | 22 | | 2 | c@d.com | 23 | | 3 | a@b.com | 24 | +----+---------+ 25 | 26 | 根据以上输入,你的查询应返回以下结果: 27 | +---------+ 28 | | Email | 29 | +---------+ 30 | | a@b.com | 31 | +---------+ 32 | 33 | 说明:所有电子邮箱都是小写字母。 34 | ``` 35 | 36 | ## 解题方案 37 | 38 | > 思路 1 39 | 40 | ```sql 41 | select Email 42 | from Person 43 | group by Email 44 | having count(1)>1 45 | ``` 46 | 或者 47 | ```sql 48 | select Email from Person group by Email having count(Email) > 1 49 | ``` 50 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0194._Transpose_File.md: -------------------------------------------------------------------------------- 1 | # 194. Transpose File 2 | 3 | **难度: Medium** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode.com/problems/transpose-file/ 10 | 11 | > 内容描述 12 | 13 | ``` 14 | Given a text file file.txt, transpose its content. 15 | 16 | You may assume that each row has the same number of columns and each field is separated by the ' ' character. 17 | 18 | Example: 19 | 20 | If file.txt has the following content: 21 | 22 | name age 23 | alice 21 24 | ryan 30 25 | Output the following: 26 | 27 | name alice ryan 28 | age 21 30 29 | ``` 30 | 31 | ## 解题方案 32 | 33 | > 思路 1 34 | 35 | 参考[illuz](https://leetcode.com/problems/transpose-file/discuss/55502/AC-solution-using-awk-and-statement-just-like-C.) 36 | 37 | ```shell 38 | awk ' 39 | { 40 | for (i = 1; i <= NF; i++) { 41 | if(NR == 1) { 42 | s[i] = $i; 43 | } else { 44 | s[i] = s[i] " " $i; 45 | } 46 | } 47 | } 48 | END { 49 | for (i = 1; s[i] != ""; i++) { 50 | print s[i]; 51 | } 52 | }' file.txt 53 | ``` 54 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0266._Palindrome_Permutation.md: -------------------------------------------------------------------------------- 1 | # 266. Palindrome Permutation 2 | 3 | **难度: Easy** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode.com/problems/palindrome-permutation/ 10 | 11 | > 内容描述 12 | 13 | ``` 14 | Given a string, determine if a permutation of the string could form a palindrome. 15 | 16 | Example 1: 17 | 18 | Input: "code" 19 | Output: false 20 | Example 2: 21 | 22 | Input: "aab" 23 | Output: true 24 | Example 3: 25 | 26 | Input: "carerac" 27 | Output: true 28 | ``` 29 | 30 | ## 解题方案 31 | 32 | > 思路 1 33 | ******- 时间复杂度: O(N)******- 空间复杂度: O(N)****** 34 | 35 | 数一下只出现奇数次数的字母有多少个,如果少于或者等于1个,都可以组成Palindrome 36 | 37 | beats 100% 38 | 39 | ```python 40 | class Solution: 41 | def canPermutePalindrome(self, s): 42 | """ 43 | :type s: str 44 | :rtype: bool 45 | """ 46 | tmp = collections.Counter(s) 47 | res = 0 48 | for i in tmp.values(): 49 | if i & 1 == 1: 50 | res += 1 51 | return res <= 1 52 | ``` 53 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0278._First_Bad_Version.md: -------------------------------------------------------------------------------- 1 | ### 278. First Bad Version 2 | 3 | 4 | 5 | 6 | 7 | 题目: 8 | 9 | 10 | 11 | 12 | 难度 : Easy 13 | 14 | 15 | 16 | 思路: 17 | 18 | 根据 search for a range 改的 19 | 20 | 21 | 22 | ```python 23 | class Solution(object): 24 | def firstBadVersion(self, n): 25 | """ 26 | :type n: int 27 | :rtype: int 28 | """ 29 | l, r = 0, n - 1 30 | while l <= r: 31 | mid = l + ((r - l) >> 2) 32 | if not isBadVersion(mid): 33 | l = mid + 1 34 | else: 35 | r = mid - 1 36 | return l 37 | ``` 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0292._nim_game.md: -------------------------------------------------------------------------------- 1 | ### 292. Nim Game 2 | 3 | 题目: 4 | 5 | 6 | 7 | 8 | 难度: 9 | 10 | Easy 11 | 12 | 13 | 对于总是优先开始的那方 14 | 15 | 16 | - 有一到三块,总是赢 17 | - 有四块,总是输 18 | - 有五块,总是赢 19 | 20 | 所以如果自己想赢,总是要迫使对方拿之后,给自己遗留5块,或者三块以及以下。 21 | 22 | - 如果是六块: 23 | - 拿一块,对方五块,对方赢 24 | - 拿两块,对方余下四块,我方赢 25 | - 拿三块,余三块,对方赢 26 | 27 | - 七块: 28 | - 拿三块,余四块,迫使对方输,总是赢 29 | 30 | 本打算用递归来看,因为对方也可以重复使用这个函数,但是会超时,所以就看了一下hint 31 | 32 | 33 | - n <= 3 能赢 √ 34 | - n == 4 总输 35 | - n = 5,6,7 总赢 36 | - n == 8, 先手如何选,总可以转成5,6,7 对方总会赢 37 | 38 | 39 | 所以 n % 4 == 0 时候,先手必输 40 | 41 | 简直是啊,有些游戏就是这样来必赢的啊,没想到你是这样的题目 42 | 43 | 44 | 45 | ``` 46 | class Solution(object): 47 | def canWinNim(self, n): 48 | """ 49 | :type n: int 50 | :rtype: bool 51 | """ 52 | return n % 4 != 0 53 | ``` 54 | 55 | 56 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0293._Flip_Game.md: -------------------------------------------------------------------------------- 1 | ### 293. Flip Game 2 | 3 | 题目: 4 | 5 | 6 | 7 | 难度: 8 | 9 | Easy 10 | 11 | 12 | 思路 13 | 14 | 15 | 16 | 17 | ```python 18 | class Solution(object): 19 | def generatePossibleNextMoves(self, s): 20 | """ 21 | :type s: str 22 | :rtype: List[str] 23 | """ 24 | res = [] 25 | if not s or len(s) <= 1: 26 | return res 27 | for i in range(len(s)-1): 28 | if s[i] == '+' and s[i+1] == '+': 29 | res.append(s[:i]+'--'+s[i+2:]) 30 | return res 31 | ``` 32 | 33 | 这里要注意一个点:s[i+2:] 虽然i+2取不到,但是s[i+2:]就是一个空字符串。 34 | 35 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0324._Wiggle_Sort_II.md: -------------------------------------------------------------------------------- 1 | ### 324. Wiggle Sort II 2 | 3 | 4 | 5 | 题目: 6 | 7 | 8 | 9 | 难度: 10 | Medium 11 | 12 | 思路: 13 | 14 | 首先这道题和[Wiggle Sort](https://github.com/Lisanaaa/thinking_in_lc/blob/master/280._Wiggle_Sort.md)要求不一样,不能有等于, 15 | 所以如果碰到一串```‘1,1,1,1,1,1’```,当调换顺序时候还是不会满足。 16 | 17 | 因此我们用新方法,首先将原数组排序,然后大的那一半数字降序插在奇数```index```上,小的那一半数字降序插在偶数```index```上 18 | 19 | 20 | ```python 21 | class Solution(object): 22 | def wiggleSort(self, nums): 23 | """ 24 | :type nums: List[int] 25 | :rtype: void Do not return anything, modify nums in-place instead. 26 | """ 27 | nums.sort() 28 | half = len(nums[::2]) 29 | nums[::2], nums[1::2] = nums[:half][::-1], nums[half:][::-1] 30 | ``` 31 | 32 | 33 | ### Follow up 34 | O(n) time, O(1) space 35 | 36 | 思路: 37 | 首先想到的是将我们上面的排序方法用堆排序实现即可,建堆O(n),调整堆O(lgN) 38 | 39 | 40 | ```python 41 | 42 | ``` 43 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0369._Plus_One_Linked_List.md: -------------------------------------------------------------------------------- 1 | ### 369.Plus One Linked List 2 | 3 | 题目: 4 | 5 | 6 | 难度 : Medium 7 | 8 | 9 | 10 | 类似题目: plus one,plus one 用递归和循环写了,对于linked list,因为most significant digit在首位,递归写起来不方便,用循环尝试,然后代码并没有实质上的区别。 11 | 12 | 13 | 14 | ``` 15 | class Solution(object): 16 | def plusOne(self, head): 17 | """ 18 | :type head: ListNode 19 | :rtype: ListNode 20 | """ 21 | lst = [] 22 | cur = head 23 | 24 | while cur: 25 | lst.append(cur) 26 | cur = cur.next 27 | 28 | carry = 1 29 | for i in range(len(lst)-1,-1,-1): 30 | lst[i].val += carry 31 | if lst[i].val < 10: 32 | carry = 0 33 | break 34 | else: 35 | lst[i].val -= 10 36 | 37 | if carry == 1: 38 | node = ListNode(1) 39 | node.next = head 40 | return node 41 | else: 42 | return head 43 | ``` 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0374._Guess_Number_Higher_or_Lower.md: -------------------------------------------------------------------------------- 1 | ### 374. Guess Number Higher or Lower 2 | 3 | 题目: 4 | 5 | 6 | 7 | 难度: 8 | 9 | Easy 10 | 11 | 12 | 思路 13 | 14 | 二分 15 | 16 | ```python 17 | class Solution(object): 18 | def guessNumber(self, n): 19 | """ 20 | :type n: int 21 | :rtype: int 22 | """ 23 | l, r = 1, n 24 | while l <= r: 25 | mid = l + ((r - l) >> 2) 26 | if guess(mid) == 1: 27 | l = mid + 1 28 | elif guess(mid) == -1: 29 | r = mid - 1 30 | else: 31 | return mid 32 | ``` 33 | 34 | 35 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0389._find_the_difference.md: -------------------------------------------------------------------------------- 1 | 2 | ### 389. Find the Difference 3 | 4 | 5 | 题目: 6 | 7 | 8 | 9 | 难度: 10 | 11 | Easy 12 | 13 | 用个字典来记录,把s加进去,把t减掉,最后剩下那个要么个数为1,要么个数为-1 14 | 15 | ```python 16 | class Solution(object): 17 | def findTheDifference(self, s, t): 18 | """ 19 | :type s: str 20 | :type t: str 21 | :rtype: str 22 | """ 23 | res = {} 24 | for i in s: 25 | res[i] = res.get(i, 0) + 1 26 | for j in t: 27 | res[j] = res.get(j, 0) - 1 28 | for key in res: 29 | if abs(res[key]) == 1: # 这里用 abs 是因为新增加的那个字母在 s 中可能未出现过 30 | return key 31 | ``` 32 | 还有一个简单的方法 33 | ```python 34 | class Solution(object): 35 | def findTheDifference(self, s, t): 36 | """ 37 | :type s: str 38 | :type t: str 39 | :rtype: str 40 | """ 41 | from collections import Counter 42 | return list((Counter(t) - Counter(s)).keys()).pop() 43 | ``` 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0422._Valid_Word_Square.md: -------------------------------------------------------------------------------- 1 | ### 422. Valid Word Square 2 | 3 | 4 | 5 | 6 | 7 | 题目: 8 | 9 | 10 | 11 | 12 | 难度 : Easy 13 | 14 | 15 | 16 | 思路: 17 | 18 | 就是对比一个矩阵内 xy == yx? 19 | 20 | try /except 真是好用 21 | 22 | AC代码 23 | 24 | 25 | 26 | ``` 27 | class Solution(object): 28 | def validWordSquare(self, words): 29 | """ 30 | :type words: List[str] 31 | :rtype: bool 32 | """ 33 | n = len(words) 34 | for i in xrange(n): 35 | m = len(words[i]) 36 | for j in xrange(m): 37 | try: 38 | if words[i][j] != words[j][i]: 39 | return False 40 | except: 41 | return False 42 | return True 43 | ``` 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0434._number_of_segments_in_a_string.md: -------------------------------------------------------------------------------- 1 | ### 434. Number of Segments in a String 2 | 3 | 题目: 4 | 5 | 6 | 7 | 难度: 8 | 9 | Easy 10 | 11 | 12 | 作弊神器Python 13 | 14 | 15 | ```python 16 | class Solution(object): 17 | def countSegments(self, s): 18 | """ 19 | :type s: str 20 | :rtype: int 21 | """ 22 | return len(s.split()) 23 | ``` 24 | 25 | 不过对于比如C++这种语言来说,应该是O(N),扫一圈应该也能得到正确答案 26 | 27 | 总之拿Python做string的题目就是作弊啊 28 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0437._path_sum_iii.md: -------------------------------------------------------------------------------- 1 | ### 437. Path Sum III 2 | 3 | 4 | 5 | 题目: 6 | 7 | 8 | 9 | 难度: 10 | Easy 11 | 12 | 思路: 13 | 14 | 15 | 16 | 17 | ```python 18 | class Solution(object): 19 | def pathSum(self, root, sum): 20 | """ 21 | :type root: TreeNode 22 | :type sum: int 23 | :rtype: int 24 | """ 25 | if not root: 26 | return 0 27 | res = self.auxPathSum(root, sum) 28 | res += self.pathSum(root.left, sum) 29 | res += self.pathSum(root.right, sum) 30 | return res 31 | def auxPathSum(self, root, sum): 32 | if not root: 33 | return 0 34 | if sum == root.val: 35 |            # 因为可能有负值, 所以sum为0也会有解, 必须加上 36 |            return 1 + self.auxPathSum(root.left, 0) + self.auxPathSum(root.right, 0) 37 | else: 38 | return self.auxPathSum(root.left, sum - root.val) + self.auxPathSum(root.right, sum - root.val) 39 | ``` 40 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0448._Find_All_Numbers_Disappeared_in_an_Array.md: -------------------------------------------------------------------------------- 1 | ### 448. Find All Numbers Disappeared in an Array 2 | 3 | 题目: 4 | 5 | 6 | 7 | 难度: 8 | 9 | Easy 10 | 11 | 12 | 13 | 14 | 15 | ```python 16 | class Solution(object): 17 | def findDisappearedNumbers(self, nums): 18 | """ 19 | :type nums: List[int] 20 | :rtype: List[int] 21 | """ 22 | return list(set(range(1, len(nums)+1)) - set(nums)) 23 | ``` 24 | 25 | 26 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0459._Repeated_Substring_Pattern.md: -------------------------------------------------------------------------------- 1 | ### 459. Repeated Substring Pattern 2 | 3 | 4 | 题目: 5 | 6 | 7 | 8 | 难度: 9 | 10 | Easy 11 | 12 | 13 | 思路 14 | 15 | - 如果存在这样的子串,那么子串的第一个字符和最后一个字符肯定跟父字符串```s```的相同。 16 | - 因此构建一个新字符串```s*2```(两个父字符串相加),去掉首尾字符 17 | - 如果此时能在其中找到```s```,说明存在这样的子串 18 | 19 | 20 | 21 | 22 | ```python 23 | class Solution(object): 24 | def repeatedSubstringPattern(self, s): 25 | """ 26 | :type s: str 27 | :rtype: bool 28 | """ 29 | return (s*2)[1:-1].find(s) != -1 30 | ``` 31 | 32 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0476._Number_Complement.md: -------------------------------------------------------------------------------- 1 | ### 476. Number Complement 2 | 3 | 4 | 5 | 题目: 6 | 7 | 8 | 9 | 难度: 10 | Easy 11 | 12 | 13 | 14 | 15 | 16 | ```python 17 | class Solution(object): 18 | def findComplement(self, num): 19 | """ 20 | :type num: int 21 | :rtype: int 22 | """ 23 | i = 1 << (len(bin(num)) -2) # 因为bin函数转化成的格式是‘0bXXXX’,头两个‘0b’要减掉去 24 | return (i - 1) ^ num 25 | # return (i - 1) - num # 这样也可以 26 | ``` 27 | 28 | 29 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0633._Sum_of_Square_Numbers.md: -------------------------------------------------------------------------------- 1 | # 633. Sum of Square Numbers 2 | 3 | **难度: Easy** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode.com/problems/sum-of-square-numbers/description/ 10 | 11 | > 内容描述 12 | 13 | ``` 14 | Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c. 15 | 16 | Example 1: 17 | Input: 5 18 | Output: True 19 | Explanation: 1 * 1 + 2 * 2 = 5 20 | Example 2: 21 | Input: 3 22 | Output: False 23 | ``` 24 | 25 | ## 解题方案 26 | 27 | > 思路 1 28 | ******- 时间复杂度: O(lgN)******- 空间复杂度: O(1)****** 29 | 30 | 31 | ```python 32 | from math import sqrt 33 | class Solution(object): 34 | def judgeSquareSum(self, c): 35 | """ 36 | :type c: int 37 | :rtype: bool 38 | """ 39 | a = 0 40 | while a * a <= c: 41 | if int(sqrt(c - a * a)) == sqrt(c - a * a): 42 | return True 43 | a += 1 44 | return False 45 | ``` 46 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0657._Judge_Route_Circle.md: -------------------------------------------------------------------------------- 1 | ### 657. Judge Route Circle 2 | 题目: 3 | 4 | 5 | 6 | 难度: 7 | 8 | Easy 9 | 10 | 11 | 12 | 13 | ```python 14 | class Solution(object): 15 | def judgeCircle(self, moves): 16 | """ 17 | :type moves: str 18 | :rtype: bool 19 | """ 20 | return moves.count('D') == moves.count('U') and moves.count('R') == moves.count('L') 21 | ``` 22 | 23 | 24 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0681._Next_Closest_Time.md: -------------------------------------------------------------------------------- 1 | ### 681. Next Closest Time 2 | 3 | 4 | 题目: 5 | 6 | 7 | 8 | 难度: 9 | 10 | Medium 11 | 12 | 13 | 14 | 思路 15 | 16 | 题目说输入一个时间,format是HH:MM, 然后输出接下来最近的一个时间,且这个时间的数字必须要在输入的时间中可以找到,所以我们用```h```, ```m``` 17 | 分别代表输入时间的小时数和分钟数,然后可以计算出输入时间的总分钟数```curr```,在未来的一天之内,我们一分钟一分钟往下面试,第一个满足的就直接 18 | 作为结果就行了. 19 | 20 | 21 | 22 | 23 | ```python 24 | class Solution(object): 25 | def nextClosestTime(self, time): 26 | """ 27 | :type time: str 28 | :rtype: str 29 | """ 30 | h, m = time.split(":") 31 |        curr = int(h) * 60 + int(m) # 这里要注意h可能会是0开头的,如输入的时间为01:22,所以需要int(h)和int(m) 32 |        result = None 33 | for i in xrange(curr+1, curr+1441): 34 | t = i % 1440 35 | h, m = t // 60, t % 60 36 | result = "%02d:%02d" % (h, m) 37 | if set(result) <= set(time): 38 | break 39 | return result 40 | ``` 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0682._Baseball_Game.md: -------------------------------------------------------------------------------- 1 | ### 682. Baseball Game 2 | 3 | 题目: 4 | 5 | 6 | 7 | 难度: 8 | 9 | Easy 10 | 11 | 12 | 思路 13 | 14 | points用来存放每一次的分数,最后求和。 15 | 16 | 17 | ```python 18 | class Solution(object): 19 | def calPoints(self, ops): 20 | """ 21 | :type ops: List[str] 22 | :rtype: int 23 | """ 24 | points = [] 25 | for i in ops: 26 | if i == 'C': 27 | points.pop() 28 | elif i == 'D': 29 | points.append(2 * points[-1]) 30 | elif i == '+': 31 | points.append(points[-1] + points[-2]) 32 | else: 33 | points.append(int(i)) 34 | return sum(points) 35 | ``` 36 | 37 | 38 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0709._To_Lower_Case.md: -------------------------------------------------------------------------------- 1 | # 1. Two Sum 2 | 3 | **难度: Easy** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode.com/problems/to-lower-case/ 10 | 11 | > 内容描述 12 | 13 | ``` 14 | 15 | Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. 16 | 17 | 18 | 19 | Example 1: 20 | 21 | Input: "Hello" 22 | Output: "hello" 23 | Example 2: 24 | 25 | Input: "here" 26 | Output: "here" 27 | Example 3: 28 | 29 | Input: "LOVELY" 30 | Output: "lovely" 31 | ``` 32 | 33 | ## 解题方案 34 | 35 | > 思路 1 36 | ******- 时间复杂度: O(N)******- 空间复杂度: O(N)****** 37 | 38 | 39 | 40 | beats 75.75% 41 | 42 | ```python 43 | class Solution: 44 | def toLowerCase(self, str): 45 | """ 46 | :type str: str 47 | :rtype: str 48 | """ 49 | return ''.join(chr(ord(c) + 32) if 'A' <= c <= 'Z' else c for c in str) 50 | ``` 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0760._Find_Anagram_Mappings.md: -------------------------------------------------------------------------------- 1 | ### 760. Find Anagram Mappings 2 | 3 | 题目: 4 | 5 | 6 | 7 | 难度: 8 | 9 | Easy 10 | 11 | 12 | 13 | 14 | 15 | ```python 16 | class Solution(object): 17 | def anagramMappings(self, A, B): 18 | """ 19 | :type A: List[int] 20 | :type B: List[int] 21 | :rtype: List[int] 22 | """ 23 | if not A: 24 | return [] 25 | res = [] 26 | for i in A: 27 | res.append(B.index(i)) 28 | return res 29 | ``` 30 | 31 | 32 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/0796._Rotate_String.md: -------------------------------------------------------------------------------- 1 | # 796. Rotate String 2 | 3 | **难度: Easy** 4 | 5 | ## 刷题内容 6 | 7 | > 原题连接 8 | 9 | * https://leetcode.com/problems/rotate-string/description/ 10 | 11 | > 内容描述 12 | 13 | ``` 14 | We are given two strings, A and B. 15 | 16 | A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' after one shift on A. Return True if and only if A can become B after some number of shifts on A. 17 | 18 | Example 1: 19 | Input: A = 'abcde', B = 'cdeab' 20 | Output: true 21 | 22 | Example 2: 23 | Input: A = 'abcde', B = 'abced' 24 | Output: false 25 | Note: 26 | 27 | A and B will have length at most 100. 28 | ``` 29 | 30 | ## 解题方案 31 | 32 | > 思路 1 33 | 34 | 35 | 一行就够了,太简单了 36 | 37 | ```python 38 | class Solution(object): 39 | def rotateString(self, A, B): 40 | """ 41 | :type A: str 42 | :type B: str 43 | :rtype: bool 44 | """ 45 | return len(A) == len(B) and B in A * 2 46 | ``` 47 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/Summary/Bit_Manipulation/位运算.md: -------------------------------------------------------------------------------- 1 | ### 位运算 2 | 3 | 位运算包括: 加 减 乘 取反 and 异或 4 | 5 | - 0110 + 0110 = 0110 * 2 ,也就是0110左移1位 6 | 7 | - 0011 * 0100 0100 = 4, 一个数乘以 2^n 即是将这个数左移n 8 | 9 | - a ^(~a) = 0 10 | 11 | - x & (~0 << n ) 这样来看,0取反全部为1,然后将其右移n位,后面的全是0,x & (~0 <>:右移 28 | 29 | ​ 30 | 31 | Bit Facts and Tricks 32 | 33 | ``` 34 | x ^ 0s = x x & 0s = 0 x | 0s = x 35 | x ^ 1s = ~x x & 1s = x x | 1s = 1s 36 | x ^ x = 0 x & x = x x | x = x 37 | ``` 38 | 39 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/Summary/Dynamic Programming.md: -------------------------------------------------------------------------------- 1 | ### Dynamic Programming 2 | 3 | 4 | 5 | - Fibonacci Numbers 6 | - Shortest Path (no cycles) 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | - subproblems 15 | 16 | - guessing 17 | 18 | - relate subproblems 19 | 20 | - recurse & memoize (bulid DP table) 21 | 22 | - solve original problem 23 | 24 | ​ 25 | 26 | 27 | 28 | 29 | 30 | 31 | ​ 32 | ​ 33 | ​ 34 | 35 | 感觉DP有几类: 36 | 37 | - 容易写出递推公式的 38 | 39 | - 画表更容易理解的 40 | 41 | ​ 42 | 43 | * DP ≈ “controlled brute force” 44 | 45 | * DP ≈ recursion + re-use 46 | 47 | ​ 48 | 49 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/Summary/Graph/README.md: -------------------------------------------------------------------------------- 1 | 1. Iterative Deepening Depth First Search (IDDFS).ipynb: copied from [Kautenja](https://gist.github.com/Kautenja/72cd5494adc12dcbfeea3e79e7b3c3ac) 2 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/Summary/Graph/graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/Python/Summary/Graph/graph.png -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/Summary/Introduction to String Searching Algorithms – topcoder.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/Python/Summary/Introduction to String Searching Algorithms – topcoder.pdf -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/Summary/Math/求平方根.md: -------------------------------------------------------------------------------- 1 | ```python 2 | import math 3 | 4 | 5 | def sqrt_newton(num): 6 | y = num / 2.0 7 | while abs(y * y - num) > 0.00000001: 8 | y = ((y * 1.0) + (1.0 * num) / y) / 2.0000 9 | return y 10 | 11 | 12 | def sqrt_binary_search(num): 13 | y = num / 2.0 14 | low = 0.0 15 | up = num * 1.0 16 | while abs(y * y - num) > 0.00000001: 17 | if (y * y > num): 18 | up = y 19 | y = low + (y - low) / 2 20 | else: 21 | low = y 22 | y = up - (up - y) / 2 23 | return y 24 | 25 | 26 | print(sqrt_binary_search(2)) 27 | print(sqrt_newton(2)) 28 | print(math.sqrt(2)) 29 | ``` 30 | -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/Summary/Maximal Square.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/Python/Summary/Maximal Square.pdf -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/Summary/Range Sum Query 2D - Immutable.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/docs/Leetcode_Solutions/Python/Summary/Range Sum Query 2D - Immutable.pdf -------------------------------------------------------------------------------- /docs/Leetcode_Solutions/Python/Summary/编程思路总结.md: -------------------------------------------------------------------------------- 1 | ## 1. OJ 基本条件 2 | 达尔大佬对刷leetcode和刷笔试题有几个建议: 3 | 4 | 1. 一般oj是有要求时间的,一般是1秒。后台判题系统都是单核单线程跑,机器性能不会太好。1秒能运行的基本操作个数的量级是10^6-10^7, 5 | 个别代码能跑到10^8,但很少见。这种情况下,看数据范围是可以大致了解到这个题目需要什么样子的时间复杂度。 6 | 比如说,n<=1000的数据范围,o(n^3)的代码就不要写了,浪费时间而已。写代码之前得先计算自己代码的复杂度,预估自己代码跑的速度,再动手。 7 | 2. 基本操作指的是逻辑和加减法,乘法比加减法慢得多,但在大复杂度的前提下,这种复杂度是可以忽略的。比如说,在一个算法o(n^3)中, 8 | 代码中一个两层嵌套循环o(n^2)就显得没什么优化意义了。优化代码主要优化算法复杂度,而那些,要不要用位运算代替除法啊, 9 | 或者两个一层循环要不要合并成一个一层循环啊,没有意义,不要纠结,同级复杂度,才有优化意义 10 | 3. 高级编程语言每个api都有自己的复杂度,在没有了解复杂度之前不建议使用那些api,最好自己实现,比如哈希表啊,堆啊, 11 | 什么语言都有实现,熟悉之前还是乖乖自己实现的好 12 | 4. 注意精度误差。。。比如开方,如果开方开的是一个整数,结果也是整数,最好写个二分,或者写个牛顿迭代求零点, 13 | 不要用内置函数sqrt,求出来是个浮点数,会有误差 14 | 15 | 16 | ## 2. 判断环 17 | - 看到链表中的环就想到快慢指针 18 | - 看到图中的环就想到并查集,wisdompeak大佬说:用union find确实是判断图是否成环的一个很好的手段。但是它更常用在无向图。 19 | 对于有向图,用并查集需要非常小心。这里的edge都是有向的,我觉得无脑用并查集会有风险。 20 | 我的总结是:union find唯一能用在有向图的地方,就是只有我们试图保证这张图是树的时候。 21 | -------------------------------------------------------------------------------- /docs/剑指offer/C++/README.md: -------------------------------------------------------------------------------- 1 | # Jianzhi-offer 2 | 3 | -------------------------------------------------------------------------------- /docs/剑指offer/Java/04_FindInPartiallySortedMatrix/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author bingo 3 | * @since 2018/10/27 4 | */ 5 | 6 | public class Solution { 7 | /** 8 | * 二维数组中的查找 9 | * @param target 目标值 10 | * @param array 二维数组 11 | * @return boolean 12 | */ 13 | public boolean find(int target, int[][] array) { 14 | if (array == null) { 15 | return false; 16 | } 17 | int rows = array.length; 18 | int columns = array[0].length; 19 | 20 | int i = rows - 1; 21 | int j = 0; 22 | while (i >= 0 && j < columns) { 23 | if (array[i][j] == target) { 24 | return true; 25 | } 26 | if (array[i][j] < target) { 27 | ++j; 28 | } else { 29 | --i; 30 | } 31 | } 32 | return false; 33 | } 34 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/06_PrintListInReversedOrder/Solution.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next = null; 6 | * 7 | * ListNode(int val) { 8 | * this.val = val; 9 | * } 10 | * } 11 | * 12 | */ 13 | import java.util.ArrayList; 14 | import java.util.Stack; 15 | 16 | /** 17 | * @author bingo 18 | * @since 2018/10/28 19 | */ 20 | public class Solution { 21 | /** 22 | * 从尾到头打印链表 23 | * @param listNode 链表头节点 24 | * @return list 25 | */ 26 | public ArrayList printListFromTailToHead(ListNode listNode) { 27 | ArrayList res = new ArrayList<>(); 28 | if (listNode == null) { 29 | return res; 30 | } 31 | Stack stack = new Stack<>(); 32 | while (listNode != null) { 33 | stack.push(listNode.val); 34 | listNode = listNode.next; 35 | } 36 | while (!stack.isEmpty()) { 37 | res.add(stack.pop()); 38 | } 39 | 40 | return res; 41 | } 42 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/09_01_QueueWithTwoStacks/README.md: -------------------------------------------------------------------------------- 1 | ## 用两个栈实现队列 2 | 3 | ### 题目描述 4 | 用两个栈来实现一个队列,完成队列的 `Push` 和 `Pop` 操作。 队列中的元素为 `int` 类型。 5 | 6 | 7 | ### 解法 8 | `Push` 操作,每次都存入 `stack1`; 9 | `Pop` 操作,每次从 `stack2` 取: 10 | - `stack2` 栈不为空时,不能将 `stack1` 元素倒入; 11 | - `stack2` 栈为空时,需要一次将 `stack1` 元素全部倒入。 12 | 13 | ```java 14 | import java.util.Stack; 15 | 16 | /** 17 | * @author bingo 18 | * @since 2018/10/28 19 | */ 20 | 21 | public class Solution { 22 | Stack stack1 = new Stack(); 23 | Stack stack2 = new Stack(); 24 | 25 | public void push(int node) { 26 | stack1.push(node); 27 | } 28 | 29 | public int pop() { 30 | if (stack2.isEmpty()) { 31 | if (stack1.isEmpty()) { 32 | return -1; 33 | } 34 | while (!stack1.isEmpty()) { 35 | stack2.push(stack1.pop()); 36 | } 37 | } 38 | return stack2.pop(); 39 | } 40 | } 41 | ``` 42 | 43 | 44 | ### 测试用例 45 | 1. 往空的队列里添加、删除元素; 46 | 2. 往非空的队列添加、删除元素; 47 | 3. 连续删除元素直至队列为空。 -------------------------------------------------------------------------------- /docs/剑指offer/Java/09_01_QueueWithTwoStacks/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Stack; 2 | 3 | /** 4 | * @author bingo 5 | * @since 2018/10/28 6 | */ 7 | 8 | public class Solution { 9 | Stack stack1 = new Stack(); 10 | Stack stack2 = new Stack(); 11 | 12 | public void push(int node) { 13 | stack1.push(node); 14 | } 15 | 16 | public int pop() { 17 | if (stack2.isEmpty()) { 18 | if (stack1.isEmpty()) { 19 | return -1; 20 | } 21 | while (!stack1.isEmpty()) { 22 | stack2.push(stack1.pop()); 23 | } 24 | } 25 | return stack2.pop(); 26 | } 27 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/09_02_StackWithTwoQueues/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | import java.util.Queue; 3 | 4 | /** 5 | * @author bingo 6 | * @since 2018/10/29 7 | */ 8 | 9 | public class Solution { 10 | 11 | private Queue queue1 = new LinkedList<>(); 12 | private Queue queue2 = new LinkedList<>(); 13 | 14 | public void push(int node) { 15 | queue1.offer(node); 16 | } 17 | 18 | public int pop() { 19 | if (queue1.isEmpty()) { 20 | throw new RuntimeException("Empty stack!"); 21 | } 22 | 23 | while (queue1.size() > 1) { 24 | queue2.offer(queue1.poll()); 25 | } 26 | 27 | int val = queue1.poll(); 28 | 29 | Queue t = queue1; 30 | queue1 = queue2; 31 | queue2 = t; 32 | return val; 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/10_01_Fibonacci/Solution.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * @author bingo 4 | * @since 2018/10/29 5 | */ 6 | 7 | public class Solution { 8 | /** 9 | * 求斐波那契数列的第n项,n从0开始 10 | * @param n 第n项 11 | * @return 第n项的值 12 | */ 13 | public int Fibonacci(int n) { 14 | if (n < 2) { 15 | return n; 16 | } 17 | int[] res = new int[n + 1]; 18 | res[0] = 0; 19 | res[1] = 1; 20 | for (int i = 2; i <= n; ++i) { 21 | res[i] = res[i - 1] + res[i - 2]; 22 | } 23 | return res[n]; 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/10_02_JumpFloor/README.md: -------------------------------------------------------------------------------- 1 | ## 跳台阶 2 | 3 | ### 题目描述 4 | 一只青蛙一次可以跳上`1`级台阶,也可以跳上`2`级。求该青蛙跳上一个`n`级的台阶总共有多少种跳法(先后次序不同算不同的结果)。 5 | 6 | ### 解法 7 | 跳上 `n` 级台阶,可以从 `n-1` 级跳 `1` 级上去,也可以从 `n-2` 级跳 `2` 级上去。所以 8 | ``` 9 | f(n) = f(n-1) + f(n-2) 10 | ``` 11 | 12 | ```java 13 | /** 14 | * @author bingo 15 | * @since 2018/11/23 16 | */ 17 | 18 | public class Solution { 19 | /** 20 | * 青蛙跳台阶 21 | * @param target 跳上的那一级台阶 22 | * @return 多少种跳法 23 | */ 24 | public int JumpFloor(int target) { 25 | if (target < 3) { 26 | return target; 27 | } 28 | int[] res = new int[target + 1]; 29 | res[1] = 1; 30 | res[2] = 2; 31 | for (int i = 3; i <= target; ++i) { 32 | res[i] = res[i - 1] + res[i - 2]; 33 | } 34 | return res[target]; 35 | } 36 | } 37 | ``` 38 | 39 | ### 测试用例 40 | 1. 功能测试(如输入 3、5、10 等); 41 | 2. 边界值测试(如输入 0、1、2); 42 | 3. 性能测试(输入较大的数字,如 40、50、100 等)。 -------------------------------------------------------------------------------- /docs/剑指offer/Java/10_02_JumpFloor/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author bingo 3 | * @since 2018/11/23 4 | */ 5 | 6 | public class Solution { 7 | /** 8 | * 青蛙跳台阶 9 | * 10 | * @param target 跳上的那一级台阶 11 | * @return 多少种跳法 12 | */ 13 | public int JumpFloor(int target) { 14 | if (target < 3) { 15 | return target; 16 | } 17 | int[] res = new int[target + 1]; 18 | res[1] = 1; 19 | res[2] = 2; 20 | for (int i = 3; i <= target; ++i) { 21 | res[i] = res[i - 1] + res[i - 2]; 22 | } 23 | return res[target]; 24 | } 25 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/10_03_JumpFloorII/README.md: -------------------------------------------------------------------------------- 1 | ## 变态跳台阶 2 | 3 | ### 题目描述 4 | 一只青蛙一次可以跳上`1`级台阶,也可以跳上`2`级……它也可以跳上`n`级。求该青蛙跳上一个`n`级的台阶总共有多少种跳法。 5 | 6 | ### 解法 7 | 跳上 `n-1` 级台阶,可以从 `n-2` 级跳 `1` 级上去,也可以从 `n-3` 级跳 `2` 级上去...也可以从 `0` 级跳上去。那么 8 | ``` 9 | f(n-1) = f(0) + f(1) + ... + f(n-2) ① 10 | ``` 11 | 12 | 跳上 `n` 级台阶,可以从 `n-1` 级跳 `1` 级上去,也可以从 `n-2` 级跳 `2` 级上去...也可以从 `0` 级跳上去。那么 13 | ``` 14 | f(n) = f(0) + f(1) + ... + f(n-2) + f(n-1) ② 15 | 16 | ②-①: 17 | f(n) - f(n-1) = f(n-1) 18 | f(n) = 2f(n-1) 19 | ``` 20 | 21 | 所以 f(n) 是一个等比数列: 22 | ``` 23 | f(n) = 2^(n-1) 24 | ``` 25 | 26 | 27 | ```java 28 | /** 29 | * @author bingo 30 | * @since 2018/11/23 31 | */ 32 | 33 | public class Solution { 34 | /** 35 | * 青蛙跳台阶II 36 | * @param target 跳上的那一级台阶 37 | * @return 多少种跳法 38 | */ 39 | public int JumpFloorII(int target) { 40 | return (int) Math.pow(2, target - 1); 41 | } 42 | } 43 | ``` 44 | 45 | ### 测试用例 46 | 1. 功能测试(如输入 3、5、10 等); 47 | 2. 边界值测试(如输入 0、1、2); 48 | 3. 性能测试(输入较大的数字,如 40、50、100 等)。 -------------------------------------------------------------------------------- /docs/剑指offer/Java/10_03_JumpFloorII/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author bingo 3 | * @since 2018/11/23 4 | */ 5 | 6 | public class Solution { 7 | /** 8 | * 青蛙跳台阶II 9 | * 10 | * @param target 跳上的那一级台阶 11 | * @return 多少种跳法 12 | */ 13 | public int JumpFloorII(int target) { 14 | return (int) Math.pow(2, target - 1); 15 | } 16 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/10_04_RectCover/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author bingo 3 | * @since 2018/11/23 4 | */ 5 | 6 | public class Solution { 7 | /** 8 | * 矩形覆盖 9 | * 10 | * @param target 2*target大小的矩形 11 | * @return 多少种覆盖方法 12 | */ 13 | public int RectCover(int target) { 14 | if (target < 3) { 15 | return target; 16 | } 17 | int[] res = new int[target + 1]; 18 | res[1] = 1; 19 | res[2] = 2; 20 | for (int i = 3; i <= target; ++i) { 21 | res[i] = res[i - 1] + res[i - 2]; 22 | } 23 | return res[target]; 24 | } 25 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/15_NumberOf1InBinary/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author bingo 3 | * @since 2018/11/20 4 | */ 5 | 6 | public class Solution { 7 | /** 8 | * 计算整数的二进制表示里1的个数 9 | * 10 | * @param n 整数 11 | * @return 1的个数 12 | */ 13 | public int NumberOf1(int n) { 14 | int cnt = 0; 15 | while (n != 0) { 16 | n = (n - 1) & n; 17 | ++cnt; 18 | } 19 | return cnt; 20 | } 21 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/16_Power/README.md: -------------------------------------------------------------------------------- 1 | ## 数值的整数次方 2 | 3 | ### 题目描述 4 | 给定一个 `double` 类型的浮点数 `base` 和 `int` 类型的整数 `exponent`。求 `base`的 `exponent` 次方。 5 | 6 | ### 解法 7 | 注意判断值数是否小于 0。另外 0 的 0 次方没有意义,也需要考虑一下,看具体题目要求。 8 | 9 | ```java 10 | /** 11 | * @author bingo 12 | * @since 2018/11/20 13 | */ 14 | 15 | public class Solution { 16 | /** 17 | * 计算数值的整数次方 18 | * @param base 底数 19 | * @param exponent 指数 20 | * @return 数值的整数次方 21 | */ 22 | public double Power(double base, int exponent) { 23 | double result = 1.0; 24 | int n = Math.abs(exponent); 25 | for (int i = 0; i < n; ++i) { 26 | result *= base; 27 | } 28 | 29 | return exponent < 0 ? 1.0 / result : result; 30 | } 31 | } 32 | 33 | ``` 34 | 35 | ### 测试用例 36 | 1. 把底数和指数分别设为正数、负数和零。 -------------------------------------------------------------------------------- /docs/剑指offer/Java/16_Power/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author bingo 3 | * @since 2018/11/20 4 | */ 5 | 6 | public class Solution { 7 | /** 8 | * 计算数值的整数次方 9 | * 10 | * @param base 底数 11 | * @param exponent 指数 12 | * @return 数值的整数次方 13 | */ 14 | public double Power(double base, int exponent) { 15 | double result = 1.0; 16 | int n = Math.abs(exponent); 17 | for (int i = 0; i < n; ++i) { 18 | result *= base; 19 | } 20 | 21 | return exponent < 0 ? 1.0 / result : result; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /docs/剑指offer/Java/18_01_DeleteNodeInList/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author bingo 3 | * @since 2018/11/20 4 | */ 5 | 6 | public class Solution { 7 | 8 | class ListNode { 9 | int val; 10 | ListNode next; 11 | } 12 | 13 | /** 14 | * 删除链表的节点 15 | * 16 | * @param head 链表头节点 17 | * @param tobeDelete 要删除的节点 18 | */ 19 | public ListNode deleteNode(ListNode head, ListNode tobeDelete) { 20 | if (head == null || tobeDelete == null) { 21 | return head; 22 | } 23 | 24 | // 删除的不是尾节点 25 | if (tobeDelete.next != null) { 26 | tobeDelete.val = tobeDelete.next.val; 27 | tobeDelete.next = tobeDelete.next.next; 28 | } 29 | // 链表中仅有一个节点 30 | else if (head == tobeDelete) { 31 | head = null; 32 | } 33 | // 删除的是尾节点 34 | else { 35 | // 当head遍历到tobeDelete时,指向的是同一个引用。可以直接将tobeDelete = null即可 36 | tobeDelete = null; 37 | } 38 | 39 | return head; 40 | } 41 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/18_02_DeleteDuplicatedNode/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author bingo 3 | * @since 2018/11/21 4 | */ 5 | 6 | /* 7 | public class ListNode { 8 | int val; 9 | ListNode next = null; 10 | 11 | ListNode(int val) { 12 | this.val = val; 13 | } 14 | } 15 | */ 16 | public class Solution { 17 | /** 18 | * 删除链表重复的节点 19 | * @param pHead 链表头节点 20 | * @return 删除节点后的链表 21 | */ 22 | public ListNode deleteDuplication(ListNode pHead) { 23 | if (pHead == null || pHead.next == null) { 24 | return pHead; 25 | } 26 | 27 | if (pHead.val == pHead.next.val) { 28 | if (pHead.next.next == null) { 29 | return null; 30 | } 31 | if (pHead.next.next.val == pHead.val) { 32 | return deleteDuplication(pHead.next); 33 | } 34 | return deleteDuplication(pHead.next.next); 35 | } 36 | pHead.next = deleteDuplication(pHead.next); 37 | return pHead; 38 | } 39 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/20_NumericStrings/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author bingo 3 | * @since 2018/11/21 4 | */ 5 | 6 | public class Solution { 7 | /** 8 | * 判断是否是数字 9 | * 10 | * @param str 11 | * @return 12 | */ 13 | public boolean isNumeric(char[] str) { 14 | return str != null && str.length != 0 && new String(str).matches("[+-]?\\d*(\\.\\d+)?([eE][+-]?\\d+)?"); 15 | } 16 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/21_ReorderArray/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author bingo 5 | * @since 2018/11/21 6 | */ 7 | 8 | public class Solution { 9 | /** 10 | * 调整数组元素顺序,使得奇数元素位于偶数元素前面,且保证奇数和奇数,偶数和偶数之间的相对位置不变。 11 | * 12 | * @param array 数组 13 | */ 14 | public void reOrderArray(int[] array) { 15 | if (array == null || array.length < 2) { 16 | return; 17 | } 18 | 19 | int numsOfOdd = 0; 20 | for (int val : array) { 21 | if (val % 2 == 1) { 22 | ++numsOfOdd; 23 | } 24 | } 25 | int[] bak = Arrays.copyOf(array, array.length); 26 | int i = 0, j = numsOfOdd; 27 | for (int val : bak) { 28 | if (val % 2 == 1) { 29 | array[i++] = val; 30 | } else { 31 | array[j++] = val; 32 | } 33 | } 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/22_KthNodeFromEnd/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author bingo 3 | * @since 2018/11/21 4 | */ 5 | 6 | /* 7 | public class ListNode { 8 | int val; 9 | ListNode next = null; 10 | 11 | ListNode(int val) { 12 | this.val = val; 13 | } 14 | }*/ 15 | public class Solution { 16 | /** 17 | * 找出链表倒数第k个节点,k从1开始 18 | * @param head 链表头部 19 | * @param k 第k个节点 20 | * @return 倒数第k个节点 21 | */ 22 | public ListNode FindKthToTail(ListNode head,int k) { 23 | if (head == null || k < 1) { 24 | return null; 25 | } 26 | 27 | ListNode pre = head; 28 | for (int i = 0; i < k - 1; ++i) { 29 | if (pre.next != null) { 30 | pre = pre.next; 31 | } else { 32 | return null; 33 | } 34 | } 35 | 36 | ListNode cur = head; 37 | while (pre.next != null) { 38 | pre = pre.next; 39 | cur = cur.next; 40 | } 41 | return cur; 42 | } 43 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/24_ReverseList/Solution.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * @author bingo 4 | * @since 2018/11/22 5 | */ 6 | 7 | /* 8 | * public class ListNode { int val; ListNode next = null; 9 | * 10 | * ListNode(int val) { this.val = val; } } 11 | */ 12 | public class Solution { 13 | /** 14 | * 反转链表 15 | * 16 | * @param head 链表头部 17 | * @return 反转后的链表 18 | */ 19 | public ListNode ReverseList(ListNode head) { 20 | if (head == null || head.next == null) { 21 | return head; 22 | } 23 | 24 | ListNode dummy = new ListNode(-1); 25 | dummy.next = null; 26 | ListNode p1 = head; 27 | ListNode p2 = p1.next; 28 | while (p1 != null) { 29 | p1.next = dummy.next; 30 | dummy.next = p1; 31 | p1 = p2; 32 | if (p1 == null) { 33 | break; 34 | } 35 | p2 = p1.next; 36 | } 37 | 38 | return dummy.next; 39 | } 40 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/27_MirrorOfBinaryTree/Solution.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * @author bingo 4 | * @since 2018/11/22 5 | */ 6 | 7 | /* 8 | * public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = 9 | * null; 10 | * 11 | * public TreeNode(int val) { this.val = val; 12 | * 13 | * } 14 | * 15 | * } 16 | */ 17 | public class Solution { 18 | /** 19 | * 将二叉树转换为它的镜像 20 | * 21 | * @param root 二叉树的根结点 22 | */ 23 | public void Mirror(TreeNode root) { 24 | if (root == null || !hasChild(root)) { 25 | return; 26 | } 27 | 28 | TreeNode t = root.left; 29 | root.left = root.right; 30 | root.right = t; 31 | Mirror(root.left); 32 | Mirror(root.right); 33 | } 34 | 35 | private boolean hasChild(TreeNode root) { 36 | return root.left != null || root.right != null; 37 | } 38 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/28_SymmetricalBinaryTree/Solution.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * @author bingo 4 | * @since 2018/11/22 5 | */ 6 | 7 | /* 8 | * public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = 9 | * null; 10 | * 11 | * public TreeNode(int val) { this.val = val; 12 | * 13 | * } 14 | * 15 | * } 16 | */ 17 | public class Solution { 18 | /** 19 | * 判断是否是对称二叉树 20 | * 21 | * @param pRoot 二叉树的根结点 22 | * @return 是否为对称二叉树 23 | */ 24 | boolean isSymmetrical(TreeNode pRoot) { 25 | return isSymmetrical(pRoot, pRoot); 26 | } 27 | 28 | private boolean isSymmetrical(TreeNode pRoot1, TreeNode pRoot2) { 29 | if (pRoot1 == null && pRoot2 == null) { 30 | return true; 31 | } 32 | if (pRoot1 == null || pRoot2 == null) { 33 | return false; 34 | } 35 | if (pRoot1.val != pRoot2.val) { 36 | return false; 37 | } 38 | 39 | return isSymmetrical(pRoot1.left, pRoot2.right) && isSymmetrical(pRoot1.right, pRoot2.left); 40 | 41 | } 42 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/30_MinInStack/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Stack; 2 | 3 | /** 4 | * @author bingo 5 | * @since 2018/11/22 6 | */ 7 | 8 | public class Solution { 9 | 10 | private Stack stack1 = new Stack<>(); 11 | private Stack stack2 = new Stack<>(); 12 | 13 | /** 14 | * 压栈 15 | * 16 | * @param node 待压入的元素 17 | */ 18 | public void push(int node) { 19 | stack1.push(node); 20 | if (stack2.isEmpty() || stack2.peek() >= node) { 21 | stack2.push(node); 22 | } else { 23 | stack2.push(stack2.peek()); 24 | } 25 | } 26 | 27 | public void pop() { 28 | stack1.pop(); 29 | stack2.pop(); 30 | } 31 | 32 | public int top() { 33 | return stack2.peek(); 34 | } 35 | 36 | /** 37 | * O(1)获取栈中最小值 38 | * 39 | * @return 最小值 40 | */ 41 | public int min() { 42 | return stack2.peek(); 43 | } 44 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/31_StackPushPopOrder/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Stack; 2 | 3 | /** 4 | * @author bingo 5 | * @since 2018/11/22 6 | */ 7 | 8 | 9 | public class Solution { 10 | /** 11 | * 判断是否是弹出序列 12 | * 13 | * @param pushA 压栈序列 14 | * @param popA 弹栈序列 15 | * @return 是否是弹出序列 16 | */ 17 | public boolean IsPopOrder(int[] pushA,int[] popA) { 18 | if (pushA == null || popA == null || pushA.length != popA.length) { 19 | return false; 20 | } 21 | 22 | Stack stack = new Stack<>(); 23 | int i = 0; 24 | int n = pushA.length; 25 | boolean flag = false; 26 | for (int val : popA) { 27 | while (stack.isEmpty() || stack.peek() != val) { 28 | if (i >= n) { 29 | flag = true; 30 | break; 31 | } 32 | stack.push(pushA[i++]); 33 | } 34 | if (flag) { 35 | break; 36 | } 37 | stack.pop(); 38 | } 39 | 40 | return stack.isEmpty(); 41 | } 42 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/42_GreatestSumOfSubarrays/Solution.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * @author bingo 4 | * @since 2018/12/7 5 | */ 6 | 7 | public class Solution { 8 | /** 9 | * 求连续子数组的最大和 10 | * 11 | * @param array 数组 12 | * @return 最大和 13 | */ 14 | public int FindGreatestSumOfSubArray(int[] array) { 15 | int n = array.length; 16 | int[] res = new int[n]; 17 | res[0] = array[0]; 18 | int max = res[0]; 19 | for (int i = 1; i < n; ++i) { 20 | res[i] = res[i - 1] > 0 ? res[i - 1] + array[i] : array[i]; 21 | max = Math.max(max, res[i]); 22 | } 23 | return max; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /docs/剑指offer/Java/45_SortArrayForMinNumber/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author bingo 5 | * @since 2018/12/8 6 | */ 7 | 8 | class Solution { 9 | 10 | /** 11 | * 打印数组元素组成的最小的数字 12 | * 13 | * @param nums 数组 14 | * @return 最小的数字 15 | */ 16 | public String printMinNumber(int[] nums) { 17 | if (nums == null || nums.length == 0) { 18 | return ""; 19 | } 20 | int n = nums.length; 21 | String[] strNums = new String[n]; 22 | for (int i = 0; i < n; ++i) { 23 | strNums[i] = String.valueOf(nums[i]); 24 | } 25 | 26 | Arrays.sort(strNums, (o1, o2) -> (o1 + o2).compareTo(o2 + o1)); 27 | 28 | StringBuilder sb = new StringBuilder(); 29 | for (String str : strNums) { 30 | sb.append(str); 31 | } 32 | return sb.toString(); 33 | } 34 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/46_TranslateNumbersToStrings/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author bingo 3 | * @since 2018/12/8 4 | */ 5 | 6 | class Solution { 7 | /** 8 | * 获取翻译字符串的方法个数 9 | * 10 | * @param s 字符串 11 | * @return 个数 12 | */ 13 | public int getTranslationCount(String s) { 14 | if (s == null || s.length() < 2) { 15 | return 1; 16 | } 17 | char[] chars = s.toCharArray(); 18 | int n = chars.length; 19 | int[] res = new int[n]; 20 | res[0] = 1; 21 | res[1] = isInRange(chars[0], chars[1]) ? 2 : 1; 22 | for (int i = 2; i < n; ++i) { 23 | res[i] = res[i - 1] + (isInRange(chars[i - 1], chars[i]) ? res[i - 2] : 0); 24 | } 25 | return res[n - 1]; 26 | } 27 | 28 | private boolean isInRange(char a, char b) { 29 | int s = (a - '0') * 10 + (b - '0'); 30 | return s >= 10 && s <= 25; 31 | } 32 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/47_MaxValueOfGifts/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author bingo 3 | * @since 2018/12/8 4 | */ 5 | 6 | class Solution { 7 | /** 8 | * 获取礼物的最大价值 9 | * 10 | * @param grid 数组 11 | * @return 最大价值 12 | */ 13 | public int getMaxValue(int[][] grid) { 14 | if (grid == null || grid.length == 0) { 15 | return 0; 16 | } 17 | int m = grid.length; 18 | int n = grid[0].length; 19 | int[][] res = new int[m][n]; 20 | res[0][0] = grid[0][0]; 21 | for (int j = 1; j < n; ++j) { 22 | res[0][j] = res[0][j - 1] + grid[0][j]; 23 | } 24 | for (int i = 1; i < m; ++i) { 25 | res[i][0] = res[i - 1][0] + grid[i][0]; 26 | } 27 | for (int i = 1; i < m; ++i) { 28 | for (int j = 1; j < n; ++j) { 29 | res[i][j] = Math.max(res[i - 1][j], res[i][j - 1]) + grid[i][j]; 30 | } 31 | } 32 | return res[m - 1][n - 1]; 33 | } 34 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/48_LongestSubstringWithoutDup/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author bingo 3 | * @since 2018/12/8 4 | */ 5 | 6 | class Solution { 7 | /** 8 | * 最长不含重复字符的子字符串 9 | * 10 | * @param s 字符串 11 | * @return 最长不重复字符子串 12 | */ 13 | public int longestSubstringWithoutDuplication(String s) { 14 | if (s == null || s.length() == 0) { 15 | return 0; 16 | } 17 | char[] chars = s.toCharArray(); 18 | int[] t = new int[26]; 19 | for (int i = 0; i < 26; ++i) { 20 | t[i] = -1; 21 | } 22 | t[chars[0] - 'a'] = 0; 23 | int n = chars.length; 24 | int[] res = new int[n]; 25 | res[0] = 1; 26 | int max = res[0]; 27 | for (int i = 1; i < n; ++i) { 28 | int index = t[chars[i] - 'a']; 29 | int d = i - index; 30 | res[i] = (index == -1 || d > res[i - 1]) ? res[i - 1] + 1 : d; 31 | 32 | t[chars[i] - 'a'] = i; 33 | max = Math.max(max, res[i]); 34 | } 35 | return max; 36 | } 37 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/50_01_FirstNotRepeatingChar/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author mcrwayfun 3 | * @version v1.0 4 | * @date Created in 2019/01/24 5 | * @description 6 | */ 7 | public class Solution { 8 | 9 | public int FirstNotRepeatingChar(String str) { 10 | 11 | if (str == null || str.length() == 0) { 12 | return -1; 13 | } 14 | 15 | Map characterMap = new HashMap<>(); 16 | // hashMap is HashTable,search cost O(1) 17 | for (int i = 0; i < str.length(); i++) { 18 | characterMap.put(str.charAt(i), characterMap.getOrDefault(str.charAt(i), 0) + 1); 19 | } 20 | 21 | for (int i = 0; i < str.length(); i++) { 22 | if (characterMap.get(str.charAt(i)) == 1) { 23 | return i; 24 | } 25 | } 26 | 27 | return -1; 28 | } 29 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/50_02_FristCharacterInStream/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author mcrwayfun 3 | * @version v1.0 4 | * @date Created in 2019/01/25 5 | * @description 6 | */ 7 | public class Solution { 8 | 9 | private StringBuilder res = new StringBuilder(); 10 | private Map characterMap = new HashMap<>(); 11 | 12 | // Insert one char from stringstream 13 | public void Insert(char ch) { 14 | res.append(ch); 15 | characterMap.put(ch, characterMap.getOrDefault(ch, 0) + 1); 16 | } 17 | 18 | // return the first appearence once char in current stringstream 19 | public char FirstAppearingOnce() { 20 | 21 | for (char c : res.toString().toCharArray()) { 22 | if (characterMap.get(c) == 1) { 23 | return c; 24 | } 25 | } 26 | 27 | return '#'; 28 | } 29 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/53_02_MissingNumber/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author bingo 3 | * @since 2018/12/8 4 | */ 5 | 6 | class Solution { 7 | /** 8 | * 获取0~n-1缺失的数字 9 | * 10 | * @param nums 数组 11 | * @return 缺失的数字 12 | */ 13 | public int getMissingNumber(int[] nums) { 14 | if (nums == null || nums.length == 0) { 15 | return 0; 16 | } 17 | int n = nums.length; 18 | int start = 0, end = n - 1; 19 | while (start <= end) { 20 | int mid = start + ((end - start) >> 1); 21 | if (nums[mid] != mid) { 22 | if (mid == 0 || nums[mid - 1] == mid - 1) { 23 | return mid; 24 | } 25 | end = mid - 1; 26 | } else { 27 | start = mid + 1; 28 | } 29 | } 30 | return start == n ? n : -1; 31 | 32 | } 33 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/53_03_IntegerIdenticalToIndex/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author bingo 3 | * @since 2018/12/10 4 | */ 5 | 6 | class Solution { 7 | /** 8 | * 找出单调递增数组中数值和下标相等的元素 9 | * 10 | * @param nums 数组 11 | * @return 数值与下标相等的元素 12 | */ 13 | public int getNumberSameAsIndex(int[] nums) { 14 | if (nums == null || nums.length == 0) { 15 | return -1; 16 | } 17 | int start = 0, end = nums.length - 1; 18 | while (start <= end) { 19 | int mid = start + ((end - start) >> 1); 20 | if (nums[mid] == mid) { 21 | return mid; 22 | } 23 | if (nums[mid] < mid) { 24 | start = mid + 1; 25 | } else { 26 | end = mid - 1; 27 | } 28 | } 29 | return -1; 30 | } 31 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/54_KthNodeInBST/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author mcrwayfun 3 | * @version 1.0 4 | * @description 5 | * @date Created in 2019/1/28 6 | */ 7 | class Solution { 8 | 9 | private int count = 0; 10 | 11 | public TreeNode KthNode(TreeNode pRoot, int k) { 12 | 13 | if (pRoot == null || k == 0) { 14 | return null; 15 | } 16 | 17 | // 左递归 18 | TreeNode retNode = KthNode(pRoot.left, k); 19 | 20 | if (retNode != null) { 21 | return retNode; 22 | } 23 | 24 | // 符合条件则返回 25 | count++; 26 | if (count == k) { 27 | return pRoot; 28 | } 29 | 30 | // 右递归 31 | retNode = KthNode(pRoot.right, k); 32 | if (retNode != null) { 33 | return retNode; 34 | } 35 | 36 | return null; 37 | } 38 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/55_01_TreeDepth/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author bingo 3 | * @since 2018/12/10 4 | */ 5 | 6 | /** 7 | * Definition for a binary tree node. 8 | * public class TreeNode { 9 | * int val; 10 | * TreeNode left; 11 | * TreeNode right; 12 | * TreeNode(int x) { val = x; } 13 | * } 14 | */ 15 | class Solution { 16 | /** 17 | * 求二叉树的深度 18 | * 19 | * @param root 二叉树根结点 20 | * @return 深度 21 | */ 22 | public int treeDepth(TreeNode root) { 23 | if (root == null) { 24 | return 0; 25 | } 26 | int lDepth = treeDepth(root.left); 27 | int rDepth = treeDepth(root.right); 28 | return 1 + Math.max(lDepth, rDepth); 29 | } 30 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/56_02_NumberAppearingOnce/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author bingo 3 | * @since 2018/12/10 4 | */ 5 | 6 | class Solution { 7 | /** 8 | * 找出数组中只出现一次的数字,其它数字都出现三次 9 | * 10 | * @param nums 数字 11 | * @return 只出现一次的数字 12 | */ 13 | public int findNumberAppearingOnce(int[] nums) { 14 | if (nums == null || nums.length == 0) { 15 | return 0; 16 | } 17 | int[] bits = new int[32]; 18 | int n = nums.length; 19 | for (int i = 0; i < n; ++i) { 20 | int val = nums[i]; 21 | for (int j = 0; j < 32; ++j) { 22 | bits[j] += (val & 1); 23 | val = val >> 1; 24 | } 25 | } 26 | int res = 0; 27 | for (int i = 0; i < 32; ++i) { 28 | if (bits[i] % 3 != 0) { 29 | res += Math.pow(2, i); 30 | } 31 | } 32 | return res; 33 | } 34 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/57_01_TwoNumbersWithSum/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author mcrwayfun 3 | * @version v1.0 4 | * @date Created in 2019/02/02 5 | * @description 6 | */ 7 | public class Solution { 8 | 9 | public ArrayList FindNumbersWithSum(int[] array, int sum) { 10 | 11 | ArrayList reList = new ArrayList<>(); 12 | 13 | if (array == null || array.length < 2 || sum <= array[0]) { 14 | return reList; 15 | } 16 | 17 | int start = 0; 18 | int end = array.length - 1; 19 | 20 | while (start < end) { 21 | 22 | int curSum = array[start] + array[end]; 23 | if (curSum == sum) { 24 | reList.add(array[start]); 25 | reList.add(array[end]); 26 | return reList; 27 | } else if (curSum < sum) { 28 | start++; 29 | } else { 30 | end--; 31 | } 32 | } 33 | 34 | // 查无 35 | return reList; 36 | } 37 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/58_01_ReverseWordsInSentence/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author bingo 3 | * @since 2018/12/12 4 | */ 5 | 6 | class Solution { 7 | /** 8 | * 翻转单词 9 | * 10 | * @param s 字符串 11 | * @return 翻转后的字符串 12 | */ 13 | public String reverseWords(String s) { 14 | if (s == null || s.length() == 0 || s.trim().equals("")) { 15 | return s; 16 | } 17 | 18 | String[] arr = s.split(" "); 19 | int p = 0, q = arr.length - 1; 20 | while (p < q) { 21 | swap(arr, p++, q--); 22 | } 23 | return String.join(" ", arr); 24 | } 25 | 26 | private void swap(String[] arr, int p, int q) { 27 | String t = arr[p]; 28 | arr[p] = arr[q]; 29 | arr[q] = t; 30 | } 31 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/58_02_LeftRotateString/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author bingo 3 | * @since 2018/12/12 4 | */ 5 | 6 | class Solution { 7 | 8 | /** 9 | * 左旋转字符串 10 | * 11 | * @param str 字符串 12 | * @param n 左旋的位数 13 | * @return 旋转后的字符串 14 | */ 15 | public String leftRotateString(String str, int n) { 16 | if (str == null || n < 1 || n > str.length()) { 17 | return str; 18 | } 19 | char[] chars = str.toCharArray(); 20 | int len = chars.length; 21 | reverse(chars, 0, n - 1); 22 | reverse(chars, n, len - 1); 23 | reverse(chars, 0, len - 1); 24 | return new String(chars); 25 | } 26 | 27 | private void reverse(char[] chars, int p, int q) { 28 | while (p < q) { 29 | swap(chars, p++, q--); 30 | } 31 | } 32 | 33 | private void swap(char[] chars, int p, int q) { 34 | char t = chars[p]; 35 | chars[p] = chars[q]; 36 | chars[q] = t; 37 | } 38 | } -------------------------------------------------------------------------------- /docs/剑指offer/Java/61_ContinousCards/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author bingo 5 | * @since 2018/12/12 6 | */ 7 | 8 | class Solution { 9 | 10 | /** 11 | * 判断是否是连续的数字 12 | * 13 | * @param numbers 数组 14 | * @return 是否是顺子 15 | */ 16 | public boolean isContinuous(int[] numbers) { 17 | if (numbers == null || numbers.length == 0) { 18 | return false; 19 | } 20 | int zeroCount = 0; 21 | Arrays.sort(numbers); 22 | for (int e : numbers) { 23 | if (e > 0) { 24 | break; 25 | } 26 | ++zeroCount; 27 | } 28 | 29 | int p = zeroCount, q = p + 1, n = numbers.length; 30 | int gap = 0; 31 | while (q < n) { 32 | if (numbers[p] == numbers[q]) { 33 | return false; 34 | } 35 | gap += (numbers[q] - numbers[p] - 1); 36 | p = q; 37 | ++q; 38 | } 39 | return gap <= zeroCount; 40 | 41 | } 42 | } -------------------------------------------------------------------------------- /docs/剑指offer/JavaScript/01-二维数组中的查找.js: -------------------------------------------------------------------------------- 1 | function Find(target, array) 2 | { 3 | // write code here 4 | const num_row = array.length; 5 | for(let i=0; i -1) { 7 | return true 8 | } 9 | } 10 | } 11 | module.exports = { 12 | Find : Find 13 | }; 14 | -------------------------------------------------------------------------------- /docs/剑指offer/JavaScript/02-替换空格.js: -------------------------------------------------------------------------------- 1 | function replaceSpace(str) 2 | { 3 | // write code here 4 | let strArr = str.split(''); 5 | return strArr.reduce((pre, next) => { 6 | return pre + (next === ' ' ? '%20' : next) 7 | }, ''); 8 | } 9 | module.exports = { 10 | replaceSpace : replaceSpace 11 | }; 12 | -------------------------------------------------------------------------------- /docs/剑指offer/JavaScript/03-从头到尾打印链表.js: -------------------------------------------------------------------------------- 1 | /*function ListNode(x){ 2 | this.val = x; 3 | this.next = null; 4 | }*/ 5 | function printListFromTailToHead(head) 6 | { 7 | // write code here 8 | let arr = []; 9 | if (!head) { 10 | return arr; 11 | } 12 | while(head) { 13 | arr.unshift(head.val) 14 | head=head.next 15 | } 16 | return arr; 17 | } 18 | module.exports = { 19 | printListFromTailToHead : printListFromTailToHead 20 | }; 21 | -------------------------------------------------------------------------------- /docs/剑指offer/JavaScript/05-用两个栈实现队列.js: -------------------------------------------------------------------------------- 1 | let stack1 = []; 2 | let stack2 = []; 3 | function push(node) 4 | { 5 | // write code here 6 | stack1.push(node) 7 | } 8 | function pop() 9 | { 10 | // write code here 11 | if(stack2.length === 0) { 12 | while (stack1.length!==0) { 13 | stack2.push(stack1.pop()) 14 | } 15 | } 16 | return stack2.pop() 17 | } 18 | module.exports = { 19 | push : push, 20 | pop : pop 21 | }; 22 | -------------------------------------------------------------------------------- /docs/剑指offer/JavaScript/06-旋转数组的最小数字.js: -------------------------------------------------------------------------------- 1 | function minNumberInRotateArray(rotateArray) 2 | { 3 | // write code here 4 | if (rotateArray.length === 0) { 5 | return 0; 6 | } else { 7 | return Math.min.apply(null, rotateArray) 8 | } 9 | } 10 | module.exports = { 11 | minNumberInRotateArray : minNumberInRotateArray 12 | }; 13 | -------------------------------------------------------------------------------- /docs/剑指offer/JavaScript/07-斐波那契数列.js: -------------------------------------------------------------------------------- 1 | function Fibonacci(n) 2 | { 3 | // write code here 4 | if (n===0 || n===1) { 5 | return n; 6 | } 7 | let n1 = 0, n2=1, sum=1; 8 | for(let i=2;i<=n;i++) { 9 | sum = n1+n2; 10 | n1=n2; 11 | n2=sum; 12 | } 13 | return sum; 14 | } 15 | module.exports = { 16 | Fibonacci : Fibonacci 17 | }; 18 | -------------------------------------------------------------------------------- /docs/剑指offer/JavaScript/08-跳台阶.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * @param {number} number 4 | * 思路: n级台阶只能由第n-1和第n-2级台阶跳上来,得到动态规划方程 5 | */ 6 | 7 | function jumpFloor(number) 8 | { 9 | // write code here 10 | if(number===0) return 0; 11 | if(number==1)return 1; 12 | if(number===2) return 2; 13 | let a=1, b=2, sum=0; 14 | for(let i=3; i<= number; i++) { 15 | sum=a+b; 16 | a=b; 17 | b=sum; 18 | } 19 | return sum; 20 | } 21 | module.exports = { 22 | jumpFloor : jumpFloor 23 | }; 24 | -------------------------------------------------------------------------------- /docs/剑指offer/JavaScript/09-变态跳台阶.js: -------------------------------------------------------------------------------- 1 | function sumArr(arr) { 2 | return arr.reduce((pre, next) => pre+next) 3 | } 4 | 5 | function jumpFloorII(number) 6 | { 7 | // write code here 8 | if(number===0) return 0; 9 | if(number==1)return 1; 10 | if(number===2) return 2; 11 | let ans = [1,2]; 12 | for(let i=3;i<=number;i++) { 13 | // 额外加一是可以直接跳到第n级台阶 14 | ans.push(sumArr(ans)+1); 15 | } 16 | return ans[ans.length -1]; 17 | } 18 | module.exports = { 19 | jumpFloorII : jumpFloorII 20 | }; 21 | -------------------------------------------------------------------------------- /docs/剑指offer/JavaScript/README.md: -------------------------------------------------------------------------------- 1 | # Jianzhi-offer 2 | 3 | -------------------------------------------------------------------------------- /docs/剑指offer/Python/01-二维数组中的查找.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | # array 二维列表 4 | def Find(self, target, array): 5 | # write code here 6 | if array == []: 7 | return False 8 | num_row = len(array) 9 | num_col = len(array[0]) 10 | 11 | i = num_col - 1 12 | j = 0 13 | 14 | while i >= 0 and j < num_row: 15 | if array[j][i] > target: 16 | i -= 1 17 | elif array[j][i] < target: 18 | j += 1 19 | else: 20 | return True -------------------------------------------------------------------------------- /docs/剑指offer/Python/02-替换空格.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # s 源字符串 3 | def replaceSpace(self, s): 4 | # write code here 5 | if not isinstance(s, str) or len(s) <= 0 or s == None: 6 | return '' 7 | spaceNum = 0 8 | for i in s: 9 | if i == " ": 10 | spaceNum += 1 11 | 12 | newStrLen = len(s) + spaceNum * 2 13 | newStr = newStrLen * [None] 14 | indexOfOriginal, indexOfNew = len(s) - 1, newStrLen - 1 15 | while indexOfNew >= 0 and indexOfOriginal <= indexOfNew: 16 | if s[indexOfOriginal] == ' ': 17 | newStr[indexOfNew - 2: indexOfNew + 1] = ['%', '2', '0'] 18 | indexOfNew -= 3 19 | indexOfOriginal -= 1 20 | else: 21 | newStr[indexOfNew] = s[indexOfOriginal] 22 | indexOfNew -= 1 23 | indexOfOriginal -= 1 24 | return ''.join(newStr) -------------------------------------------------------------------------------- /docs/剑指offer/Python/03-从尾到头打印链表.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class ListNode: 3 | def __init__(self, x): 4 | self.val = x 5 | self.next = None 6 | 7 | class Solution: 8 | # 返回从尾部到头部的列表值序列,例如[1,2,3] 9 | def printListFromTailToHead(self, listNode): 10 | # write code here 11 | if not listNode: 12 | return [] 13 | 14 | result =[] 15 | 16 | while listNode: 17 | result.insert(0, listNode.val) 18 | listNode = listNode.next 19 | return result -------------------------------------------------------------------------------- /docs/剑指offer/Python/04-重建二叉树.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class TreeNode: 3 | def __init__(self, x): 4 | self.val = x 5 | self.left = None 6 | self.right = None 7 | class Solution: 8 | # 返回构造的TreeNode根节点 9 | def reConstructBinaryTree(self, pre, tin): 10 | # write code here 11 | if not pre and not tin: 12 | return None 13 | root = TreeNode(pre[0]) 14 | if set(pre) != set(tin): 15 | return None 16 | i = tin.index(pre[0]) 17 | root.left = self.reConstructBinaryTree(pre[1:i+1],tin[:i]) 18 | root.right = self.reConstructBinaryTree(pre[i+1:], tin[i+1:]) 19 | return root -------------------------------------------------------------------------------- /docs/剑指offer/Python/05-用两个栈实现队列.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def __init__(self): 4 | self.stack1 = [] 5 | self.stack2 = [] 6 | 7 | def push(self, node): 8 | # write code here 9 | self.stack1.append(node) 10 | 11 | def pop(self): 12 | # return xx 13 | if len(self.stack1) == 0 and len(self.stack2) == 0: 14 | return 15 | elif len(self.stack2) == 0: 16 | while len(self.stack1) > 0: 17 | self.stack2.append(self.stack1.pop()) 18 | 19 | return self.stack2.pop() -------------------------------------------------------------------------------- /docs/剑指offer/Python/06-旋转数组的最小数字.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def minNumberInRotateArray(self, rotateArray): 4 | # write code here 5 | if len(rotateArray) == 0: 6 | return 0 7 | front = 0 8 | rear = len(rotateArray) - 1 9 | minVal = rotateArray[0] 10 | 11 | if rotateArray[front] < rotateArray[rear]: 12 | return rotateArray[front] 13 | else: 14 | while (rear - front) > 1: 15 | mid = (front + rear) // 2 16 | if rotateArray[mid] >= rotateArray[front]: 17 | front = mid 18 | elif rotateArray[mid] <= rotateArray[rear]: 19 | rear = mid 20 | elif rotateArray[front] == rotateArray[rear] == rotateArray[mid]: 21 | for i in range(1, len(rotateArray)): 22 | if rotateArray[i] < minVal: 23 | minVal = rotateArray[i] 24 | rear = i 25 | minVal = rotateArray[rear] 26 | return minVal -------------------------------------------------------------------------------- /docs/剑指offer/Python/07-斐波那契数列.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def Fibonacci(self, n): 4 | # write code here 5 | tempArray = [0,1] 6 | if n >= 2: 7 | for i in range(2, n+1): 8 | tempArray[i%2] = tempArray[0] + tempArray[1] 9 | return tempArray[n%2] -------------------------------------------------------------------------------- /docs/剑指offer/Python/08-跳台阶.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def jumpFloor(self, number): 4 | # write code here 5 | tempArray = [1, 2] 6 | if number >= 3: 7 | for i in range(3, number+1): 8 | tempArray[(i+1)%2] = tempArray[0] + tempArray[1] 9 | return tempArray[(number + 1)%2] -------------------------------------------------------------------------------- /docs/剑指offer/Python/09-变态跳台阶.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def jumpFloorII(self, number): 4 | # write code here 5 | ans = 1 6 | if number >= 2: 7 | for i in range(number-1): 8 | ans = ans * 2 9 | return ans -------------------------------------------------------------------------------- /docs/剑指offer/Python/10-矩形覆盖.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def rectCover(self, number): 4 | # write code here 5 | if number == 0: 6 | return 0 7 | tempArray = [1,2] 8 | if number >= 3: 9 | for i in range(3,number+1): 10 | tempArray[(i+1)%2] = tempArray[0] + tempArray[1] 11 | return tempArray[(number+1)%2] -------------------------------------------------------------------------------- /docs/剑指offer/Python/11-二进制中1的个数.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def NumberOf1(self, n): 3 | # write code here 4 | count = 0 5 | if n < 0: 6 | n = n & 0xffffffff 7 | while n != 0: 8 | count += 1 9 | n = (n - 1) & n 10 | return count -------------------------------------------------------------------------------- /docs/剑指offer/Python/12-数值的整数次方.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def Power(self, base, exponent): 3 | # write code here 4 | try: 5 | ret = self.power_value(base, abs(exponent)) 6 | if exponent < 0: 7 | return 1.0 / ret 8 | 9 | except ZeroDivisionError: 10 | print('Error: base is zero') 11 | else: 12 | return ret 13 | 14 | def power_value(self,base, exponent): 15 | if exponent == 0: 16 | return 1 17 | if exponent == 1: 18 | return base 19 | ret = self.power_value(base, exponent >> 1) 20 | ret *= ret 21 | if exponent & 1 == 1: 22 | ret *= base 23 | return ret -------------------------------------------------------------------------------- /docs/剑指offer/Python/13-调整数组顺序使奇数位于偶数前面.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reOrderArray(self, array): 3 | # write code here 4 | return sorted(array,key=lambda c:c%2,reverse=True) -------------------------------------------------------------------------------- /docs/剑指offer/Python/14-链表中倒数第k个结点.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # class ListNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.next = None 6 | 7 | class Solution: 8 | def FindKthToTail(self, head, k): 9 | # write code here 10 | if head == None or k <= 0: 11 | return None 12 | 13 | pAhead = head 14 | pBhead = None 15 | 16 | for i in range(k-1): 17 | if pAhead.next != None: 18 | pAhead = pAhead.next 19 | else: 20 | return None 21 | 22 | pBhead = head 23 | while pAhead.next != None: 24 | pAhead = pAhead.next 25 | pBhead = pBhead.next 26 | return pBhead -------------------------------------------------------------------------------- /docs/剑指offer/Python/15-反转链表.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # class ListNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.next = None 6 | class Solution: 7 | # 返回ListNode 8 | def ReverseList(self, pHead): 9 | # write code here 10 | pReversedHead = None 11 | pNode = pHead 12 | pPrev = None 13 | while pNode != None: 14 | pNext = pNode.next 15 | if pNext == None: 16 | pReversedHead = pNode 17 | 18 | pNode.next = pPrev 19 | pPrev = pNode 20 | pNode = pNext 21 | return pReversedHead -------------------------------------------------------------------------------- /docs/剑指offer/Python/16-合并两个排序的链表.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # class ListNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.next = None 6 | class Solution: 7 | # 返回合并后列表 8 | def Merge(self, pHead1, pHead2): 9 | # write code here 10 | if pHead1 == None: 11 | return pHead2 12 | if pHead2 == None: 13 | return pHead1 14 | 15 | pMergeHead = None 16 | if pHead1.val < pHead2.val: 17 | pMergeHead = pHead1 18 | pMergeHead.next = self.Merge(pHead1.next,pHead2) 19 | else: 20 | pMergeHead = pHead2 21 | pMergeHead.next = self.Merge(pHead1,pHead2.next) 22 | return pMergeHead -------------------------------------------------------------------------------- /docs/剑指offer/Python/17-树的子结构.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # class TreeNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | class Solution: 8 | def HasSubtree(self, pRoot1, pRoot2): 9 | # write code here 10 | result = False 11 | if pRoot1 != None and pRoot2 != None: 12 | if pRoot1.val == pRoot2.val: 13 | result = self.DoesTree1haveTree2(pRoot1,pRoot2) 14 | if not result: 15 | result = self.HasSubtree(pRoot1.left,pRoot2) 16 | if not result: 17 | result = self.HasSubtree(pRoot1.right,pRoot2) 18 | 19 | return result 20 | 21 | def DoesTree1haveTree2(self,pRoot1,pRoot2): 22 | if pRoot2 == None: 23 | return True 24 | if pRoot1 == None: 25 | return False 26 | if pRoot1.val != pRoot2.val: 27 | return False 28 | 29 | return self.DoesTree1haveTree2(pRoot1.left, pRoot2.left) and self.DoesTree1haveTree2(pRoot1.right, pRoot2.right) -------------------------------------------------------------------------------- /docs/剑指offer/Python/18-二叉树的镜像.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # class TreeNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | class Solution: 8 | # 返回镜像树的根节点 9 | def Mirror(self, root): 10 | # write code here 11 | if root == None: 12 | return 13 | if root.left == None and root.right == None: 14 | return root 15 | 16 | ptemp = root.left 17 | root.left = root.right 18 | root.right = ptemp 19 | 20 | self.Mirror(root.left) 21 | self.Mirror(root.right) -------------------------------------------------------------------------------- /docs/剑指offer/Python/20-包含min函数的栈.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def __init__(self): 4 | self.stack = [] 5 | self.minStack = [] 6 | 7 | def push(self, node): 8 | # write code here 9 | self.stack.append(node) 10 | if self.minStack == [] or node < self.min(): 11 | self.minStack.append(node) 12 | else: 13 | temp = self.min() 14 | self.minStack.append(temp) 15 | 16 | def pop(self): 17 | # write code here 18 | if self.stack == None or self.minStack == None: 19 | return None 20 | self.minStack.pop() 21 | self.stack.pop() 22 | 23 | def top(self): 24 | # write code here 25 | return self.stack[-1] 26 | 27 | def min(self): 28 | # write code here 29 | return self.minStack[-1] -------------------------------------------------------------------------------- /docs/剑指offer/Python/21-栈的压入、弹出序列.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def IsPopOrder(self, pushV, popV): 4 | # write code here 5 | if pushV == [] or popV == []: 6 | return False 7 | 8 | stack = [] 9 | for i in pushV: 10 | stack.append(i) 11 | while len(stack) and stack[-1] == popV[0]: 12 | stack.pop() 13 | popV.pop(0) 14 | 15 | if len(stack): 16 | return False 17 | else: 18 | return True -------------------------------------------------------------------------------- /docs/剑指offer/Python/22-从上往下打印二叉树.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # class TreeNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | class Solution: 8 | # 返回从上到下每个节点值列表,例:[1,2,3] 9 | def PrintFromTopToBottom(self, root): 10 | # write code here 11 | if root is None: 12 | return [] 13 | queue = [] 14 | result = [] 15 | 16 | queue.append(root) 17 | while len(queue) > 0: 18 | currentRoot = queue.pop(0) 19 | result.append(currentRoot.val) 20 | if currentRoot.left: 21 | queue.append(currentRoot.left) 22 | if currentRoot.right: 23 | queue.append(currentRoot.right) 24 | 25 | return result -------------------------------------------------------------------------------- /docs/剑指offer/Python/23-二叉搜索树的后序遍历序列.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def VerifySquenceOfBST(self, sequence): 4 | # write code here 5 | if sequence == []: 6 | return False 7 | 8 | length = len(sequence) 9 | root = sequence[-1] 10 | 11 | for i in range(length): 12 | if sequence[i] > root: 13 | break 14 | 15 | for j in range(i, length): 16 | if sequence[j] < root: 17 | return False 18 | 19 | left = True 20 | if i > 0: 21 | left = self.VerifySquenceOfBST(sequence[:i]) 22 | right = True 23 | if j < length - 1: 24 | right = self.VerifySquenceOfBST(sequence[i:length-1]) 25 | 26 | return left and right -------------------------------------------------------------------------------- /docs/剑指offer/Python/24-二叉树中和为某一值的路径.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # class TreeNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | class Solution: 8 | # 返回二维列表,内部每个列表表示找到的路径 9 | def FindPath(self, root, expectNumber): 10 | # write code here 11 | if not root or root.val > expectNumber: 12 | return [] 13 | 14 | if not root.left and not root.right and root.val == expectNumber: 15 | return [[root.val]] 16 | else: 17 | expectNumber -= root.val 18 | left = self.FindPath(root.left,expectNumber) 19 | right = self.FindPath(root.right,expectNumber) 20 | 21 | result = [[root.val]+i for i in left] 22 | for i in right: 23 | result.append([root.val]+i) 24 | 25 | return sorted(result, key=lambda x:-len(x)) -------------------------------------------------------------------------------- /docs/剑指offer/Python/27-字符串的排列.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def Permutation(self, ss): 4 | # write code here 5 | if not ss: 6 | return [] 7 | if len(ss) == 1: 8 | return list(ss) 9 | 10 | charList = list(ss) 11 | charList.sort() 12 | pStr = [] 13 | for i in range(0,len(charList)): 14 | if i > 0 and charList[i] == charList[i-1]: 15 | continue 16 | temp = self.Permutation(''.join(charList[:i])+''.join(charList[i+1:])) 17 | for j in temp: 18 | pStr.append(charList[i] + j) 19 | 20 | return pStr -------------------------------------------------------------------------------- /docs/剑指offer/Python/28-数组中出现次数超过一半的数字.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def MoreThanHalfNum_Solution(self, numbers): 3 | # write code here 4 | length = len(numbers) 5 | if not numbers: 6 | return 0 7 | result = numbers[0] 8 | times = 1 9 | 10 | for i in range(1,length): 11 | if times == 0: 12 | result = numbers[i] 13 | times = 1 14 | elif numbers[i] == result: 15 | times += 1 16 | else: 17 | times -= 1 18 | 19 | if not self.CheckNoreThanHalf(numbers,length,result): 20 | return 0 21 | return result 22 | 23 | def CheckNoreThanHalf(self, numbers,length,number): 24 | times = 0 25 | for i in range(length): 26 | if numbers[i] == number: 27 | times += 1 28 | 29 | if times*2 <= length: 30 | return False 31 | return True -------------------------------------------------------------------------------- /docs/剑指offer/Python/29-最小的k个数.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def GetLeastNumbers_Solution(self, tinput, k): 4 | # write code here 5 | if not tinput or k > len(tinput): 6 | return [] 7 | tinput = self.quick_sort(tinput) 8 | return tinput[:k] 9 | 10 | def quick_sort(self,lst): 11 | if not lst: 12 | return [] 13 | pivot = lst[0] 14 | left = self.quick_sort([x for x in lst[1: ] if x < pivot]) 15 | right = self.quick_sort([x for x in lst[1: ] if x >= pivot]) 16 | 17 | return left + [pivot] + right -------------------------------------------------------------------------------- /docs/剑指offer/Python/30-连续数组的最大和.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def FindGreatestSumOfSubArray(self, array): 4 | # write code here 5 | if not array: 6 | return 0 7 | 8 | cur_sum = 0 9 | max_sum = array[0] 10 | 11 | for i in range(len(array)): 12 | if cur_sum <= 0: 13 | cur_sum = array[i] 14 | else: 15 | cur_sum += array[i] 16 | 17 | if cur_sum > max_sum: 18 | max_sum = cur_sum 19 | 20 | return max_sum -------------------------------------------------------------------------------- /docs/剑指offer/Python/31-整数中1出现的次数.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def NumberOf1Between1AndN_Solution(self, n): 3 | # write code here 4 | count=0 5 | for i in range(1,n+1): 6 | while i: 7 | if i%10==1: 8 | count+=1 9 | i=i/10 10 | return count 11 | 12 | # 数学规律 13 | # -*- coding:utf-8 -*- 14 | class Solution: 15 | def NumberOf1Between1AndN_Solution(self, n): 16 | # write code here 17 | count, m =0, 1 18 | while m <= n: 19 | count += (n // m + 8) // 10 * m + (n // m % 10 == 1) * (n % m + 1) 20 | m*=10 21 | return count -------------------------------------------------------------------------------- /docs/剑指offer/Python/32-把数组排成最小的数.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def PrintMinNumber(self, numbers): 4 | # write code here 5 | if not numbers: 6 | return '' 7 | 8 | str_num = [str(m) for m in numbers] 9 | for i in range(len(numbers)-1): 10 | for j in range(i+1,len(numbers)): 11 | if str_num[i] + str_num[j] > str_num[j] + str_num[i]: 12 | str_num[i],str_num[j] = str_num[j] ,str_num[i] 13 | 14 | return ''.join(str_num) -------------------------------------------------------------------------------- /docs/剑指offer/Python/33-丑数.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def GetUglyNumber_Solution(self, index): 4 | # write code here 5 | if not index: 6 | return 0 7 | 8 | ugly_number = [1]*index 9 | next_index = 1 10 | 11 | index2 = 0 12 | index3 = 0 13 | index5 = 0 14 | 15 | while next_index < index: 16 | minValue = min(ugly_number[index2]*2, ugly_number[index3]*3,ugly_number[index5]*5) 17 | ugly_number[next_index] = minValue 18 | 19 | while ugly_number[index2]*2 <= ugly_number[next_index]: 20 | index2 += 1 21 | 22 | while ugly_number[index3]*3 <= ugly_number[next_index]: 23 | index3 += 1 24 | 25 | while ugly_number[index5]*5 <= ugly_number[next_index]: 26 | index5 += 1 27 | 28 | next_index += 1 29 | 30 | return ugly_number[-1] -------------------------------------------------------------------------------- /docs/剑指offer/Python/34-第一个只出现一次的字符.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def FirstNotRepeatingChar(self, s): 4 | # write code here 5 | if not s: 6 | return -1 7 | 8 | store = {} 9 | lis = list(s) 10 | 11 | for i in lis: 12 | if i not in store.keys(): 13 | store[i] = 0 14 | store[i] += 1 15 | 16 | for i in lis: 17 | if store[i] == 1: 18 | return s.index(i) 19 | 20 | return -1 -------------------------------------------------------------------------------- /docs/剑指offer/Python/36-两个链表的第一个公共节点.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # class ListNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.next = None 6 | class Solution: 7 | def FindFirstCommonNode(self, pHead1, pHead2): 8 | # write code here 9 | if not pHead1 or not pHead2: 10 | return None 11 | 12 | p1,p2 = pHead1,pHead2 13 | len1 = len2 = 0 14 | while p1: 15 | len1 += 1 16 | p1 = p1.next 17 | 18 | while p2: 19 | len2 += 1 20 | p2 = p2.next 21 | 22 | if len1 > len2: 23 | while len1 - len2: 24 | pHead1 = pHead1.next 25 | len1 -= 1 26 | else: 27 | while len2 - len1: 28 | pHead2 = pHead2.next 29 | len2 -= 1 30 | 31 | while pHead1 and pHead2: 32 | if pHead1 is pHead2: 33 | return pHead1 34 | pHead1 = pHead1.next 35 | pHead2 = pHead2.next 36 | 37 | return None -------------------------------------------------------------------------------- /docs/剑指offer/Python/38-二叉树的深度.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # class TreeNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | class Solution: 8 | def TreeDepth(self, pRoot): 9 | # write code here 10 | if not pRoot: 11 | return 0 12 | return max(self.TreeDepth(pRoot.left),self.TreeDepth(pRoot.right)) + 1 -------------------------------------------------------------------------------- /docs/剑指offer/Python/39-平衡二叉树.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # class TreeNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | class Solution: 8 | def __init__(self): 9 | self.flag = True 10 | 11 | def IsBalanced_Solution(self, pRoot): 12 | # write code here 13 | self.getDepth(pRoot) 14 | return self.flag 15 | 16 | def getDepth(self, root): 17 | if not root: 18 | return 0 19 | left = self.getDepth(root.left) + 1 20 | right = self.getDepth(root.right) + 1 21 | 22 | if abs(left - right) > 1: 23 | self.flag = False 24 | 25 | return left if left > right else right -------------------------------------------------------------------------------- /docs/剑指offer/Python/41-和为s的连续整数序列.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def FindContinuousSequence(self, tsum): 4 | # write code here 5 | small, big,res = 1, 2, [] 6 | csum = small + big 7 | while small < big: 8 | if csum > tsum: 9 | csum -= small 10 | small += 1 11 | else: 12 | if csum == tsum: 13 | res.append([i for i in range(small,big+1)]) 14 | big += 1 15 | csum += big 16 | return res -------------------------------------------------------------------------------- /docs/剑指offer/Python/42-和为s的两个数字.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def FindNumbersWithSum(self, array, tsum): 4 | # write code here 5 | if not array or not tsum: 6 | return [] 7 | start = 0 8 | end = len(array) - 1 9 | while start < end: 10 | csum = array[start] + array[end] 11 | 12 | if csum < tsum: 13 | start += 1 14 | elif csum > tsum: 15 | end -= 1 16 | else: 17 | return [array[start],array[end]] 18 | 19 | return [] -------------------------------------------------------------------------------- /docs/剑指offer/Python/43-左旋字符串.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def LeftRotateString(self, s, n): 4 | # write code here 5 | if len(s) <= 0 or n < 0 or len(s) < n: 6 | return '' 7 | lis = list(s) 8 | self.Reverse(lis) 9 | length = len(s) 10 | pivot = length - n 11 | frontlist = self.Reverse(lis[:pivot]) 12 | behindlist = self.Reverse(lis[pivot:]) 13 | res = ''.join(frontlist) + ''.join(behindlist) 14 | return res 15 | 16 | def Reverse(self,lis): 17 | if not lis or len(lis) <= 0: 18 | return '' 19 | start = 0 20 | end = len(lis) - 1 21 | while start < end: 22 | lis[start], lis[end] = lis[end], lis[start] 23 | start += 1 24 | end -= 1 25 | 26 | return lis -------------------------------------------------------------------------------- /docs/剑指offer/Python/45-扑克牌顺子.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def IsContinuous(self, numbers): 4 | # write code here 5 | if not numbers or len(numbers) == 0: 6 | return False 7 | 8 | transdict = {'A':1,'J':11,'Q':12,'K':13} 9 | for i in range(len(numbers)): 10 | if numbers[i] in transdict.keys(): 11 | numbers[i] = transdict[numbers[i]] 12 | 13 | numbers = sorted(numbers) 14 | number_0 = 0 15 | number_gap = 0 16 | 17 | i = 0 18 | while i < len(numbers) and numbers[i] == 0: 19 | number_0 += 1 20 | i += 1 21 | 22 | front = number_0 23 | behind = front + 1 24 | while behind < len(numbers): 25 | if numbers[front] == numbers[behind]: 26 | return False 27 | number_gap += numbers[behind] - numbers[front] - 1 28 | front = behind 29 | behind += 1 30 | return False if number_gap > number_0 else True -------------------------------------------------------------------------------- /docs/剑指offer/Python/46-孩子们的游戏(圆圈中最后剩下的数).py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def LastRemaining_Solution(self, n, m): 4 | # write code here 5 | if n < 1 or m < 1: 6 | return -1 7 | con = range(n) 8 | start = 0 9 | end = -1 10 | while con: 11 | k = (start + m - 1) % n 12 | end = con.pop(k) 13 | n -= 1 14 | start = k 15 | return end 16 | 17 | # -*- coding:utf-8 -*- 18 | class Solution: 19 | def LastRemaining_Solution(self, n, m): 20 | # write code here 21 | if n < 1 or m < 1: 22 | return -1 23 | idx = 0 24 | for i in range(1,n+1): 25 | idx = (idx + m ) % i 26 | return idx -------------------------------------------------------------------------------- /docs/剑指offer/Python/47-求1+2+3+...+n.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def Sum_Solution(self, n): 4 | # write code here 5 | return self.sumN(n) 6 | 7 | def sum0(self, n): 8 | return 0 9 | 10 | def sumN(self,n): 11 | fun = {False:self.sum0,True: self.sumN} 12 | return n + fun[not not n](n - 1) 13 | 14 | # -*- coding:utf-8 -*- 15 | class Solution: 16 | def Sum_Solution(self, n): 17 | # write code here 18 | return n and self.Sum_Solution(n - 1) + n -------------------------------------------------------------------------------- /docs/剑指offer/Python/48-不用加减乘除做加法.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def Add(self, num1, num2): 4 | # write code here 5 | while num2 != 0: 6 | temp = num1 ^ num2 7 | num2 = (num1 & num2) << 1 8 | num1 = temp & 0xFFFFFFFF 9 | return num1 if num1 >> 31 == 0 else num1 - 4294967296 -------------------------------------------------------------------------------- /docs/剑指offer/Python/49-把字符串转化成整数.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def StrToInt(self, s): 4 | # write code here 5 | flag = False 6 | if not s or len(s) < 1: 7 | return 0 8 | num = [] 9 | numdict = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9} 10 | for i in s: 11 | if i in numdict.keys(): 12 | num.append(numdict[i]) 13 | elif i == '+' or i == '-': 14 | continue 15 | else: 16 | return 0 17 | ans = 0 18 | if len(num) == 1 and num[0] == 0: 19 | flag = True 20 | return 0 21 | for i in num: 22 | ans = ans*10 + i 23 | if s[0] == '-': 24 | ans = 0 - ans 25 | return ans -------------------------------------------------------------------------------- /docs/剑指offer/Python/50-数组中重复的数字.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | # 这里要特别注意~找到任意重复的一个值并赋值到duplication[0] 4 | # 函数返回True/False 5 | def duplicate(self, numbers, duplication): 6 | # write code here 7 | if not numbers or len(numbers) < 0: 8 | return False 9 | for i in numbers: 10 | if i < 0 or i > len(numbers) - 1: 11 | return False 12 | for i in range(len(numbers)): 13 | while numbers[i] != i: 14 | if numbers[i] == numbers[numbers[i]]: 15 | duplication[0] = numbers[i] 16 | return True 17 | else: 18 | idx = numbers[i] 19 | numbers[i],numbers[idx] = numbers[idx],numbers[i] 20 | return False -------------------------------------------------------------------------------- /docs/剑指offer/Python/51-构建乘积数组.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def multiply(self, A): 4 | # write code here 5 | if not A or len(A) <= 0: 6 | return 7 | length = len(A) 8 | lis = [1] * length 9 | for i in range(1,length): 10 | lis[i] = lis[i-1] * A[i-1] 11 | temp = 1 12 | for i in range(length-2,-1,-1): 13 | temp = temp * A[i+1] 14 | lis[i] *= temp 15 | return lis -------------------------------------------------------------------------------- /docs/剑指offer/Python/54-字符流中第一个不重复的字符.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | # init 4 | def __init__(self): 5 | self.dic = {} 6 | self.lis = [] 7 | # 返回对应char 8 | def FirstAppearingOnce(self): 9 | # write code here 10 | while len(self.lis) > 0 and self.dic[self.lis[0]] == 2: 11 | self.lis.pop(0) 12 | if len(self.lis) == 0: 13 | return '#' 14 | else: 15 | return self.lis[0] 16 | 17 | def Insert(self, char): 18 | # write code here 19 | if char not in self.dic.keys(): 20 | self.dic[char] = 1 21 | self.lis.append(char) 22 | elif self.dic[char]: 23 | self.dic[char] = 2 -------------------------------------------------------------------------------- /docs/剑指offer/Python/56-删除链表中重复的结点.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # class ListNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.next = None 6 | class Solution: 7 | def deleteDuplication(self, pHead): 8 | # write code here 9 | if pHead is None or pHead.next is None: 10 | return pHead 11 | first = ListNode(-1) 12 | first.next = pHead 13 | last = first 14 | while pHead and pHead.next: 15 | if pHead.val == pHead.next.val: 16 | val = pHead.val 17 | while pHead and val == pHead.val: 18 | pHead = pHead.next 19 | last.next = pHead 20 | 21 | else: 22 | last = pHead 23 | pHead = pHead.next 24 | return first.next -------------------------------------------------------------------------------- /docs/剑指offer/Python/58-对称的二叉树.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # class TreeNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | class Solution: 8 | def isSymmetrical(self, pRoot): 9 | # write code here 10 | return self.selfIsSym(pRoot,pRoot) 11 | 12 | def selfIsSym(self,root1,root2): 13 | if root1 == root2 and root2 == None: 14 | return True 15 | if root1 == None or root2 == None: 16 | return False 17 | if root1.val != root2.val: 18 | return False 19 | return self.selfIsSym(root1.left, root2.right) and self.selfIsSym(root1.right,root2.left) -------------------------------------------------------------------------------- /docs/剑指offer/Python/60-把二叉树打印成多行.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # class TreeNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | class Solution: 8 | 9 | def Print(self, pRoot): 10 | # write code here 11 | if pRoot is None: 12 | return [] 13 | nodes,res = [pRoot],[] 14 | while nodes: 15 | curStack, nextStack = [],[] 16 | for node in nodes: 17 | curStack.append(node.val) 18 | if node.left: 19 | nextStack.append(node.left) 20 | if node.right: 21 | nextStack.append(node.right) 22 | res.append(curStack) 23 | nodes = nextStack 24 | return res -------------------------------------------------------------------------------- /docs/剑指offer/Python/61-序列化二叉树.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # class TreeNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | class Solution: 8 | def __init__(self): 9 | self.flag = -1 10 | 11 | def Serialize(self, root): 12 | # write code here 13 | if not root: 14 | return '#,' 15 | return str(root.val)+','+self.Serialize(root.left)+self.Serialize(root.right) 16 | 17 | def Deserialize(self, s): 18 | # write code here 19 | self.flag += 1 20 | l = s.split(',') 21 | 22 | if self.flag >= len(s): 23 | return None 24 | root = None 25 | 26 | if l[self.flag] != '#': 27 | root = TreeNode(int(l[self.flag])) 28 | root.left = self.Deserialize(s) 29 | root.right = self.Deserialize(s) 30 | return root 31 | -------------------------------------------------------------------------------- /docs/剑指offer/Python/62-二叉搜索树的第k个结点.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # class TreeNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | class Solution: 8 | # 返回对应节点TreeNode 9 | def KthNode(self, pRoot, k): 10 | # write code here 11 | if not pRoot or not k: 12 | return 13 | res = [] 14 | def traverse(node): 15 | if len(res) >= k or not node: 16 | return 17 | traverse(node.left) 18 | res.append(node) 19 | traverse(node.right) 20 | traverse(pRoot) 21 | if len(res) < k: 22 | return 23 | return res[k-1] 24 | -------------------------------------------------------------------------------- /docs/剑指offer/Python/64-滑动窗口的最大值.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | class Solution: 3 | def maxInWindows(self, num, size): 4 | # write code here 5 | if not num or size <= 0: 6 | return [] 7 | deque = [] 8 | if len(num) >= size: 9 | index = [] 10 | for i in range(size): 11 | while len(index) > 0 and num[i] > num[index[-1]]: 12 | index.pop() 13 | index.append(i) 14 | 15 | for i in range(size, len(num)): 16 | deque.append(num[index[0]]) 17 | while len(index) > 0 and num[i] >= num[index[-1]]: 18 | index.pop() 19 | if len(index) > 0 and index[0] <= i - size: 20 | index.pop(0) 21 | index.append(i) 22 | 23 | deque.append(num[index[0]]) 24 | return deque -------------------------------------------------------------------------------- /docs/剑指offer/Python/README.md: -------------------------------------------------------------------------------- 1 | [此处](https://kaiyuanyokii2n.com/offer-python.html)为作者博客网站, 感兴趣的去可以看看 2 | -------------------------------------------------------------------------------- /gen_readme_file.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # _*_ coding:utf-8 _*_ 3 | import os 4 | 5 | 6 | if __name__ == '__main__': 7 | readme_template = 'README_TEMPLATE.md' 8 | readme_file = 'README.md' 9 | python_solution_dirpath = './docs/Leetcode_Solutions/Python' 10 | file_path_template = '- [{filename}](./docs/Leetcode_Solutions/Python/{file})\n' 11 | file_name_list = '' 12 | 13 | for dirpath, dirnames, filenames in os.walk(python_solution_dirpath): 14 | filenames.sort() 15 | for filename in filenames: 16 | file_name_list += file_path_template.format(filename=os.path.splitext(filename)[0], file=filename) 17 | break 18 | 19 | with open(readme_template, 'r') as fr, open(readme_file, 'w') as fw: 20 | content = fr.read() 21 | content = content.replace('{leetcode_solution_list}', file_name_list) 22 | # print(content) 23 | fw.write(content) 24 | 25 | print('Congratulation, readme file generate successfully!') 26 | -------------------------------------------------------------------------------- /images/005/1.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /images/005/manacher1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/005/manacher1.png -------------------------------------------------------------------------------- /images/005/manacher2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/005/manacher2.png -------------------------------------------------------------------------------- /images/005/manacher3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/005/manacher3.jpg -------------------------------------------------------------------------------- /images/011/1.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /images/011/question_11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/011/question_11.jpg -------------------------------------------------------------------------------- /images/033/1.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /images/033/rotated-array12_09_2017.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/033/rotated-array12_09_2017.jpg -------------------------------------------------------------------------------- /images/042/1.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /images/042/rainwatertrap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/042/rainwatertrap.png -------------------------------------------------------------------------------- /images/065/1.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /images/065/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/065/image.png -------------------------------------------------------------------------------- /images/118/PascalTriangleAnimated2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/118/PascalTriangleAnimated2.gif -------------------------------------------------------------------------------- /images/218/1.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /images/218/skyline1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/218/skyline1.jpg -------------------------------------------------------------------------------- /images/218/skyline2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/218/skyline2.jpg -------------------------------------------------------------------------------- /images/343/1.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /images/343/WechatIMG509.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/343/WechatIMG509.jpeg -------------------------------------------------------------------------------- /images/367/1.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /images/367/WechatIMG505.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/367/WechatIMG505.jpeg -------------------------------------------------------------------------------- /images/371/1.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /images/371/18C549E8937CF66D42060DCB802661C8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/371/18C549E8937CF66D42060DCB802661C8.jpg -------------------------------------------------------------------------------- /images/371/A4CF94C4A380101180708CF0CF35265B.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/371/A4CF94C4A380101180708CF0CF35265B.jpg -------------------------------------------------------------------------------- /images/463/1.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /images/463/island.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/463/island.png -------------------------------------------------------------------------------- /images/470/1.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /images/470/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/470/image.png -------------------------------------------------------------------------------- /images/815/1.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /images/815/WechatIMG6057.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/815/WechatIMG6057.png -------------------------------------------------------------------------------- /images/84/1.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /images/84/histogram1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/84/histogram1.png -------------------------------------------------------------------------------- /images/84/histogram_area1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/84/histogram_area1.png -------------------------------------------------------------------------------- /images/935/1.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /images/935/keypad.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/935/keypad.png -------------------------------------------------------------------------------- /images/935/knight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/935/knight.png -------------------------------------------------------------------------------- /images/MainPage/ApacheCN-group.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/MainPage/ApacheCN-group.png -------------------------------------------------------------------------------- /images/N-Queens.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/N-Queens.png -------------------------------------------------------------------------------- /images/Project cornerstone/3221532952133_.pic_hd.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/Project cornerstone/3221532952133_.pic_hd.jpg -------------------------------------------------------------------------------- /images/Project cornerstone/3231532952152_.pic_hd.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/Project cornerstone/3231532952152_.pic_hd.jpg -------------------------------------------------------------------------------- /images/Project cornerstone/approve letter.md: -------------------------------------------------------------------------------- 1 | 2 | ![](https://github.com/apachecn/LeetCode/blob/master/images/Project%20cornerstone/3221532952133_.pic_hd.jpg) 3 | ![](https://github.com/apachecn/LeetCode/blob/master/images/Project%20cornerstone/3231532952152_.pic_hd.jpg) 4 | -------------------------------------------------------------------------------- /images/README.md: -------------------------------------------------------------------------------- 1 | # Images for the whole repository! 2 | -------------------------------------------------------------------------------- /images/SortingAlgorithm/BubbleSort_1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/BubbleSort_1.gif -------------------------------------------------------------------------------- /images/SortingAlgorithm/InsertSort_1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/InsertSort_1.gif -------------------------------------------------------------------------------- /images/SortingAlgorithm/MergeSort_1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/MergeSort_1.gif -------------------------------------------------------------------------------- /images/SortingAlgorithm/QuickSort_1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/QuickSort_1.gif -------------------------------------------------------------------------------- /images/SortingAlgorithm/RadixSort_1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/RadixSort_1.gif -------------------------------------------------------------------------------- /images/SortingAlgorithm/RadixSort_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/RadixSort_2.png -------------------------------------------------------------------------------- /images/SortingAlgorithm/SelectionSort_1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/SelectionSort_1.gif -------------------------------------------------------------------------------- /images/SortingAlgorithm/ShellSort_1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/ShellSort_1.gif -------------------------------------------------------------------------------- /images/SortingAlgorithm/ShellSort_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/ShellSort_2.png -------------------------------------------------------------------------------- /images/SortingAlgorithm/ShellSort_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/ShellSort_3.png -------------------------------------------------------------------------------- /images/SortingAlgorithm/ShellSort_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/ShellSort_4.png -------------------------------------------------------------------------------- /images/SortingAlgorithm/八大排序算法性能.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/八大排序算法性能.png -------------------------------------------------------------------------------- /images/SortingAlgorithm/八大排序算法总结.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/八大排序算法总结.png -------------------------------------------------------------------------------- /images/SortingAlgorithm/冒泡排序.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/冒泡排序.gif -------------------------------------------------------------------------------- /images/SortingAlgorithm/基数排序.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/基数排序.gif -------------------------------------------------------------------------------- /images/SortingAlgorithm/希尔排序.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/希尔排序.png -------------------------------------------------------------------------------- /images/SortingAlgorithm/归并排序.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/归并排序.gif -------------------------------------------------------------------------------- /images/SortingAlgorithm/快速排序.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/快速排序.gif -------------------------------------------------------------------------------- /images/SortingAlgorithm/构建大顶堆.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/构建大顶堆.png -------------------------------------------------------------------------------- /images/SortingAlgorithm/直接插入排序.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/直接插入排序.gif -------------------------------------------------------------------------------- /images/SortingAlgorithm/简单选择排序.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/简单选择排序.gif -------------------------------------------------------------------------------- /images/SortingAlgorithm/调整大顶堆.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/SortingAlgorithm/调整大顶堆.png -------------------------------------------------------------------------------- /images/WechatIMG439.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/WechatIMG439.jpeg -------------------------------------------------------------------------------- /images/readme_badges/71EC577ABCDF4D4AE8082BED5D204919.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/readme_badges/71EC577ABCDF4D4AE8082BED5D204919.jpg -------------------------------------------------------------------------------- /images/readme_badges/WechatIMG649.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/readme_badges/WechatIMG649.jpeg -------------------------------------------------------------------------------- /images/readme_badges/organization.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/readme_badges/organization.jpg -------------------------------------------------------------------------------- /images/sketch1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/sketch1.png -------------------------------------------------------------------------------- /images/union_find8D76B4AFCB73CED67BE37B92B385A55C.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/union_find8D76B4AFCB73CED67BE37B92B385A55C.jpg -------------------------------------------------------------------------------- /images/线程转换.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/images/线程转换.jpg -------------------------------------------------------------------------------- /src/51CFCB8475F4ED5B1CAD5F23CF96887A.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/src/51CFCB8475F4ED5B1CAD5F23CF96887A.jpg -------------------------------------------------------------------------------- /src/58.md: -------------------------------------------------------------------------------- 1 | ![](https://github.com/apachecn/awesome-leetcode/blob/master/src/WechatIMG438.jpeg) 2 | 3 | ![](https://github.com/apachecn/awesome-leetcode/blob/master/src/WechatIMG437.jpeg) 4 | -------------------------------------------------------------------------------- /src/5C3A0153C03FD214FE68B002903E2135.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/src/5C3A0153C03FD214FE68B002903E2135.jpg -------------------------------------------------------------------------------- /src/B9A582497F833DDE4E93FA5BAA0BA4EB.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/src/B9A582497F833DDE4E93FA5BAA0BA4EB.jpg -------------------------------------------------------------------------------- /src/WechatIMG437.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/src/WechatIMG437.jpeg -------------------------------------------------------------------------------- /src/WechatIMG438.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuifengshen/awesome-leetcode/f4bb8a6d74c53df4e273a40387683576532ce10c/src/WechatIMG438.jpeg -------------------------------------------------------------------------------- /src/data_structure.md: -------------------------------------------------------------------------------- 1 | # 我们来一起每周学习数据结构吧,可以在群里一起讨论问题 2 | 3 | # qq群号码:303677416 4 | 5 | ![](https://github.com/apachecn/awesome-leetcode/blob/master/images/WechatIMG439.jpeg) 6 | 7 | -------------------------------------------------------------------------------- /src/liulishuo.md: -------------------------------------------------------------------------------- 1 | ```python 2 | 3 | """第一题""" 4 | n = int(input()) 5 | nums = [] 6 | for i in range(n): 7 | nums.append(int(input())) 8 | 9 | max_sum, max_end = nums[0], nums[0] 10 | for i in range(1, len(nums)): 11 | max_end = max(max_end + nums[i], nums[i]) 12 | max_sum = max(max_sum, max_end) 13 | print(max_sum) 14 | 15 | 16 | """问答题 17 | 18 | Let's say the egg1 and egg2 19 | 20 | Firstly, use egg1 from 2nd floor and drop it, if egg1 break, then use egg2 to check 1th floor, if egg2 doesn't break, then the highest floor we need is 1th, otherwise 2nd floor is what we want. If from neither floor two eggs don't break, we can do this from 4th and 3rd, 6th and 5th, 8th and 7th,,,,,,100th and 99th. 21 | 22 | From this method, we can easily get this problem reduced by 2 and totally time complexity will be O(lgn) if in programming. 23 | 24 | """ 25 | 26 | ``` 27 | -------------------------------------------------------------------------------- /src/py2.x/TreeRecursionIterator.py: -------------------------------------------------------------------------------- 1 | # coding:utf8 2 | 3 | from __future__ import print_function 4 | class Node(): 5 | def __init__(self, value, left=None, right=None): 6 | self.value = value 7 | self.left = left 8 | self.right = right 9 | 10 | def midRecusion(node): 11 | if node is None: 12 | return 13 | midRecusion(node.left) 14 | print(node.value, end=' ') 15 | midRecusion(node.right) 16 | 17 | def midIterator(node): 18 | stack = [] 19 | while stack or node: 20 | if node is not None: 21 | stack.append(node) 22 | node = node.left 23 | else: 24 | node = stack.pop(-1) 25 | print(node.value, end=' ') 26 | node = node.right 27 | 28 | if __name__ == "__main__": 29 | node = Node("D", Node("B", Node("A"), Node("C")), Node("E", right=Node("G", left=Node("F")))) 30 | 31 | print('\n中序遍历<递归>:') 32 | midRecusion(node) 33 | 34 | print('\n中序遍历<迭代>:') 35 | midIterator(node) -------------------------------------------------------------------------------- /src/py2.x/list2iteration.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # coding:utf8 3 | ''' 4 | 迭代使用的是循环结构。 5 | 递归使用的是选择结构。 6 | ''' 7 | from __future__ import print_function 8 | 9 | # 递归求解 10 | def calculate(l): 11 | if len(l) <= 1: 12 | return l[0] 13 | value = calculate(l[1:]) 14 | return 10**(len(l) - 1) * l[0] + value 15 | 16 | 17 | # 迭代求解 18 | def calculate2(l): 19 | result = 0 20 | while len(l) >= 1: 21 | result += 10 ** (len(l)-1) * l[0] 22 | l = l[1:] 23 | return result 24 | 25 | 26 | l1 = [1, 2, 3] 27 | l2 = [4, 5] 28 | sum = 0 29 | result = calculate(l1) + calculate(l2) 30 | # result = calculate2(l1) + calculate2(l2) 31 | print(result) 32 | -------------------------------------------------------------------------------- /src/py3.x/TreeRecursionIterator.py: -------------------------------------------------------------------------------- 1 | # coding:utf8 2 | 3 | from __future__ import print_function 4 | class Node(): 5 | def __init__(self, value, left=None, right=None): 6 | self.value = value 7 | self.left = left 8 | self.right = right 9 | 10 | def midRecusion(node): 11 | if node is None: 12 | return 13 | midRecusion(node.left) 14 | print(node.value, end=' ') 15 | midRecusion(node.right) 16 | 17 | def midIterator(node): 18 | stack = [] 19 | while stack or node: 20 | if node is not None: 21 | stack.append(node) 22 | node = node.left 23 | else: 24 | node = stack.pop(-1) 25 | print(node.value, end=' ') 26 | node = node.right 27 | 28 | if __name__ == "__main__": 29 | node = Node("D", Node("B", Node("A"), Node("C")), Node("E", right=Node("G", left=Node("F")))) 30 | 31 | print('\n中序遍历<递归>:') 32 | midRecusion(node) 33 | 34 | print('\n中序遍历<迭代>:') 35 | midIterator(node) -------------------------------------------------------------------------------- /src/py3.x/list2iteration.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # coding:utf8 3 | ''' 4 | 迭代使用的是循环结构。 5 | 递归使用的是选择结构。 6 | ''' 7 | from __future__ import print_function 8 | 9 | # 递归求解 10 | def calculate(l): 11 | if len(l) <= 1: 12 | return l[0] 13 | value = calculate(l[1:]) 14 | return 10**(len(l) - 1) * l[0] + value 15 | 16 | 17 | # 迭代求解 18 | def calculate2(l): 19 | result = 0 20 | while len(l) >= 1: 21 | result += 10 ** (len(l)-1) * l[0] 22 | l = l[1:] 23 | return result 24 | 25 | 26 | l1 = [1, 2, 3] 27 | l2 = [4, 5] 28 | sum = 0 29 | result = calculate(l1) + calculate(l2) 30 | # result = calculate2(l1) + calculate2(l2) 31 | print(result) 32 | -------------------------------------------------------------------------------- /src/py3.x/sort/BubbleSort.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | 3 | 4 | # 冒泡排序 5 | # 1. 外层循环负责帮忙递减内存循环的次数【1, len-1】 6 | # 2. 内层循环负责前后两两比较, index 的取值范围【0, len-2】 len-1-i 次,求最大值放到最后 7 | def bubble_sort(nums): 8 | # [1, len-1] 9 | for i in range(1, len(nums)-1): 10 | # [0, len-1-i] 11 | for j in range(len(nums)-i): # j为列表下标 12 | if nums[j] > nums[j+1]: 13 | nums[j], nums[j+1] = nums[j+1], nums[j] 14 | return nums 15 | 16 | 17 | if __name__ == "__main__": 18 | nums = [2, 6, 8, 5, 1, 4, 9, 3, 7] 19 | bubble_sort(nums) 20 | print('result:', nums) 21 | -------------------------------------------------------------------------------- /src/py3.x/sort/InsertionSort.py: -------------------------------------------------------------------------------- 1 | # coding:utf8 2 | """ 3 | 插入排序和冒泡排序的区别在于: 4 | 5 | 插入排序的前提是:左边是有序的数列 6 | 而冒泡排序:相邻的值进行交换,一共进行n次交换 7 | """ 8 | from __future__ import print_function 9 | 10 | 11 | def insertionSort(nums): 12 | if not nums or len(nums) < 2: 13 | return nums 14 | 15 | for i in range(1, len(nums)): 16 | for j in range(i): 17 | if nums[i] < nums[j]: 18 | nums[i], nums[j] = nums[j], nums[i] 19 | return nums 20 | 21 | 22 | if __name__ == "__main__": 23 | nums = [5, 1, 9, 3, 2, 7] 24 | print('input: ', nums) 25 | nums = insertionSort(nums) 26 | print("result: ", nums) 27 | -------------------------------------------------------------------------------- /src/py3.x/sort/MergeSort.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | 4 | def MergeSort(nums): 5 | if len(nums) <= 1: 6 | return nums 7 | num = int(len(nums)/2) 8 | # 从中间,进行数据的拆分, 递归的返回数据进行迭代排序 9 | left = MergeSort(nums[:num]) 10 | right = MergeSort(nums[num:]) 11 | print("left: ", left) 12 | print("right: ", right) 13 | print("-" * 20) 14 | return Merge(left, right) 15 | 16 | 17 | def Merge(left, right): 18 | l, r = 0, 0 19 | result = [] 20 | while l < len(left) and r < len(right): 21 | if left[l] < right[r]: 22 | result.append(left[l]) 23 | l += 1 24 | else: 25 | result.append(right[r]) 26 | r += 1 27 | result += left[l:] 28 | result += right[r:] 29 | return result 30 | 31 | 32 | if __name__ == "__main__": 33 | nums = [2, 6, 8, 5, 1, 4, 9, 3, 7] 34 | nums = MergeSort(nums) 35 | print('result:', nums) 36 | -------------------------------------------------------------------------------- /src/py3.x/sort/QuickSort.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # coding:utf8 3 | 4 | 5 | def quick_sort(nums, start, end): 6 | i = start 7 | j = end 8 | # 结束排序 9 | if i >= j: 10 | return 11 | # 保存首个数值 12 | key = nums[i] 13 | # 一次排序,i和j的值不断的靠拢,然后最终停止,结束一次排序 14 | while i < j: 15 | # 和最右边的比较,如果>=key,然后j-1,慢慢的和前一个值比较;如果值key,那么就交换位置 21 | while i < j and key >= nums[i]: 22 | print(key, nums[i], '*' * 30) 23 | i += 1 24 | nums[j] = nums[i] 25 | nums[i] = key 26 | # 左边排序 27 | quick_sort(nums, start, i-1) 28 | # 右边排序 29 | quick_sort(nums, i+1, end) 30 | 31 | 32 | if __name__ == "__main__": 33 | nums = [2, 6, 8, 5, 1, 4, 9, 3, 7] 34 | quick_sort(nums, 0, len(nums) - 1) 35 | print('result:', nums) 36 | -------------------------------------------------------------------------------- /src/py3.x/sort/SelectionSort.py: -------------------------------------------------------------------------------- 1 | # coding:utf8 2 | """ 3 | 选择排序和冒泡排序的区别在于: 4 | 5 | 选择排序的前提是:找到最大值的位置,最后才进行1次交换 6 | 而冒泡排序:相邻的值进行交换,一共进行n次交换 7 | """ 8 | from __future__ import print_function 9 | 10 | 11 | def selection_sort(l): 12 | length = len(l) - 1 13 | 14 | while length: 15 | index = length 16 | # 第一个数字,和后面每一个数字进行对比,找出最大值,放到最后!! 17 | for j in range(length): 18 | if l[j] > l[index]: 19 | index = j 20 | l[length], l[index] = l[index], l[length] 21 | print(len(l) - length, l) 22 | length -= 1 23 | 24 | 25 | if __name__ == "__main__": 26 | l = [5, 1, 9, 3, 2, 7] 27 | print(l) 28 | selection_sort(l) 29 | print("result: " + str(l)) -------------------------------------------------------------------------------- /src/py3.x/sort/ShellSort.py: -------------------------------------------------------------------------------- 1 | # coding: utf8 2 | 3 | from __future__ import print_function 4 | def insert_sort(l, start, increment): 5 | for i in range(start+increment, len(l), increment): 6 | for j in range(start, len(l[:i]), increment): 7 | if l[i] < l[j]: 8 | l[i], l[j] = l[j], l[i] 9 | print(increment, '--',l) 10 | return l 11 | 12 | def shell_sort(l, increment): 13 | # 依次进行分层 14 | while increment: 15 | # 每一层,都进行n次插入排序 16 | for i in range(0, increment): 17 | insert_sort(l, i, increment) 18 | increment -= 1 19 | return l 20 | 21 | if __name__ == "__main__": 22 | l = [5, 2, 9, 8, 1, 10, 3, 4, 7] 23 | increment = len(l)/3+1 if len(l)%3 else len(l)/3 24 | print("开始", l) 25 | l = shell_sort(l, increment) 26 | print("结束", l) -------------------------------------------------------------------------------- /src/qqchangE_problem.md: -------------------------------------------------------------------------------- 1 | ```python 2 | def canPartition(nums): 3 | def dfs(nums, target, num): 4 | n = len(nums) 5 | for i in range(n): 6 | B = nums[:i] + nums[i + 1:] 7 | if num + nums[i] == target: 8 | return True 9 | elif num + nums[i] < target: 10 | if dfs(B, target, num + nums[i]): 11 | return True 12 | elif num == 0: # 有一个数比sum/2还大,直接返回False 13 | return False 14 | return False 15 | 16 | total = sum(nums) 17 | 18 | if total % 2 != 0: 19 | return False 20 | 21 | target = total // 2 22 | 23 | nums.sort(reverse=True) # 逆序排序,先从大的开始判断,速度会更快 24 | 25 | res = dfs(nums, target, 0) 26 | return res 27 | 28 | 29 | print(canPartition([3, 3, 3, 4, 5])) 30 | print(canPartition([1,1,2])) 31 | print(canPartition([1,2,3,4])) 32 | print(canPartition([2,3])) 33 | print(canPartition([2,2,2,2,6])) 34 | print(canPartition([1,2,2,2,2,7])) 35 | 36 | 37 | output: 38 | True 39 | True 40 | True 41 | False 42 | False 43 | True 44 | ``` 45 | -------------------------------------------------------------------------------- /src/sohuchangyouweipinhui.md: -------------------------------------------------------------------------------- 1 | ```python 2 | """搜狐畅游第一题""" 3 | nums = [int(i) for i in input().split()] 4 | print(int(sum(nums) - (len(nums)-1)*(len(nums)-2)/2)) 5 | 6 | 7 | """唯品会第一题""" 8 | import heapq 9 | [k, n] = [int(i) for i in input().split()] 10 | topk = [] 11 | for i in range(n): 12 | nums = [int(i) for i in input().split()] 13 | for num in nums: 14 | heapq.heappush(topk, num) 15 | res = 0 16 | for i in range(k): 17 | res = heapq.heappop(topk) 18 | print(res) 19 | 20 | 21 | """唯品会第二题""" 22 | print(bin(sum([int(i, 2) for i in input().split()]))[2:]) 23 | ``` 24 | -------------------------------------------------------------------------------- /src/times.md: -------------------------------------------------------------------------------- 1 | http://bigocheatsheet.com/ 2 | 3 | 4 | https://algs4.cs.princeton.edu/cheatsheet/ 5 | 6 | https://wiki.python.org/moin/TimeComplexity 7 | --------------------------------------------------------------------------------