├── 0050_pow ├── Makefile ├── pow.cc └── pow.c ├── 0008_atoi └── Makefile ├── 0069_sqrt ├── Makefile └── sqrt.cc ├── 0135_candy ├── Makefile ├── candy.cc └── candy.c ├── 0001_two_sum ├── Makefile └── two_sum.cc ├── 0018_four_sum └── Makefile ├── 0051_n_queens └── Makefile ├── 0066_plus_one ├── Makefile └── plus_one.cc ├── 0078_subsets ├── Makefile └── subsets.cc ├── 0112_path_sum ├── Makefile └── path_sum.cc ├── 0120_triangle ├── Makefile └── triangle.cc ├── 0155_min_stack └── Makefile ├── 0912_sort_an_array └── Makefile ├── 0015_three_sum ├── Makefile └── three_sum.cc ├── 0027_remove_element ├── Makefile ├── rm_elem.cc └── rm_elem.c ├── 0045_jump_game_ii ├── Makefile ├── jump_game.cc └── jump_game.c ├── 0048_rotate_image ├── Makefile └── rotate.cc ├── 0052_n_queens_ii ├── Makefile └── n_queens.cc ├── 0055_jump_game ├── Makefile └── jump_game.c ├── 0067_add_binary ├── Makefile └── add_binary.c ├── 0089_gray_code ├── Makefile └── gray_code.c ├── 0090_subsets_ii ├── Makefile └── subsets.cc ├── 0100_same_tree ├── Makefile ├── same_tree.c └── same_tree.cc ├── 0113_path_sum_ii └── Makefile ├── 0139_word_break └── Makefile ├── 0146_lru_cache └── Makefile ├── 0148_sort_list └── Makefile ├── 0162_find_peak_element ├── Makefile ├── peak.cc └── peak.c ├── 0164_maximum_gap └── Makefile ├── 0167_two_sum_ii ├── Makefile └── two_sum.cc ├── 0174_dungeon_game └── Makefile ├── 0198_house_robber ├── Makefile ├── robber.cc └── robber.c ├── 0208_implement_trie └── Makefile ├── 0213_house_robber_ii ├── Makefile ├── robber.cc └── robber.c ├── 0028_implement_strstr └── Makefile ├── 0029_divide_two_integers ├── Makefile ├── divide.cc └── divide.c ├── 0036_valid_sudoku └── Makefile ├── 0037_sudoku_solver └── Makefile ├── 0038_count_and_say └── Makefile ├── 0046_permutations ├── Makefile └── permutations.cc ├── 0049_group_anagrams ├── Makefile └── anagrams.cc ├── 0054_spiral_matrix └── Makefile ├── 0061_rotate_list └── Makefile ├── 0062_unique_path ├── Makefile └── unique_path.c ├── 0063_unique_paths_ii └── Makefile ├── 0065_valid_number ├── Makefile └── valid_number.c ├── 0071_simplify_path └── Makefile ├── 0072_edit_distance ├── Makefile └── edit_distance.cc ├── 0073_set_matrix_zeroes ├── Makefile ├── set_zero.c └── set_zero.cc ├── 0075_sort_colors ├── Makefile ├── sort_colors.cc └── sort_colors.c ├── 0077_combinations ├── Makefile └── combinations.cc ├── 0079_word_search └── Makefile ├── 0091_decode_ways ├── Makefile └── decode_ways.c ├── 0126_word_ladder_ii └── Makefile ├── 0127_word_ladder └── Makefile ├── 0133_clone_graph └── Makefile ├── 0134_gas_station ├── Makefile └── gas_station.c ├── 0136_single_number ├── Makefile ├── single_number.cc └── single_number.c ├── 0140_word_break_ii └── Makefile ├── 0143_reorder_list └── Makefile ├── 0169_majority_element ├── Makefile ├── majority.cc └── majority.c ├── 0189_rotate_array ├── Makefile ├── rotate_array.cc └── rotate_array.c ├── 0190_reverse_bits ├── Makefile └── reverse_bits.c ├── 0200_number_of_islands └── Makefile ├── 0202_happy_number ├── Makefile └── happy_number.c ├── 0204_count_primes ├── Makefile ├── count_primes.cc └── count_primes.c ├── 0212_word_search_ii └── Makefile ├── 0224_basic_calculator └── Makefile ├── 0322_coin_change └── Makefile ├── 0518_coin_change_ii ├── Makefile ├── coin_change.cc └── coin_change.c ├── 0002_add_two_numbers └── Makefile ├── 0007_reverse_integer ├── Makefile └── reverse_integer.cc ├── 0022_generate_parathesis ├── Makefile └── parenthesis.cc ├── 0023_merge_k_sorted_lists └── Makefile ├── 0024_swap_nodes_in_pairs ├── Makefile └── swap_nodes.cc ├── 0034_search_for_a_range └── Makefile ├── 0039_combination_sum ├── Makefile └── combination_sum.cc ├── 0042_trapping_rain_water ├── Makefile └── trap_water.cc ├── 0047_permutations_ii └── Makefile ├── 0053_maximum_subarray ├── Makefile └── max_subarray.cc ├── 0056_merge_intervals └── Makefile ├── 0057_insert_interval └── Makefile ├── 0058_length_of_last_word ├── Makefile └── word_length.c ├── 0059_spiral_matrix_ii └── Makefile ├── 0068_text_justification └── Makefile ├── 0070_climbing_stairs ├── Makefile ├── climb_stairs.cc └── climb_stairs.c ├── 0074_search_a_2d_matrix └── Makefile ├── 0086_partition_list └── Makefile ├── 0087_scramble_string └── Makefile ├── 0088_merge_sorted_array ├── Makefile ├── merge_array.cc └── merge_array.c ├── 0093_restore_ip_addresses └── Makefile ├── 0101_symmetric_tree ├── Makefile └── symmetric_tree.cc ├── 0118_pascal_triangle └── Makefile ├── 0137_single_number_ii └── Makefile ├── 0141_linked_list_cycle ├── Makefile ├── list_cycle.cc └── list_cycle.c ├── 0142_linked_list_cycle_ii ├── Makefile └── list_cycle.cc ├── 0165_compare_version_numbers ├── Makefile └── version.c ├── 0172_factorial_trailing_zeros ├── Makefile ├── zeroes.cc └── zeroes.c ├── 0179_largest_number ├── Makefile └── largest_number.cc ├── 0201_bitwise_and_of_numbers_range ├── Makefile └── and.c ├── 0206_reverse_linked_list ├── Makefile └── reverse_list.cc ├── 0207_course_schedule └── Makefile ├── 0211_add_and_search_word └── Makefile ├── 0221_maximal_square └── Makefile ├── 0227_basic_calculator_ii └── Makefile ├── 0229_majority_element_ii └── Makefile ├── 0991_broken_calculator ├── Makefile ├── calculator.cc └── calculator.c ├── 1143_longest_common_subsequence ├── Makefile ├── lcs.cc └── lcs.c ├── 0006_zigzag_conversion ├── Makefile ├── zigzag_conversion.cc └── zigzag_conversion.c ├── 0009_palindrome_number ├── Makefile ├── palindrome_number.cc └── palindrome_number.c ├── 0011_container_with_most_water ├── Makefile ├── container.cc └── container.c ├── 0012_roman_numeral └── Makefile ├── 0013_roman_to_integer ├── Makefile ├── roman_to_integer.cc └── roman_to_integer.c ├── 0014_longest_common_prefix ├── Makefile ├── common_prefix.cc └── common_prefix.c ├── 0020_valid_parentheses ├── Makefile ├── valid_parentheses.c └── valid_parentheses.cc ├── 0021_merge_two_sorted_lists ├── Makefile ├── merge_lists.cc └── merge_lists.c ├── 0031_next_permutation ├── Makefile └── next_permutation.cc ├── 0040_combination_sum_ii ├── Makefile └── combination_sum.cc ├── 0043_multiply_strings ├── Makefile ├── multiply_strings.cc └── multiply_strings.c ├── 0044_wildcard_matching └── Makefile ├── 0064_minumum_path_sum └── Makefile ├── 0085_maximal_rectangle └── Makefile ├── 0092_reverse_linked_list_ii └── Makefile ├── 0096_unique_binary_search_trees ├── Makefile ├── unique_bst.cc └── unique_bst.c ├── 0098_validate_binary_search_tree ├── Makefile └── valid_bst.cc ├── 0110_balanced_binary_tree ├── Makefile └── balanced_bst.cc ├── 0115_distinct_subsequences └── Makefile ├── 0119_pascal_triangle_ii ├── Makefile └── pascal_triangle.c ├── 0121_best_time_to_buy_and_sell_stock ├── Makefile ├── stock.cc └── stock.c ├── 0125_valid_palindrome └── Makefile ├── 0129_sum_root_to_leaf_numbers └── Makefile ├── 0130_surrounded_regions └── Makefile ├── 0147_insertion_sort_list └── Makefile ├── 0149_max_points_on_a_line └── Makefile ├── 0151_reverse_words_in_a_string ├── Makefile └── reverse.c ├── 0152_maximum_product_subarray ├── Makefile └── subarray.c ├── 0168_excel_sheet_column_title ├── Makefile └── sheet_column.c ├── 0173_binary_search_tree_iterator └── Makefile ├── 0191_number_of_one_bits ├── Makefile └── hamming_weight.c ├── 0199_binary_tree_right_side_view └── Makefile ├── 0203_remove_linked_list_element └── Makefile ├── 0205_isomorphic_strings ├── Makefile ├── isomorphic_strings.c └── isomorphic_strings.cpp ├── 0209_minimum_size_subarray_sum ├── Makefile └── mini_size.c ├── 0210_course_schedule_ii └── Makefile ├── 0216_combination_sum_iii ├── Makefile └── combination_sum.cc ├── 0226_invert_binary_tree ├── Makefile └── invert_binary_tree.cc ├── 0230_kth_smallest_element_in_a_bst ├── Makefile └── kth_bst.cc ├── 0239_sliding_window_maximum ├── Makefile └── slide_window.cc ├── 0300_longest_increasing_subsequence ├── Makefile └── lis.cc ├── 0354_russian_doll_envelopes ├── Makefile └── russian_doll.cc ├── 0516_longest_palindromic_subsequence ├── Makefile ├── lps.cc └── lps.c ├── 0016_three_sum_closest ├── Makefile └── three_sum_closest.cc ├── 0025_reverse_nodes_in_k_group ├── Makefile └── reverse_nodes.cc ├── 0026_remove_duplicates_from_sorted_array ├── Makefile ├── rm_dup.cc └── rm_dup.c ├── 0035_search_insert_position ├── Makefile ├── insert_position.cc └── insert_position.c ├── 0041_first_missing_positive ├── Makefile ├── missing_positive.cc └── missing_positive.c ├── 0060_permutation_sequence └── Makefile ├── 0076_minimum_window_substring └── Makefile ├── 0083_remove_duplicates_from_sorted_list ├── Makefile └── rm_dup.cc ├── 0095_unique_binary_search_trees_ii └── Makefile ├── 0097_interleaving_string └── Makefile ├── 0099_recover_binary_search_tree └── Makefile ├── 0102_binary_tree_level_order_traversal └── Makefile ├── 0104_maximum_depth_of_binary_tree ├── Makefile ├── bst_depth.c └── bst_depth.cc ├── 0111_minimum_depth_of_binary_tree ├── Makefile ├── bst_depth.c └── bst_depth.cc ├── 0114_flatten_binary_tree_to_linked_list └── Makefile ├── 0122_best_time_to_buy_and_sell_stock_ii ├── Makefile ├── stock.cc └── stock.c ├── 0123_best_time_to_buy_and_sell_stock_iii └── Makefile ├── 0124_binary_tree_maximum_path_sum ├── Makefile └── bst_max_path.cc ├── 0128_longest_consecutive_sequence ├── Makefile └── consec_seq.cc ├── 0138_copy_list_with_random_pointer └── Makefile ├── 0150_evaluate_reverse_polish_notation └── Makefile ├── 0166_fraction_to_recurring_decimal └── Makefile ├── 0171_excel_sheet_column_number ├── Makefile └── sheet_column.c ├── 0188_best_time_to_buy_and_sell_stock_iv └── Makefile ├── 0214_shortest_palindrome └── Makefile ├── 0215_kth_largest_element_in_an_array ├── Makefile └── kth_elem.cc ├── 0010_regular_expression_matching ├── Makefile ├── regular_expression.c └── regular_expression.cc ├── 0019_remove_nth_node_from_end_of_list ├── Makefile └── remove_end.cc ├── 0032_longest_valid_parentheses ├── Makefile └── valid_parentheses.cc ├── 0033_search_in_rotated_sorted_array ├── Makefile └── rotated_array.cc ├── 0080_remove_duplicates_from_sorted_array_ii ├── Makefile └── rm_dups.c ├── 0082_remove_duplicates_from_sorted_list_ii └── Makefile ├── 0107_binary_tree_level_order_traversal_ii └── Makefile ├── 0131_palindrome_patitioning └── Makefile ├── 0132_palindrome_patitioning_ii └── Makefile ├── 0144_binary_tree_preorder_traversal ├── Makefile └── bst_preorder.cc ├── 0145_binary_tree_postorder_traversal └── Makefile ├── 0153_find_minimum_in_rotated_sorted_array ├── Makefile └── minimum.c ├── 0154_find_minimum_in_rotated_sorted_array_ii ├── Makefile └── minimum.c ├── 0160_intersection_of_two_linked_list ├── Makefile └── intersection.cc ├── 0236_lowest_common_ancestor_of_a_binary_tree ├── Makefile └── bst_lca.cc ├── 0567_permutation_in_string ├── Makefile ├── permutation_in_string.cc └── permutation_in_string.c ├── 0084_largest_rectangle_in_histogram └── Makefile ├── 0094_binary_tree_inorder_traversal ├── Makefile └── bst_inorder_traversal.c ├── 0103_binary_tree_zigzag_level_order_traversal └── Makefile ├── 0116_populating_next_right_pointers_in_each_node ├── Makefile └── connect.cc ├── 0297_serialize_and_deserialize_binary_tree └── Makefile ├── 0438_find_all_anagrams_in_a_string ├── Makefile └── anagrams_in_string.cc ├── 0990_satisfiability_of_equality_equations └── Makefile ├── 0004_median_of_two_sorted_array ├── Makefile └── median_of_two_sorted_array.cc ├── 0030_substring_with_concatenation_of_all_words ├── Makefile └── concatenation.cc ├── 0081_search_in_rotated_sorted_array_ii └── Makefile ├── 0108_convert_sorted_array_to_binary_search_tree ├── Makefile ├── bst_convert.c └── bst_convert.cc ├── 0109_convert_sorted_list_to_binary_search_tree ├── Makefile └── bst_convert.c ├── 0117_populating_next_right_pointers_in_each_node_ii └── Makefile ├── 0235_lowest_common_ancestor_of_a_binary_search_tree ├── Makefile └── bst_lca.cc ├── 0005_longest_palindromic_substring ├── Makefile └── longest_palindromic_substring.cc ├── 1312_minimum_insertion_steps_to_make_a_string_palindrome ├── Makefile ├── insertion.cc └── insertion.c ├── README.md ├── 0003_longest_substring_without_repeat ├── Makefile ├── longest_substring_without_repeat.cc └── longest_substring_without_repeat.c ├── 0460_lfu_cache └── Makefile ├── 0017_letter_combinations_of_a_phone_number ├── Makefile └── letter_combinations.cpp ├── 0105_construct_binary_tree_from_preorder_and_inorder_traversal └── Makefile ├── 0106_construct_binary_tree_from_inorder_and_postorder_traversal └── Makefile ├── .gitignore ├── 0704_binary_search └── binary_search.c ├── 0416_partition_equal_subset_sum └── partition.cc ├── 0543_diameter_of_binary_tree └── diameter_bst.cc ├── 0337_house_robber_iii └── robber.cc ├── 1372_longest_zigzag_path_in_a_binary_tree └── zigzag.cc ├── 0563_binary_tree_tilt └── tilt.cc ├── 0560_subarray_sum_equals_k └── subarray_sum.cc └── LICENSE /0050_pow/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test pow.c 3 | -------------------------------------------------------------------------------- /0008_atoi/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test atoi.c 3 | -------------------------------------------------------------------------------- /0069_sqrt/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test sqrt.c 3 | -------------------------------------------------------------------------------- /0135_candy/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test candy.c 3 | -------------------------------------------------------------------------------- /0001_two_sum/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test two_sum.c 3 | -------------------------------------------------------------------------------- /0018_four_sum/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test four_sum.c 3 | -------------------------------------------------------------------------------- /0051_n_queens/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test n_queens.c 3 | -------------------------------------------------------------------------------- /0066_plus_one/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test plus_one.c 3 | -------------------------------------------------------------------------------- /0078_subsets/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test subsets.c 3 | -------------------------------------------------------------------------------- /0112_path_sum/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test path_sum.c 3 | -------------------------------------------------------------------------------- /0120_triangle/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test triangle.c 3 | -------------------------------------------------------------------------------- /0155_min_stack/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test stack.c 3 | -------------------------------------------------------------------------------- /0912_sort_an_array/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -o test sort.c 3 | -------------------------------------------------------------------------------- /0015_three_sum/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test three_sum.c 3 | -------------------------------------------------------------------------------- /0027_remove_element/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test rm_elem.c 3 | -------------------------------------------------------------------------------- /0045_jump_game_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test jump_game.c 3 | -------------------------------------------------------------------------------- /0048_rotate_image/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test rotate.c 3 | -------------------------------------------------------------------------------- /0052_n_queens_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test n_queens.c 3 | -------------------------------------------------------------------------------- /0055_jump_game/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test jump_game.c 3 | -------------------------------------------------------------------------------- /0067_add_binary/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test add_binary.c 3 | -------------------------------------------------------------------------------- /0089_gray_code/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test gray_code.c 3 | -------------------------------------------------------------------------------- /0090_subsets_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test subsets.c 3 | -------------------------------------------------------------------------------- /0100_same_tree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test same_tree.c 3 | -------------------------------------------------------------------------------- /0113_path_sum_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test path_sum.c 3 | -------------------------------------------------------------------------------- /0139_word_break/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test word_break.c 3 | -------------------------------------------------------------------------------- /0146_lru_cache/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test lru_cache.c 3 | -------------------------------------------------------------------------------- /0148_sort_list/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test sort_list.c 3 | -------------------------------------------------------------------------------- /0162_find_peak_element/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test peak.c 3 | -------------------------------------------------------------------------------- /0164_maximum_gap/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test max_gap.c 3 | -------------------------------------------------------------------------------- /0167_two_sum_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test two_sum.c 3 | -------------------------------------------------------------------------------- /0174_dungeon_game/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test dungeon.c 3 | -------------------------------------------------------------------------------- /0198_house_robber/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test robber.c 3 | -------------------------------------------------------------------------------- /0208_implement_trie/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test trie.c 3 | -------------------------------------------------------------------------------- /0213_house_robber_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test robber.c 3 | -------------------------------------------------------------------------------- /0028_implement_strstr/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test strstr.c 3 | -------------------------------------------------------------------------------- /0029_divide_two_integers/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test divide.c 3 | -------------------------------------------------------------------------------- /0036_valid_sudoku/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test valid_sudoku.c 3 | -------------------------------------------------------------------------------- /0037_sudoku_solver/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test sudoku_solver.c 3 | -------------------------------------------------------------------------------- /0038_count_and_say/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test count_and_say.c 3 | -------------------------------------------------------------------------------- /0046_permutations/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test permutations.c 3 | -------------------------------------------------------------------------------- /0049_group_anagrams/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test anagrams.c 3 | -------------------------------------------------------------------------------- /0054_spiral_matrix/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test spiral_matrix.c 3 | -------------------------------------------------------------------------------- /0061_rotate_list/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test rotate_list.c 3 | -------------------------------------------------------------------------------- /0062_unique_path/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test unique_path.c 3 | -------------------------------------------------------------------------------- /0063_unique_paths_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test unique_path.c 3 | -------------------------------------------------------------------------------- /0065_valid_number/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test valid_number.c 3 | -------------------------------------------------------------------------------- /0071_simplify_path/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test simplify_path.c 3 | -------------------------------------------------------------------------------- /0072_edit_distance/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test edit_distance.c 3 | -------------------------------------------------------------------------------- /0073_set_matrix_zeroes/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test set_zero.c 3 | -------------------------------------------------------------------------------- /0075_sort_colors/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test sort_colors.c 3 | -------------------------------------------------------------------------------- /0077_combinations/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test combinations.c 3 | -------------------------------------------------------------------------------- /0079_word_search/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test word_search.c 3 | -------------------------------------------------------------------------------- /0091_decode_ways/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test decode_ways.c 3 | -------------------------------------------------------------------------------- /0126_word_ladder_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test word_ladder.c 3 | -------------------------------------------------------------------------------- /0127_word_ladder/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test word_ladder.c 3 | -------------------------------------------------------------------------------- /0133_clone_graph/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test clone_graph.c 3 | -------------------------------------------------------------------------------- /0134_gas_station/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test gas_station.c 3 | -------------------------------------------------------------------------------- /0136_single_number/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test single_number.c 3 | -------------------------------------------------------------------------------- /0140_word_break_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test word_break.c 3 | -------------------------------------------------------------------------------- /0143_reorder_list/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test reorder_list.c 3 | -------------------------------------------------------------------------------- /0169_majority_element/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test majority.c 3 | -------------------------------------------------------------------------------- /0189_rotate_array/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test rotate_array.c 3 | -------------------------------------------------------------------------------- /0190_reverse_bits/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test reverse_bits.c 3 | -------------------------------------------------------------------------------- /0200_number_of_islands/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test islands.c 3 | -------------------------------------------------------------------------------- /0202_happy_number/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test happy_number.c 3 | -------------------------------------------------------------------------------- /0204_count_primes/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test count_primes.c 3 | -------------------------------------------------------------------------------- /0212_word_search_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test word_search.c 3 | -------------------------------------------------------------------------------- /0224_basic_calculator/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test calculator.c 3 | -------------------------------------------------------------------------------- /0322_coin_change/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test coin_change.c 3 | -------------------------------------------------------------------------------- /0518_coin_change_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test coin_change.c 3 | -------------------------------------------------------------------------------- /0002_add_two_numbers/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test add_two_numbers.c 3 | -------------------------------------------------------------------------------- /0007_reverse_integer/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test reverse_integer.c 3 | -------------------------------------------------------------------------------- /0022_generate_parathesis/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test parenthesis.c 3 | -------------------------------------------------------------------------------- /0023_merge_k_sorted_lists/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test merge_lists.c 3 | -------------------------------------------------------------------------------- /0024_swap_nodes_in_pairs/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test swap_nodes.c 3 | -------------------------------------------------------------------------------- /0034_search_for_a_range/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test range_search.c 3 | -------------------------------------------------------------------------------- /0039_combination_sum/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test combination_sum.c 3 | -------------------------------------------------------------------------------- /0042_trapping_rain_water/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test trap_water.c 3 | -------------------------------------------------------------------------------- /0047_permutations_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test permutations.c 3 | -------------------------------------------------------------------------------- /0053_maximum_subarray/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test max_subarray.c 3 | -------------------------------------------------------------------------------- /0056_merge_intervals/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test merge_intervals.c 3 | -------------------------------------------------------------------------------- /0057_insert_interval/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test insert_interval.c 3 | -------------------------------------------------------------------------------- /0058_length_of_last_word/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test word_length.c 3 | -------------------------------------------------------------------------------- /0059_spiral_matrix_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test spiral_matrix.c 3 | -------------------------------------------------------------------------------- /0068_text_justification/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test justification.c 3 | -------------------------------------------------------------------------------- /0070_climbing_stairs/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test climb_stairs.c 3 | -------------------------------------------------------------------------------- /0074_search_a_2d_matrix/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test matrix_search.c 3 | -------------------------------------------------------------------------------- /0086_partition_list/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test partition_list.c 3 | -------------------------------------------------------------------------------- /0087_scramble_string/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test scramble_string.c 3 | -------------------------------------------------------------------------------- /0088_merge_sorted_array/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test merge_array.c 3 | -------------------------------------------------------------------------------- /0093_restore_ip_addresses/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test ip_addr.c 3 | -------------------------------------------------------------------------------- /0101_symmetric_tree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test symmetric_tree.c 3 | -------------------------------------------------------------------------------- /0118_pascal_triangle/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test pascal_triangle.c 3 | -------------------------------------------------------------------------------- /0137_single_number_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test single_number.c 3 | -------------------------------------------------------------------------------- /0141_linked_list_cycle/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test list_cycle.c 3 | -------------------------------------------------------------------------------- /0142_linked_list_cycle_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test list_cycle.c 3 | -------------------------------------------------------------------------------- /0165_compare_version_numbers/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test version.c 3 | -------------------------------------------------------------------------------- /0172_factorial_trailing_zeros/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test zeroes.c 3 | -------------------------------------------------------------------------------- /0179_largest_number/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test largest_number.c 3 | -------------------------------------------------------------------------------- /0201_bitwise_and_of_numbers_range/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test and.c 3 | -------------------------------------------------------------------------------- /0206_reverse_linked_list/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test reverse_list.c 3 | -------------------------------------------------------------------------------- /0207_course_schedule/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test course_schedule.c 3 | -------------------------------------------------------------------------------- /0211_add_and_search_word/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test word_dict.c 3 | -------------------------------------------------------------------------------- /0221_maximal_square/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test maximal_square.c 3 | -------------------------------------------------------------------------------- /0227_basic_calculator_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test calculator.c 3 | -------------------------------------------------------------------------------- /0229_majority_element_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test majority.c 3 | -------------------------------------------------------------------------------- /0991_broken_calculator/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test calculator.c 3 | -------------------------------------------------------------------------------- /1143_longest_common_subsequence/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test lcs.c 3 | -------------------------------------------------------------------------------- /0006_zigzag_conversion/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test zigzag_conversion.c 3 | -------------------------------------------------------------------------------- /0009_palindrome_number/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test palindrome_number.c 3 | -------------------------------------------------------------------------------- /0011_container_with_most_water/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test container.c 3 | -------------------------------------------------------------------------------- /0012_roman_numeral/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o roman_numeral roman_numeral.c 3 | -------------------------------------------------------------------------------- /0013_roman_to_integer/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test roman_to_integer.c 3 | -------------------------------------------------------------------------------- /0014_longest_common_prefix/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test common_prefix.c 3 | -------------------------------------------------------------------------------- /0020_valid_parentheses/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test valid_parentheses.c 3 | -------------------------------------------------------------------------------- /0021_merge_two_sorted_lists/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test merge_lists.c 3 | -------------------------------------------------------------------------------- /0031_next_permutation/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test next_permutation.c 3 | -------------------------------------------------------------------------------- /0040_combination_sum_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test combination_sum.c 3 | -------------------------------------------------------------------------------- /0043_multiply_strings/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test multiply_strings.c 3 | -------------------------------------------------------------------------------- /0044_wildcard_matching/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test wildcard_matching.c 3 | -------------------------------------------------------------------------------- /0064_minumum_path_sum/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test minimum_path_sum.c 3 | -------------------------------------------------------------------------------- /0085_maximal_rectangle/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test maximal_rectangle.c 3 | -------------------------------------------------------------------------------- /0092_reverse_linked_list_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test reverse_list.c 3 | -------------------------------------------------------------------------------- /0096_unique_binary_search_trees/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test unique_bst.c 3 | -------------------------------------------------------------------------------- /0098_validate_binary_search_tree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test valid_bst.c 3 | -------------------------------------------------------------------------------- /0110_balanced_binary_tree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test balanced_bst.c 3 | -------------------------------------------------------------------------------- /0115_distinct_subsequences/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test distinct_subseq.c 3 | -------------------------------------------------------------------------------- /0119_pascal_triangle_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test pascal_triangle.c 3 | -------------------------------------------------------------------------------- /0121_best_time_to_buy_and_sell_stock/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test stock.c 3 | -------------------------------------------------------------------------------- /0125_valid_palindrome/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test valid_palindrome.c 3 | -------------------------------------------------------------------------------- /0129_sum_root_to_leaf_numbers/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test sum_tree.c 3 | -------------------------------------------------------------------------------- /0130_surrounded_regions/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test surrounded_regions.c 3 | -------------------------------------------------------------------------------- /0147_insertion_sort_list/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test insert_sort_list.c 3 | -------------------------------------------------------------------------------- /0149_max_points_on_a_line/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test points_on_line.c 3 | -------------------------------------------------------------------------------- /0151_reverse_words_in_a_string/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test reverse.c 3 | -------------------------------------------------------------------------------- /0152_maximum_product_subarray/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test subarray.c 3 | -------------------------------------------------------------------------------- /0168_excel_sheet_column_title/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test sheet_column.c 3 | -------------------------------------------------------------------------------- /0173_binary_search_tree_iterator/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test bst_iter.c 3 | -------------------------------------------------------------------------------- /0191_number_of_one_bits/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test hamming_weight.c 3 | -------------------------------------------------------------------------------- /0199_binary_tree_right_side_view/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test bst_right.c 3 | -------------------------------------------------------------------------------- /0203_remove_linked_list_element/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test rm_elem.c 3 | -------------------------------------------------------------------------------- /0205_isomorphic_strings/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test isomorphic_strings.c 3 | -------------------------------------------------------------------------------- /0209_minimum_size_subarray_sum/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test mini_size.c 3 | -------------------------------------------------------------------------------- /0210_course_schedule_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test course_schedule.c 3 | -------------------------------------------------------------------------------- /0216_combination_sum_iii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test combination_sum.c 3 | -------------------------------------------------------------------------------- /0226_invert_binary_tree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test invert_binary_tree.c 3 | -------------------------------------------------------------------------------- /0230_kth_smallest_element_in_a_bst/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test kth_bst.c 3 | -------------------------------------------------------------------------------- /0239_sliding_window_maximum/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test slide_window.c 3 | -------------------------------------------------------------------------------- /0300_longest_increasing_subsequence/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test lis.c 3 | -------------------------------------------------------------------------------- /0354_russian_doll_envelopes/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test russian_doll.c 3 | -------------------------------------------------------------------------------- /0516_longest_palindromic_subsequence/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test lps.c 3 | -------------------------------------------------------------------------------- /0016_three_sum_closest/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test three_sum_closest.c -lm 3 | -------------------------------------------------------------------------------- /0025_reverse_nodes_in_k_group/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test reverse_nodes.c 3 | -------------------------------------------------------------------------------- /0026_remove_duplicates_from_sorted_array/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test rm_dup.c 3 | -------------------------------------------------------------------------------- /0035_search_insert_position/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test insert_position.c 3 | -------------------------------------------------------------------------------- /0041_first_missing_positive/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test missing_positive.c 3 | -------------------------------------------------------------------------------- /0060_permutation_sequence/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test permutation_sequence.c 3 | -------------------------------------------------------------------------------- /0076_minimum_window_substring/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test window_substring.c 3 | -------------------------------------------------------------------------------- /0083_remove_duplicates_from_sorted_list/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test rm_dup.c 3 | -------------------------------------------------------------------------------- /0095_unique_binary_search_trees_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test unique_bst.c 3 | -------------------------------------------------------------------------------- /0097_interleaving_string/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test interleaving_string.c 3 | -------------------------------------------------------------------------------- /0099_recover_binary_search_tree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test recover_bst.c 3 | -------------------------------------------------------------------------------- /0102_binary_tree_level_order_traversal/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test bst_bfs.c 3 | -------------------------------------------------------------------------------- /0104_maximum_depth_of_binary_tree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test bst_depth.c 3 | -------------------------------------------------------------------------------- /0111_minimum_depth_of_binary_tree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test bst_depth.c 3 | -------------------------------------------------------------------------------- /0114_flatten_binary_tree_to_linked_list/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test flatten.c 3 | -------------------------------------------------------------------------------- /0122_best_time_to_buy_and_sell_stock_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test stock.c 3 | -------------------------------------------------------------------------------- /0123_best_time_to_buy_and_sell_stock_iii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test stock.c 3 | -------------------------------------------------------------------------------- /0124_binary_tree_maximum_path_sum/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test bst_max_path.c 3 | -------------------------------------------------------------------------------- /0128_longest_consecutive_sequence/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test consec_seq.c 3 | -------------------------------------------------------------------------------- /0138_copy_list_with_random_pointer/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test copy_list.c 3 | -------------------------------------------------------------------------------- /0150_evaluate_reverse_polish_notation/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test eval_rpn.c 3 | -------------------------------------------------------------------------------- /0166_fraction_to_recurring_decimal/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test fraction.c 3 | -------------------------------------------------------------------------------- /0171_excel_sheet_column_number/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test sheet_column.c 3 | -------------------------------------------------------------------------------- /0188_best_time_to_buy_and_sell_stock_iv/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test stock.c 3 | -------------------------------------------------------------------------------- /0214_shortest_palindrome/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test shortest_palindrome.c 3 | -------------------------------------------------------------------------------- /0215_kth_largest_element_in_an_array/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test kth_elem.c 3 | -------------------------------------------------------------------------------- /0010_regular_expression_matching/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test regular_expression.c 3 | -------------------------------------------------------------------------------- /0019_remove_nth_node_from_end_of_list/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test remove_end.c 3 | -------------------------------------------------------------------------------- /0032_longest_valid_parentheses/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test valid_parentheses.c 3 | -------------------------------------------------------------------------------- /0033_search_in_rotated_sorted_array/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test rotated_array.c 3 | -------------------------------------------------------------------------------- /0080_remove_duplicates_from_sorted_array_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test rm_dups.c 3 | -------------------------------------------------------------------------------- /0082_remove_duplicates_from_sorted_list_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test rm_dup.c 3 | -------------------------------------------------------------------------------- /0107_binary_tree_level_order_traversal_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test bst_bfs.c 3 | -------------------------------------------------------------------------------- /0131_palindrome_patitioning/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test palindrome_partition.c 3 | -------------------------------------------------------------------------------- /0132_palindrome_patitioning_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test palindrome_partition.c 3 | -------------------------------------------------------------------------------- /0144_binary_tree_preorder_traversal/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test bst_preorder.c 3 | -------------------------------------------------------------------------------- /0145_binary_tree_postorder_traversal/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test bst_postorder.c 3 | -------------------------------------------------------------------------------- /0153_find_minimum_in_rotated_sorted_array/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test minimum.c 3 | -------------------------------------------------------------------------------- /0154_find_minimum_in_rotated_sorted_array_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test minimum.c 3 | -------------------------------------------------------------------------------- /0160_intersection_of_two_linked_list/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test intersection.c 3 | -------------------------------------------------------------------------------- /0236_lowest_common_ancestor_of_a_binary_tree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test bst_lca.c 3 | -------------------------------------------------------------------------------- /0567_permutation_in_string/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test permutation_in_string.c 3 | -------------------------------------------------------------------------------- /0084_largest_rectangle_in_histogram/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test rect_in_histogram.c 3 | -------------------------------------------------------------------------------- /0094_binary_tree_inorder_traversal/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test bst_inorder_traversal.c 3 | -------------------------------------------------------------------------------- /0103_binary_tree_zigzag_level_order_traversal/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test bst_zigzag.c 3 | -------------------------------------------------------------------------------- /0116_populating_next_right_pointers_in_each_node/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test connect.c 3 | -------------------------------------------------------------------------------- /0297_serialize_and_deserialize_binary_tree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test tree_serdes.c 3 | -------------------------------------------------------------------------------- /0438_find_all_anagrams_in_a_string/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test anagrams_in_string.c 3 | -------------------------------------------------------------------------------- /0990_satisfiability_of_equality_equations/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -o test equality_equations.c 3 | -------------------------------------------------------------------------------- /0004_median_of_two_sorted_array/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test median_of_two_sorted_array.c 3 | -------------------------------------------------------------------------------- /0030_substring_with_concatenation_of_all_words/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test concatenation.c 3 | -------------------------------------------------------------------------------- /0081_search_in_rotated_sorted_array_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test search_rotated_array.c 3 | -------------------------------------------------------------------------------- /0108_convert_sorted_array_to_binary_search_tree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test bst_convert.c 3 | -------------------------------------------------------------------------------- /0109_convert_sorted_list_to_binary_search_tree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test bst_convert.c 3 | -------------------------------------------------------------------------------- /0117_populating_next_right_pointers_in_each_node_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test connect.c 3 | -------------------------------------------------------------------------------- /0235_lowest_common_ancestor_of_a_binary_search_tree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test bst_lca.c 3 | -------------------------------------------------------------------------------- /0005_longest_palindromic_substring/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test longest_palindromic_substring.c 3 | -------------------------------------------------------------------------------- /1312_minimum_insertion_steps_to_make_a_string_palindrome/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test insertion.c 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LEETCODE SOLUTIONS 2 | Easy and Understandable C/C++ Solutions of Some Leetcode Questions. 3 | -------------------------------------------------------------------------------- /0003_longest_substring_without_repeat/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test longest_substring_without_repeat.c 3 | -------------------------------------------------------------------------------- /0460_lfu_cache/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test lfu_cache.c 3 | #g++ -O1 -std=c++11 -o test lfu_cache.cc 4 | -------------------------------------------------------------------------------- /0017_letter_combinations_of_a_phone_number/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o letter_combinations letter_combinations.c 3 | -------------------------------------------------------------------------------- /0105_construct_binary_tree_from_preorder_and_inorder_traversal/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test binary_tree_build.c 3 | -------------------------------------------------------------------------------- /0106_construct_binary_tree_from_inorder_and_postorder_traversal/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -O1 -o test binary_tree_build.c 3 | -------------------------------------------------------------------------------- /0172_factorial_trailing_zeros/zeroes.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int trailingZeroes(int n) { 8 | return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5); 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /0991_broken_calculator/calculator.cc: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int brokenCalc(int X, int Y) { 4 | int step = 0; 5 | while (X < Y) { 6 | Y = Y & 1 ? Y + 1 : Y / 2; 7 | step++; 8 | } 9 | step += X - Y; 10 | return step; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /0136_single_number/single_number.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int singleNumber(vector& nums) { 8 | int res = nums[0]; 9 | for (int i = 1; i < nums.size(); i++) { 10 | res ^= nums[i]; 11 | } 12 | return res; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /0189_rotate_array/rotate_array.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | void rotate(vector& nums, int k) { 8 | k %= nums.size(); 9 | reverse(nums.begin(), nums.end() - k); 10 | reverse(nums.end() - k, nums.end()); 11 | reverse(nums.begin(), nums.end()); 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /0162_find_peak_element/peak.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int findPeakElement(vector& nums) { 8 | int i; 9 | for (i = 1; i < nums.size(); i++) { 10 | if (nums[i] < nums[i - 1]) { 11 | break; 12 | } 13 | } 14 | return i - 1; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Libraries 12 | *.lib 13 | *.a 14 | *.la 15 | *.lo 16 | 17 | # Shared objects (inc. Windows DLLs) 18 | *.dll 19 | *.so 20 | *.so.* 21 | *.dylib 22 | 23 | # Executables 24 | *.exe 25 | *.out 26 | *.app 27 | *.i*86 28 | *.x86_64 29 | *.hex 30 | 31 | # Debug files 32 | *.dSYM/ 33 | -------------------------------------------------------------------------------- /0027_remove_element/rm_elem.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int removeElement(vector& nums, int val) { 8 | int count = 0; 9 | for (int i = 0; i < nums.size(); i++) { 10 | if (nums[i] != val) { 11 | nums[count++] = nums[i]; 12 | } 13 | } 14 | return count; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /0070_climbing_stairs/climb_stairs.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int climbStairs(int n) { 8 | int a = 1; 9 | int b = 2; 10 | int c = 0; 11 | for (int i = 3; i <= n; i++) { 12 | c = a + b; 13 | a = b; 14 | b = c; 15 | } 16 | return n == 1 ? a : (n == 2 ? b : c); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /0026_remove_duplicates_from_sorted_array/rm_dup.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int removeDuplicates(vector& nums) { 8 | int size = 0; 9 | for (int i = 1; i < nums.size(); i++) { 10 | if (nums[size] != nums[i]) { 11 | nums[++size] = nums[i]; 12 | } 13 | } 14 | 15 | return size + 1; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /0122_best_time_to_buy_and_sell_stock_ii/stock.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int maxProfit(vector& prices) { 8 | int diff, sum = 0; 9 | for (int i = 1; i < prices.size(); i++) { 10 | diff = prices[i] - prices[i - 1]; 11 | if (diff > 0) { 12 | sum += diff; 13 | } 14 | } 15 | return sum; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /0300_longest_increasing_subsequence/lis.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int lengthOfLIS(vector& nums) { 8 | vector ans; 9 | for (int x : nums) { 10 | auto it = lower_bound(ans.begin(), ans.end(), x); 11 | if (it == ans.end()) { 12 | ans.push_back(x); 13 | } 14 | else *it = x; 15 | } 16 | return ans.size(); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /0172_factorial_trailing_zeros/zeroes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static int trailingZeroes(int n) 5 | { 6 | /* As 10 = 2 * 5 so we just count how many fives in it */ 7 | return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5); 8 | } 9 | 10 | int main(int argc, char **argv) 11 | { 12 | if (argc != 2) { 13 | fprintf(stderr, "Usage: ./test n\n"); 14 | exit(-1); 15 | } 16 | 17 | printf("%d\n", trailingZeroes(atoi(argv[1]))); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /0171_excel_sheet_column_number/sheet_column.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static int titleToNumber(char *s) 5 | { 6 | int n = 0; 7 | while (*s != '\0') { 8 | n *= 26; 9 | n += *s++ - 'A' + 1; 10 | } 11 | 12 | return n; 13 | } 14 | 15 | int main(int argc, char **argv) 16 | { 17 | if (argc != 2) { 18 | fprintf(stderr, "Usage: ./test ABC\n"); 19 | exit(-1); 20 | } 21 | 22 | printf("%d\n", titleToNumber(argv[1])); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /0053_maximum_subarray/max_subarray.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int maxSubArray(vector& nums) { 8 | int sum = 0, max_sum = INT_MIN; 9 | for (int i = 0; i < nums.size(); i++) { 10 | if (sum < 0) { 11 | sum = nums[i]; 12 | } else { 13 | sum += nums[i]; 14 | } 15 | max_sum = max(sum, max_sum); 16 | } 17 | return max_sum; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /0096_unique_binary_search_trees/unique_bst.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /* 6 | * f(n) = f(0)f(n-1) + f(1)f(n-2) + ... + f(n-2)f(1) + f(n-1)f(0) 7 | */ 8 | class Solution { 9 | public: 10 | int numTrees(int n) { 11 | vector sum(n + 1); 12 | sum[0] = 1; 13 | for (int i = 1; i <= n; i++) { 14 | for (int j = 0; j < i; j++) { 15 | sum[i] += sum[j] * sum[i - j - 1]; 16 | } 17 | } 18 | return sum[n]; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0035_search_insert_position/insert_position.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int searchInsert(vector& nums, int target) { 8 | int lo = -1; 9 | int hi = nums.size(); 10 | while (lo + 1 < hi) { 11 | int mid = lo + (hi - lo) / 2; 12 | if (target > nums[mid]) { 13 | lo = mid; 14 | } else { 15 | hi = mid; 16 | } 17 | } 18 | return hi; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /0191_number_of_one_bits/hamming_weight.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static int hammingWeight(uint32_t n) 6 | { 7 | int count = 0; 8 | while (n > 0) { 9 | n = n & (n - 1); 10 | count++; 11 | } 12 | return count; 13 | } 14 | 15 | int main(int argc, char **argv) 16 | { 17 | if (argc != 2) { 18 | fprintf(stderr, "Usage: ./test n\n"); 19 | exit(-1); 20 | } 21 | 22 | printf("%d\n", hammingWeight(atoi(argv[1]))); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /0104_maximum_depth_of_binary_tree/bst_depth.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct TreeNode { 5 | int val; 6 | struct TreeNode *left; 7 | struct TreeNode *right; 8 | }; 9 | 10 | static int maxDepth(struct TreeNode* root) 11 | { 12 | if (root == NULL) { 13 | return 0; 14 | } 15 | int l = maxDepth(root->left) + 1; 16 | int r = maxDepth(root->right) + 1; 17 | return l > r ? l : r; 18 | } 19 | 20 | int main(void) 21 | { 22 | printf("%d\n", maxDepth(NULL)); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /0136_single_number/single_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static int singleNumber(int *nums, int numsSize) 5 | { 6 | int i, s = 0; 7 | for (i = 0; i < numsSize; i++) { 8 | s ^= nums[i]; 9 | } 10 | return s; 11 | } 12 | 13 | int main(int argc, char **argv) 14 | { 15 | int i, count = argc - 1; 16 | int *nums = malloc(count * sizeof(int)); 17 | for (i = 0; i < count; i++) { 18 | nums[i] = atoi(argv[i + 1]); 19 | } 20 | printf("%d\n", singleNumber(nums, count)); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /0169_majority_element/majority.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int majorityElement(vector& nums) { 8 | int major, count = 0; 9 | for (int i = 0; i < nums.size(); i++) { 10 | if (count == 0) { 11 | major = nums[i]; 12 | count++; 13 | } else if (nums[i] != major) { 14 | count--; 15 | } else { 16 | count++; 17 | } 18 | } 19 | return major; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /0198_house_robber/robber.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int rob(vector& nums) { 8 | int taken = 0; 9 | int untaken = 0; 10 | for (int i = 0; i < nums.size(); i++) { 11 | int last_taken = taken; 12 | /* last untaken + nums[i]*/ 13 | taken = untaken + nums[i]; 14 | /* max(last untaken, last taken) */ 15 | untaken = max(untaken, last_taken); 16 | } 17 | return max(taken, untaken); 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /0215_kth_largest_element_in_an_array/kth_elem.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int findKthLargest(vector& nums, int k) { 8 | priority_queue, greater> queue; 9 | for (const auto i : nums) { 10 | if (queue.size() < k) { 11 | queue.push(i); 12 | } else if (i > queue.top()) { 13 | queue.pop(); 14 | queue.push(i); 15 | } 16 | } 17 | return queue.top(); 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /0991_broken_calculator/calculator.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int brokenCalc(int X, int Y) 6 | { 7 | int step = 0; 8 | while (X < Y) { 9 | Y = Y & 1 ? Y + 1 : Y / 2; 10 | step++; 11 | } 12 | step += X - Y; 13 | return step; 14 | } 15 | 16 | int main(int argc, char **argv) 17 | { 18 | if (argc != 3) { 19 | fprintf(stderr, "Usage: ./test x y"); 20 | exit(-1); 21 | } 22 | 23 | int x = atoi(argv[1]); 24 | int y = atoi(argv[2]); 25 | printf("%d\n", brokenCalc(x, y)); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /0066_plus_one/plus_one.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | vector plusOne(vector& digits) { 8 | int carry = 1; 9 | vector res; 10 | for (int i = digits.size() - 1; i >= 0; i--) { 11 | int d = digits[i] + carry; 12 | res.push_back(d % 10); 13 | carry = d / 10; 14 | } 15 | 16 | if (carry > 0) { 17 | res.push_back(carry); 18 | } 19 | 20 | reverse(res.begin(), res.end()); 21 | return res; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /0162_find_peak_element/peak.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int findPeakElement(int* nums, int numsSize) 6 | { 7 | int i; 8 | for (i = 1; i < numsSize; i++) { 9 | if (nums[i] < nums[i - 1]) { 10 | break; 11 | } 12 | } 13 | return i - 1; 14 | } 15 | 16 | int main(int argc, char **argv) 17 | { 18 | int i, count = argc - 1; 19 | int *nums = malloc(count * sizeof(int)); 20 | for (i = 0; i < count; i++) { 21 | nums[i] = atoi(argv[i + 1]); 22 | } 23 | printf("%d\n", findPeakElement(nums, count)); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /0003_longest_substring_without_repeat/longest_substring_without_repeat.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int lengthOfLongestSubstring(string s) { 8 | vector count(256); 9 | int len = 0; 10 | int i, j; 11 | 12 | for (i = 0, j = 0; i < s.length(); i++) { 13 | count[s[i]]++; 14 | while (count[s[i]] > 1) { 15 | len = i - j > len ? i - j : len; 16 | count[s[j++]] -= 1; 17 | } 18 | } 19 | 20 | return i - j > len ? i - j : len; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /0078_subsets/subsets.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | vector> subsets(vector& nums) { 8 | vector> res; 9 | dfs(nums, 0, res); 10 | return res; 11 | } 12 | 13 | private: 14 | vector stack; 15 | void dfs(vector& nums, int start, vector>& res) { 16 | res.push_back(stack); 17 | for (int i = start; i < nums.size(); i++) { 18 | stack.push_back(nums[i]); 19 | dfs(nums, i + 1, res); 20 | stack.pop_back(); 21 | } 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /0058_length_of_last_word/word_length.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int lengthOfLastWord(char *s) 6 | { 7 | int word_len = 0; 8 | int len = strlen(s); 9 | 10 | while (len > 0 && s[--len] == ' ') {} 11 | 12 | while (len >= 0 && s[len] != ' ') { 13 | word_len++; 14 | len--; 15 | } 16 | 17 | return word_len; 18 | } 19 | 20 | int main(int argc, char **argv) 21 | { 22 | if (argc != 2) { 23 | fprintf(stderr, "Usage: ./test word\n"); 24 | exit(-1); 25 | } 26 | 27 | printf("%d\n", lengthOfLastWord(argv[1])); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /0088_merge_sorted_array/merge_array.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | void merge(vector& nums1, int m, vector& nums2, int n) { 8 | int i = m - 1; 9 | int j = n - 1; 10 | int k = nums1.size() - 1; 11 | while (i >= 0 && j >= 0) { 12 | if (nums1[i] < nums2[j]) { 13 | nums1[k--] = nums2[j--]; 14 | } else { 15 | nums1[k--] = nums1[i--]; 16 | } 17 | } 18 | 19 | while (j >= 0) { 20 | nums1[k--] = nums2[j--]; 21 | } 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /0201_bitwise_and_of_numbers_range/and.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static int rangeBitwiseAnd(int m, int n) 5 | { 6 | int i, res = 0; 7 | for (i = 0; m > 0 && n > 0; i++) { 8 | if (m == n && (m & 1)) { 9 | res |= 1 << i; 10 | } 11 | 12 | m = m >> 1; 13 | n = n >> 1; 14 | } 15 | 16 | return res; 17 | } 18 | 19 | int main(int argc, char **argv) 20 | { 21 | if (argc != 3) { 22 | fprintf(stderr, "Usage: ./test m n\n"); 23 | exit(-1); 24 | } 25 | 26 | printf("%d\n", rangeBitwiseAnd(atoi(argv[1]), atoi(argv[2]))); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /0007_reverse_integer/reverse_integer.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int reverse(int x) { 8 | int n = 0; 9 | while (x != 0) { 10 | // Checking the over/underflow. 11 | int r = x % 10; 12 | if (n > INT_MAX / 10 || (n == INT_MAX / 10 && r > 7)) { 13 | return 0; 14 | } 15 | if (n < INT_MIN / 10 || (n == INT_MIN / 10 && r < -8)) { 16 | return 0; 17 | } 18 | 19 | n = n * 10 + r; 20 | x = x / 10; 21 | } 22 | return n; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /0014_longest_common_prefix/common_prefix.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | string longestCommonPrefix(vector& strs) { 8 | string lcp; 9 | if (strs.empty()) { 10 | return lcp; 11 | } 12 | for (int j = 0; j < strs[0].length(); j++) { 13 | char c = strs[0][j]; 14 | for (int i = 1; i < strs.size(); i++) { 15 | if (c != strs[i][j]) { 16 | return lcp; 17 | } 18 | } 19 | lcp.push_back(c); 20 | } 21 | return lcp; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /0011_container_with_most_water/container.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int maxArea(vector& height) { 8 | int min = 0, max = height.size() - 1; 9 | int area_max = 0; 10 | while (min < max) { 11 | int area = (max - min) * (height[min] < height[max] ? height[min] : height[max]); 12 | area_max = area > area_max ? area : area_max; 13 | if (height[min] < height[max]) { 14 | min++; 15 | } else { 16 | max--; 17 | } 18 | } 19 | return area_max; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /0121_best_time_to_buy_and_sell_stock/stock.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int maxProfit(vector& prices) { 8 | if (prices.size() == 0) { 9 | return 0; 10 | } 11 | 12 | int diff = 0; 13 | int minimum = prices[0]; 14 | for (int i = 1; i < prices.size(); i++) { 15 | if (prices[i] < minimum) { 16 | minimum = prices[i]; 17 | } else { 18 | diff = prices[i] - minimum > diff ? prices[i] - minimum : diff; 19 | } 20 | } 21 | 22 | return diff; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /0001_two_sum/two_sum.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | vector twoSum(vector& nums, int target) { 8 | vector res; 9 | unordered_map ht; 10 | for (int i = 0; i < nums.size(); i++) { 11 | int other = target - nums[i]; 12 | if (ht.count(other)) { 13 | /* Only one solution for purpose of this problem */ 14 | res.append(ht[other]); 15 | res.append(i); 16 | return res; 17 | } 18 | ht[nums[i]] = i; 19 | } 20 | return res; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /0122_best_time_to_buy_and_sell_stock_ii/stock.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | static int maxProfit(int* prices, int pricesSize) 7 | { 8 | int i, j, total = 0; 9 | for (i = 1; i < pricesSize; i++) { 10 | total += prices[i] > prices[i - 1] ? prices[i] - prices[i - 1] : 0; 11 | } 12 | return total; 13 | } 14 | 15 | int main(int argc, char **argv) 16 | { 17 | int i, count = argc - 1; 18 | int *nums = malloc(count * sizeof(int)); 19 | for (i = 0; i < count; i++) { 20 | nums[i] = atoi(argv[i + 1]); 21 | } 22 | printf("%d\n", maxProfit(nums, count)); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /0050_pow/pow.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | double myPow(double x, int n) { 8 | if (n == INT_MIN) { 9 | double t = dfs(x, -(n / 2)); 10 | return 1 / t * 1 / t; 11 | } else { 12 | return n < 0 ? 1 / dfs(x, -n) : dfs(x, n); 13 | } 14 | } 15 | 16 | private: 17 | double dfs(double x, int n) { 18 | if (n == 0) { 19 | return 1; 20 | } else if (n == 1) { 21 | return x; 22 | } else { 23 | double t = dfs(x, n / 2); 24 | return (n % 2) ? (x * t * t) : (t * t); 25 | } 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /0179_largest_number/largest_number.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | string largestNumber(vector& nums) { 8 | vector strs; 9 | for (const auto i : nums) { 10 | strs.push_back(to_string(i)); 11 | } 12 | 13 | auto cmp = [](string s1, string s2) { return s1 + s2 > s2 + s1; }; 14 | sort(strs.begin(), strs.end(), cmp); 15 | 16 | if (strs[0] == "0") { 17 | return "0"; 18 | } 19 | 20 | string res; 21 | for (const auto& s : strs) { 22 | res += s; 23 | } 24 | return res; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /0167_two_sum_ii/two_sum.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | vector twoSum(vector& numbers, int target) { 8 | vector res; 9 | int i = 0; 10 | int j = numbers.size() - 1; 11 | while (i < j) { 12 | int sum = numbers[i] + numbers[j]; 13 | if (sum < target) { 14 | i++; 15 | } else if (sum > target) { 16 | j--; 17 | } else { 18 | res.push_back(i + 1); 19 | res.push_back(j + 1); 20 | break; 21 | } 22 | } 23 | return res; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /0204_count_primes/count_primes.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int countPrimes(int n) { 8 | if (n < 3) { 9 | return 0; 10 | } 11 | 12 | vector marked(n); 13 | int count = n >> 1; 14 | for (int i = 3; i * i <= n; i += 2) { 15 | if (!marked[i]) { 16 | for (int j = i * i; j < n; j += (i << 1)) { 17 | if (!marked[j]) { 18 | marked[j] = true; 19 | --count; 20 | } 21 | } 22 | } 23 | } 24 | return count; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /0704_binary_search/binary_search.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int search(int* nums, int numsSize, int target){ 4 | if(target > nums[numsSize-1] || target < nums[0])return -1; 5 | int begin = -1 ,end = numsSize; 6 | while(begin < end-1){ 7 | int half = (begin+end)/2; 8 | if(nums[half] 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | vector> combine(int n, int k) { 8 | vector> res; 9 | dfs(n, k, 1, res); 10 | return res; 11 | } 12 | 13 | private: 14 | vector stack; 15 | void dfs(int n, int k, int start, vector>& res) { 16 | if (stack.size() == k) { 17 | res.push_back(stack); 18 | } else { 19 | for (int i = start; i <= n; i++) { 20 | stack.push_back(i); 21 | dfs(n, k, i + 1, res); 22 | stack.pop_back(); 23 | } 24 | } 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /0100_same_tree/same_tree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct TreeNode { 6 | int val; 7 | struct TreeNode *left; 8 | struct TreeNode *right; 9 | }; 10 | 11 | static bool isSameTree(struct TreeNode* p, struct TreeNode* q) 12 | { 13 | if (p == NULL && q == NULL) { 14 | return true; 15 | } 16 | if (p == NULL || q == NULL) { 17 | return false; 18 | } 19 | if (p->val != q->val) { 20 | return false; 21 | } 22 | return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); 23 | } 24 | 25 | int main(void) 26 | { 27 | printf("%s\n", isSameTree(NULL, NULL) ? "true" : "false"); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /0096_unique_binary_search_trees/unique_bst.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* 5 | * f(n) = f(0)f(n-1) + f(1)f(n-2) + ... + f(n-2)f(1) + f(n-1)f(0) 6 | */ 7 | 8 | static int numTrees(int n) { 9 | int i, j; 10 | int *sum = malloc((n + 1) * sizeof(int)); 11 | sum[0] = 1; 12 | for (i = 1; i <= n; i++) { 13 | for (j = 0; j < i; j++) { 14 | sum[i] += sum[j] * sum[i - j - 1]; 15 | } 16 | } 17 | return sum[n]; 18 | } 19 | 20 | 21 | int main(int argc, char **argv) 22 | { 23 | if (argc != 2) { 24 | fprintf(stderr, "Usage: ./test n\n"); 25 | exit(-1); 26 | } 27 | printf("%d\n", numTrees(atoi(argv[1]))); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /0048_rotate_image/rotate.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | void rotate(vector>& matrix) { 8 | int size = matrix.size(); 9 | for (int i = 0; i < size / 2; i++) { 10 | int low = i, high = size - i - 1; 11 | for (int j = low; j < high; j++) { 12 | int tmp = matrix[i][j]; 13 | matrix[i][j] = matrix[size - 1 - j][i]; 14 | matrix[size - 1 - j][i] = matrix[size - 1 - i][size - 1 - j]; 15 | matrix[size - 1 - i][size - 1 - j] = matrix[j][size - 1 - i]; 16 | matrix[j][size - 1 - i] = tmp; 17 | } 18 | } 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /0141_linked_list_cycle/list_cycle.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for singly-linked list. 7 | * struct ListNode { 8 | * int val; 9 | * ListNode *next; 10 | * ListNode(int x) : val(x), next(nullptr) {} 11 | * }; 12 | */ 13 | class Solution { 14 | public: 15 | bool hasCycle(ListNode *head) { 16 | struct ListNode *fast = head; 17 | struct ListNode *slow = head; 18 | while (fast != nullptr && fast->next != nullptr) { 19 | slow = slow->next; 20 | fast = fast->next->next; 21 | if (fast == slow) { 22 | return true; 23 | } 24 | } 25 | return false; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /0104_maximum_depth_of_binary_tree/bst_depth.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * struct TreeNode { 8 | * int val; 9 | * TreeNode *left; 10 | * TreeNode *right; 11 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 13 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 14 | * }; 15 | */ 16 | class Solution { 17 | public: 18 | int maxDepth(TreeNode* root) { 19 | if (root == nullptr) { 20 | return 0; 21 | } 22 | return 1 + max(maxDepth(root->left), maxDepth(root->right)); 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /0111_minimum_depth_of_binary_tree/bst_depth.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct TreeNode { 5 | int val; 6 | struct TreeNode *left; 7 | struct TreeNode *right; 8 | }; 9 | 10 | static int minDepth(struct TreeNode* root) 11 | { 12 | if (root == NULL) { 13 | return 0; 14 | } 15 | 16 | int l = minDepth(root->left) + 1; 17 | int r = minDepth(root->right) + 1; 18 | return l < r ? (l > 1 ? l : r) : (r > 1 ? r : l); 19 | } 20 | 21 | int main(void) 22 | { 23 | struct TreeNode root, n1; 24 | root.val = 1; 25 | n1.val = 2; 26 | root.left = &n1; 27 | root.right = NULL; 28 | n1.left = NULL; 29 | n1.right = NULL; 30 | printf("%d\n", minDepth(&root)); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /0416_partition_equal_subset_sum/partition.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | bool canPartition(vector& nums) { 8 | int sum = 0; 9 | for (int n : nums) { 10 | sum += n; 11 | } 12 | if (sum % 2 != 0) { 13 | return false; 14 | } 15 | 16 | vector dp(sum / 2 + 1, false); 17 | dp[0] = true; 18 | for (int i = 0; i < nums.size(); i++) { 19 | for (int j = sum / 2 ; j >= 0; j--) { 20 | if (j >= nums[i]) { 21 | dp[j] = dp[j] || dp[j - nums[i]]; 22 | } 23 | } 24 | } 25 | 26 | return dp[sum / 2]; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /1312_minimum_insertion_steps_to_make_a_string_palindrome/insertion.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int minInsertions(string s) { 8 | int len = s.length(); 9 | vector dp(len); 10 | for (int i = len - 2; i >= 0; i--) { 11 | int left_down = 0; 12 | for (int j = i + 1; j < len; j++) { 13 | int down = dp[j]; 14 | if (s[i] == s[j]) { 15 | dp[j] = left_down; 16 | } else { 17 | dp[j] = min(down, dp[j - 1]) + 1; 18 | } 19 | left_down = down; 20 | } 21 | } 22 | return dp[len - 1]; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /0027_remove_element/rm_elem.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int removeElement(int *nums, int numsSize, int val) 6 | { 7 | int i, count = 0; 8 | for (i = 0; i < numsSize; i++) { 9 | if (nums[i] != val) { 10 | nums[count++] = nums[i]; 11 | } 12 | } 13 | return count; 14 | } 15 | 16 | int main(int argc, char **argv) 17 | { 18 | int i; 19 | int *nums = malloc((argc - 2) * sizeof(int)); 20 | for (i = 0; i < argc - 2; i++) { 21 | nums[i] = atoi(argv[i + 2]); 22 | } 23 | 24 | int count = removeElement(nums, argc - 2, atoi(argv[1])); 25 | for (i = 0; i < count; i++) { 26 | printf("%d ", nums[i]); 27 | } 28 | printf("\n"); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /0035_search_insert_position/insert_position.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static int searchInsert(int* nums, int numsSize, int target) 5 | { 6 | int low = -1; 7 | int high = numsSize; 8 | while (low + 1 < high) { 9 | int mid = low + (high - low) / 2; 10 | if (target > nums[mid]) { 11 | low = mid; 12 | } else { 13 | high = mid; 14 | } 15 | } 16 | return high; 17 | } 18 | 19 | int main(int argc, char **argv) 20 | { 21 | int i; 22 | int *nums = malloc((argc - 2) * sizeof(int)); 23 | for (i = 0; i < argc - 2; i++) { 24 | nums[i] = atoi(argv[i + 2]); 25 | } 26 | printf("%d\n", searchInsert(nums, argc - 2, atoi(argv[1]))); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /0050_pow/pow.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | double fast_pow(double x, int n) 7 | { 8 | if (n == 0) { return 1.0; } 9 | if (n == 1) { return x; } 10 | double t = fast_pow(x, n / 2); 11 | return n & 1 ? t * t * x : t * t; 12 | } 13 | 14 | double my_pow(double x, int n) 15 | { 16 | if (n == INT_MIN) { 17 | double t = 1 / fast_pow(x, -(n / 2)); 18 | return t * t; 19 | } 20 | return n < 0 ? 1 / fast_pow(x, -n) : fast_pow(x, n); 21 | } 22 | 23 | int main(int argc, char **argv) 24 | { 25 | if (argc != 3) { 26 | fprintf(stderr, "Usage: ./test x n\n"); 27 | exit(-1); 28 | } 29 | printf("%lf\n", my_pow(atoi(argv[1]), atoi(argv[2]))); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /0135_candy/candy.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int candy(vector& ratings) { 8 | vector candies(ratings.size(), 1); 9 | for (int i = 1; i < ratings.size(); i++) { 10 | if (ratings[i] > ratings[i - 1]) { 11 | candies[i] = candies[i - 1] + 1; 12 | } 13 | } 14 | 15 | int sum = candies[ratings.size() - 1]; 16 | for (int i = ratings.size() - 2; i >= 0; i--) { 17 | if (ratings[i] > ratings[i + 1] && candies[i] <= candies[i + 1]) { 18 | candies[i] = candies[i + 1] + 1; 19 | } 20 | sum += candies[i]; 21 | } 22 | return sum; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /0153_find_minimum_in_rotated_sorted_array/minimum.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static int findMin(int* nums, int numsSize) 6 | { 7 | int lo = 0; 8 | int hi = numsSize - 1; 9 | while (lo < hi) { 10 | int mid = lo + (hi - lo) / 2; 11 | if (nums[mid] < nums[hi]) { 12 | hi = mid; 13 | } else { 14 | lo = mid + 1; 15 | } 16 | } 17 | return nums[lo]; 18 | } 19 | 20 | int main(int argc, char **argv) 21 | { 22 | int i, count = argc - 1; 23 | int *nums = malloc(count * sizeof(int)); 24 | for (i = 0; i < count; i++) { 25 | nums[i] = atoi(argv[i + 1]); 26 | } 27 | printf("%d\n", findMin(nums, count)); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /0026_remove_duplicates_from_sorted_array/rm_dup.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | static int removeDuplicates(int* nums, int numsSize) 6 | { 7 | int i, size = 0; 8 | for (i = 1; i < numsSize; i++) { 9 | if (nums[size] != nums[i]) { 10 | nums[++size] = nums[i]; 11 | } 12 | } 13 | 14 | return size + 1; 15 | } 16 | 17 | int main(int argc, char **argv) 18 | { 19 | int i, size = argc - 1; 20 | int *nums = malloc(size * sizeof(int)); 21 | for (i = 0; i < argc - 1; i++) { 22 | nums[i] = atoi(argv[i + 1]); 23 | } 24 | 25 | int count = removeDuplicates(nums, size); 26 | for (i = 0; i < count; i++) { 27 | printf("%d ", nums[i]); 28 | } 29 | printf("\n"); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /0128_longest_consecutive_sequence/consec_seq.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int longestConsecutive(vector& nums) { 8 | int res = 0; 9 | unordered_set s; 10 | for (int i = 0; i < nums.size(); i++) { 11 | s.insert(nums[i]); 12 | } 13 | for (int n : nums) { 14 | if (!s.count(n - 1)) { 15 | int len = 0; 16 | int num = n; 17 | while (s.count(num)) { 18 | s.erase(num); 19 | num++; 20 | len++; 21 | } 22 | res = len > res ? len : res; 23 | } 24 | } 25 | return res; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /0010_regular_expression_matching/regular_expression.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | static bool isMatch(char *s, char *p) 7 | { 8 | if (*p == '\0') { 9 | return *s == '\0'; 10 | } 11 | 12 | bool first_match = (*s != '\0') && (*s == *p || *p == '.'); 13 | 14 | if (p[1] == '*') { 15 | return isMatch(s, p + 2) || (first_match && isMatch(s + 1, p)); 16 | } else { 17 | return first_match && isMatch(s + 1, p + 1); 18 | } 19 | } 20 | 21 | int main(int argc, char **argv) 22 | { 23 | if (argc != 3) { 24 | fprintf(stderr, "Usage: ./test string pattern\n"); 25 | exit(-1); 26 | } 27 | 28 | printf("%s\n", isMatch(argv[1], argv[2]) ? "true" : "false"); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /0003_longest_substring_without_repeat/longest_substring_without_repeat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | int lengthOfLongestSubstring(char *s) 7 | { 8 | int count[256] = {0}; 9 | int len = 0; 10 | int i, j; 11 | 12 | for (i = 0, j = 0; s[i] != '\0'; i++) { 13 | count[s[i]]++; 14 | while (count[s[i]] > 1) { 15 | len = i - j > len ? i - j : len; 16 | count[s[j++]] -= 1; 17 | } 18 | } 19 | 20 | return i - j > len ? i - j : len; 21 | } 22 | 23 | int main(int argc, char **argv) 24 | { 25 | if (argc != 2) { 26 | fprintf(stderr, "Usage: ./test string\n"); 27 | exit(-1); 28 | } 29 | 30 | printf("%d\n", lengthOfLongestSubstring(argv[1])); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /0005_longest_palindromic_substring/longest_palindromic_substring.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | string longestPalindrome(string s) { 8 | bool dp[1005][1005]; 9 | int n = s.size(); 10 | int strlen = 0, start; 11 | for(int i=n-1; i>=0; i--){ 12 | for(int j=i; j 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int change(int amount, vector& coins) { 8 | vector> dp(coins.size() + 1, vector(amount + 1)); 9 | for (int i = 0; i <= coins.size(); i++) { 10 | dp[i][0] = 1; 11 | } 12 | 13 | for (int i = 1; i <= coins.size(); i++) { 14 | for (int j = 1; j <= amount; j++) { 15 | if (j >= coins[i - 1]) { 16 | dp[i][j] = dp[i - 1][j] + dp[i][j - coins[i - 1]]; 17 | } else { 18 | dp[i][j] = dp[i - 1][j]; 19 | } 20 | } 21 | } 22 | return dp[coins.size()][amount]; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /0213_house_robber_ii/robber.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int rob(vector& nums) { 8 | if (nums.size() == 1) { 9 | return nums[0]; 10 | } else { 11 | return max(rob_(nums.begin() + 1, nums.end()), rob_(nums.begin(), nums.end() - 1)); 12 | } 13 | } 14 | 15 | private: 16 | int rob_(vector::iterator begin, vector::iterator end) { 17 | int taken = 0; 18 | int untaken = 0; 19 | vector::iterator i; 20 | for (i = begin; i != end; i++) { 21 | int tmp_taken = taken; 22 | taken = untaken + *i; 23 | untaken = max(untaken, tmp_taken); 24 | } 25 | return max(taken, untaken); 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /1143_longest_common_subsequence/lcs.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int longestCommonSubsequence(string text1, string text2) { 8 | int l1 = text1.length(); 9 | int l2 = text2.length(); 10 | vector dp(l2 + 1); 11 | int up = 0; 12 | for (int i = 1; i <= l1; i++) { 13 | int left_up = 0; 14 | for (int j = 1; j <= l2; j++) { 15 | up = dp[j]; 16 | if (text1[i - 1] == text2[j - 1]) { 17 | dp[j] = left_up + 1; 18 | } else { 19 | dp[j] = max(up, dp[j - 1]); 20 | } 21 | left_up = up; 22 | } 23 | } 24 | return dp[l2]; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /0516_longest_palindromic_subsequence/lps.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int longestPalindromeSubseq(string s) { 8 | int len = s.length(); 9 | vector dp(len, 1); 10 | // We have to use level traverse to reduce the dp table size 11 | for (int i = len - 2; i >= 0; i--) { 12 | int left_down = 0; 13 | for (int j = i + 1; j < len; j++) { 14 | int down = dp[j]; 15 | if (s[i] == s[j]) { 16 | dp[j] = left_down + 2; 17 | } else { 18 | dp[j] = max(down, dp[j - 1]); 19 | } 20 | left_down = down; 21 | } 22 | } 23 | return dp[len - 1]; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /0121_best_time_to_buy_and_sell_stock/stock.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static int maxProfit(int* prices, int pricesSize) 5 | { 6 | if (pricesSize == 0) { 7 | return 0; 8 | } 9 | 10 | int i, diff = 0, min = prices[0]; 11 | for (i = 1; i < pricesSize; i++) { 12 | if (prices[i] < min) { 13 | min = prices[i]; 14 | } else { 15 | diff = prices[i] - min > diff ? prices[i] - min : diff; 16 | } 17 | } 18 | 19 | return diff; 20 | } 21 | 22 | int main(int argc, char **argv) 23 | { 24 | int i, count = argc - 1; 25 | int *nums = malloc(count * sizeof(int)); 26 | for (i = 0; i < count; i++) { 27 | nums[i] = atoi(argv[i + 1]); 28 | } 29 | printf("%d\n", maxProfit(nums, count)); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /0216_combination_sum_iii/combination_sum.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | vector> combinationSum3(int k, int n) { 8 | vector> res; 9 | dfs(1, 9, k, n, res); 10 | return res; 11 | } 12 | private: 13 | vector stack; 14 | void dfs(int start, int size, int k, int target, vector>& res) { 15 | if (stack.size() == k) { 16 | if (target == 0) { 17 | res.push_back(stack); 18 | } 19 | } else { 20 | for (int i = start; i <= size; i++) { 21 | stack.push_back(i); 22 | dfs(i + 1, size, k, target - i, res); 23 | stack.pop_back(); 24 | } 25 | } 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /0154_find_minimum_in_rotated_sorted_array_ii/minimum.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static int findMin(int* nums, int numsSize) 5 | { 6 | int lo = 0; 7 | int hi = numsSize - 1; 8 | 9 | while (lo < hi) { 10 | int mid = lo + (hi - lo) / 2; 11 | if (nums[mid] < nums[hi]) { 12 | hi = mid; 13 | } else if (nums[mid] > nums[hi]) { 14 | lo = mid + 1; 15 | } else { 16 | hi--; 17 | } 18 | } 19 | 20 | return nums[lo]; 21 | } 22 | 23 | int main(int argc, char **argv) 24 | { 25 | int i, count = argc - 1; 26 | int *nums = malloc(count * sizeof(int)); 27 | for (i = 0; i < count; i++) { 28 | nums[i] = atoi(argv[i + 1]); 29 | } 30 | printf("%d\n", findMin(nums, count)); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /0045_jump_game_ii/jump_game.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int jump(vector& nums) { 8 | int steps = 0; 9 | int right = 0; 10 | int farthest = 0; 11 | // 1. Exhaust all the right boundries in the location range of [i...farthest] 12 | // 2. When i reaches the farthest boundary, update the farthest boundry 13 | // and the step number. 14 | // 3. Apply condition i < size - 1 and iterator i++ to avoid overflow. 15 | for (int i = 0; i < nums.size() - 1; i++) { 16 | right = max(i + nums[i], right); 17 | if (i == farthest) { 18 | farthest = right; 19 | steps++; 20 | } 21 | } 22 | 23 | return steps; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /0022_generate_parathesis/parenthesis.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | vector generateParenthesis(int n) { 8 | vector res; 9 | dfs(n, 0, 0, res); 10 | return res; 11 | } 12 | 13 | private: 14 | string stack; 15 | void dfs(int n, int l, int r, vector& res) { 16 | if (stack.length() == 2 * n) { 17 | res.push_back(stack); 18 | return; 19 | } 20 | 21 | if (l < n) { 22 | stack.push_back('('); 23 | dfs(n, l + 1, r, res); 24 | stack.pop_back(); 25 | } 26 | 27 | if (r < l) { 28 | stack.push_back(')'); 29 | dfs(n, l, r + 1, res); 30 | stack.pop_back(); 31 | } 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /0111_minimum_depth_of_binary_tree/bst_depth.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * struct TreeNode { 8 | * int val; 9 | * TreeNode *left; 10 | * TreeNode *right; 11 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 13 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 14 | * }; 15 | */ 16 | class Solution { 17 | public: 18 | int minDepth(TreeNode* root) { 19 | if (root == nullptr) { 20 | return 0; 21 | } 22 | int ld = minDepth(root->left) + 1; 23 | int rd = minDepth(root->right) + 1; 24 | return ld < rd ? (ld > 1 ? ld : rd) : (rd > 1 ? rd : ld); 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /0206_reverse_linked_list/reverse_list.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for singly-linked list. 7 | * struct ListNode { 8 | * int val; 9 | * ListNode *next; 10 | * ListNode() : val(0), next(nullptr) {} 11 | * ListNode(int x) : val(x), next(nullptr) {} 12 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 13 | * }; 14 | */ 15 | class Solution { 16 | public: 17 | ListNode* reverseList(ListNode* head) { 18 | ListNode *prev = nullptr; 19 | ListNode *p = head; 20 | while (p != nullptr) { 21 | // prev <- p <- q 22 | ListNode *q = p->next; 23 | p->next = prev; 24 | // step 25 | prev = p; 26 | p = q; 27 | } 28 | return prev; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /0049_group_anagrams/anagrams.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | vector> groupAnagrams(vector& strs) { 8 | vector> res; 9 | unordered_map> ht; 10 | for (const auto& str : strs) { 11 | int counts[26] = { 0 }; 12 | for (char c : str) { 13 | counts[c - 'a']++; 14 | } 15 | 16 | string key; 17 | for (int i : counts) { 18 | key.push_back('#'); 19 | key.push_back(i + '0'); 20 | } 21 | 22 | ht[key].push_back(str); 23 | } 24 | 25 | for (const auto& t : ht) { 26 | res.push_back(t.second); 27 | } 28 | return res; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /0543_diameter_of_binary_tree/diameter_bst.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * struct TreeNode { 8 | * int val; 9 | * TreeNode *left; 10 | * TreeNode *right; 11 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 12 | * }; 13 | */ 14 | class Solution { 15 | public: 16 | int diameterOfBinaryTree(TreeNode* root) { 17 | dfs(root); 18 | return max_diameter; 19 | } 20 | 21 | private: 22 | int max_diameter = 0; 23 | int dfs(TreeNode* root) { 24 | if (root == nullptr) { 25 | return 0; 26 | } 27 | 28 | int ld = dfs(root->left); 29 | int rd = dfs(root->right); 30 | max_diameter = max(max_diameter, ld + rd); 31 | return 1 + max(ld, rd); 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /0062_unique_path/unique_path.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static int uniquePaths(int m, int n) 5 | { 6 | int row, col; 7 | int *grids = malloc(m * n * sizeof(int)); 8 | for (col = 0; col < m; col++) { 9 | grids[col] = 1; 10 | } 11 | for (row = 0; row < n; row++) { 12 | grids[row * m] = 1; 13 | } 14 | for (row = 1; row < n; row++) { 15 | for (col = 1; col < m; col++) { 16 | grids[row * m + col] = grids[row * m + col - 1] + grids[(row - 1) * m + col]; 17 | } 18 | } 19 | return grids[m * n - 1]; 20 | } 21 | 22 | int main(int argc, char **argv) 23 | { 24 | if (argc != 3) { 25 | fprintf(stderr, "Usage: ./test m n\n"); 26 | exit(-1); 27 | } 28 | printf("%d\n", uniquePaths(atoi(argv[1]), atoi(argv[2]))); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /0205_isomorphic_strings/isomorphic_strings.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static bool isIsomorphic(char *s, char *t) 6 | { 7 | int i; 8 | char smap[128] = { '\0' }; 9 | char tmap[128] = { '\0' }; 10 | while (*s != '\0' && *t != '\0') { 11 | if ((smap[*s] != '\0' && smap[*s] != *t) || 12 | (tmap[*t] != '\0' && tmap[*t] != *s)) { 13 | return false; 14 | } 15 | smap[*s] = *t; 16 | tmap[*t] = *s; 17 | s++; 18 | t++; 19 | } 20 | return *s == *t; 21 | } 22 | 23 | int main(int argc, char **argv) 24 | { 25 | if (argc != 3) { 26 | fprintf(stderr, "Usage: ./test s1 s2\n"); 27 | exit(-1); 28 | } 29 | 30 | printf("%s\n", isIsomorphic(argv[1], argv[2]) ? "true" : "false"); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /0438_find_all_anagrams_in_a_string/anagrams_in_string.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | vector findAnagrams(string s, string p) { 8 | int count[128] = { 0 }; 9 | for (char c : p) { 10 | count[c]++; 11 | } 12 | 13 | vector res; 14 | int l = 0, r = 0, len = 0; 15 | while (r < s.length()) { 16 | if (--count[s[r++]] >= 0) { 17 | len++; 18 | } 19 | 20 | if (r - l >= p.length()) { 21 | if (len == p.length()) { 22 | res.push_back(l); 23 | } 24 | if (++count[s[l++]] > 0) { 25 | len--; 26 | } 27 | } 28 | } 29 | 30 | return res; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /0011_container_with_most_water/container.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static int maxArea(int* height, int heightSize) 5 | { 6 | int min = 0, max = heightSize - 1; 7 | int area_max = 0; 8 | while (min < max) { 9 | int area = (max - min) * (height[min] < height[max] ? height[min] : height[max]); 10 | area_max = area > area_max ? area : area_max; 11 | if (height[min] < height[max]) { 12 | min++; 13 | } else { 14 | max--; 15 | } 16 | } 17 | return area_max; 18 | } 19 | 20 | int main(int argc, char **argv) 21 | { 22 | int i, count = argc - 1; 23 | int *nums = malloc(count * sizeof(int)); 24 | for (i = 0; i < count; i++) { 25 | nums[i] = atoi(argv[i + 1]); 26 | } 27 | printf("%d\n", maxArea(nums, count)); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /0168_excel_sheet_column_title/sheet_column.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static char *convertToTitle(int n) 5 | { 6 | if (n <= 0) { 7 | return ""; 8 | } 9 | 10 | char *result = malloc(1024); 11 | int len = 0; 12 | do { 13 | result[len++] = ((n - 1) % 26) + 'A'; 14 | n = (n - 1) / 26; 15 | } while (n > 0); 16 | result[len] = '\0'; 17 | 18 | int i, j; 19 | for (i = 0, j = len - 1; i < j; i++, j--) { 20 | char c = result[i]; 21 | result[i] = result[j]; 22 | result[j] = c; 23 | } 24 | return result; 25 | } 26 | 27 | int main(int argc, char **argv) 28 | { 29 | if (argc != 2) { 30 | fprintf(stderr, "Usage: ./test n\n"); 31 | exit(-1); 32 | } 33 | 34 | printf("%s\n", convertToTitle(atoi(argv[1]))); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /0006_zigzag_conversion/zigzag_conversion.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | string convert(string s, int numRows) { 8 | if (numRows <= 1) { 9 | return s; 10 | } 11 | 12 | string new_s; 13 | for (int row = 0; row < numRows; row++) { 14 | int delta; 15 | int interval1 = numRows + (numRows - 2) - row * 2; 16 | int interval2 = 2 * row; 17 | bool flag = false; 18 | for (int i = row; i < s.length(); i += delta) { 19 | new_s.push_back(s[i]); 20 | do { 21 | delta = !flag ? interval1 : interval2; 22 | flag = !flag; 23 | } while (delta == 0); 24 | } 25 | } 26 | 27 | return new_str; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /0190_reverse_bits/reverse_bits.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static uint32_t reverseBits(uint32_t n) 6 | { 7 | const uint32_t MASK1 = 0x55555555; 8 | const uint32_t MASK2 = 0x33333333; 9 | const uint32_t MASK4 = 0x0f0f0f0f; 10 | const uint32_t MASK8 = 0x00ff00ff; 11 | 12 | // Extract and swap the even and odd bit groups. 13 | n = (n & MASK1) << 1 | ((n >> 1) & MASK1); 14 | n = (n & MASK2) << 2 | ((n >> 2) & MASK2); 15 | n = (n & MASK4) << 4 | ((n >> 4) & MASK4); 16 | n = (n & MASK8) << 8 | ((n >> 8) & MASK8); 17 | return n << 16 | n >> 16; 18 | } 19 | 20 | int main(int argc, char **argv) 21 | { 22 | if (argc != 2) { 23 | fprintf(stderr, "Usage: ./test n\n"); 24 | exit(-1); 25 | } 26 | 27 | printf("%u\n", reverseBits(atoi(argv[1]))); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /0031_next_permutation/next_permutation.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | void nextPermutation(vector& nums) { 8 | // find the first smaller element in decreasing sequence from back to 9 | // forth. 10 | int i = nums.size() - 2; 11 | while (i >= 0 && nums[i] >= nums[i + 1]) { 12 | i--; 13 | } 14 | 15 | // if found, find the first bigger element from back to forth and swap 16 | // them. 17 | if (i >= 0) { 18 | int j = nums.size() - 1; 19 | while (j >= 0 && nums[i] >= nums[j]) { 20 | j--; 21 | } 22 | swap(nums[i], nums[j]); 23 | } 24 | 25 | // reverse the subsequence into increasing one. 26 | reverse(nums.begin() + i + 1, nums.end()); 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /0075_sort_colors/sort_colors.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | void sortColors(vector& nums) { 8 | int i = 0, j = nums.size() - 1; 9 | while (i < j) { 10 | if (nums[i] == 0) { 11 | i++; 12 | continue; 13 | } 14 | if (nums[j] != 0) { 15 | j--; 16 | continue; 17 | } 18 | swap(nums[i], nums[j]); 19 | } 20 | 21 | j = nums.size() - 1; 22 | while (i < j) { 23 | if (nums[i] == 1) { 24 | i++; 25 | continue; 26 | } 27 | if (nums[j] != 1) { 28 | j--; 29 | continue; 30 | } 31 | swap(nums[i], nums[j]); 32 | } 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /0226_invert_binary_tree/invert_binary_tree.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * struct TreeNode { 8 | * int val; 9 | * TreeNode *left; 10 | * TreeNode *right; 11 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 13 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 14 | * }; 15 | */ 16 | class Solution { 17 | public: 18 | TreeNode* invertTree(TreeNode* root) { 19 | if (root == nullptr) { 20 | return nullptr; 21 | } 22 | 23 | TreeNode* l = invertTree(root->left); 24 | TreeNode* r = invertTree(root->right); 25 | root->left = r; 26 | root->right = l; 27 | return root; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /0014_longest_common_prefix/common_prefix.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | static char* longestCommonPrefix(char** strs, int strsSize) 7 | { 8 | if (strsSize == 0) { 9 | return ""; 10 | } 11 | 12 | int i, count = 0; 13 | char *result = malloc(1000); 14 | while (strs[0][count] != '\0') { 15 | char c = strs[0][count]; 16 | for (i = 1; i < strsSize; i++) { 17 | if (c != strs[i][count]) { 18 | break; 19 | } 20 | } 21 | 22 | if (i == strsSize) { 23 | result[count++] = c; 24 | } else { 25 | break; 26 | } 27 | } 28 | result[count++] = '\0'; 29 | return result; 30 | } 31 | 32 | int main(int argc, char **argv) 33 | { 34 | printf("%s\n", longestCommonPrefix(argv + 1, argc - 1)); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /0009_palindrome_number/palindrome_number.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | bool isPalindrome(int x) { 8 | if (x == 0) { 9 | return true; 10 | } 11 | if (x < 0) { 12 | return false; 13 | } 14 | return x == reverse(x); 15 | } 16 | 17 | private: 18 | int reverse(int x) { 19 | int n = 0; 20 | while (x != 0) { 21 | // Checking the over/underflow. 22 | int r = x % 10; 23 | if (n > INT_MAX / 10 || (n == INT_MAX / 10 && r > 7)) { 24 | return 0; 25 | } 26 | if (n < INT_MIN / 10 || (n == INT_MIN / 10 && r < -8)) { 27 | return 0; 28 | } 29 | 30 | n = n * 10 + r; 31 | x = x / 10; 32 | } 33 | return n; 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /0204_count_primes/count_primes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | int countPrimes(int n) 7 | { 8 | if (n < 3) { 9 | return 0; 10 | } 11 | 12 | int i, j; 13 | bool *marked = malloc(n); 14 | memset(marked, false, n); 15 | int count = n >> 1; 16 | 17 | for (i = 3; i * i <= n; i += 2) { 18 | if (!marked[i]) { 19 | for (j = i * i; j < n; j += (i << 1)) { 20 | if (!marked[j]) { 21 | marked[j] = true; 22 | --count; 23 | } 24 | } 25 | } 26 | } 27 | return count; 28 | } 29 | 30 | int main(int argc, char **argv) 31 | { 32 | if (argc != 2) { 33 | fprintf(stderr, "Usage: ./test n\n"); 34 | exit(-1); 35 | } 36 | 37 | printf("%d\n", countPrimes(atoi(argv[1]))); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /0069_sqrt/sqrt.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int mySqrt(int x) { 8 | if (x == 0) { 9 | return 0; 10 | } 11 | 12 | unsigned int lo = 1, hi = x; 13 | unsigned int mid = (lo + hi) / 2; 14 | // Firstly test mid > x / mid to decide whether hi = mid; 15 | // else then test mid + 1 > x / (mid + 1) to decide whether the mid is located; 16 | // Otherwise assign low = mid. 17 | for (; ;) { 18 | if (mid > x / mid) { 19 | hi = mid; 20 | } else { 21 | if (mid + 1 > x / (mid + 1)) { 22 | break; 23 | } else { 24 | lo = mid; 25 | } 26 | } 27 | mid = (lo + hi) / 2; 28 | } 29 | 30 | return mid; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /0169_majority_element/majority.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static int majorityElement(int* nums, int numsSize) 5 | { 6 | int i, major, count = 0; 7 | for (i = 0; i < numsSize; i++) { 8 | if (count == 0) { 9 | major = nums[i]; 10 | count++; 11 | } else if (major == nums[i]) { 12 | count++; 13 | } else { 14 | count--; 15 | } 16 | } 17 | return major; 18 | } 19 | 20 | int main(int argc, char **argv) 21 | { 22 | if (argc < 2) { 23 | fprintf(stderr, "Usage: ./test target n1 n2...\n"); 24 | exit(-1); 25 | } 26 | 27 | int i, count = argc - 1; 28 | int *nums = malloc(count * sizeof(int)); 29 | for (i = 0; i < count; i++) { 30 | nums[i] = atoi(argv[i + 1]); 31 | } 32 | 33 | printf("%d\n", majorityElement(nums, count)); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /0090_subsets_ii/subsets.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | vector> subsetsWithDup(vector& nums) { 8 | vector> res; 9 | sort(nums.begin(), nums.end()); 10 | dfs(nums, 0, res); 11 | return res; 12 | } 13 | 14 | private: 15 | vector stack; 16 | void dfs(vector& nums, int start, vector>& res) { 17 | res.push_back(stack); 18 | int last = INT_MIN; 19 | for (int i = start; i < nums.size(); i++) { 20 | if (last != nums[i]) { 21 | /* No duplicate candidate elements in the same level position */ 22 | stack.push_back(nums[i]); 23 | dfs(nums, i + 1, res); 24 | stack.pop_back(); 25 | } 26 | last = nums[i]; 27 | } 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /0142_linked_list_cycle_ii/list_cycle.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for singly-linked list. 7 | * struct ListNode { 8 | * int val; 9 | * ListNode *next; 10 | * ListNode(int x) : val(x), next(nullptr) {} 11 | * }; 12 | */ 13 | class Solution { 14 | public: 15 | ListNode* detectCycle(ListNode *head) { 16 | ListNode *fast = head; 17 | ListNode *slow = head; 18 | while (fast != nullptr && fast->next != nullptr) { 19 | slow = slow->next; 20 | fast = fast->next->next; 21 | if (fast == slow) { 22 | fast = head; 23 | while (fast != slow) { 24 | fast = fast->next; 25 | slow = slow->next; 26 | } 27 | return fast; 28 | } 29 | } 30 | return nullptr; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /0337_house_robber_iii/robber.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * struct TreeNode { 8 | * int val; 9 | * TreeNode *left; 10 | * TreeNode *right; 11 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 12 | * }; 13 | */ 14 | class Solution { 15 | public: 16 | int rob(TreeNode* root) { 17 | auto res = dfs(root); 18 | return max(res.first, res.second); 19 | } 20 | 21 | pair dfs(TreeNode *root) { 22 | if (root == nullptr) { 23 | return make_pair(0, 0); 24 | } 25 | auto subl = dfs(root->left); 26 | auto subr = dfs(root->right); 27 | int taken = root->val + subl.second + subr.second; 28 | int untaken = max(subl.first, subl.second) + max(subr.first, subr.second); 29 | return make_pair(taken, untaken); 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /0055_jump_game/jump_game.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static inline int max(int a, int b) 6 | { 7 | return a > b ? a : b; 8 | } 9 | 10 | static bool canJump(int* nums, int numsSize) 11 | { 12 | int i, pos = 0; 13 | for (i = 0; i < numsSize - 1; i++) { 14 | if (pos < i || pos >= numsSize - 1) { 15 | /* pos < i means nums[pos] == 0 */ 16 | break; 17 | } 18 | /* if all positive number it always can arrive. */ 19 | pos = max(i + nums[i], pos); 20 | } 21 | 22 | return pos >= numsSize - 1; 23 | } 24 | 25 | int main(int argc, char **argv) 26 | { 27 | int i, count = argc - 1; 28 | int *nums = malloc(count * sizeof(int)); 29 | for (i = 0; i < count; i++) { 30 | nums[i] = atoi(argv[i + 1]); 31 | } 32 | printf("%s\n", canJump(nums, count) ? "true" : "false"); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /0041_first_missing_positive/missing_positive.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int firstMissingPositive(vector& nums) { 8 | if (nums.size() == 0) { 9 | return 1; 10 | } 11 | 12 | int i = 0; 13 | while (i < nums.size()) { 14 | /* nums[i] should be i+1 and nums[nums[i] - 1] should be nums[i] */ 15 | if (nums[i] > 0 && nums[i] != i + 1 && nums[i] - 1 < nums.size() && nums[nums[i] - 1] != nums[i]) { 16 | // Let nums[nums[i] - 1] = nums[i] 17 | swap(nums[i], nums[nums[i] - 1]); 18 | } else { 19 | i++; 20 | } 21 | } 22 | 23 | for (i = 0; i < nums.size(); i++) { 24 | if (nums[i] != i + 1) { 25 | break; 26 | } 27 | } 28 | 29 | return i + 1; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /0089_gray_code/gray_code.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | ** Return an array of size *returnSize. 6 | ** Note: The returned array must be malloced, assume caller calls free(). 7 | **/ 8 | int* grayCode(int n, int* returnSize) { 9 | if (n < 0) { 10 | return NULL; 11 | } 12 | 13 | int i, count = 1 << n; 14 | int *codes = malloc(count * sizeof(int)); 15 | for (i = 0; i < count; i++) { 16 | codes[i] = (i >> 1) ^ i; 17 | } 18 | 19 | *returnSize = 1 << n; 20 | return codes; 21 | } 22 | 23 | int main(int argc, char **argv) 24 | { 25 | if (argc != 2) { 26 | fprintf(stderr, "Usage: ./test n\n"); 27 | exit(-1); 28 | } 29 | 30 | int i, count; 31 | int *list = grayCode(atoi(argv[1]), &count); 32 | for (i = 0; i < count; i++) { 33 | printf("%d ", list[i]); 34 | } 35 | printf("\n"); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /0235_lowest_common_ancestor_of_a_binary_search_tree/bst_lca.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * struct TreeNode { 8 | * int val; 9 | * TreeNode *left; 10 | * TreeNode *right; 11 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 12 | * }; 13 | */ 14 | 15 | class Solution { 16 | public: 17 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 18 | if (root == nullptr || root->val == p->val || root->val == q->val) { 19 | return root; 20 | } else if (root->val < p->val && root->val < q->val) { 21 | return lowestCommonAncestor(root->right, p, q); 22 | } else if (root->val > p->val && root->val > q->val) { 23 | return lowestCommonAncestor(root->left, p, q); 24 | } else { 25 | return root; 26 | } 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /0567_permutation_in_string/permutation_in_string.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | bool checkInclusion(string s1, string s2) { 8 | int count[128] = { 0 }; 9 | for (char c : s1) { 10 | count[c]++; 11 | } 12 | 13 | int l = 0, r = 0, hits = 0; 14 | while (r < s2.length()) { 15 | if (--count[s2[r++]] >= 0) { 16 | hits++; 17 | } 18 | 19 | // When the window length equals to the hit length, 20 | // the permutation is contained. 21 | if (r - l >= s1.length()) { 22 | if (hits == s1.length()) { 23 | return true; 24 | } 25 | if (++count[s2[l++]] > 0) { 26 | hits--; 27 | } 28 | } 29 | } 30 | 31 | return false; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /0072_edit_distance/edit_distance.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int minDistance(string word1, string word2) { 8 | int l1 = word1.length(); 9 | int l2 = word2.length(); 10 | vector dp(l2 + 1); 11 | for (int i = 0; i <= l2; i++) { 12 | dp[i] = i; 13 | } 14 | 15 | int up = 0; 16 | for (int i = 1; i <= l1; i++) { 17 | int left_up = dp[0]; 18 | dp[0] = i; 19 | for (int j = 1; j <= l2; j++) { 20 | up = dp[j]; 21 | if (word1[i - 1] == word2[j - 1]) { 22 | dp[j] = left_up; 23 | } else { 24 | dp[j] = 1 + min(left_up, min(up, dp[j - 1])); 25 | } 26 | left_up = up; 27 | } 28 | } 29 | 30 | return dp[l2]; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /0091_decode_ways/decode_ways.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | static int numDecodings(char* s) 7 | { 8 | int len = strlen(s); 9 | if (len == 0) { 10 | return 0; 11 | } 12 | 13 | int a = 1; 14 | int b = s[0] == '0' ? 0 : a; 15 | int c = b; 16 | /* DP: How many counts in sequence c = f(a, b) and c counts s[i - 1] */ 17 | for (int i = 2; i <= len; i++) { 18 | c = s[i - 1] == '0' ? 0 : b; 19 | int num = (s[i - 2] - '0') * 10 + (s[i - 1] - '0'); 20 | if (num >= 10 && num <= 26) { 21 | c += a; 22 | } 23 | a = b; 24 | b = c; 25 | } 26 | 27 | return c; 28 | } 29 | 30 | int main(int argc, char **argv) 31 | { 32 | if (argc != 2) { 33 | fprintf(stderr, "Usage: ./test number\n"); 34 | exit(-1); 35 | } 36 | printf("%d\n", numDecodings(argv[1])); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /0100_same_tree/same_tree.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * struct TreeNode { 8 | * int val; 9 | * TreeNode *left; 10 | * TreeNode *right; 11 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 13 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 14 | * }; 15 | */ 16 | class Solution { 17 | public: 18 | bool isSameTree(TreeNode* p, TreeNode* q) { 19 | if (p == nullptr && q == nullptr) { 20 | return true; 21 | } 22 | if (p == nullptr || q == nullptr) { 23 | return false; 24 | } 25 | if (p->val != q->val) { 26 | return false; 27 | } 28 | return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /0083_remove_duplicates_from_sorted_list/rm_dup.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for singly-linked list. 7 | * struct ListNode { 8 | * int val; 9 | * ListNode *next; 10 | * ListNode() : val(0), next(nullptr) {} 11 | * ListNode(int x) : val(x), next(nullptr) {} 12 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 13 | * }; 14 | */ 15 | class Solution { 16 | public: 17 | ListNode* deleteDuplicates(ListNode* head) { 18 | if (head == nullptr) { 19 | return nullptr; 20 | } 21 | 22 | ListNode* prev = head; 23 | ListNode *p = prev->next; 24 | while (p != nullptr) { 25 | if (p->val != prev->val) { 26 | prev->next = p; 27 | prev = p; 28 | } 29 | p = p->next; 30 | } 31 | prev->next = p; 32 | return head; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /0202_happy_number/happy_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | static bool recursive(int n, bool *used, const int *sq) 7 | { 8 | if (n == 1) return true; 9 | if (n < 10000) { 10 | if (used[n]) return false; 11 | used[n] = true; 12 | } 13 | int sum = 0; 14 | while (n > 0) { 15 | sum += sq[n % 10]; 16 | n /= 10; 17 | } 18 | return recursive(sum, used, sq); 19 | } 20 | 21 | static bool isHappy(int n) 22 | { 23 | bool used[10000] = { false }; 24 | const static int sq[10] = { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 }; 25 | return recursive(n, used, sq); 26 | } 27 | 28 | int main(int argc, char **argv) 29 | { 30 | if (argc != 2) { 31 | fprintf(stderr, "Usage: ./test n\n"); 32 | exit(-1); 33 | } 34 | 35 | printf("%s\n", isHappy(atoi(argv[1])) ? "true" : "false"); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /1372_longest_zigzag_path_in_a_binary_tree/zigzag.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * struct TreeNode { 8 | * int val; 9 | * TreeNode *left; 10 | * TreeNode *right; 11 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 12 | * }; 13 | */ 14 | class Solution { 15 | public: 16 | int longestZigZag(TreeNode* root) { 17 | dfs(root); 18 | return maxzz - 1; 19 | } 20 | private: 21 | int maxzz = 0; 22 | pair dfs(TreeNode *root) { 23 | if (root == nullptr) { 24 | return make_pair(0, 0); 25 | } 26 | 27 | auto subl = dfs(root->left); 28 | auto subr = dfs(root->right); 29 | int sublzz = 1 + subl.second; 30 | int subrzz = 1 + subr.first; 31 | maxzz = max(maxzz, max(sublzz, subrzz)); 32 | return make_pair(sublzz, subrzz); 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /0205_isomorphic_strings/isomorphic_strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | string mp(string s) 8 | { 9 | map m; 10 | string ans = ""; 11 | for (auto c : s) 12 | { 13 | if (m.find(c) == m.end()) 14 | { 15 | m[c] = m.size(); 16 | } 17 | ans += m[c]; 18 | } 19 | return ans; 20 | } 21 | bool isIsomorphic(string s, string t) 22 | { 23 | if (mp(s) == mp(t)) 24 | { 25 | return true; 26 | } 27 | return false; 28 | } 29 | }; 30 | 31 | int main() 32 | { 33 | string s, t; 34 | cout << "Enter 2 Strings : "; 35 | cin >> s >> t; 36 | Solution sol; 37 | if (sol.isIsomorphic(s, t)) 38 | { 39 | cout << "True"; 40 | } 41 | else 42 | { 43 | cout << "False"; 44 | } 45 | } -------------------------------------------------------------------------------- /0563_binary_tree_tilt/tilt.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * struct TreeNode { 8 | * int val; 9 | * TreeNode *left; 10 | * TreeNode *right; 11 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 13 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 14 | * }; 15 | */ 16 | class Solution { 17 | public: 18 | int findTilt(TreeNode* root) { 19 | dfs(root); 20 | return tilt; 21 | } 22 | 23 | private: 24 | int tilt = 0; 25 | int dfs(TreeNode *root) { 26 | if (root == nullptr) { 27 | return 0; 28 | } 29 | 30 | int subl = dfs(root->left); 31 | int subr = dfs(root->right); 32 | tilt += abs(subl - subr); 33 | return root->val + subl + subr; 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /0088_merge_sorted_array/merge_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | static void merge(int* nums1, int m, int* nums2, int n) 6 | { 7 | int i = m - 1, j = n - 1, k = nums1Size - 1; 8 | while (i >= 0 && j >= 0) { 9 | if (nums1[i] >= nums2[j]) { 10 | nums1[k--] = nums1[i--]; 11 | } else { 12 | nums1[k--] = nums2[j--]; 13 | } 14 | } 15 | 16 | while (j >= 0) { 17 | nums1[k--] = nums2[j--]; 18 | } 19 | } 20 | 21 | int main(int argc, char **argv) 22 | { 23 | int i; 24 | int nums1[] = { 1, 3, 5, 7, 9, 0, 0, 0, 0, 0 }; 25 | int nums2[] = { 2, 4, 6, 8, 10 }; 26 | int size1 = 5;//sizeof(nums1) / sizeof(*nums1); 27 | int size2 = sizeof(nums2) / sizeof(*nums2); 28 | merge(nums1, size1, nums2, size2); 29 | for (i = 0; i < sizeof(nums1) / sizeof(*nums1); i++) { 30 | printf("%d ", nums1[i]); 31 | } 32 | printf("\n"); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /0043_multiply_strings/multiply_strings.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | string multiply(string num1, string num2) { 8 | vector v(num1.length() + num2.length()); 9 | for (int i = num2.length() - 1; i >= 0; i--) { 10 | for (int j = num1.length() - 1; j >= 0; j--) { 11 | int a = num2[j] - '0'; 12 | int b = num1[i] - '0'; 13 | v[i + j + 1] = a * b; 14 | } 15 | } 16 | 17 | int i, carry = 0; 18 | string res(v.size(), '0'); 19 | for (i = v.size() - 1; i >= 0; i--) { 20 | carry += v[i]; 21 | res[i] += carry % 10; 22 | carry /= 10; 23 | } 24 | 25 | for (i = 0; i < res.length() - 1; i++) { 26 | if (res[i] != '0') { 27 | break; 28 | } 29 | } 30 | 31 | return res.substr(i); 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /0004_median_of_two_sorted_array/median_of_two_sorted_array.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | double findMedianSortedArrays(vector& nums1, vector& nums2) { 8 | int sum = nums1.size() + nums2.size(); 9 | vector nums; 10 | int i = 0, j = 0, k; 11 | int half = sum / 2 + 1; 12 | for (k = 0; k < half; k++) { 13 | int n; 14 | if (i < nums1.size() && j < nums2.size()) { 15 | n = (nums1[i] < nums2[j]) ? nums1[i++] : nums2[j++]; 16 | } else if (i < nums1.size()) { 17 | n = nums1[i++]; 18 | } else if (j < nums2.size()) { 19 | n = nums2[j++]; 20 | } 21 | nums.push_back(n); 22 | } 23 | 24 | if (sum % 2) { 25 | return nums[k-1]; 26 | } else { 27 | return (nums[k-1] + nums[k-2]) / 2.0; 28 | } 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /0560_subarray_sum_equals_k/subarray_sum.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int subarraySum(vector& nums, int k) { 8 | int res = 0, sum = 0; 9 | unordered_map pre_sum_cnt; 10 | 11 | // The prefix sum array records the sum of nums[0...i], so we have 12 | // presum[j] - presum[j] = k when the sum of nums[i...j] equals k. 13 | // The presum[0] should always be 0. And pre_sum_cnt[0] = 1. 14 | pre_sum_cnt[0] = 1; 15 | for (const auto n : nums) { 16 | // Here the sum means sum of nums[0...j] and the sum0 means sum 17 | // of nums[0...i] then there will be sum - sum0 = k. 18 | sum += n; 19 | int sum0 = sum - k; 20 | if (ht.count(sum0)) { 21 | res += pre_sum_cnt[sum0]; 22 | } 23 | pre_sum_cnt[sum]++; 24 | } 25 | 26 | return res; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /0112_path_sum/path_sum.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * struct TreeNode { 8 | * int val; 9 | * TreeNode *left; 10 | * TreeNode *right; 11 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 13 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 14 | * }; 15 | */ 16 | class Solution { 17 | public: 18 | bool hasPathSum(TreeNode* root, int sum) { 19 | if (root == nullptr) { 20 | // Here is non leaf 21 | return false; 22 | } else if (root->left == nullptr && root->right == nullptr && root->val == sum) { 23 | // Here must be leaf 24 | return true; 25 | } else { 26 | return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); 27 | } 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /0042_trapping_rain_water/trap_water.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int trap(vector& height) { 8 | /* In fact if we find the relative higher bar position and then the 9 | * water level of the position would be determined by the opposite side. 10 | */ 11 | int res = 0; 12 | int l = 0, l_max = 0; 13 | int r = height.size() - 1, r_max = 0; 14 | 15 | while (l < r) { 16 | // lmax is the highest in height[0...l] and 17 | // rmax is the highest in height[r...size - 1] 18 | l_max = max(height[l], l_max); 19 | r_max = max(height[r], r_max); 20 | if (l_max < r_max) { 21 | res += l_max - height[l]; 22 | l++; 23 | } else { 24 | res += r_max - height[r]; 25 | r--; 26 | } 27 | } 28 | 29 | return res; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /0024_swap_nodes_in_pairs/swap_nodes.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for singly-linked list. 7 | * struct ListNode { 8 | * int val; 9 | * ListNode *next; 10 | * ListNode() : val(0), next(nullptr) {} 11 | * ListNode(int x) : val(x), next(nullptr) {} 12 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 13 | * }; 14 | */ 15 | class Solution { 16 | public: 17 | ListNode* swapPairs(ListNode* head) { 18 | struct ListNode dummy, *prev = &dummy, *p = head; 19 | dummy.next = head; 20 | while (p != nullptr && p->next != nullptr) { 21 | struct ListNode *q = p->next; 22 | /* deletion */ 23 | p->next = q->next; 24 | /* insertion */ 25 | q->next = prev->next; 26 | prev->next = q; 27 | /* iteration */ 28 | prev = p; 29 | p = p->next; 30 | } 31 | return dummy.next; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /0198_house_robber/robber.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static inline int max(int a, int b) 6 | { 7 | return a > b ? a : b; 8 | } 9 | 10 | static int rob(int* nums, int numsSize) 11 | { 12 | int i; 13 | int taken = 0; 14 | int untaken = 0; 15 | /* Record max profits of nums[0...i] respectively */ 16 | for (i = 0; i < numsSize; i++) { 17 | int last_taken = taken; 18 | /* Taken or untaken nums[i] */ 19 | /* last taken + nums[i] */ 20 | taken = untaken + nums[i]; 21 | /* max(last untaken, last taken) */ 22 | untaken = max(last_taken, untaken); 23 | } 24 | 25 | return max(taken, untaken); 26 | } 27 | 28 | int main(int argc, char **argv) 29 | { 30 | int i, count = argc - 1; 31 | int *nums = malloc(count * sizeof(int)); 32 | for (i = 0; i < count; i++) { 33 | nums[i] = atoi(argv[i + 1]); 34 | } 35 | printf("%d\n", rob(nums, count)); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /0039_combination_sum/combination_sum.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | vector> combinationSum(vector& candidates, int target) { 8 | vector> res; 9 | dfs(candidates, 0, target, res); 10 | return res; 11 | } 12 | 13 | private: 14 | vector stack; 15 | void dfs(vector& candidates, int start, int target, vector>& res) { 16 | if (target < 0) { 17 | return; 18 | } else if (target == 0) { 19 | res.push_back(stack); 20 | } else { 21 | for (int i = start; i < candidates.size(); i++) { 22 | stack.push_back(candidates[i]); 23 | /* The elements in solution can be taken as many times as you can for the purpose of the problem */ 24 | dfs(candidates, i, target - candidates[i], res); 25 | stack.pop_back(); 26 | } 27 | } 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /0021_merge_two_sorted_lists/merge_lists.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for singly-linked list. 7 | * struct ListNode { 8 | * int val; 9 | * ListNode *next; 10 | * ListNode() : val(0), next(nullptr) {} 11 | * ListNode(int x) : val(x), next(nullptr) {} 12 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 13 | * }; 14 | */ 15 | class Solution { 16 | public: 17 | ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { 18 | struct ListNode *tail, dummy; 19 | tail = &dummy; 20 | dummy.next = l1; 21 | 22 | while (l1 != nullptr && l2 != nullptr) { 23 | if (l1->val <= l2->val) { 24 | tail->next = l1; 25 | l1 = l1->next; 26 | } else { 27 | tail->next = l2; 28 | l2 = l2->next; 29 | } 30 | } 31 | 32 | tail->next = l1 != nullptr ? l1 : l2; 33 | 34 | return dummy.next; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /0032_longest_valid_parentheses/valid_parentheses.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int longestValidParentheses(string s) { 8 | stack stk; 9 | int invalid = -1; 10 | int len = 0, max_len = 0; 11 | for (int i = 0; i < s.length(); i++) { 12 | if (s[i] == '(') { 13 | stk.push(i); 14 | } else { 15 | if (stk.empty()) { 16 | invalid = i; 17 | } else { 18 | stk.pop(); 19 | if (stk.empty()) { 20 | /* locate the remote ')' */ 21 | max_len = max(i - invalid, max_len); 22 | } else { 23 | /* locate the remote '(' */ 24 | max_len = max(i - stk.top(), max_len); 25 | } 26 | } 27 | } 28 | } 29 | 30 | return max_len; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /0108_convert_sorted_array_to_binary_search_tree/bst_convert.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | struct TreeNode { 6 | int val; 7 | struct TreeNode *left; 8 | struct TreeNode *right; 9 | }; 10 | 11 | static struct TreeNode *partition(int *nums, int lo, int hi) 12 | { 13 | if (lo > hi) { 14 | return NULL; 15 | } 16 | int mid = lo + (hi - lo) / 2; 17 | struct TreeNode *node = malloc(sizeof(*node)); 18 | node->val = nums[mid]; 19 | node->left = partition(nums, lo, mid - 1); 20 | node->right = partition(nums, mid + 1, hi); 21 | return node; 22 | } 23 | 24 | static struct TreeNode* sortedArrayToBST(int* nums, int numsSize) 25 | { 26 | return partition(nums, 0, numsSize - 1); 27 | } 28 | 29 | int main(int argc, char **argv) 30 | { 31 | int i, count = argc - 1; 32 | int *nums = malloc(count * sizeof(int)); 33 | for (i = 0; i < count; i++) { 34 | nums[i] = atoi(argv[i + 1]); 35 | } 36 | sortedArrayToBST(nums, count); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /0120_triangle/triangle.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int minimumTotal(vector>& triangle) { 8 | for (auto & t : triangle) { 9 | passed.push_back(vector(t.size(), false)); 10 | sums.push_back(vector(t.size())); 11 | } 12 | return dfs(triangle, 0, 0); 13 | } 14 | private: 15 | vector> passed; 16 | vector> sums; 17 | int dfs(vector>& triangle, int row, int col) { 18 | if (row == triangle.size() - 1) { 19 | return triangle[row][col]; 20 | } else if (passed[row][col]) { 21 | return sums[row][col]; 22 | } else { 23 | int s1 = dfs(triangle, row + 1, col); 24 | int s2 = dfs(triangle, row + 1, col + 1); 25 | sums[row][col] = triangle[row][col] + (s1 < s2 ? s1 : s2); 26 | passed[row][col] = true; 27 | return sums[row][col]; 28 | } 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /1312_minimum_insertion_steps_to_make_a_string_palindrome/insertion.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | static inline int min(int a, int b) 7 | { 8 | return a < b ? a : b; 9 | } 10 | 11 | int minInsertions(char * s){ 12 | int i, j, len = strlen(s); 13 | int *dp = malloc(len * sizeof(int)); 14 | memset(dp, 0, len * sizeof(int)); 15 | for (i = len - 2; i >= 0; i--) { 16 | int left_down = 0; 17 | for (j = i + 1; j < len; j++) { 18 | int down = dp[j]; 19 | if (s[i] == s[j]) { 20 | dp[j] = left_down; 21 | } else { 22 | dp[j] = min(down, dp[j - 1]) + 1; 23 | } 24 | left_down = down; 25 | } 26 | } 27 | return dp[len - 1]; 28 | } 29 | 30 | int main(int argc, char **argv) 31 | { 32 | if (argc != 2) { 33 | fprintf(stderr, "Usage: ./test s\n"); 34 | exit(-1); 35 | } 36 | 37 | printf("%d\n", minInsertions(argv[1])); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /0009_palindrome_number/palindrome_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | static int reverse(int x) 7 | { 8 | int n = 0; 9 | while (x != 0) { 10 | int r = x % 10; 11 | /* Checking the over/underflow. */ 12 | if (n > INT_MAX / 10 || (n == INT_MAX / 10 && r > 7)) { 13 | return 0; 14 | } 15 | if (n < INT_MIN / 10 || (n == INT_MIN / 10 && r < -8)) { 16 | return 0; 17 | } 18 | n = n * 10 + r; 19 | x /= 10; 20 | } 21 | return n; 22 | } 23 | 24 | static bool isPalindrome(int x) 25 | { 26 | if (x == 0) { 27 | return true; 28 | } 29 | if (x < 0) { 30 | return false; 31 | } 32 | return x == reverse(x); 33 | } 34 | 35 | int main(int argc, char **argv) 36 | { 37 | if (argc < 2) { 38 | fprintf(stderr, "Usage: ./test number\n"); 39 | exit(-1); 40 | } 41 | 42 | printf("%s\n", isPalindrome(atoi(argv[1])) ? "true" : "false"); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /0030_substring_with_concatenation_of_all_words/concatenation.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | vector findSubstring(string s, vector& words) { 8 | vector res; 9 | if (s.empty() || words.empty()) { 10 | return res; 11 | } 12 | 13 | unordered_map ht; 14 | for (const auto& w : words) { 15 | ht[w]++; 16 | } 17 | 18 | int len = words[0].length(); 19 | for (int i = 0, j = 0; i < s.length() - words.size() * len + 1; i++) { 20 | unordered_map counting; 21 | for (j = 0; j < words.size(); j++) { 22 | string word = s.substr(i + j * len, len); 23 | if (++counting[word] > ht[word]) { 24 | break; 25 | } 26 | } 27 | if (j == words.size()) { 28 | res.push_back(i); 29 | } 30 | } 31 | 32 | return res; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /0239_sliding_window_maximum/slide_window.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | vector maxSlidingWindow(vector& nums, int k) { 8 | vector res; 9 | // In order to measure the moving size of the sliding window, we 10 | // need to store the index instead of element into the window. 11 | vector indexes(nums.size()); 12 | int left = 0, right = 0; 13 | for (int i = 0; i < nums.size(); i++) { 14 | while (right > left && nums[i] >= nums[indexes[right - 1]]) { 15 | right--; 16 | } 17 | indexes[right++] = i; 18 | 19 | // The last position of sliding window 20 | if (i >= k - 1) { 21 | res.push_back(nums[indexes[left]]); 22 | } 23 | 24 | // The length of sliding window 25 | if (i - indexes[left] + 1 >= k) { 26 | left++; 27 | } 28 | } 29 | return res; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /0134_gas_station/gas_station.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize) 5 | { 6 | int i, j, store = 0, start = -1; 7 | 8 | /* the solution(start) is guaranteed to be 9 | * unique for the purpose of this problem 10 | */ 11 | for (i = 0; i < gasSize; i++) { 12 | if (start < 0) { 13 | start = i; 14 | } 15 | store += gas[i] - cost[i]; 16 | if (store < 0) { 17 | store = 0; 18 | start = -1; 19 | } 20 | } 21 | 22 | /* if start == -1 just return */ 23 | for (i = 0; i < start; i++) { 24 | store += gas[i] - cost[i]; 25 | if (store < 0) { 26 | return -1; 27 | } 28 | } 29 | 30 | return start; 31 | } 32 | 33 | int main(void) 34 | { 35 | int gas[] = { 4 }; 36 | int cost[] = { 5 }; 37 | int count = sizeof(gas) / sizeof(*gas); 38 | printf("%d\n", canCompleteCircuit(gas, count, cost, count)); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /0124_binary_tree_maximum_path_sum/bst_max_path.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * struct TreeNode { 8 | * int val; 9 | * TreeNode *left; 10 | * TreeNode *right; 11 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 13 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 14 | * }; 15 | */ 16 | class Solution { 17 | public: 18 | int maxPathSum(TreeNode* root) { 19 | dfs(root); 20 | return max_sum; 21 | } 22 | 23 | private: 24 | int max_sum = INT_MIN; 25 | int dfs(TreeNode *root) { 26 | if (root == nullptr) { 27 | return 0; 28 | } 29 | 30 | int subl = max(0, dfs(root->left)); 31 | int subr = max(0, dfs(root->right)); 32 | int sum = root->val + subl + subr; 33 | max_sum = max(sum, max_sum); 34 | return root->val + max(subl, subr); 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /0052_n_queens_ii/n_queens.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int totalNQueens(int n) { 8 | vector stack(n); 9 | return dfs(n, 0, stack); 10 | } 11 | 12 | private: 13 | int dfs(int n, int row, vector& stack) { 14 | int count = 0; 15 | if (row == n) { 16 | return count + 1; 17 | } else { 18 | for (int i = 0; i < n; i++) { 19 | if (row == 0 || !conflict(stack, row, i)) { 20 | stack[row] = i; 21 | count += dfs(n, row + 1, stack); 22 | } 23 | } 24 | return count; 25 | } 26 | } 27 | 28 | bool conflict(vector& stack, int row, int col) { 29 | for (int i = 0; i < row; i++) { 30 | /* If occupied or in one line */ 31 | if (col == stack[i] || abs(row - i) == abs(col - stack[i])) { 32 | return true; 33 | } 34 | } 35 | return false; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /0020_valid_parentheses/valid_parentheses.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static bool isValid(char *s) 6 | { 7 | int n = 0; 8 | char stack[100]; 9 | 10 | while (*s != '\0') { 11 | switch(*s) { 12 | case '(': 13 | case '[': 14 | case '{': 15 | stack[n++] = *s; 16 | break; 17 | case ')': 18 | if (n == 0 || stack[--n] != '(') return false; 19 | break; 20 | case ']': 21 | if (n == 0 || stack[--n] != '[') return false; 22 | break; 23 | case '}': 24 | if (n == 0 || stack[--n] != '{') return false; 25 | break; 26 | default: 27 | return false; 28 | } 29 | s++; 30 | } 31 | 32 | return n == 0; 33 | } 34 | 35 | int main(int argc, char **argv) 36 | { 37 | if (argc != 2) { 38 | fprintf(stderr, "Usage: ./test xxxx"); 39 | exit(-1); 40 | } 41 | printf("%s\n", isValid(argv[1]) ? "true" : "false"); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /0209_minimum_size_subarray_sum/mini_size.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static inline int min(int a, int b) 6 | { 7 | return a < b ? a : b; 8 | } 9 | 10 | static int minSubArrayLen(int s, int* nums, int numsSize) 11 | { 12 | int i, sum = 0, len = INT_MAX, start = 0; 13 | for (i = 0; i < numsSize; i++) { 14 | sum += nums[i]; 15 | while (sum >= s) { 16 | /* sliding window */ 17 | len = min(len, i - start + 1); 18 | sum -= nums[start++]; 19 | } 20 | } 21 | 22 | return len == INT_MAX ? 0 : len; 23 | } 24 | 25 | int main(int argc, char **argv) 26 | { 27 | if (argc < 3) { 28 | fprintf(stderr, "Usage: ./test sum n1 n2...\n"); 29 | exit(-1); 30 | } 31 | 32 | int i, count = argc - 2; 33 | int sum = atoi(argv[1]); 34 | int *nums = malloc(count * sizeof(int)); 35 | for (i = 0; i < count; i++) { 36 | nums[i] = atoi(argv[i + 2]); 37 | } 38 | printf("%d\n", minSubArrayLen(sum, nums, count)); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /0236_lowest_common_ancestor_of_a_binary_tree/bst_lca.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | /** 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 11 | * }; 12 | */ 13 | class Solution { 14 | public: 15 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 16 | if (root == nullptr || root == p || root == q) { 17 | return root; 18 | } 19 | 20 | TreeNode *l = lowestCommonAncestor(root->left, p, q); 21 | TreeNode *r = lowestCommonAncestor(root->right, p, q); 22 | if (l != nullptr && r != nullptr) { 23 | return root; 24 | } else { 25 | return l != nullptr ? l : r; 26 | } 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /0108_convert_sorted_array_to_binary_search_tree/bst_convert.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * struct TreeNode { 8 | * int val; 9 | * TreeNode *left; 10 | * TreeNode *right; 11 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 13 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 14 | * }; 15 | */ 16 | class Solution { 17 | public: 18 | TreeNode* sortedArrayToBST(vector& nums) { 19 | return partition(nums, 0, nums.size() - 1); 20 | } 21 | 22 | private: 23 | TreeNode *partition(vector& nums, int lo, int hi) { 24 | if (lo > hi) { 25 | return nullptr; 26 | } 27 | int mid = lo + (hi - lo) / 2; 28 | TreeNode *root = new TreeNode(nums[mid]); 29 | root->left = partition(nums, lo, mid - 1); 30 | root->right = partition(nums, mid + 1, hi); 31 | return root; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /0110_balanced_binary_tree/balanced_bst.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * struct TreeNode { 8 | * int val; 9 | * TreeNode *left; 10 | * TreeNode *right; 11 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 13 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 14 | * }; 15 | */ 16 | class Solution { 17 | public: 18 | bool isBalanced(TreeNode* root) { 19 | bool balance = true; 20 | depth(root, balance); 21 | return balance; 22 | } 23 | 24 | private: 25 | int depth(TreeNode *root, bool& balance) { 26 | if (!balance || root == nullptr) { 27 | return 0; 28 | } 29 | int ld = depth(root->left, balance) + 1; 30 | int rd = depth(root->right, balance) + 1; 31 | if (balance) { 32 | balance = abs(ld - rd) <= 1; 33 | } 34 | return max(ld, rd); 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /0033_search_in_rotated_sorted_array/rotated_array.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int search(vector& nums, int target) { 8 | int lo = 0; 9 | int hi = nums.size() - 1; 10 | 11 | for (lo <= hi) { 12 | int mid = lo + (hi - lo) / 2; 13 | if (nums[mid] == target) { 14 | return mid; 15 | } 16 | 17 | // lo might be mid 18 | // We only need to consider non-rotated sorted array search 19 | if (nums[lo] <= nums[mid]) { 20 | if (nums[lo] <= target && target < nums[mid]) { 21 | hi = mid - 1; 22 | } else { 23 | lo = mid + 1; 24 | } 25 | } else { 26 | if (nums[mid] < target && target <= nums[hi]) { 27 | lo = mid + 1; 28 | } else { 29 | hi = mid - 1; 30 | } 31 | } 32 | } 33 | 34 | return -1; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /0144_binary_tree_preorder_traversal/bst_preorder.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * struct TreeNode { 8 | * int val; 9 | * TreeNode *left; 10 | * TreeNode *right; 11 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 13 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 14 | * }; 15 | */ 16 | class Solution { 17 | public: 18 | vector preorderTraversal(TreeNode* root) { 19 | vector res; 20 | stack stk; 21 | while (!stk.empty() || root != nullptr) { 22 | if (root != nullptr) { 23 | res.push_back(root->val); 24 | stk.push(root); 25 | root = root->left; 26 | } else { 27 | root = stk.top(); 28 | stk.pop(); 29 | root = root->right; 30 | } 31 | } 32 | return res; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /0109_convert_sorted_list_to_binary_search_tree/bst_convert.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | struct ListNode { 6 | int val; 7 | struct ListNode *next; 8 | }; 9 | 10 | struct TreeNode { 11 | int val; 12 | struct TreeNode *left; 13 | struct TreeNode *right; 14 | }; 15 | 16 | static struct TreeNode *dfs(struct ListNode **head, int lo, int hi) 17 | { 18 | if (lo > hi) { 19 | return NULL; 20 | } 21 | 22 | int mid = lo + (hi - lo) / 2; 23 | struct TreeNode *node = malloc(sizeof(*node)); 24 | node->left = dfs(head, lo, mid - 1); 25 | node->val = (*head)->val; 26 | (*head) = (*head)->next; 27 | node->right = dfs(head, mid + 1, hi); 28 | return node; 29 | } 30 | 31 | static struct TreeNode* sortedListToBST(struct ListNode* head) 32 | { 33 | struct ListNode *p; 34 | int len = 0; 35 | for (p = head; p != NULL; p = p->next) { 36 | len++; 37 | } 38 | return dfs(&head, 0, len - 1); 39 | } 40 | 41 | int main(int argc, char **argv) 42 | { 43 | sortedListToBST(NULL); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /0116_populating_next_right_pointers_in_each_node/connect.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /* 6 | // Definition for a Node. 7 | class Node { 8 | public: 9 | int val; 10 | Node* left; 11 | Node* right; 12 | Node* next; 13 | 14 | Node() : val(0), left(NULL), right(NULL), next(NULL) {} 15 | 16 | Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {} 17 | 18 | Node(int _val, Node* _left, Node* _right, Node* _next) 19 | : val(_val), left(_left), right(_right), next(_next) {} 20 | }; 21 | */ 22 | 23 | class Solution { 24 | public: 25 | Node* connect(Node* root) { 26 | if (root == nullptr) { 27 | return root; 28 | } 29 | 30 | if (root->left != nullptr) { 31 | root->left->next = root->right; 32 | } 33 | Node *next = root->next; 34 | if (root->right != nullptr && next != nullptr) { 35 | root->right->next = next->left; 36 | } 37 | connect(root->left); 38 | connect(root->right); 39 | return root; 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /0045_jump_game_ii/jump_game.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | static inline int max(int a, int b) 6 | { 7 | return a > b ? a : b; 8 | } 9 | 10 | static int jump(int* nums, int numsSize) 11 | { 12 | int i, right = 0; 13 | int steps = 0; 14 | int fartest = 0; 15 | /* 1. Exhaust all the right boundries in the location range of [i...farthest] 16 | * 2. When i reaches the farthest boundary, update the farthest boundry 17 | * and the step number. 18 | * 3. Apply condition i < size - 1 and iterator i++ to avoid overflow. */ 19 | for (i = 0; i < numsSize; i++) { 20 | fartest = max(i + nums[i], fartest); 21 | if (i == right) { 22 | right = fartest; 23 | steps++; 24 | } 25 | } 26 | 27 | return steps; 28 | } 29 | 30 | int main(int argc, char **argv) 31 | { 32 | int i, count = argc - 1; 33 | int *nums = malloc(count * sizeof(int)); 34 | for (i = 0; i < count; i++) { 35 | nums[i] = atoi(argv[i + 1]); 36 | } 37 | printf("%d\n", jump(nums, count)); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /0029_divide_two_integers/divide.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int divide(int dividend, int divisor) { 8 | int signal = 1; 9 | unsigned int dvd = dividend; 10 | if (dividend < 0) { 11 | signal *= -1; 12 | dvd = ~dvd + 1; 13 | } 14 | 15 | unsigned int dvs = divisor; 16 | if (divisor < 0) { 17 | signal *= -1; 18 | dvs = ~dvs + 1; 19 | } 20 | 21 | int shift = 0; 22 | while (dvd > dvs << shift) { 23 | shift++; 24 | } 25 | 26 | unsigned int res = 0; 27 | while (dvd >= dvs) { 28 | while (dvd < dvs << shift) { 29 | shift--; 30 | } 31 | res |= (unsigned int) 1 << shift; 32 | dvd -= dvs << shift; 33 | } 34 | 35 | if (signal == 1 && res >= INT_MAX) { 36 | return INT_MAX; 37 | } else { 38 | return res * signal; 39 | } 40 | } 41 | }; 42 | 43 | -------------------------------------------------------------------------------- /0141_linked_list_cycle/list_cycle.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct ListNode { 6 | int val; 7 | struct ListNode *next; 8 | }; 9 | 10 | static bool hasCycle(struct ListNode *head) 11 | { 12 | struct ListNode *fast = head; 13 | struct ListNode *slow = head; 14 | while (fast != NULL && fast->next != NULL) { 15 | slow = slow->next; 16 | fast = fast->next->next; 17 | if (fast == slow) { 18 | return true; 19 | } 20 | } 21 | 22 | return false; 23 | } 24 | 25 | int main(int argc, char **argv) 26 | { 27 | int i, count = argc - 1; 28 | struct ListNode *head = NULL, *p, *prev; 29 | for (i = 0; i < count; i++) { 30 | p = malloc(sizeof(*p)); 31 | p->val = atoi(argv[i + 1]); 32 | p->next = NULL; 33 | if (head == NULL) { 34 | head = p; 35 | } else { 36 | prev->next = p; 37 | } 38 | prev = p; 39 | } 40 | p->next = head; 41 | 42 | printf("%s\n", hasCycle(head) ? "true" : "false"); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /0567_permutation_in_string/permutation_in_string.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | bool checkInclusion(char * s1, char * s2) 8 | { 9 | int i, count[128] = { 0 }, pat_len; 10 | int l = 0, r = 0, len = 0; 11 | 12 | for (i = 0; s1[i] != '\0'; i++) { 13 | count[s1[i]]++; 14 | } 15 | pat_len = i; 16 | 17 | while (s2[r] != '\0') { 18 | if (--count[s2[r++]] >= 0) { 19 | len++; 20 | } 21 | 22 | while (len >= pat_len) { 23 | if (r - l == pat_len) { 24 | return true; 25 | } 26 | if (++count[s2[l++]] > 0) { 27 | len--; 28 | } 29 | } 30 | } 31 | 32 | return false; 33 | } 34 | 35 | int main(int argc, char **argv) 36 | { 37 | if (argc != 3) { 38 | fprintf(stderr, "Usage: ./test string pattern\n"); 39 | exit(-1); 40 | } 41 | 42 | char *t = argv[1]; 43 | char *s = argv[2]; 44 | printf("%s\n", checkInclusion(t, s) ? "true" : "false"); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /0016_three_sum_closest/three_sum_closest.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int threeSumClosest(vector& nums, int target) { 8 | if (nums.size() < 3) { 9 | return INT_MAX; 10 | } 11 | 12 | int min_diff = INT_MAX; 13 | sort(nums.begin(), nums.end()); 14 | 15 | for (int i = 0; i < nums.size() - 2; i++) { 16 | if (i > 0 && nums[i - 1] == nums[i]) { continue; } 17 | 18 | int lo = i + 1; 19 | int hi = nums.size() - 1; 20 | while (lo < hi) { 21 | int diff = nums[i] + nums[lo] + nums[hi] - target; 22 | if (abs(diff) < abs(min_diff)) { 23 | min_diff = diff; 24 | } 25 | if (diff < 0) { 26 | lo++; 27 | } else if (diff > 0) { 28 | hi--; 29 | } else { 30 | return target; 31 | } 32 | } 33 | } 34 | 35 | return min_diff + target; 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /0019_remove_nth_node_from_end_of_list/remove_end.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for singly-linked list. 7 | * struct ListNode { 8 | * int val; 9 | * ListNode *next; 10 | * ListNode() : val(0), next(nullptr) {} 11 | * ListNode(int x) : val(x), next(nullptr) {} 12 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 13 | * }; 14 | */ 15 | class Solution { 16 | public: 17 | ListNode* removeNthFromEnd(ListNode* head, int n) { 18 | if (n == 0) { 19 | return nullptr; 20 | } 21 | 22 | struct ListNode *p, *prev, dummy; 23 | dummy.next = head; 24 | p = prev = &dummy; 25 | while (n-- > 0) { 26 | p = p->next; 27 | } 28 | 29 | while (p->next != nullptr) { 30 | p = p->next; 31 | prev = prev->next; 32 | } 33 | 34 | struct ListNode *tmp = prev->next; 35 | prev->next = tmp->next; 36 | if (tmp == head) { 37 | head = tmp->next; 38 | } 39 | 40 | return head; 41 | } 42 | }; 43 | -------------------------------------------------------------------------------- /0021_merge_two_sorted_lists/merge_lists.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct ListNode { 5 | int val; 6 | struct ListNode *next; 7 | }; 8 | 9 | static struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) 10 | { 11 | struct ListNode dummy; 12 | struct ListNode *tail = &dummy; 13 | 14 | while (l1 != NULL && l2 != NULL) { 15 | if (l1->val <= l2->val) { 16 | tail->next = l1; 17 | l1 = l1->next; 18 | } else { 19 | tail->next = l2; 20 | l2 = l2->next; 21 | } 22 | tail = tail->next; 23 | } 24 | 25 | tail->next = l1 != NULL ? l1 : l2; 26 | 27 | return dummy.next; 28 | } 29 | 30 | int main(int argc, char **argv) 31 | { 32 | struct ListNode l1; 33 | l1.val = 2; 34 | l1.next = NULL; 35 | struct ListNode l2; 36 | l2.val = 1; 37 | l2.next = NULL; 38 | struct ListNode * list = mergeTwoLists(&l1, &l2); 39 | while (list != NULL) { 40 | printf("%d ", list->val); 41 | list = list->next; 42 | } 43 | printf("\n"); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /0046_permutations/permutations.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | vector> permute(vector& nums) { 8 | vector> res; 9 | vector used(nums.size()); 10 | dfs(nums, used, res); 11 | return res; 12 | } 13 | 14 | private: 15 | vector stack; 16 | void dfs(vector& nums, vector& used, vector>& res) { 17 | if (stack.size() == nums.size()) { 18 | res.push_back(stack); 19 | return; 20 | } 21 | 22 | // Reverse order is allowed in different levels, always starts from [0] 23 | for (int i = 0; i < nums.size(); i++) { 24 | if (!used[i]) { 25 | // Used marks all the allowable remaining elements in the next 26 | // DFS levels 27 | used[i] = true; 28 | stack.push_back(nums[i]); 29 | dfs(nums, used, res); 30 | stack.pop_back(); 31 | used[i] = false; 32 | } 33 | } 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /0094_binary_tree_inorder_traversal/bst_inorder_traversal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct TreeNode { 5 | int val; 6 | struct TreeNode *left; 7 | struct TreeNode *right; 8 | }; 9 | 10 | static void traverse(struct TreeNode *node, int *result, int *count) 11 | { 12 | if (node == NULL) { 13 | return; 14 | } 15 | 16 | traverse(node->left, result, count); 17 | result[*count] = node->val; 18 | (*count)++; 19 | traverse(node->right, result, count); 20 | } 21 | 22 | /** 23 | ** Return an array of size *returnSize. 24 | ** Note: The returned array must be malloced, assume caller calls free(). 25 | **/ 26 | static int* inorderTraversal(struct TreeNode* root, int* returnSize) 27 | { 28 | if (root == NULL) { 29 | *returnSize = 0; 30 | return NULL; 31 | } 32 | int count = 0; 33 | int *result = malloc(5000 * sizeof(int)); 34 | traverse(root, result, &count); 35 | *returnSize = count; 36 | return result; 37 | } 38 | 39 | int main() 40 | { 41 | int count = 0; 42 | inorderTraversal(NULL, &count); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /0101_symmetric_tree/symmetric_tree.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * struct TreeNode { 8 | * int val; 9 | * TreeNode *left; 10 | * TreeNode *right; 11 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 13 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 14 | * }; 15 | */ 16 | class Solution { 17 | public: 18 | bool isSymmetric(TreeNode* root) { 19 | if (root == nullptr) { 20 | return true; 21 | } 22 | return dfs(root->left, root->right); 23 | } 24 | 25 | private: 26 | bool dfs(TreeNode *l, TreeNode *r) { 27 | if (l == nullptr && r == nullptr) { 28 | return true; 29 | } 30 | if (l == nullptr || r == nullptr) { 31 | return false; 32 | } 33 | if (l->val != r->val) { 34 | return false; 35 | } 36 | return dfs(l->left, r->right) && dfs(l->right, r->left); 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /0010_regular_expression_matching/regular_expression.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | /* recursive version too slow thanks to substr() */ 8 | bool isMatch(string s, string p) { 9 | vector> dp(s.size() + 1, vector(p.size() + 1, false)); 10 | dp[0][0] = true; 11 | for (int i = 1; i <= p.length(); i++) { 12 | dp[0][i] = p[0][i - 1] == '*' && i >= 2 && dp[0][i - 2]; 13 | } 14 | 15 | for (int i = 1; i <= s.length(); i++) { 16 | for (int j = 1; j <= p.length(); j++) { 17 | bool match; 18 | if (j >= 2 && p[j - 1] == '*') { 19 | match = (s[i - 1] == p[j - 2] || p[j - 2] == '.'); 20 | dp[i][j] = dp[i][j - 2] || (match && dp[i - 1][j]); 21 | } else { 22 | match = (s[i - 1] == p[j - 1] || p[j - 1] == '.'); 23 | dp[i][j] = match && dp[i - 1][j - 1]; 24 | } 25 | } 26 | } 27 | 28 | return dp[s.size()][p.size()]; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /0070_climbing_stairs/climb_stairs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static int dfs(int n, int *steps) 6 | { 7 | if (n == 1) { 8 | return 1; 9 | } else if (n == 2) { 10 | return 2; 11 | } else if (steps[n] > 0) { 12 | return steps[n]; 13 | } else { 14 | steps[n] += dfs(n - 1, steps); 15 | steps[n] += dfs(n - 2, steps); 16 | return steps[n]; 17 | } 18 | } 19 | 20 | static int climbStairs(int n) 21 | { 22 | #if 1 23 | if (n < 1) return 0; 24 | int *steps = malloc((n + 1) * sizeof(int)); 25 | memset(steps, 0, (n + 1) * sizeof(int)); 26 | return dfs(n, steps); 27 | #else 28 | int i, a = 1, b = 2, c; 29 | for (i = 3; i <= n; i++) { 30 | c = a + b; 31 | a = b; 32 | b = c; 33 | } 34 | return n == 1 ? a : (n == 2 ? b : c); 35 | #endif 36 | } 37 | 38 | int main(int argc, char **argv) 39 | { 40 | if (argc != 2) { 41 | fprintf(stderr, "Usage: ./test n\n"); 42 | exit(-1); 43 | } 44 | 45 | printf("%d\n", climbStairs(atoi(argv[1]))); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /0006_zigzag_conversion/zigzag_conversion.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static char* convert(char* s, int numRows) 6 | { 7 | if (numRows <= 1) return s; 8 | 9 | int len = strlen(s); 10 | char *new_str = malloc(len + 1); 11 | char *p = new_str; 12 | int row = 0; 13 | for (row = 0; row < numRows; row++) { 14 | int interval1 = numRows + (numRows - 2) - row * 2; 15 | int interval2 = 2 * row; 16 | int flag = 0; 17 | int i = row; 18 | while (i < len) { 19 | *p++ = s[i]; 20 | int delta = 0; 21 | do { 22 | delta = flag == 0 ? interval1 : interval2; 23 | flag = !flag; 24 | } while (delta == 0); 25 | i += delta; 26 | } 27 | } 28 | 29 | new_str[len] = '\0'; 30 | return new_str; 31 | } 32 | 33 | int main(int argc, char **argv) 34 | { 35 | if (argc < 3) { 36 | fprintf(stderr, "./test string num\n"); 37 | exit(-1); 38 | } 39 | 40 | printf("%s\n", convert(argv[1], atoi(argv[2]))); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /0067_add_binary/add_binary.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static char* addBinary(char* a, char* b) 6 | { 7 | int len1 = strlen(a); 8 | int len2 = strlen(b); 9 | int len = len1 > len2 ? len1 + 1 : len2 + 1; 10 | char *result = malloc(len + 1); 11 | result[len] = '\0'; 12 | result[len - 1] = '\0'; 13 | 14 | int i, j, carry = 0; 15 | len = 0; 16 | for (i = len1 - 1, j = len2 - 1; carry || i >= 0 || j >= 0; i--, j--) { 17 | int na = i >= 0 ? a[i] - '0' : 0; 18 | int nb = j >= 0 ? b[j] - '0' : 0; 19 | result[len++] = (na ^ nb ^ carry) + '0'; 20 | carry = carry + na + nb >= 2 ? 1 : 0; 21 | } 22 | 23 | for (i = 0, j = len - 1; i < j; i++, j--) { 24 | char c = result[i]; 25 | result[i] = result[j]; 26 | result[j] = c; 27 | } 28 | 29 | return result; 30 | } 31 | 32 | int main(int argc, char **argv) 33 | { 34 | if (argc != 3) { 35 | fprintf(stderr, "Usage: ./test s1 s2\n"); 36 | exit(-1); 37 | } 38 | printf("%s\n", addBinary(argv[1], argv[2])); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /0075_sort_colors/sort_colors.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | static inline void swap(int *a, int *b) 7 | { 8 | int tmp = *a; 9 | *a = *b; 10 | *b = tmp; 11 | } 12 | 13 | void sortColors(int* nums, int numsSize) 14 | { 15 | int i, j = 0; 16 | for (i = 0; i < numsSize; i++) { 17 | if (nums[i] == 0) { 18 | swap(nums + j, nums + i); 19 | j++; 20 | } 21 | } 22 | 23 | for (i = j; i < numsSize; i++) { 24 | if (nums[i] == 1) { 25 | swap(nums + j, nums + i); 26 | j++; 27 | } 28 | } 29 | } 30 | 31 | int main(int argc, char **argv) 32 | { 33 | if (argc < 2) { 34 | fprintf(stderr, "Usage: ./test 2 0 2 1 1 0\n"); 35 | exit(-1); 36 | } 37 | int i, count = argc - 1; 38 | int *nums = malloc(count * sizeof(int)); 39 | for (i = 0; i < count; i++) { 40 | nums[i] = atoi(argv[i + 1]); 41 | } 42 | sortColors(nums, count); 43 | for (i = 0; i < count; i++) { 44 | printf("%d ", nums[i]); 45 | } 46 | printf("\n"); 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /0017_letter_combinations_of_a_phone_number/letter_combinations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | vector m = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 8 | vector ans; 9 | vector letterCombinations(string digits) 10 | { 11 | solve(0, "", digits); 12 | return ans; 13 | } 14 | void solve(int i, string path, string digits) 15 | { 16 | if (i >= digits.size()) 17 | { 18 | if (path.size() > 0) 19 | ans.push_back(path); 20 | return; 21 | } 22 | for (auto c : m[digits[i] - '0']) 23 | { 24 | path += c; 25 | solve(i + 1, path, digits); 26 | path.pop_back(); 27 | } 28 | } 29 | }; 30 | 31 | int main() 32 | { 33 | cout << "Enter A string : "; 34 | string s; 35 | cin >> s; 36 | Solution sol; 37 | // vector ans = sol.letterCombinations(s) ; 38 | for (auto str : sol.letterCombinations(s)) 39 | { 40 | cout << str << " "; 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /0065_valid_number/valid_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | static bool isNumber(const char *s) 7 | { 8 | while (*s == ' ') 9 | ++s; 10 | bool if_find_num = false; 11 | if (*s == '-' || *s == '+') 12 | ++s; 13 | while (isdigit(*s)) { 14 | if_find_num = true; 15 | ++s; 16 | } 17 | if (*s == '.') 18 | ++s; 19 | while (isdigit(*s)) { 20 | if_find_num = true; 21 | ++s; 22 | } 23 | if (if_find_num == true && *s == 'e') { 24 | ++s; 25 | if (*s == '+' || *s == '-') 26 | ++s; 27 | if_find_num = false; 28 | while (isdigit(*s)) { 29 | if_find_num = true; 30 | ++s; 31 | } 32 | } 33 | while (*s == ' ') 34 | ++s; 35 | return *s == '\0' && if_find_num == true; 36 | } 37 | 38 | int main(int argc, char** argv) 39 | { 40 | if (argc != 2) { 41 | fprintf(stderr, "Usage: ./test number\n"); 42 | exit(-1); 43 | } 44 | printf("%s\n", isNumber(argv[1]) ? "true" : "false"); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Leo Ma 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /0073_set_matrix_zeroes/set_zero.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void setZeroes(int** matrix, int matrixSize, int* matrixColSize) 6 | { 7 | int row, col; 8 | bool bRow = false, bCol = false; 9 | for (row = 0; row < matrixSize; row++) { 10 | for (col = 0; col < matrixColSize[row]; col++) { 11 | if (matrix[row][col] == 0) { 12 | if (row == 0) bRow = true; 13 | if (col == 0) bCol = true; 14 | matrix[0][col] = matrix[row][0] = 0; 15 | } 16 | } 17 | } 18 | 19 | for (row = 1; row < matrixSize; row++) { 20 | for(col = 1; col < matrixColSize[row]; col++){ 21 | if (matrix[0][col] == 0 || matrix[row][0] == 0) { 22 | matrix[row][col] = 0; 23 | } 24 | } 25 | } 26 | 27 | if (bRow) { 28 | memset(matrix[0], 0, matrixColSize[0] * sizeof(int)); 29 | } 30 | 31 | if (bCol) { 32 | for (row = 0; row < matrixSize; row++) { 33 | matrix[row][0] = 0; 34 | } 35 | } 36 | } 37 | 38 | int main(int argc, char **argv) 39 | { 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /0189_rotate_array/rotate_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static void reverse(int *nums, int lo, int hi) 5 | { 6 | while (lo < hi) { 7 | int tmp = nums[lo]; 8 | nums[lo] = nums[hi]; 9 | nums[hi] = tmp; 10 | lo++; 11 | hi--; 12 | } 13 | } 14 | 15 | static void rotate(int* nums, int numsSize, int k) 16 | { 17 | k %= numsSize; 18 | if (k == 0) { 19 | return; 20 | } 21 | 22 | reverse(nums, 0, numsSize - 1 - k); 23 | reverse(nums, numsSize - k, numsSize - 1); 24 | reverse(nums, 0, numsSize - 1); 25 | } 26 | 27 | int main(int argc, char **argv) 28 | { 29 | if (argc < 2) { 30 | fprintf(stderr, "Usage: ./test k n1 n2...\n"); 31 | exit(-1); 32 | } 33 | 34 | int k = atoi(argv[1]); 35 | int i, count = argc - 2; 36 | int *nums = malloc(count * sizeof(int)); 37 | for (i = 0; i < count; i++) { 38 | nums[i] = atoi(argv[i + 2]); 39 | } 40 | 41 | rotate(nums, count, k); 42 | 43 | for (i = 0; i < count; i++) { 44 | printf("%d ", nums[i]); 45 | } 46 | printf("\n"); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /0152_maximum_product_subarray/subarray.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | static inline int min(int a, int b) 7 | { 8 | return a < b ? a : b; 9 | } 10 | 11 | static inline int max(int a, int b) 12 | { 13 | return a > b ? a : b; 14 | } 15 | 16 | static int maxProduct(int* nums, int numsSize) 17 | { 18 | int i, maximum = INT_MIN; 19 | int dp_min = 1; 20 | int dp_max = 1; 21 | /* dp records the min or max result of subarray nums[0...i] */ 22 | for (i = 0; i < numsSize; i++) { 23 | int prev_min = dp_min; 24 | int prev_max = dp_max; 25 | dp_min = min(nums[i], min(prev_min * nums[i], prev_max * nums[i])); 26 | dp_max = max(nums[i], max(prev_min * nums[i], prev_max * nums[i])); 27 | maximum = max(dp_max, maximum); 28 | } 29 | 30 | return maximum; 31 | } 32 | 33 | int main(int argc, char **argv) 34 | { 35 | int i, count = argc - 1; 36 | int *nums = malloc(count * sizeof(int)); 37 | for (i = 0; i < count; i++) { 38 | nums[i] = atoi(argv[i + 1]); 39 | } 40 | printf("%d\n", maxProduct(nums, count)); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /0015_three_sum/three_sum.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | vector> threeSum(vector& nums) { 8 | vector> res; 9 | if (nums.size() >= 3) { 10 | sort(nums.begin(), nums.end()); 11 | for (int i = 0; i < nums.size() - 2; i++) { 12 | if (i > 0 && nums[i - 1] == nums[i]) { continue; } 13 | twoSum(nums.begin() + i + 1, nums.end() - 1, -nums[i], res); 14 | } 15 | } 16 | return res; 17 | } 18 | 19 | private: 20 | void twoSum(vector::iterator lo, vector::iterator hi, int target, vector>& res) { 21 | while (lo < hi) { 22 | int sum = *lo + *hi; 23 | if (sum < target) { 24 | lo++; 25 | } else if (sum > target) { 26 | hi--; 27 | } else { 28 | res.push_back(vector {-target, *lo, *hi}); 29 | while (++lo < hi && *lo == *(lo - 1)) {} 30 | while (lo < hi-- && *hi == *(hi + 1)) {} 31 | } 32 | } 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /0013_roman_to_integer/roman_to_integer.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int romanToInt(string s) { 8 | int curr = roman_to_integer(s[0]); 9 | int result = curr; 10 | 11 | for (int i = 1; i < s.length(); i++) { 12 | int prev = curr; 13 | curr = roman_to_integer(s[i]); 14 | /* left < right : IV(4), XL(40), IX(9) ... */ 15 | if (prev < curr) { 16 | result -= prev - (curr - prev); 17 | } else { 18 | result += curr; 19 | } 20 | } 21 | 22 | return result; 23 | } 24 | 25 | private: 26 | int roman_to_integer(char c) { 27 | switch(c) { 28 | case 'I': 29 | return 1; 30 | case 'V': 31 | return 5; 32 | case 'X': 33 | return 10; 34 | case 'L': 35 | return 50; 36 | case 'C': 37 | return 100; 38 | case 'D': 39 | return 500; 40 | case 'M': 41 | return 1000; 42 | default: 43 | return 0; 44 | } 45 | } 46 | }; 47 | -------------------------------------------------------------------------------- /0029_divide_two_integers/divide.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int divide(int dividend, int divisor) 6 | { 7 | int signal = 1; 8 | unsigned int dvd = dividend; 9 | if (dividend < 0) { 10 | signal *= -1; 11 | dvd = ~dvd + 1; 12 | } 13 | 14 | unsigned int dvs = divisor; 15 | if (divisor < 0) { 16 | signal *= -1; 17 | dvs = ~dvs + 1; 18 | } 19 | 20 | int shift = 0; 21 | while (dvd > dvs << shift) { 22 | shift++; 23 | } 24 | 25 | unsigned int res = 0; 26 | while (dvd >= dvs) { 27 | while (dvd < dvs << shift) { 28 | shift--; 29 | } 30 | res |= (unsigned int) 1 << shift; 31 | dvd -= dvs << shift; 32 | } 33 | 34 | if (signal == 1 && res >= INT_MAX) { 35 | return INT_MAX; 36 | } else { 37 | return res * signal; 38 | } 39 | } 40 | 41 | int main(int argc, char **argv) 42 | { 43 | if (argc != 3) { 44 | fprintf(stderr, "Usage: ./test dividend divisor\n"); 45 | exit(-1); 46 | } 47 | printf("%d\n", divide(atoi(argv[1]), atoi(argv[2]))); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /0119_pascal_triangle_ii/pascal_triangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | ** Return an array of size *returnSize. 6 | ** Note: The returned array must be malloced, assume caller calls free(). 7 | **/ 8 | static int* getRow(int rowIndex, int* returnSize) 9 | { 10 | int *row = malloc((rowIndex + 1) * sizeof(int)); 11 | *returnSize = rowIndex + 1; 12 | 13 | int num = rowIndex + 1; 14 | if (rowIndex < 1) { 15 | row[0] = 1; 16 | return row; 17 | } 18 | 19 | int i, j; 20 | for (i = 1; i <= rowIndex; i++) { 21 | num = i + 1; 22 | row[0] = 1; 23 | row[num - 1] = 1; 24 | for (j = num - 2; j >= 1; j--) { 25 | row[j] = row[j - 1] + row[j]; 26 | } 27 | } 28 | 29 | return row; 30 | } 31 | 32 | int main(int argc, char **argv) 33 | { 34 | if (argc != 2) { 35 | fprintf(stderr, "Usage: ./test n\n"); 36 | exit(-1); 37 | } 38 | int i, j, count = 0, row_idx = atoi(argv[1]); 39 | int *row = getRow(row_idx, &count); 40 | for (i = 0; i < count; i++) { 41 | printf("%d ", row[i]); 42 | } 43 | printf("\n"); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /0080_remove_duplicates_from_sorted_array_ii/rm_dups.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static int removeDuplicates(int* nums, int numsSize) 5 | { 6 | if (numsSize == 0) { 7 | return 0; 8 | } 9 | 10 | int i; 11 | int len = 0; 12 | int count = 1; 13 | for (i = 1; i < numsSize; i++) { 14 | /* Find the start position to be replaced */ 15 | if (nums[len] == nums[i]) { 16 | if (count < 2) { 17 | count++; 18 | /* Replace in each iteration */ 19 | nums[++len] = nums[i]; 20 | } 21 | } else { 22 | /* Here there are more than 2 duplicates */ 23 | count = 1; 24 | nums[++len] = nums[i]; 25 | } 26 | } 27 | 28 | return len + 1; 29 | } 30 | 31 | int main(int argc, char **argv) 32 | { 33 | int i, count = argc - 1; 34 | int *nums = malloc(count * sizeof(int)); 35 | for (i = 0; i < count; i++) { 36 | nums[i] = atoi(argv[i + 1]); 37 | } 38 | count = removeDuplicates(nums, count); 39 | for (i = 0; i < count; i++) { 40 | printf("%d ", nums[i]); 41 | } 42 | printf("\n"); 43 | } 44 | -------------------------------------------------------------------------------- /0230_kth_smallest_element_in_a_bst/kth_bst.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * struct TreeNode { 8 | * int val; 9 | * TreeNode *left; 10 | * TreeNode *right; 11 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 13 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 14 | * }; 15 | */ 16 | class Solution { 17 | public: 18 | int kthSmallest(TreeNode* root, int k) { 19 | stack stk; 20 | while (!stk.empty() || root != nullptr) { 21 | if (root != nullptr) { 22 | /* Store the parent node */ 23 | stk.push(root); 24 | root = root->left; 25 | } else { 26 | /* Pop up the parent node */ 27 | root = stk.top(); 28 | if (--k == 0) { 29 | break; 30 | } 31 | stk.pop(); 32 | root = root->right; 33 | } 34 | } 35 | 36 | return root->val; 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /0135_candy/candy.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int candy(int* ratings, int ratingsSize) 6 | { 7 | if (ratingsSize == 0) { 8 | return 0; 9 | } 10 | 11 | int i, *candies = malloc(ratingsSize * sizeof(int)); 12 | candies[0] = 1; 13 | 14 | for (i = 1; i < ratingsSize; i++) { 15 | if (ratings[i] > ratings[i - 1]) { 16 | candies[i] = candies[i - 1] + 1; 17 | } else { 18 | /* Set as least so that it will be reset from the opposite side */ 19 | candies[i] = 1; 20 | } 21 | } 22 | 23 | int sum = candies[ratingsSize - 1]; 24 | for (i = ratingsSize - 2; i >= 0; i--) { 25 | if (ratings[i] > ratings[i + 1] && candies[i] <= candies[i + 1]) { 26 | candies[i] = candies[i + 1] + 1; 27 | } 28 | sum += candies[i]; 29 | } 30 | 31 | return sum; 32 | } 33 | 34 | int main(int argc, char **argv) 35 | { 36 | int i, count = argc - 1; 37 | int *nums = malloc(count * sizeof(int)); 38 | for (i = 0; i < count; i++) { 39 | nums[i] = atoi(argv[i + 1]); 40 | } 41 | printf("%d\n", candy(nums, count)); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /0213_house_robber_ii/robber.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static inline int max(int a, int b) 6 | { 7 | return a > b ? a : b; 8 | } 9 | 10 | static int _rob(int* nums, int numsSize) 11 | { 12 | int i; 13 | int taken = 0; 14 | int untaken = 0; 15 | /* Record max profits of nums[0...i] respectively */ 16 | for (i = 0; i < numsSize; i++) { 17 | int tmp_taken = taken; 18 | /* Taken or untaken nums[i] */ 19 | taken = untaken + nums[i]; 20 | untaken = max(tmp_taken, untaken); 21 | } 22 | 23 | return max(taken, untaken); 24 | } 25 | 26 | static int rob(int* nums, int numsSize) 27 | { 28 | if (numsSize == 1) { 29 | return nums[0]; 30 | } else { 31 | /* The first and the last element are adjacent */ 32 | return max(_rob(nums + 1, numsSize - 1), _rob(nums, numsSize - 1)); 33 | } 34 | } 35 | 36 | int main(int argc, char **argv) 37 | { 38 | int i, count = argc - 1; 39 | int *nums = malloc(count * sizeof(int)); 40 | for (i = 0; i < count; i++) { 41 | nums[i] = atoi(argv[i + 1]); 42 | } 43 | printf("%d\n", rob(nums, count)); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /0518_coin_change_ii/coin_change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int change(int amount, int* coins, int coinsSize) 6 | { 7 | int i, j; 8 | unsigned int **dp = malloc((coinsSize + 1) * sizeof(unsigned int *)); 9 | 10 | for (i = 0; i <= coinsSize; i++) { 11 | dp[i] = calloc(amount + 1, sizeof(unsigned int)); 12 | dp[i][0] = 1; 13 | } 14 | 15 | for (i = 1; i <= coinsSize; i++) { 16 | for (j = 1; j <= amount; j++) { 17 | if (j - coins[i - 1] >= 0) { 18 | dp[i][j] = dp[i - 1][j] + dp[i][j - coins[i - 1]]; 19 | } else { 20 | dp[i][j] = dp[i - 1][j]; 21 | } 22 | } 23 | } 24 | 25 | return dp[coinsSize][amount]; 26 | } 27 | 28 | int main(int argc, char **argv) 29 | { 30 | if (argc < 3) { 31 | fprintf(stderr, "Usage: ./test 11 1 2 5"); 32 | exit(-1); 33 | } 34 | 35 | int amount = atoi(argv[1]); 36 | int i, size = argc - 2; 37 | int *coins = malloc(size * sizeof(int)); 38 | for (i = 0; i < size; i++) { 39 | coins[i] = atoi(argv[i + 2]); 40 | } 41 | printf("%d\n", change(amount, coins, size)); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /0151_reverse_words_in_a_string/reverse.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static reverse(char *s, int lo, int hi) 6 | { 7 | while (lo < hi) { 8 | char c = s[lo]; 9 | s[lo] = s[hi]; 10 | s[hi] = c; 11 | lo++; 12 | hi--; 13 | } 14 | } 15 | 16 | static void reverseWords(char *s) 17 | { 18 | int i = 0, j = 0; 19 | while (s[i] != '\0') { 20 | while (s[i] == ' ') { 21 | i++; 22 | } 23 | if (s[i] == '\0') break; 24 | while (s[i] != '\0' && s[i] != ' ') { 25 | s[j++] = s[i++]; 26 | } 27 | s[j++] = s[i]; 28 | } 29 | s[j] = '\0'; 30 | s[--j] = '\0'; 31 | reverse(s, 0, j - 1); 32 | 33 | for (i = 0, j = 0; s[i] != '\0';) { 34 | for (j = i; s[j] != '\0' && s[j] != ' '; j++) {} 35 | reverse(s, i, j - 1); 36 | for (i = j; s[i] != '\0' && s[i] == ' '; i++) {} 37 | } 38 | } 39 | 40 | int main(int argc, char **argv) 41 | { 42 | if (argc != 2) { 43 | fprintf(stderr, "Usage: ./test string\n"); 44 | exit(-1); 45 | } 46 | reverseWords(argv[1]); 47 | printf("%s\n", argv[1]); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /0354_russian_doll_envelopes/russian_doll.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int maxEnvelopes(vector>& envelopes) { 8 | vector piles; 9 | sort(envelopes.begin(), envelopes.end(), compare); 10 | for (const auto& e : envelopes) { 11 | int pos = binary_search(piles, -1, piles.size(), e[1]); 12 | if (pos == piles.size()) { 13 | piles.push_back(e[1]); 14 | } 15 | piles[pos] = e[1]; 16 | } 17 | return piles.size(); 18 | } 19 | private: 20 | static bool compare(const vector& a, const vector& b) { 21 | int wa = a[0]; 22 | int wb = b[0]; 23 | int ha = a[1]; 24 | int hb = b[1]; 25 | return wa == wb ? ha > hb : wa < wb; 26 | } 27 | 28 | int binary_search(vector& nums, int lo, int hi, int target) { 29 | while (lo + 1 < hi) { 30 | int mid = lo + (hi - lo) / 2; 31 | if (nums[mid] < target) { 32 | lo = mid; 33 | } else { 34 | hi = mid; 35 | } 36 | } 37 | return hi; 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /0020_valid_parentheses/valid_parentheses.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | bool isValid(string s) { 8 | stack stk; 9 | for (int i = 0; i < s.length(); i++) { 10 | switch(s[i]) { 11 | case '(': 12 | case '[': 13 | case '{': 14 | stk.push(s[i]); 15 | break; 16 | case ')': 17 | if (stk.empty() || stk.top() != '(') { 18 | return false; 19 | } else { 20 | stk.pop(); 21 | break; 22 | } 23 | case ']': 24 | if (stk.empty() || stk.top() != '[') { 25 | return false; 26 | } else { 27 | stk.pop(); 28 | break; 29 | } 30 | case '}': 31 | if (stk.empty() || stk.top() != '{') { 32 | return false; 33 | } else { 34 | stk.pop(); 35 | break; 36 | } 37 | } 38 | } 39 | return stk.empty(); 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /0041_first_missing_positive/missing_positive.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static inline void swap(int *a, int *b) 5 | { 6 | int tmp = *a; 7 | *a = *b; 8 | *b = tmp; 9 | } 10 | 11 | static int firstMissingPositive(int* nums, int numsSize) 12 | { 13 | if (numsSize == 0) { 14 | return 1; 15 | } 16 | 17 | int i = 0; 18 | while (i < numsSize) { 19 | /* nums[i] should be i+1 and nums[nums[i] - 1] should be nums[i] */ 20 | if (nums[i] != i + 1 && nums[i] > 0 && nums[i] <= numsSize && nums[nums[i] - 1] != nums[i]) { 21 | /* let nums[nums[i] - 1] = nums[i] */ 22 | swap(nums + i, nums + nums[i] - 1); 23 | } else { 24 | i++; 25 | } 26 | } 27 | 28 | for (i = 0; i < numsSize; i++) { 29 | if (nums[i] != i + 1) { 30 | break; 31 | } 32 | } 33 | 34 | return i + 1; 35 | } 36 | 37 | int main(int argc, char **argv) 38 | { 39 | int i, count = argc - 1; 40 | int *nums = malloc(count * sizeof(int)); 41 | for (i = 0; i < count; i++) { 42 | nums[i] = atoi(argv[i + 1]); 43 | } 44 | printf("%d\n", firstMissingPositive(nums, count)); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /0073_set_matrix_zeroes/set_zero.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | public: 6 | void setZeroes(vector>& matrix) { 7 | bool bRow = false, bCol = false; 8 | for (int row = 0; row < matrix.size(); row++) { 9 | for (int col = 0; col < matrix[row].size(); col++) { 10 | if (matrix[row][col] == 0) { 11 | if (row == 0) { bRow = true; } 12 | if (col == 0) { bCol = true; } 13 | matrix[0][col] = matrix[row][0] = 0; 14 | } 15 | } 16 | } 17 | 18 | for (int row = 1; row < matrix.size(); row++) { 19 | for (int col = 1; col < matrix[row].size(); col++) { 20 | if (matrix[0][col] == 0 || matrix[row][0] == 0) { 21 | matrix[row][col] = 0; 22 | } 23 | } 24 | } 25 | 26 | if (bRow) { 27 | for (auto& m : matrix[0]) { 28 | m = 0; 29 | } 30 | } 31 | 32 | if (bCol) { 33 | for (int row = 0; row < matrix.size(); row++) { 34 | matrix[row][0] = 0; 35 | } 36 | } 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /0160_intersection_of_two_linked_list/intersection.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for singly-linked list. 7 | * struct ListNode { 8 | * int val; 9 | * ListNode *next; 10 | * ListNode(int x) : val(x), next(NULL) {} 11 | * }; 12 | */ 13 | class Solution { 14 | public: 15 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { 16 | int lenA = getListLength(headA); 17 | int lenB = getListLength(headB); 18 | if (lenA > lenB) { 19 | for (int i = 0; i < lenA - lenB; i++) { 20 | headA = headA->next; 21 | } 22 | } else { 23 | for (int i = 0; i < lenB - lenA; i++) { 24 | headB = headB->next; 25 | } 26 | } 27 | 28 | while (headA != nullptr && headB != nullptr && headA != headB) { 29 | headA = headA->next; 30 | headB = headB->next; 31 | } 32 | 33 | return headA; 34 | } 35 | private: 36 | int getListLength(ListNode *h) { 37 | int len = 0; 38 | while (h != nullptr) { 39 | len++; 40 | h = h->next; 41 | } 42 | return len; 43 | } 44 | }; 45 | -------------------------------------------------------------------------------- /1143_longest_common_subsequence/lcs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | static int max(int a, int b) 7 | { 8 | return a > b ? a : b; 9 | } 10 | 11 | int longestCommonSubsequence(char * text1, char * text2) 12 | { 13 | int i, j; 14 | int l1 = strlen(text1); 15 | int l2 = strlen(text2); 16 | int **dp = malloc((l1 + 1) * sizeof(int *)); 17 | for (i = 0; i < l1 + 1; i++) { 18 | dp[i] = malloc((l2 + 1) * sizeof(int)); 19 | } 20 | memset(dp[0], 0, (l2 + 1) * sizeof(int)); 21 | for (i = 1; i <= l1; i++) { 22 | dp[i][0] = 0; 23 | } 24 | 25 | for (i = 1; i <= l1; i++) { 26 | for (j = 1; j <= l2; j++) { 27 | if (text1[i - 1] == text2[j - 1]) { 28 | dp[i][j] = dp[i - 1][j - 1] + 1; 29 | } else { 30 | dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); 31 | } 32 | } 33 | } 34 | return dp[l1][l2]; 35 | } 36 | 37 | int main(int argc, char **argv) 38 | { 39 | if (argc != 3) { 40 | fprintf(stderr, "Usage: ./test s1 s2\n"); 41 | exit(-1); 42 | } 43 | 44 | printf("%d\n", longestCommonSubsequence(argv[1], argv[2])); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /0098_validate_binary_search_tree/valid_bst.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * struct TreeNode { 8 | * int val; 9 | * TreeNode *left; 10 | * TreeNode *right; 11 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 12 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 13 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 14 | * }; 15 | */ 16 | class Solution { 17 | public: 18 | bool isValidBST(TreeNode* root) { 19 | stack stk; 20 | int prev = INT_MIN; 21 | bool first = true; 22 | while (!stk.empty() || root != nullptr) { 23 | if (root != nullptr) { 24 | stk.push(root); 25 | root = root->left; 26 | } else { 27 | root = stk.top(); 28 | stk.pop(); 29 | if (!first && prev >= root->val) { 30 | return false; 31 | } 32 | first = false; 33 | prev = root->val; 34 | root = root->right; 35 | } 36 | } 37 | return true; 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /0165_compare_version_numbers/version.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static int compareVersioin(char *v1, char *v2) 5 | { 6 | int ret = 0; 7 | while (*v1 != '\0' || *v2 != '\0') { 8 | while (*v1 != '\0' && *v1 == '0') { 9 | v1++; 10 | } 11 | while (*v2 != '\0' && *v2 == '0') { 12 | v2++; 13 | } 14 | 15 | int n1 = 0; 16 | while (*v1 != '\0' && *v1 != '.') { 17 | n1 = n1 * 10 + (*v1 - '0'); 18 | v1++; 19 | } 20 | 21 | int n2 = 0; 22 | while (*v2 != '\0' && *v2 != '.') { 23 | n2 = n2 * 10 + (*v2 - '0'); 24 | v2++; 25 | } 26 | 27 | ret = n1 - n2; 28 | if (ret != 0) { 29 | return ret > 0 ? 1 : -1; 30 | } 31 | 32 | if (*v1 == '.') { 33 | v1++; 34 | } 35 | if (*v2 == '.') { 36 | v2++; 37 | } 38 | } 39 | return ret; 40 | } 41 | 42 | int main(int argc, char **argv) 43 | { 44 | if (argc != 3) { 45 | fprintf(stderr, "Usage: ./test versioin1 version2\n"); 46 | exit(-1); 47 | } 48 | printf("%d\n", compareVersioin(argv[1], argv[2])); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /0516_longest_palindromic_subsequence/lps.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | static inline int max(int a, int b) 7 | { 8 | return a > b ? a : b; 9 | } 10 | 11 | int longestPalindromeSubseq(char * s) 12 | { 13 | int i, j, k; 14 | int len = strlen(s); 15 | int **dp = malloc(len * sizeof(int *)); 16 | 17 | /* The dp array indicates the length of palindrome subsequence of 18 | * nums[i...j] */ 19 | for (i = 0; i < len; i++) { 20 | dp[i] = malloc(len * sizeof(int)); 21 | memset(dp[i], 0, len * sizeof(int)); 22 | dp[i][i] = 1; 23 | } 24 | 25 | for (k = 1; k < len; k++) { 26 | for (i = 0; i < len - k; i++) { 27 | j = i + k; 28 | if (s[i] == s[j]) { 29 | dp[i][j] = dp[i + 1][j - 1] + 2; 30 | } else { 31 | dp[i][j] = max(dp[i][j - 1], dp[i + 1][j]); 32 | } 33 | } 34 | } 35 | return dp[0][len - 1]; 36 | } 37 | 38 | int main(int argc, char **argv) 39 | { 40 | if (argc != 2) { 41 | fprintf(stderr, "Usage: ./test s\n"); 42 | exit(-1); 43 | } 44 | 45 | printf("%d\n", longestPalindromeSubseq(argv[1])); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /0013_roman_to_integer/roman_to_integer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static int roman_to_integer(char c) 5 | { 6 | switch(c) { 7 | case 'I': 8 | return 1; 9 | case 'V': 10 | return 5; 11 | case 'X': 12 | return 10; 13 | case 'L': 14 | return 50; 15 | case 'C': 16 | return 100; 17 | case 'D': 18 | return 500; 19 | case 'M': 20 | return 1000; 21 | default: 22 | return 0; 23 | } 24 | } 25 | 26 | int romanToInt (char *s) 27 | { 28 | int i, curr = roman_to_integer(s[0]); 29 | int result = curr; 30 | 31 | for (i = 1; s[i] != '\0'; i++) { 32 | int prev = curr; 33 | curr = roman_to_integer(s[i]); 34 | /* left < right : IV(4), XL(40), IX(9) ... */ 35 | if (prev < curr) { 36 | result -= prev - (curr - prev); 37 | } else { 38 | result += curr; 39 | } 40 | } 41 | 42 | return result; 43 | } 44 | 45 | int main(int argc, char **argv) 46 | { 47 | if (argc < 2) { 48 | fprintf(stderr, "Usage: ./test roman\n"); 49 | exit(-1); 50 | } 51 | printf("%d\n", romanToInt(argv[1])); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /0043_multiply_strings/multiply_strings.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | static char* multiply(char* num1, char* num2) 7 | { 8 | if (*num1 == '\0') { 9 | return num1; 10 | } 11 | 12 | if (*num2 == '\0') { 13 | return num2; 14 | } 15 | 16 | int i, j; 17 | int len1 = strlen(num1); 18 | int len2 = strlen(num2); 19 | char *result = malloc(len1 + len2 + 1); 20 | memset(result, '0', len1 + len2 + 1); 21 | result[len1 + len2] = '\0'; 22 | 23 | for (i = len2 - 1; i >= 0; i--) { 24 | int carry = 0; 25 | for (j = len1 - 1; j >= 0; j--) { 26 | carry += (num2[i] - '0') * (num1[j] - '0') + (result[i + j + 1] - '0'); 27 | result[i + j + 1] = carry % 10 + '0'; 28 | carry /= 10; 29 | } 30 | result[i + j + 1] = carry + '0'; 31 | } 32 | 33 | while (result[0] == '0' && result[1] != '\0') { 34 | result++; 35 | } 36 | 37 | return result; 38 | } 39 | 40 | int main(int argc, char **argv) 41 | { 42 | if (argc != 3) { 43 | fprintf(stderr, "Usage: ./test m1 m2\n"); 44 | exit(-1); 45 | } 46 | printf("%s\n", multiply(argv[1], argv[2])); 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /0040_combination_sum_ii/combination_sum.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | vector> combinationSum2(vector& candidates, int target) { 8 | vector> res; 9 | sort(candidates.begin(), candidates.end()); 10 | dfs(candidates, 0, target, res); 11 | return res; 12 | } 13 | 14 | private: 15 | vector stack; 16 | void dfs(vector& candidates, int start, int target, vector>& res) { 17 | if (target < 0) { 18 | return; 19 | } else if (target == 0) { 20 | res.push_back(stack); 21 | } else { 22 | int last = INT_MIN; 23 | for (int i = start; i < candidates.size(); i++) { 24 | if (last != candidates[i]) { 25 | /* No duplicate combinations in the same level position */ 26 | stack.push_back(candidates[i]); 27 | /* i + 1 limits the candidate range in next levels */ 28 | dfs(candidates, i + 1, target - candidates[i], res); 29 | stack.pop_back(); 30 | } 31 | last = candidates[i]; 32 | } 33 | } 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /0025_reverse_nodes_in_k_group/reverse_nodes.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | * Definition for singly-linked list. 7 | * struct ListNode { 8 | * int val; 9 | * ListNode *next; 10 | * ListNode() : val(0), next(nullptr) {} 11 | * ListNode(int x) : val(x), next(nullptr) {} 12 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 13 | * }; 14 | */ 15 | class Solution { 16 | public: 17 | ListNode* reverseGroup(ListNode* head, int k) { 18 | int len = 0; 19 | struct ListNode dummy, *prev = &dummy; 20 | dummy.next = head; 21 | 22 | for (; head != nullptr; head = head->next) { 23 | if (++len % k == 0) { 24 | struct ListNode *p = prev->next; 25 | while (prev->next != head) { 26 | struct ListNode *q = p->next; 27 | /* deletion */ 28 | p->next = q->next; 29 | /* insertion */ 30 | q->next = prev->next; 31 | prev->next = q; 32 | } 33 | /* iteration */ 34 | prev = p; 35 | head = p; 36 | } 37 | } 38 | 39 | return dummy.next; 40 | } 41 | }; 42 | --------------------------------------------------------------------------------