├── 3Sum ├── 3Sum.h ├── Makefile └── test.cpp ├── 3Sum_closest ├── 3Sum_closest.h ├── Makefile └── test.cpp ├── 4Sum ├── 4Sum.h ├── Makefile └── test.cpp ├── LICENSE ├── LeetCode.html ├── LeetCode_files ├── 200px-Telephone-keypad2.svg.png ├── 250px-Sudoku-by-L2G-20050714.svg.png ├── 250px-Sudoku-by-L2G-20050714_solution.svg.png ├── 8-queens.png ├── 8521ce19280cb4ce8da26cbfde769b09-bpthumb.jpg ├── LclRCoAgEAXAE7WP6ie8zSqLrihaq1C3D6LfmTRGdwBnvim2FotwV6PQ6mco6g35nHI9U7HSQduOkaSKwbPJf8tUCmYv.css ├── PY5RDsMwCEMvVMb2sQOlCWojNQkCqi47_Wha7QPZQg-bJ6r1jaY3hlRyhTnIw3V64byn1FlIFWeG2IQwuv9jjsRdrZX8pQRCkao5VoqrDvRKPs7jar7FJYFaEJBguS6DueyZJaG3CtqrhQ-seVk3HyMZmHfexP3d0SSN54Ab75uHclO7O_Fg_gE.css ├── VY5LDsIwDEQv1NRC9Cjsq3ysNCixi-0KcntCYcPC8njmjeQr3B8HSp8ukJNT8-LEW6EMdx2Oygii-M7ktJP5l9tK3uoYQ_kwrdAPmIecnruLTIZkYBs2VAhHSj2wKqyFIuTKwddpgcitDcwJ7rWf3QV8GtsFL-cdOeGKlAsheFUU-7O-j88ahWu9sRuVNw.js ├── ga(1).js ├── ga(2).js ├── ga.js ├── gdsr.css.php ├── histogram.png ├── histogram_area.png ├── leetcodelogo.png ├── rainwatertrap.png ├── robot_maze.png └── yygpKbDS10_MSqzQS8_PT89JTSzILNZLzs8Fi-nnZCYV62cVlqYWVZZm6hvqWegZGUP5uqWZermZeXpZxQA.js ├── NOTES.md ├── PROGRESS.md ├── README.md ├── TODO.md ├── add_binary ├── Makefile ├── add_binary.h └── test.cpp ├── add_two_numbers ├── Makefile ├── add_two_numbers.h └── test.cpp ├── anagrams ├── Makefile ├── anagrams.h └── test.cpp ├── balanced_binary_tree ├── Makefile ├── balanced_binary_tree.h └── test.cpp ├── best_time_to_buy_and_sell_stock ├── Makefile ├── best_time_to_buy_and_sell_stock.h └── test.cpp ├── best_time_to_buy_and_sell_stock_2 ├── Makefile ├── best_time_to_buy_and_sell_stock_2.h └── test.cpp ├── best_time_to_buy_and_sell_stock_3 ├── Makefile ├── best_time_to_buy_and_sell_stock_3.h └── test.cpp ├── binary_tree_inorder_traversal ├── Makefile ├── binary_tree_inorder_traversal.h └── test.cpp ├── binary_tree_level_order_traversal ├── Makefile ├── binary_tree_level_order_traversal.h └── test.cpp ├── binary_tree_level_order_traversal_2 ├── Makefile ├── binary_tree_level_order_traversal_2.h └── test.cpp ├── binary_tree_maximum_path_sum ├── Makefile ├── binary_tree_maximum_path_sum.h └── test.cpp ├── binary_tree_zigzag_level_order_traversal ├── Makefile ├── binary_tree_zigzag_level_order_traversal.h └── test.cpp ├── climbing_stairs ├── Makefile ├── climbing_stairs.h └── test.cpp ├── combination_sum ├── Makefile ├── combination_sum.h └── test.cpp ├── combination_sum_2 ├── Makefile ├── combination_sum_2.h └── test.cpp ├── combinations ├── Makefile ├── combinations.h └── test.cpp ├── const-var.exp ├── construct_binary_tree_from_inorder_and_postorder_traversal ├── Makefile ├── construct_binary_tree_from_inorder_and_postorder_traversal.h └── test.cpp ├── construct_binary_tree_from_preorder_and_inorder_traversal ├── Makefile ├── construct_binary_tree_from_preorder_and_inorder_traversal.h └── test.cpp ├── container_with_most_water ├── Makefile ├── container_with_most_water.h └── test.cpp ├── convert_sorted_array_to_binary_search_tree ├── Makefile ├── convert_sorted_array_to_binary_search_tree.h └── test.cpp ├── convert_sorted_list_to_binary_search_tree ├── Makefile ├── convert_sorted_list_to_binary_search_tree.h └── test.cpp ├── count_and_say ├── Makefile ├── count_and_say.h └── test.cpp ├── decode_ways ├── Makefile ├── decode_ways.h └── test.cpp ├── distinct_subsequences ├── Makefile ├── distinct_subsequences.h └── test.cpp ├── divide_two_integers ├── Makefile ├── divide_two_integers.h └── test.cpp ├── edit_distance ├── Makefile ├── edit_distance.h └── test.cpp ├── first_missing_positive ├── Makefile ├── first_missing_positive.h └── test.cpp ├── flatten_binary_tree_to_linked_list ├── Makefile ├── flatten_binary_tree_to_linked_list.h └── test.cpp ├── generate.sh ├── generate_parentheses ├── Makefile ├── generate_parentheses.h └── test.cpp ├── gray_code ├── Makefile ├── gray_code.h └── test.cpp ├── implement_strStr ├── Makefile ├── implement_strStr.h └── test.cpp ├── include └── test.h ├── insert_interval ├── Makefile ├── insert_interval.h └── test.cpp ├── integer_to_roman ├── Makefile ├── integer_to_roman.h └── test.cpp ├── interleaving_string ├── Makefile ├── interleaving_string.h └── test.cpp ├── jump_game ├── Makefile ├── jump_game.h └── test.cpp ├── jump_game_2 ├── Makefile ├── jump_game_2.h └── test.cpp ├── largest_rectangle_in_histogram ├── Makefile ├── largest_rectangle_in_histogram.h └── test.cpp ├── length_of_last_word ├── Makefile ├── length_of_last_word.h └── test.cpp ├── letter_combinations_of_a_phone_number ├── Makefile ├── letter_combinations_of_a_phone_number.h └── test.cpp ├── longest_common_prefix ├── Makefile ├── longest_common_prefix.h └── test.cpp ├── longest_consecutive_sequence ├── Makefile ├── longest_consecutive_sequence.h └── test.cpp ├── longest_palindromic_substring ├── Makefile ├── longest_palindromic_substring.h └── test.cpp ├── longest_substring_without_repeating_characters ├── Makefile ├── longest_substring_without_repeating_characters.h └── test.cpp ├── longest_valid_parentheses ├── Makefile ├── longest_valid_parentheses.h └── test.cpp ├── maximal_rectangle ├── Makefile ├── maximal_rectangle.h └── test.cpp ├── maximum_depth_of_binary_tree ├── Makefile ├── maximum_depth_of_binary_tree.h └── test.cpp ├── maximum_subarray ├── Makefile ├── maximum_subarray.h └── test.cpp ├── median_of_two_sorted_arrays ├── Makefile ├── median_of_two_sorted_arrays.h └── test.cpp ├── merge_intervals ├── Makefile ├── merge_intervals.h └── test.cpp ├── merge_k_sorted_lists ├── Makefile ├── merge_k_sorted_lists.h └── test.cpp ├── merge_sorted_array ├── Makefile ├── merge_sorted_array.h └── test.cpp ├── merge_two_sorted_lists ├── Makefile ├── merge_two_sorted_lists.h └── test.cpp ├── minimum_depth_of_binary_tree ├── Makefile ├── minimum_depth_of_binary_tree.h └── test.cpp ├── minimum_path_sum ├── Makefile ├── minimum_path_sum.h └── test.cpp ├── minimum_window_substring ├── Makefile ├── minimum_window_substring.h └── test.cpp ├── multiply_strings ├── Makefile ├── multiply_strings.h └── test.cpp ├── n-Queens ├── Makefile ├── n-Queens.h └── test.cpp ├── n-Queens_2 ├── Makefile ├── n-Queens_2.h └── test.cpp ├── next_permutation ├── Makefile ├── next_permutation.h └── test.cpp ├── palindrome_number ├── Makefile ├── palindrome_number.h └── test.cpp ├── palindrome_partitioning ├── Makefile ├── palindrome_partitioning.h └── test.cpp ├── palindrome_partitioning_2 ├── Makefile ├── palindrome_partitioning_2.h └── test.cpp ├── partition_list ├── Makefile ├── partition_list.h └── test.cpp ├── pascal_triangle ├── Makefile ├── pascal_triangle.h └── test.cpp ├── pascal_triangle_2 ├── Makefile ├── pascal_triangle_2.h └── test.cpp ├── path_sum ├── Makefile ├── path_sum.h └── test.cpp ├── path_sum_2 ├── Makefile ├── path_sum_2.h └── test.cpp ├── permutation_sequence ├── Makefile ├── permutation_sequence.h └── test.cpp ├── permutations ├── Makefile ├── permutations.h └── test.cpp ├── permutations_2 ├── Makefile ├── permutations_2.h └── test.cpp ├── plus_one ├── Makefile ├── plus_one.h └── test.cpp ├── populating_next_right_pointers_in_each_node ├── Makefile ├── populating_next_right_pointers_in_each_node.h └── test.cpp ├── populating_next_right_pointers_in_each_node_2 ├── Makefile ├── populating_next_right_pointers_in_each_node_2.h └── test.cpp ├── pow ├── Makefile ├── pow.h └── test.cpp ├── recover_binary_search_tree ├── Makefile ├── recover_binary_search_tree.h └── test.cpp ├── regular_expression_matching ├── Makefile ├── regular_expression_matching.h └── test.cpp ├── remove_duplicates_from_sorted_array ├── Makefile ├── remove_duplicates_from_sorted_array.h └── test.cpp ├── remove_duplicates_from_sorted_array_2 ├── Makefile ├── remove_duplicates_from_sorted_array_2.h └── test.cpp ├── remove_duplicates_from_sorted_list ├── Makefile ├── remove_duplicates_from_sorted_list.h └── test.cpp ├── remove_duplicates_from_sorted_list_2 ├── Makefile ├── remove_duplicates_from_sorted_list_2.h └── test.cpp ├── remove_element ├── Makefile ├── remove_element.h └── test.cpp ├── remove_nth_node_from_end_of_list ├── Makefile ├── remove_nth_node_from_end_of_list.h └── test.cpp ├── restore_ip_addresses ├── Makefile ├── restore_ip_addresses.h └── test.cpp ├── reverse_integer ├── Makefile ├── reverse_integer.h └── test.cpp ├── reverse_linked_list_2 ├── Makefile ├── reverse_linked_list_2.h └── test.cpp ├── reverse_nodes_in_k_group ├── Makefile ├── reverse_nodes_in_k_group.h └── test.cpp ├── roman_to_integer ├── Makefile ├── roman_to_integer.h └── test.cpp ├── rotate_image ├── Makefile ├── rotate_image.h └── test.cpp ├── rotate_list ├── Makefile ├── rotate_list.h └── test.cpp ├── same_tree ├── Makefile ├── same_tree.h └── test.cpp ├── scramble_string ├── Makefile ├── scramble_string.h └── test.cpp ├── search_a_2D_matrix ├── Makefile ├── search_a_2D_matrix.h └── test.cpp ├── search_for_a_range ├── Makefile ├── search_for_a_range.h └── test.cpp ├── search_in_rotated_sorted_array ├── Makefile ├── search_in_rotated_sorted_array.h └── test.cpp ├── search_in_rotated_sorted_array_2 ├── Makefile ├── search_in_rotated_sorted_array_2.h └── test.cpp ├── search_insert_position ├── Makefile ├── search_insert_position.h └── test.cpp ├── set_matrix_zeroes ├── Makefile ├── set_matrix_zeroes.h └── test.cpp ├── simplify_path ├── Makefile ├── simplify_path.h └── test.cpp ├── sort_colors ├── Makefile ├── sort_colors.h └── test.cpp ├── spiral_matrix ├── Makefile ├── spiral_matrix.h └── test.cpp ├── spiral_matrix_2 ├── Makefile ├── spiral_matrix_2.h └── test.cpp ├── sqrt ├── Makefile ├── sqrt.h └── test.cpp ├── string_to_integer ├── Makefile ├── string_to_integer.h └── test.cpp ├── subsets ├── Makefile ├── subsets.h └── test.cpp ├── subsets_2 ├── Makefile ├── subsets_2.h └── test.cpp ├── substring_with_concatenation_of_all_words ├── Makefile ├── substring_with_concatenation_of_all_words.h └── test.cpp ├── sudoku_solver ├── Makefile ├── sudoku_solver.h └── test.cpp ├── sum_root_to_leaf_numbers ├── Makefile ├── sum_root_to_leaf_numbers.h └── test.cpp ├── surrounded_regions ├── Makefile ├── surrounded_regions.h └── test.cpp ├── swap_nodes_in_pairs ├── Makefile ├── swap_nodes_in_pairs.h └── test.cpp ├── symmetric_tree ├── Makefile ├── symmetric_tree.h └── test.cpp ├── template ├── Makefile ├── test.cpp └── test_file.h ├── text_justification ├── Makefile ├── test.cpp └── text_justification.h ├── trapping_rain_water ├── Makefile ├── test.cpp └── trapping_rain_water.h ├── triangle ├── Makefile ├── test.cpp └── triangle.h ├── two_sum ├── Makefile ├── test.cpp └── two_sum.h ├── unique_binary_search_trees ├── Makefile ├── test.cpp └── unique_binary_search_trees.h ├── unique_binary_search_trees_2 ├── Makefile ├── test.cpp └── unique_binary_search_trees_2.h ├── unique_paths ├── Makefile ├── test.cpp └── unique_paths.h ├── unique_paths_2 ├── Makefile ├── test.cpp └── unique_paths_2.h ├── valid_number ├── Makefile ├── test.cpp └── valid_number.h ├── valid_palindrome ├── Makefile ├── test.cpp └── valid_palindrome.h ├── valid_parentheses ├── Makefile ├── test.cpp └── valid_parentheses.h ├── valid_sudoku ├── Makefile ├── test.cpp └── valid_sudoku.h ├── validate_binary_search_tree ├── Makefile ├── test.cpp └── validate_binary_search_tree.h ├── wildcard_matching ├── Makefile ├── test.cpp └── wildcard_matching.h ├── word_ladder ├── Makefile ├── test.cpp └── word_ladder.h ├── word_ladder_2 ├── Makefile ├── test.cpp └── word_ladder_2.h ├── word_search ├── Makefile ├── test.cpp └── word_search.h └── zigZag_conversion ├── Makefile ├── test.cpp └── zigZag_conversion.h /3Sum/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /3Sum/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "3Sum.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {-1, 0, 1, 2, -1, -4}; 19 | vector v; 20 | INT_ARR_TO_VEC(v, a); 21 | 22 | print_vector_vector(solution.threeSum(v)); 23 | } 24 | 25 | { 26 | int a[] = {-1, 0, 1, 0, 2, -1, 0, 2, -4}; 27 | vector v; 28 | INT_ARR_TO_VEC(v, a); 29 | 30 | print_vector_vector(solution.threeSum(v)); 31 | } 32 | 33 | { 34 | int a[] = {-1, 0, 18, 9, -30, -4}; 35 | vector v; 36 | INT_ARR_TO_VEC(v, a); 37 | 38 | print_vector_vector(solution.threeSum(v)); 39 | } 40 | 41 | //Error test cases from leetcode.com 42 | { 43 | vector v; 44 | 45 | print_vector_vector(solution.threeSum(v)); 46 | } 47 | 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /3Sum_closest/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /3Sum_closest/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "3Sum_closest.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {-1, 2, 1, -4}; 19 | vector v; 20 | INT_ARR_TO_VEC(v, a); 21 | 22 | int target = 1; 23 | cout << solution.threeSumClosest(v, target) << endl; 24 | } 25 | 26 | { 27 | int a[] = {-1, 2, 1, -4}; 28 | vector v; 29 | INT_ARR_TO_VEC(v, a); 30 | 31 | int target = 2; 32 | cout << solution.threeSumClosest(v, target) << endl; 33 | } 34 | 35 | { 36 | int a[] = {-1, 2, 1, 8, 2, 3,-4, 8, 8}; 37 | vector v; 38 | INT_ARR_TO_VEC(v, a); 39 | 40 | int target = 25; 41 | cout << solution.threeSumClosest(v, target) << endl; 42 | } 43 | 44 | //Error test cases from leetcode.com 45 | 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /4Sum/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /LeetCode_files/200px-Telephone-keypad2.svg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hex108/leetcode/e1df4bc43646ab08e79afb56dfccf09edc6e71a2/LeetCode_files/200px-Telephone-keypad2.svg.png -------------------------------------------------------------------------------- /LeetCode_files/250px-Sudoku-by-L2G-20050714.svg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hex108/leetcode/e1df4bc43646ab08e79afb56dfccf09edc6e71a2/LeetCode_files/250px-Sudoku-by-L2G-20050714.svg.png -------------------------------------------------------------------------------- /LeetCode_files/250px-Sudoku-by-L2G-20050714_solution.svg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hex108/leetcode/e1df4bc43646ab08e79afb56dfccf09edc6e71a2/LeetCode_files/250px-Sudoku-by-L2G-20050714_solution.svg.png -------------------------------------------------------------------------------- /LeetCode_files/8-queens.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hex108/leetcode/e1df4bc43646ab08e79afb56dfccf09edc6e71a2/LeetCode_files/8-queens.png -------------------------------------------------------------------------------- /LeetCode_files/8521ce19280cb4ce8da26cbfde769b09-bpthumb.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hex108/leetcode/e1df4bc43646ab08e79afb56dfccf09edc6e71a2/LeetCode_files/8521ce19280cb4ce8da26cbfde769b09-bpthumb.jpg -------------------------------------------------------------------------------- /LeetCode_files/histogram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hex108/leetcode/e1df4bc43646ab08e79afb56dfccf09edc6e71a2/LeetCode_files/histogram.png -------------------------------------------------------------------------------- /LeetCode_files/histogram_area.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hex108/leetcode/e1df4bc43646ab08e79afb56dfccf09edc6e71a2/LeetCode_files/histogram_area.png -------------------------------------------------------------------------------- /LeetCode_files/leetcodelogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hex108/leetcode/e1df4bc43646ab08e79afb56dfccf09edc6e71a2/LeetCode_files/leetcodelogo.png -------------------------------------------------------------------------------- /LeetCode_files/rainwatertrap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hex108/leetcode/e1df4bc43646ab08e79afb56dfccf09edc6e71a2/LeetCode_files/rainwatertrap.png -------------------------------------------------------------------------------- /LeetCode_files/robot_maze.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hex108/leetcode/e1df4bc43646ab08e79afb56dfccf09edc6e71a2/LeetCode_files/robot_maze.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Fuck the leetcode.com 2 | 3 | I find designing algorithm is a very cool thing. I have solved some problems which I did not know how to solve. And I have recognized that implementing effectively is very very important. 4 | 5 | [NOTES.md](https://github.com/hex108/leetcode/blob/master/NOTES.md) is my notes. 6 | 7 | ## About 8 | ### How to wirte a new program 9 | `./generate.sh your_program_name` 10 | 11 | It will generate a directory named 'your\_program\_name' and some files in it using the template in directory 'template'. 12 | 13 | ### Where is questions 14 | I have saved questions to 'Leetcode.html'. You could also visit [Leetcode](http://www.leetcode.com). 15 | 16 | ## Last 17 | Best wishes to you! 18 | -------------------------------------------------------------------------------- /TODO.md: -------------------------------------------------------------------------------- 1 | * A star 2 | 3 | what is the difference between A star and backtracking? 4 | 5 | * segment tree 6 | 7 | * cast size_t to int 8 | 9 | * Pruning (decision trees) 10 | 11 | * hash in stl 12 | -------------------------------------------------------------------------------- /add_binary/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /add_binary/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "add_binary.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | string a = "11"; 19 | string b = "1"; 20 | 21 | cout << solution.addBinary(a, b) << endl; 22 | } 23 | 24 | { 25 | string a = "11"; 26 | string b = "11"; 27 | 28 | cout << solution.addBinary(a, b) << endl; 29 | } 30 | 31 | //Error test cases from leetcode.com 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /add_two_numbers/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /add_two_numbers/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "add_two_numbers.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | ListNode n1(2), n2(4), n3(3), n4(5), n5(6), n6(4); 19 | n1.next = &n2; 20 | n2.next = &n3; 21 | 22 | n4.next = &n5; 23 | n5.next = &n6; 24 | 25 | print_list_node(solution.addTwoNumbers(&n1, &n4)); 26 | } 27 | 28 | { 29 | ListNode n1(2), n2(4), n3(9), n4(9), n5(9), n6(9); 30 | n1.next = &n2; 31 | 32 | n3.next = &n4; 33 | n4.next = &n5; 34 | n5.next = &n6; 35 | 36 | print_list_node(solution.addTwoNumbers(&n1, &n3)); 37 | } 38 | 39 | //Error test cases from leetcode.com 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /anagrams/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /anagrams/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "anagrams.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | char *a[] = { 19 | "test", "hello", "loleh", "", "lehlo", "hello9" 20 | }; 21 | 22 | vector v; 23 | 24 | for(int i = 0; i < sizeof(a) / sizeof(a[0]); i ++){ 25 | v.push_back(a[i]); 26 | } 27 | 28 | print_vector(solution.anagrams(v)); 29 | } 30 | 31 | //Error test cases from leetcode.com 32 | { 33 | char *a[] = { 34 | "tea","and","ate","eat","dan" 35 | }; 36 | 37 | vector v; 38 | 39 | for(int i = 0; i < sizeof(a) / sizeof(a[0]); i ++){ 40 | v.push_back(a[i]); 41 | } 42 | 43 | print_vector(solution.anagrams(v)); 44 | } 45 | 46 | { 47 | char *a[] = { 48 | "and", "dan" 49 | }; 50 | 51 | vector v; 52 | 53 | for(int i = 0; i < sizeof(a) / sizeof(a[0]); i ++){ 54 | v.push_back(a[i]); 55 | } 56 | 57 | print_vector(solution.anagrams(v)); 58 | } 59 | 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /balanced_binary_tree/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /balanced_binary_tree/balanced_binary_tree.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | /** 15 | * Definition for binary tree 16 | * struct TreeNode { 17 | * int val; 18 | * TreeNode *left; 19 | * TreeNode *right; 20 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 21 | * }; 22 | */ 23 | class Solution { 24 | // If root is balanced tree, return its height, otherwise return -1. 25 | int get_height(TreeNode *root){ 26 | if(root == NULL) 27 | return 0; 28 | 29 | int left_height = get_height(root->left); 30 | if(left_height == -1) 31 | return -1; 32 | 33 | int right_height = get_height(root->right); 34 | if(right_height == -1) 35 | return -1; 36 | 37 | int root_height; 38 | if(right_height >= left_height && right_height - left_height <= 1) 39 | root_height = right_height + 1; 40 | else if(left_height > right_height && left_height - right_height <= 1) 41 | root_height = left_height + 1; 42 | else 43 | root_height = -1; 44 | 45 | return root_height; 46 | } 47 | public: 48 | bool isBalanced(TreeNode *root) { 49 | int height = get_height(root); 50 | 51 | return height != -1; 52 | } 53 | }; 54 | -------------------------------------------------------------------------------- /balanced_binary_tree/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "balanced_binary_tree.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | // true 19 | TreeNode n1(1), n2(2), n3(3), n4(4), n5(5); 20 | n1.left = &n2; 21 | n1.right = &n3; 22 | n2.left = &n4; 23 | n2.right = &n5; 24 | 25 | cout << solution.isBalanced(&n1) << endl; 26 | } 27 | 28 | { 29 | // false 30 | TreeNode n1(1), n2(2), n3(3), n4(4), n5(5); 31 | n1.left = &n2; 32 | n1.right = &n3; 33 | n2.left = &n4; 34 | n4.right = &n5; 35 | 36 | cout << solution.isBalanced(&n1) << endl; 37 | } 38 | 39 | //Error test cases from leetcode.com 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /best_time_to_buy_and_sell_stock/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /best_time_to_buy_and_sell_stock/best_time_to_buy_and_sell_stock.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "test.h" 16 | using namespace std; 17 | 18 | class Solution { 19 | public: 20 | int maxProfit(vector &prices) { 21 | int len = prices.size(); 22 | 23 | int index_min, index_max, max_diff; 24 | 25 | index_min = index_max = 0; 26 | max_diff = 0; 27 | 28 | for(int i = 1; i <= len - 1; i ++){ 29 | if(prices[i] >= prices[index_max]){ 30 | max_diff = prices[i] - prices[index_min]; 31 | index_max = i; 32 | }else if(prices[i] < prices[index_min]){ 33 | index_min = i; 34 | }else if(index_min >= index_max){ 35 | int diff = prices[i] - prices[index_min]; 36 | if(diff > max_diff){ 37 | index_max = i; 38 | max_diff = diff; 39 | } 40 | } 41 | } 42 | 43 | return max_diff; 44 | } 45 | }; 46 | -------------------------------------------------------------------------------- /best_time_to_buy_and_sell_stock/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "best_time_to_buy_and_sell_stock.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | 18 | //Error test cases from leetcode.com 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /best_time_to_buy_and_sell_stock_2/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /best_time_to_buy_and_sell_stock_2/best_time_to_buy_and_sell_stock_2.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "test.h" 16 | using namespace std; 17 | 18 | class Solution { 19 | public: 20 | int maxProfit(vector &prices) { 21 | if(prices.size() < 2) 22 | return 0; 23 | 24 | int profit = 0; 25 | for(int buy = 0; buy < prices.size() - 1;){ 26 | int sell = buy + 1; 27 | while(sell < prices.size() && prices[sell] >= prices[sell - 1]) 28 | sell ++; 29 | 30 | if(sell > buy + 1) 31 | profit += prices[sell - 1] - prices[buy]; 32 | 33 | buy = sell; 34 | } 35 | 36 | return profit; 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /best_time_to_buy_and_sell_stock_2/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "best_time_to_buy_and_sell_stock_2.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | 18 | //Error test cases from leetcode.com 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /best_time_to_buy_and_sell_stock_3/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /best_time_to_buy_and_sell_stock_3/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "best_time_to_buy_and_sell_stock_3.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | 18 | //Error test cases from leetcode.com 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /binary_tree_inorder_traversal/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /binary_tree_inorder_traversal/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "binary_tree_inorder_traversal.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | TreeNode n1(1), n2(2), n3(3); 19 | n1.right = &n2; 20 | n2.left = &n3; 21 | 22 | print_vector(solution.inorderTraversal(&n1)); 23 | } 24 | 25 | //Error test cases from leetcode.com 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /binary_tree_level_order_traversal/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /binary_tree_level_order_traversal/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "binary_tree_level_order_traversal.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | TreeNode n1(3), n2(9), n3(20), n4(15), n5(7); 19 | n1.left = &n2; 20 | n1.right = &n3; 21 | n3.left = &n4; 22 | n3.right = &n5; 23 | 24 | print_vector_vector(solution.levelOrder(&n1)); 25 | } 26 | 27 | //Error test cases from leetcode.com 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /binary_tree_level_order_traversal_2/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /binary_tree_level_order_traversal_2/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "binary_tree_level_order_traversal_2.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | TreeNode n1(3), n2(9), n3(20), n4(15), n5(7); 19 | n1.left = &n2; 20 | n1.right = &n3; 21 | n3.left = &n4; 22 | n3.right = &n5; 23 | 24 | print_vector_vector(solution.levelOrderBottom(&n1)); 25 | } 26 | 27 | //Error test cases from leetcode.com 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /binary_tree_maximum_path_sum/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /binary_tree_maximum_path_sum/binary_tree_maximum_path_sum.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "test.h" 16 | using namespace std; 17 | 18 | /** 19 | * Definition for binary tree 20 | * struct TreeNode { 21 | * int val; 22 | * TreeNode *left; 23 | * TreeNode *right; 24 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 25 | * }; 26 | */ 27 | class Solution { 28 | int max; 29 | 30 | int maxSum(TreeNode *node){ 31 | if(node == NULL) 32 | return 0; 33 | 34 | int left = maxSum(node->left); 35 | int right = maxSum(node->right); 36 | int val = node->val; 37 | 38 | int sum = left + right + val; 39 | if(sum > max) 40 | max = sum; 41 | 42 | sum = val; 43 | int big = left > right? left : right; 44 | if(big > 0){ 45 | sum += big; 46 | } 47 | if(sum > max){ 48 | max = sum; 49 | } 50 | 51 | return sum; 52 | } 53 | public: 54 | int maxPathSum(TreeNode *root) { 55 | if(root == NULL) 56 | return 0; 57 | 58 | max = root->val; 59 | maxSum(root); 60 | 61 | return max; 62 | } 63 | }; 64 | -------------------------------------------------------------------------------- /binary_tree_maximum_path_sum/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "binary_tree_maximum_path_sum.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | 18 | //Error test cases from leetcode.com 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /binary_tree_zigzag_level_order_traversal/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /binary_tree_zigzag_level_order_traversal/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "binary_tree_zigzag_level_order_traversal.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | TreeNode n1(3), n2(9), n3(20), n4(15), n5(7); 19 | n1.left = &n2; 20 | n1.right = &n3; 21 | n3.left = &n4; 22 | n3.right = &n5; 23 | 24 | print_vector_vector(solution.zigzagLevelOrder(&n1)); 25 | } 26 | 27 | //Error test cases from leetcode.com 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /climbing_stairs/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /climbing_stairs/climbing_stairs.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | class Solution { 15 | public: 16 | int climbStairs(int n) { 17 | if(n <= 1) 18 | return n; 19 | 20 | //f(n) = f(n - 1) + f(n - 2); 21 | int f0 = 1, f1 = 1; 22 | int count = 2; 23 | int f2; 24 | 25 | while(count <= n){ 26 | f2 = f0 + f1; 27 | f0 = f1; 28 | f1 = f2; 29 | count ++; 30 | } 31 | 32 | return f2; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /climbing_stairs/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "climbing_stairs.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | cout << solution.climbStairs(0) << endl; 18 | 19 | cout << solution.climbStairs(1) << endl; 20 | 21 | cout << solution.climbStairs(5) << endl; 22 | 23 | //Error test cases from leetcode.com 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /combination_sum/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /combination_sum/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "combination_sum.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {2, 3, 6, 7}; 19 | vector v; 20 | INT_ARR_TO_VEC(v, a); 21 | 22 | int target = 7; 23 | 24 | print_vector_vector(solution.combinationSum(v, target)); 25 | } 26 | 27 | { 28 | int a[] = {2, 3, 6, 7}; 29 | vector v; 30 | INT_ARR_TO_VEC(v, a); 31 | 32 | int target = 0; 33 | 34 | print_vector_vector(solution.combinationSum(v, target)); 35 | } 36 | 37 | { 38 | int a[] = {2, 3, 6, 7}; 39 | vector v; 40 | INT_ARR_TO_VEC(v, a); 41 | 42 | int target = 20; 43 | 44 | print_vector_vector(solution.combinationSum(v, target)); 45 | } 46 | 47 | //Error test cases from leetcode.com 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /combination_sum_2/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /combination_sum_2/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "combination_sum_2.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {2, 3, 6, 7}; 19 | vector v; 20 | INT_ARR_TO_VEC(v, a); 21 | 22 | int target = 7; 23 | 24 | print_vector_vector(solution.combinationSum2(v, target)); 25 | } 26 | 27 | { 28 | int a[] = {2, 3, 6, 7}; 29 | vector v; 30 | INT_ARR_TO_VEC(v, a); 31 | 32 | int target = 0; 33 | 34 | print_vector_vector(solution.combinationSum2(v, target)); 35 | } 36 | 37 | { 38 | int a[] = {10, 1, 2, 7, 6, 1, 5}; 39 | vector v; 40 | INT_ARR_TO_VEC(v, a); 41 | 42 | int target = 8; 43 | 44 | print_vector_vector(solution.combinationSum2(v, target)); 45 | } 46 | 47 | //Error test cases from leetcode.com 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /combinations/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /combinations/combinations.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "test.h" 13 | using namespace std; 14 | 15 | class Solution { 16 | vector > get_combinations(int start, int end, int k, vector pre_combination){ 17 | vector > combinations; 18 | 19 | if(k == 0){ 20 | if(pre_combination.size() != 0) 21 | combinations.push_back(pre_combination); 22 | 23 | return combinations; 24 | } 25 | 26 | if(end - start + 1 < k) 27 | return combinations; 28 | 29 | // case 1: not include start 30 | combinations = get_combinations(start + 1, end, k, pre_combination); 31 | 32 | // case 2: include start 33 | pre_combination.push_back(start); 34 | vector > tmp = get_combinations(start + 1, end, k - 1, pre_combination); 35 | combinations.insert(combinations.end(), tmp.begin(), tmp.end()); 36 | 37 | return combinations; 38 | } 39 | 40 | public: 41 | vector > combine(int n, int k) { 42 | vector tmp; 43 | 44 | return get_combinations(1, n, k, tmp); 45 | } 46 | }; 47 | -------------------------------------------------------------------------------- /combinations/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "combinations.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int n = 4; 19 | int k = 2; 20 | print_vector_vector(solution.combine(n, k)); 21 | } 22 | 23 | { 24 | int n = 4; 25 | int k = 0; 26 | print_vector_vector(solution.combine(n, k)); 27 | } 28 | 29 | { 30 | int n = 0; 31 | int k = 2; 32 | print_vector_vector(solution.combine(n, k)); 33 | } 34 | 35 | //Error test cases from leetcode.com 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /construct_binary_tree_from_inorder_and_postorder_traversal/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /construct_binary_tree_from_inorder_and_postorder_traversal/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "construct_binary_tree_from_inorder_and_postorder_traversal.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {2, 4, 1, 5, 3, 6}; 19 | vector in_oder; 20 | INT_ARR_TO_VEC(in_oder, a); 21 | 22 | int b[] = {4, 2, 5, 6, 3, 1}; 23 | vector post_oder; 24 | INT_ARR_TO_VEC(post_oder, b); 25 | 26 | draw_tree(solution.buildTree(in_oder, post_oder), "1.dot"); 27 | } 28 | 29 | //Error test cases from leetcode.com 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /construct_binary_tree_from_preorder_and_inorder_traversal/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /construct_binary_tree_from_preorder_and_inorder_traversal/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "construct_binary_tree_from_preorder_and_inorder_traversal.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {1, 2, 4, 3, 5, 6}; 19 | vector pre_oder; 20 | INT_ARR_TO_VEC(pre_oder, a); 21 | 22 | int b[] = {2, 4, 1, 5, 3, 6}; 23 | vector in_oder; 24 | INT_ARR_TO_VEC(in_oder, b); 25 | 26 | draw_tree(solution.buildTree(pre_oder, in_oder), "1.dot"); 27 | } 28 | 29 | //Error test cases from leetcode.com 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /container_with_most_water/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /container_with_most_water/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "container_with_most_water.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {3, 8, 3, 4, 6}; 19 | vector v; 20 | INT_ARR_TO_VEC(v, a); 21 | 22 | cout << solution.maxArea(v) << endl; 23 | } 24 | 25 | //Error test cases from leetcode.com 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /convert_sorted_array_to_binary_search_tree/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /convert_sorted_array_to_binary_search_tree/convert_sorted_array_to_binary_search_tree.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | /** 15 | * Definition for binary tree 16 | * struct TreeNode { 17 | * int val; 18 | * TreeNode *left; 19 | * TreeNode *right; 20 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 21 | * }; 22 | */ 23 | class Solution { 24 | TreeNode *convert_to_bst(vector &num, int start, int end){ 25 | if(start > end) 26 | return NULL; 27 | 28 | if(start == end){ 29 | TreeNode *node = new TreeNode(num[start]); 30 | return node; 31 | } 32 | 33 | int mid = start + (end - start) / 2; 34 | TreeNode *root = new TreeNode(num[mid]); 35 | TreeNode *left = convert_to_bst(num, start, mid - 1); 36 | TreeNode *right = convert_to_bst(num, mid + 1, end); 37 | 38 | root->left = left; 39 | root->right = right; 40 | 41 | return root; 42 | } 43 | 44 | public: 45 | TreeNode *sortedArrayToBST(vector &num) { 46 | return convert_to_bst(num, 0, num.size() - 1); 47 | } 48 | }; 49 | -------------------------------------------------------------------------------- /convert_sorted_array_to_binary_search_tree/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "convert_sorted_array_to_binary_search_tree.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {1, 2, 3, 4, 5, 6}; 19 | vector v; 20 | INT_ARR_TO_VEC(v, a); 21 | 22 | draw_tree(solution.sortedArrayToBST(v), "1.dot"); 23 | } 24 | 25 | //Error test cases from leetcode.com 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /convert_sorted_list_to_binary_search_tree/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /convert_sorted_list_to_binary_search_tree/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "convert_sorted_list_to_binary_search_tree.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | ListNode n1(1), n2(2), n3(3), n4(4), n5(5), n6(6); 19 | n1.next = &n2; 20 | n2.next = &n3; 21 | n3.next = &n4; 22 | n4.next = &n5; 23 | n5.next = &n6; 24 | 25 | draw_tree(solution.sortedListToBST(&n1), "1.dot"); 26 | } 27 | 28 | //Error test cases from leetcode.com 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /count_and_say/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /count_and_say/count_and_say.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "test.h" 13 | 14 | using namespace std; 15 | 16 | class Solution { 17 | public: 18 | string countAndSay(int n) { 19 | string s = "1"; 20 | 21 | for(int i = 1; i < n; i ++){ 22 | string tmp; 23 | 24 | for(size_t j = 0; j < s.size(); ){ 25 | char c = s[j]; 26 | int count = 1; 27 | j ++; 28 | while(j < s.size() && s[j] == c){ 29 | count ++; 30 | j ++; 31 | } 32 | tmp.push_back('0' + count); 33 | tmp.push_back(c); 34 | } 35 | 36 | s = tmp; 37 | } 38 | 39 | return s; 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /count_and_say/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "count_and_say.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int n = 3; 19 | cout << solution.countAndSay(n) << endl; 20 | } 21 | 22 | { 23 | int n = 5; 24 | cout << solution.countAndSay(n) << endl; 25 | } 26 | 27 | //Error test cases from leetcode.com 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /decode_ways/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /decode_ways/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "decode_ways.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | string s("12"); 19 | 20 | cout << solution.numDecodings(s) << endl; 21 | } 22 | 23 | { 24 | string s("123"); 25 | 26 | cout << solution.numDecodings(s) << endl; 27 | } 28 | 29 | //Error test cases from leetcode.com 30 | { 31 | string s("0"); 32 | 33 | cout << solution.numDecodings(s) << endl; 34 | } 35 | 36 | { 37 | string s("10"); 38 | 39 | cout << solution.numDecodings(s) << endl; 40 | } 41 | 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /distinct_subsequences/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /distinct_subsequences/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "distinct_subsequences.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | string S = "rabbbit"; 18 | string T = "rabbit"; 19 | cout << solution.numDistinct(S, T) << endl; 20 | 21 | S = "rabbbit"; 22 | T = "rabiit"; 23 | cout << solution.numDistinct(S, T) << endl; 24 | 25 | S = "rabbbit"; 26 | T = ""; 27 | cout << solution.numDistinct(S, T) << endl; 28 | 29 | S = "rabrab"; 30 | T = "rab"; 31 | cout << solution.numDistinct(S, T) << endl; 32 | 33 | //Error test cases from leetcode.com 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /divide_two_integers/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /divide_two_integers/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "divide_two_integers.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a = 12; 19 | int b = 3; 20 | cout << solution.divide(a, b) << endl; 21 | } 22 | 23 | { 24 | int a = 12; 25 | int b = 5; 26 | cout << solution.divide(a, b) << endl; 27 | } 28 | 29 | //Error test cases from leetcode.com 30 | { 31 | // time out 32 | int a = 2147483647; 33 | int b = 1; 34 | cout << solution.divide(a, b) << endl; 35 | } 36 | 37 | { 38 | // time out 39 | int a = -2147483648; 40 | int b = -2147483648; 41 | cout << solution.divide(a, b) << endl; 42 | } 43 | 44 | { 45 | // time out 46 | int a = -2147483648; 47 | int b = 1; 48 | cout << solution.divide(a, b) << endl; 49 | } 50 | 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /edit_distance/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /edit_distance/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "edit_distance.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | string w1 = "abc"; 18 | string w2 = "abc"; 19 | cout << solution.minDistance(w1, w2) << endl; 20 | 21 | w1 = ""; 22 | w2 = "abc"; 23 | cout << solution.minDistance(w1, w2) << endl; 24 | 25 | w1 = ""; 26 | w2 = ""; 27 | cout << solution.minDistance(w1, w2) << endl; 28 | 29 | w1 = "abe"; 30 | w2 = "cde"; 31 | cout << solution.minDistance(w1, w2) << endl; 32 | 33 | w1 = "abc"; 34 | w2 = "ac"; 35 | cout << solution.minDistance(w1, w2) << endl; 36 | 37 | w1 = "ac"; 38 | w2 = "adc"; 39 | cout << solution.minDistance(w1, w2) << endl; 40 | 41 | //Error test cases from leetcode.com 42 | w1 = "a"; 43 | w2 = "b"; 44 | cout << solution.minDistance(w1, w2) << endl; 45 | 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /first_missing_positive/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /first_missing_positive/first_missing_positive.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "test.h" 13 | using namespace std; 14 | 15 | class Solution { 16 | public: 17 | int firstMissingPositive(int A[], int n) { 18 | if(n <= 0) 19 | return 1; 20 | 21 | // Swap all legal numbers to right places where A[i] = i + 1. 22 | for(int i = 0; i < n; i ++){ 23 | int tmp = A[i]; 24 | if(tmp <= 0 || tmp >= n) 25 | continue; 26 | 27 | /* 1. some numbers are just in the right place 28 | * 2. some numbers might be dumplicated. 29 | * All the above situations need not be handled. 30 | */ 31 | if(tmp != i + 1 && A[tmp - 1] != tmp){ 32 | swap(A[i], A[tmp - 1]); 33 | 34 | if(tmp > i) // The swapped number need be handled. 35 | i --; 36 | } 37 | } 38 | 39 | for(int i = 0; i < n; i ++){ 40 | if(A[i] != i + 1) 41 | return i + 1; 42 | } 43 | 44 | return n + 1; 45 | } 46 | }; 47 | -------------------------------------------------------------------------------- /first_missing_positive/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "first_missing_positive.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {1, 2, 0}; 19 | cout << solution.firstMissingPositive(a, sizeof(a)/sizeof(a[0])) << endl; 20 | } 21 | 22 | { 23 | int a[] = {3, 4, -1, 1}; 24 | cout << solution.firstMissingPositive(a, sizeof(a)/sizeof(a[0])) << endl; 25 | } 26 | 27 | //Error test cases from leetcode.com 28 | { 29 | int a[] = {}; 30 | cout << solution.firstMissingPositive(a, sizeof(a)/sizeof(a[0])) << endl; 31 | } 32 | 33 | { 34 | int a[] = {1, 1}; 35 | cout << solution.firstMissingPositive(a, sizeof(a)/sizeof(a[0])) << endl; 36 | } 37 | 38 | { 39 | int a[] = {2, 2, 2, 2, 2}; 40 | cout << solution.firstMissingPositive(a, sizeof(a)/sizeof(a[0])) << endl; 41 | } 42 | 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /flatten_binary_tree_to_linked_list/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /flatten_binary_tree_to_linked_list/flatten_binary_tree_to_linked_list.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | /** 15 | * Definition for binary tree 16 | * struct TreeNode { 17 | * int val; 18 | * TreeNode *left; 19 | * TreeNode *right; 20 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 21 | * }; 22 | */ 23 | class Solution { 24 | // preorder visit 25 | void do_flatten(TreeNode *root, TreeNode *&tail){ 26 | if(root == NULL) 27 | return; 28 | 29 | TreeNode *left = root->left; 30 | TreeNode *right = root->right; 31 | 32 | tail->right = root; 33 | tail->left = NULL; 34 | tail = root; 35 | 36 | do_flatten(left, tail); 37 | do_flatten(right, tail); 38 | } 39 | 40 | public: 41 | void flatten(TreeNode *root) { 42 | TreeNode dummy_node(0); 43 | TreeNode *tail = &dummy_node; 44 | 45 | do_flatten(root, tail); 46 | } 47 | }; 48 | -------------------------------------------------------------------------------- /flatten_binary_tree_to_linked_list/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "flatten_binary_tree_to_linked_list.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | TreeNode n1(1), n2(2), n3(3), n4(4), n5(5), n6(6), n7(7); 19 | n1.left = &n2; 20 | n1.right = &n3; 21 | n2.left = &n4; 22 | n2.right = &n5; 23 | n3.left = &n6; 24 | n3.right = &n7; 25 | 26 | solution.flatten(&n1); 27 | draw_tree(&n1, "1.dot"); 28 | } 29 | 30 | //Error test cases from leetcode.com 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /generate.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ $# -ne 1 ]; then 4 | echo "usage: ./$0 code_name" 5 | exit 0 6 | fi 7 | 8 | CODE_NAME=$1 9 | 10 | mkdir $CODE_NAME 11 | 12 | cp template/test_file.h $CODE_NAME/$CODE_NAME.h 13 | 14 | cp template/test.cpp $CODE_NAME/test.cpp 15 | sed -i "s/TEST_FILE_H/$CODE_NAME\.h/" $CODE_NAME/test.cpp 16 | 17 | cp template/Makefile $CODE_NAME/Makefile 18 | 19 | REAL_NAME=$(echo $CODE_NAME | sed -e 's/_/ /g' -e 's/\b[a-z]/\U&/g' -e 's/\b2\b/II/g' -e 's/\b3\b/III/g') 20 | 21 | echo $(date +"%D ") $REAL_NAME >> PROGRESS.md 22 | echo "" >> PROGRESS.md 23 | -------------------------------------------------------------------------------- /generate_parentheses/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /generate_parentheses/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "generate_parentheses.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int n = 3; 19 | print_vector(solution.generateParenthesis(n)); 20 | } 21 | 22 | //Error test cases from leetcode.com 23 | { 24 | int n = 4; 25 | print_vector(solution.generateParenthesis(n)); 26 | } 27 | 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /gray_code/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /gray_code/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "gray_code.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int n = 2; 19 | 20 | print_vector(solution.grayCode(n)); 21 | } 22 | 23 | { 24 | int n = 3; 25 | 26 | print_vector(solution.grayCode(n)); 27 | } 28 | 29 | //Error test cases from leetcode.com 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /implement_strStr/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /insert_interval/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /integer_to_roman/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /integer_to_roman/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "integer_to_roman.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int num = 1994; 19 | 20 | cout << solution.intToRoman(num) << endl; 21 | } 22 | 23 | //Error test cases from leetcode.com 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /interleaving_string/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /interleaving_string/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "interleaving_string.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | string s1 = "aabcc"; 18 | string s2 = "dbbca"; 19 | string s3 = "aadbbcbcac"; 20 | cout << solution.isInterleave(s1, s2, s3) << endl; 21 | 22 | s3 = "aadbbbaccc"; 23 | cout << solution.isInterleave(s1, s2, s3) << endl; 24 | 25 | s1 = "abad"; 26 | s2 = "acae"; 27 | s3 = "acabaade"; 28 | cout << solution.isInterleave(s1, s2, s3) << endl; 29 | 30 | //Error test cases from leetcode.com 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /jump_game/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /jump_game/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "jump_game.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | // true 19 | int A[] = {2, 3, 1, 1, 4}; 20 | int n = sizeof(A) / sizeof(A[0]); 21 | 22 | cout << solution.canJump(A, n) << endl; 23 | } 24 | 25 | { 26 | // false 27 | int A[] = {3, 2, 1, 0, 4}; 28 | int n = sizeof(A) / sizeof(A[0]); 29 | 30 | cout << solution.canJump(A, n) << endl; 31 | } 32 | 33 | //Error test cases from leetcode.com 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /jump_game_2/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /jump_game_2/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "jump_game_2.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {2, 3, 1, 1, 4}; 19 | int n = sizeof(a) / sizeof(a[0]); 20 | 21 | cout << solution.jump(a, n) << endl;; 22 | } 23 | 24 | //Error test cases from leetcode.com 25 | { 26 | int a[] = {9,4,5,4,1,8,1,2,0,7,8,7,0,6,6,1,1,2,5,0,9,8,4,7,9,6,8,1,4,0,8,5,5,3,9,8,1,2,2,3,0,1,3,2,7,9,3,0,1}; 27 | int n = sizeof(a) / sizeof(a[0]); 28 | 29 | cout << solution.jump(a, n) << endl;; 30 | } 31 | 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /largest_rectangle_in_histogram/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /largest_rectangle_in_histogram/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "largest_rectangle_in_histogram.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {2, 1, 5, 6, 2, 3}; 19 | vector v; 20 | INT_ARR_TO_VEC(v, a); 21 | 22 | cout << solution.largestRectangleArea(v) << endl; 23 | } 24 | 25 | //Error test cases from leetcode.com 26 | { 27 | int a[] = {4, 2, 0, 3, 2, 5}; 28 | vector v; 29 | INT_ARR_TO_VEC(v, a); 30 | 31 | cout << solution.largestRectangleArea(v) << endl; 32 | } 33 | 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /length_of_last_word/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /length_of_last_word/length_of_last_word.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "test.h" 16 | using namespace std; 17 | 18 | class Solution { 19 | void skip_spaces(const char *&s){ 20 | while(*s == ' ') 21 | s ++; 22 | } 23 | 24 | int skip_words(const char *&s){ 25 | int len = 0; 26 | 27 | while(*s != 0 && *s != ' '){ 28 | s ++; 29 | len ++; 30 | } 31 | 32 | return len; 33 | } 34 | 35 | public: 36 | int lengthOfLastWord(const char *s) { 37 | if(s == NULL) 38 | return 0; 39 | 40 | int pre_length = 0; 41 | while(*s){ 42 | skip_spaces(s); 43 | 44 | int len = skip_words(s); 45 | if(len != 0) 46 | pre_length = len; 47 | } 48 | 49 | return pre_length; 50 | } 51 | }; 52 | -------------------------------------------------------------------------------- /length_of_last_word/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "length_of_last_word.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | char *s = "Hello world"; 19 | 20 | cout << solution.lengthOfLastWord(s) << endl; 21 | } 22 | 23 | { 24 | char *s = " "; 25 | 26 | cout << solution.lengthOfLastWord(s) << endl; 27 | } 28 | 29 | //Error test cases from leetcode.com 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /letter_combinations_of_a_phone_number/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /letter_combinations_of_a_phone_number/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "letter_combinations_of_a_phone_number.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | string digits = "23"; 19 | print_vector(solution.letterCombinations(digits)); 20 | } 21 | 22 | { 23 | string digits = "12340"; 24 | print_vector(solution.letterCombinations(digits)); 25 | } 26 | 27 | //Error test cases from leetcode.com 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /longest_common_prefix/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /longest_common_prefix/longest_common_prefix.h: -------------------------------------------------------------------------------- 1 | /* Notes: Compare less times. 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | class Solution { 15 | int get_minimum_len(vector &strs){ 16 | int min = strs[0].size(); 17 | 18 | for(size_t i = 1; i < strs.size(); i ++){ 19 | int size = strs[i].size(); 20 | if(size < min) 21 | min = size; 22 | } 23 | 24 | return min; 25 | } 26 | public: 27 | string longestCommonPrefix(vector &strs) { 28 | if(strs.size() == 0) 29 | return ""; 30 | 31 | int longest = 0; 32 | int min_len = get_minimum_len(strs); 33 | size_t longest_step = (strs.size() + 1) / 2; 34 | 35 | while(1){ 36 | if(longest >= min_len) 37 | break; 38 | 39 | for(size_t step = 1; step <= longest_step; step = step * 2){ 40 | //cout << "step : " << step << endl; 41 | for(size_t i = 0; i + step < strs.size(); i += 2 * step){ 42 | //cout << "i : " << i << endl; 43 | if(strs[i][longest] != strs[i + step][longest]) 44 | return strs[0].substr(0, longest); 45 | } 46 | } 47 | longest ++; 48 | } 49 | 50 | return strs[0].substr(0, longest); 51 | } 52 | }; 53 | -------------------------------------------------------------------------------- /longest_consecutive_sequence/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /longest_consecutive_sequence/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "longest_consecutive_sequence.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | vector v; 18 | 19 | int arr1[] = {-1, 0, 1, 5, 9, 6}; 20 | INT_ARR_TO_VEC(v, arr1); 21 | cout << solution.longestConsecutive(v) << endl; 22 | 23 | int arr2[] = {2147483646, -2147483647, 200, 0, 2, 2147483644, -2147483645, 2147483645}; 24 | INT_ARR_TO_VEC(v, arr2); 25 | cout << solution.longestConsecutive(v) << endl; 26 | 27 | //Error test cases from leetcode.com 28 | int arr3[] = {2147483646, -2147483647, 200, 0, 2, 2147483644, -2147483645, 2147483645}; 29 | INT_ARR_TO_VEC(v, arr3); 30 | cout << solution.longestConsecutive(v) << endl; 31 | 32 | int arr4[] = {1, 3, 5, 2, 4}; 33 | INT_ARR_TO_VEC(v, arr4); 34 | cout << solution.longestConsecutive(v) << endl; 35 | 36 | int arr5[] = {9, 1, 4, 7, 3, -1, 0, 5, 8, -1, 6}; 37 | INT_ARR_TO_VEC(v, arr5); 38 | cout << solution.longestConsecutive(v) << endl; 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /longest_palindromic_substring/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /longest_palindromic_substring/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "longest_palindromic_substring.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | string s = "abcddcefgh"; 19 | cout << solution.longestPalindrome(s) << endl; 20 | } 21 | 22 | { 23 | string s = "abcdefg"; 24 | cout << solution.longestPalindrome(s) << endl; 25 | } 26 | 27 | { 28 | string s = "abcddcefabccbade"; 29 | cout << solution.longestPalindrome(s) << endl; 30 | } 31 | 32 | //Error test cases from leetcode.com 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /longest_substring_without_repeating_characters/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /longest_substring_without_repeating_characters/longest_substring_without_repeating_characters.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | class Solution { 15 | void init_record(int record[], int n){ 16 | for(int i = 0; i < n; i ++){ 17 | record[i] = -1; 18 | } 19 | } 20 | 21 | int get_max(int i, int j){ 22 | if(i >= j) 23 | return i; 24 | else 25 | return j; 26 | } 27 | 28 | public: 29 | int lengthOfLongestSubstring(string s) { 30 | const int n_charactres = 256; 31 | int record[n_charactres] = {0}; 32 | 33 | init_record(record, n_charactres); 34 | 35 | int start = 0, max_len = 0; 36 | size_t i; 37 | for(i = 0; i < s.size(); i ++){ 38 | int c = (int)s[i]; 39 | if(record[c] >= start){ // A repeat in [start, i] 40 | max_len = get_max(max_len, i - start); // [start, i-1]. 41 | start = record[c] + 1; 42 | } 43 | record[c] = i; 44 | } 45 | 46 | max_len = get_max(max_len, i - start); // [start, s.size()) 47 | 48 | return max_len; 49 | } 50 | }; 51 | -------------------------------------------------------------------------------- /longest_substring_without_repeating_characters/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "longest_substring_without_repeating_characters.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | string s = "abcabcbb"; 19 | cout << solution.lengthOfLongestSubstring(s) << endl; 20 | } 21 | 22 | { 23 | string s = "bbbbb"; 24 | cout << solution.lengthOfLongestSubstring(s) << endl; 25 | } 26 | 27 | //Error test cases from leetcode.com 28 | { 29 | string s = "abcd"; 30 | cout << solution.lengthOfLongestSubstring(s) << endl; 31 | } 32 | 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /longest_valid_parentheses/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /longest_valid_parentheses/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "longest_valid_parentheses.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { // 4 18 | string s = ")()())"; 19 | cout << solution.longestValidParentheses(s) << endl; 20 | } 21 | 22 | { // 6 23 | string s = ")(())())"; 24 | cout << solution.longestValidParentheses(s) << endl; 25 | } 26 | 27 | { // 4 28 | string s = "))(()()"; 29 | cout << solution.longestValidParentheses(s) << endl; 30 | } 31 | 32 | { // 0 33 | string s = ""; 34 | cout << solution.longestValidParentheses(s) << endl; 35 | } 36 | 37 | //Error test cases from leetcode.com 38 | { // 2 39 | string s = "()(()"; 40 | cout << solution.longestValidParentheses(s) << endl; 41 | } 42 | 43 | { // 6 44 | string s = "()(())"; 45 | cout << solution.longestValidParentheses(s) << endl; 46 | } 47 | 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /maximal_rectangle/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /maximal_rectangle/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "maximal_rectangle.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | char a[][5] = { 19 | {'1', '0', '1', '1', '1'}, 20 | {'1', '0', '1', '1', '0'}, 21 | {'0', '0', '1', '1', '1'}, 22 | {'1', '0', '1', '0', '1'}, 23 | }; 24 | 25 | vector > v; 26 | arr_of_arr_to_vec(v, a[0], 4, 5); 27 | 28 | cout << solution.maximalRectangle(v) << endl; 29 | } 30 | 31 | //Error test cases from leetcode.com 32 | 33 | return '0'; 34 | } 35 | -------------------------------------------------------------------------------- /maximum_depth_of_binary_tree/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /maximum_depth_of_binary_tree/maximum_depth_of_binary_tree.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | /** 15 | * Definition for binary tree 16 | * struct TreeNode { 17 | * int val; 18 | * TreeNode *left; 19 | * TreeNode *right; 20 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 21 | * }; 22 | */ 23 | class Solution { 24 | public: 25 | int maxDepth(TreeNode *root) { 26 | if(root == NULL) 27 | return 0; 28 | 29 | int left_depth = maxDepth(root->left); 30 | int right_depth = maxDepth(root->right); 31 | 32 | int max = left_depth >= right_depth? left_depth : right_depth; 33 | 34 | return max + 1; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /maximum_depth_of_binary_tree/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "maximum_depth_of_binary_tree.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | TreeNode n1(1), n2(2), n3(3), n4(4); 19 | n1.left = &n2; 20 | n1.right = &n3; 21 | n2.left = &n4; 22 | 23 | cout << solution.maxDepth(&n1) << endl; 24 | } 25 | 26 | //Error test cases from leetcode.com 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /maximum_subarray/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /maximum_subarray/maximum_subarray.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "test.h" 16 | using namespace std; 17 | 18 | class Solution { 19 | public: 20 | int maxSubArray(int A[], int n) { 21 | int max_sum = INT_MIN; 22 | 23 | int cur_sum = 0; 24 | bool first_num = true; 25 | for(int i = 0; i < n; i ++){ 26 | int num = A[i]; 27 | if(num < 0){ 28 | if(first_num){ 29 | max_sum = max(num, max_sum); 30 | }else{ 31 | max_sum = max(max_sum, cur_sum); 32 | 33 | cur_sum += num; 34 | if(cur_sum <= 0){ 35 | cur_sum = 0; 36 | first_num = true; 37 | } 38 | } 39 | }else{ 40 | cur_sum += num; 41 | first_num = false; 42 | } 43 | } 44 | 45 | if(first_num == false) 46 | max_sum = max(cur_sum, max_sum); 47 | 48 | return max_sum; 49 | } 50 | }; 51 | -------------------------------------------------------------------------------- /maximum_subarray/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "maximum_subarray.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4}; 19 | int n = sizeof(a) / sizeof(a[0]); 20 | 21 | cout << solution.maxSubArray(a, n) << endl; 22 | } 23 | 24 | { 25 | int a[] = {-2, -11, -3, -1}; 26 | int n = sizeof(a) / sizeof(a[0]); 27 | 28 | cout << solution.maxSubArray(a, n) << endl; 29 | } 30 | 31 | { 32 | int a[] = {3, 2, 4, 1}; 33 | int n = sizeof(a) / sizeof(a[0]); 34 | 35 | cout << solution.maxSubArray(a, n) << endl; 36 | } 37 | 38 | //Error test cases from leetcode.com 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /median_of_two_sorted_arrays/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /merge_intervals/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /merge_intervals/merge_intervals.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | /** 15 | * Definition for an interval. 16 | * struct Interval { 17 | * int start; 18 | * int end; 19 | * Interval() : start(0), end(0) {} 20 | * Interval(int s, int e) : start(s), end(e) {} 21 | * }; 22 | */ 23 | class Solution { 24 | static int cmp(const Interval &a, const Interval &b){ 25 | if(a.start < b.start) 26 | return 1; 27 | else if(a.start == b.start) 28 | return a.end < b.end; 29 | else 30 | return 0; 31 | } 32 | public: 33 | vector merge(vector &intervals) { 34 | sort(intervals.begin(), intervals.end(), cmp); 35 | 36 | vector merged_intervals; 37 | vector::iterator iter; 38 | for(iter = intervals.begin(); iter != intervals.end();){ 39 | Interval cur = *iter; 40 | iter ++; 41 | Interval next = *iter; 42 | while(iter != intervals.end()){ 43 | if(cur.end < iter->start) 44 | break; 45 | 46 | if(cur.end >= iter->start && cur.end <= iter->end) 47 | cur.end = iter->end; 48 | 49 | iter ++; 50 | } 51 | 52 | merged_intervals.push_back(cur); 53 | } 54 | 55 | return merged_intervals; 56 | } 57 | }; 58 | -------------------------------------------------------------------------------- /merge_intervals/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "merge_intervals.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | Interval a(1, 3), b(2, 6), c(8, 10), d(15,18); 19 | vector v; 20 | v.push_back(a); 21 | v.push_back(b); 22 | v.push_back(c); 23 | v.push_back(d); 24 | 25 | print_vector(solution.merge(v)); 26 | } 27 | 28 | { 29 | Interval a(1, 3), b(2, 6), c(6, 10), d(9,18); 30 | vector v; 31 | v.push_back(a); 32 | v.push_back(b); 33 | v.push_back(c); 34 | v.push_back(d); 35 | 36 | print_vector(solution.merge(v)); 37 | } 38 | 39 | //Error test cases from leetcode.com 40 | { 41 | Interval a(1, 4), b(2, 3); 42 | vector v; 43 | v.push_back(a); 44 | v.push_back(b); 45 | 46 | print_vector(solution.merge(v)); 47 | } 48 | 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /merge_k_sorted_lists/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /merge_sorted_array/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /merge_sorted_array/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "merge_sorted_array.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | int a1[100] = {1, 6, 9}; 18 | int b1[] = {2, 4, 7}; 19 | solution.merge(a1, 3, b1, 3); 20 | print_arr(a1, 6); 21 | 22 | int a2[100] = {9, 6, 1}; 23 | int b2[] = {7, 4, 2}; 24 | solution.merge(a2, 3, b2, 3); 25 | print_arr(a2, 6); 26 | 27 | int a3[100] = {1, 6, 9}; 28 | int b3[] = {7, 4, 2}; 29 | solution.merge(a3, 3, b3, 3); 30 | print_arr(a3, 6); 31 | 32 | int a4[100] = {9, 6, 1}; 33 | int b4[] = {2, 4, 7}; 34 | solution.merge(a4, 3, b4, 3); 35 | print_arr(a4, 6); 36 | 37 | //Error test cases from leetcode.com 38 | /* Just a mismatch, I use a >= b, the system use a > b. 39 | */ 40 | int a5[100] = {1}; 41 | int b5[] = {2}; 42 | solution.merge(a5, 1, b5, 1); 43 | print_arr(a5, 2); 44 | 45 | int a6[100] = {}; 46 | int b6[] = {1, 2, 3 ,4 , 5}; 47 | solution.merge(a6, 0, b6, 5); 48 | print_arr(a6, 5); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /merge_two_sorted_lists/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /merge_two_sorted_lists/merge_two_sorted_lists.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | /** 15 | * Definition for singly-linked list. 16 | * struct ListNode { 17 | * int val; 18 | * ListNode *next; 19 | * ListNode(int x) : val(x), next(NULL) {} 20 | * }; 21 | */ 22 | class Solution { 23 | public: 24 | // Assume they are ascending list. 25 | ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { 26 | if(l1 == NULL) 27 | return l2; 28 | if(l2 == NULL) 29 | return l1; 30 | 31 | ListNode *head = NULL, *tail = NULL; 32 | if(l1->val <= l2->val){ 33 | head = tail = l1; 34 | l1 = l1->next; 35 | }else{ 36 | head = tail = l2; 37 | l2 = l2->next; 38 | } 39 | 40 | while(l1 && l2){ 41 | if(l1->val <= l2->val){ 42 | tail->next = l1; 43 | l1 = l1->next; 44 | }else{ 45 | tail->next = l2; 46 | l2 = l2->next; 47 | } 48 | tail = tail->next; 49 | } 50 | 51 | if(l1) 52 | tail->next = l1; 53 | else 54 | tail->next = l2; 55 | 56 | return head; 57 | } 58 | }; 59 | -------------------------------------------------------------------------------- /merge_two_sorted_lists/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "merge_two_sorted_lists.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | ListNode n1(1), n2(5), n3(8), n4(2), n5(4); 19 | n1.next = &n2; 20 | n2.next = &n3; 21 | 22 | n4.next = &n5; 23 | print_list_node(solution.mergeTwoLists(&n1, &n4)); 24 | } 25 | 26 | { 27 | ListNode n1(2), n2(5), n3(8), n4(2), n5(4); 28 | n1.next = &n2; 29 | n2.next = &n3; 30 | 31 | n4.next = &n5; 32 | print_list_node(solution.mergeTwoLists(&n1, &n4)); 33 | } 34 | 35 | { 36 | ListNode n1(2), n2(5), n3(8); 37 | n1.next = &n2; 38 | n2.next = &n3; 39 | 40 | print_list_node(solution.mergeTwoLists(&n1, NULL)); 41 | } 42 | 43 | //Error test cases from leetcode.com 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /minimum_depth_of_binary_tree/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /minimum_depth_of_binary_tree/minimum_depth_of_binary_tree.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "test.h" 16 | using namespace std; 17 | 18 | /** 19 | * Definition for binary tree 20 | * struct TreeNode { 21 | * int val; 22 | * TreeNode *left; 23 | * TreeNode *right; 24 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 25 | * }; 26 | */ 27 | class Solution { 28 | public: 29 | int minDepth(TreeNode *root) { 30 | if(root == NULL) 31 | return 0; 32 | 33 | if(root->left == NULL && root->right == NULL) 34 | return 1; 35 | 36 | int min; 37 | if(root->left && root->right){ 38 | int left_depth = minDepth(root->left); 39 | int right_depth = minDepth(root->right); 40 | min = left_depth < right_depth? left_depth : right_depth; 41 | }else if(root->left){ 42 | min = minDepth(root->left); 43 | }else{ 44 | min = minDepth(root->right); 45 | } 46 | 47 | return min + 1; 48 | } 49 | }; 50 | -------------------------------------------------------------------------------- /minimum_depth_of_binary_tree/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "minimum_depth_of_binary_tree.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | 18 | //Error test cases from leetcode.com 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /minimum_path_sum/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /minimum_path_sum/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "minimum_path_sum.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[][4] = { 19 | {1, 2, 3, 4}, 20 | {5, 7, 2, 1}, 21 | {3, 1, 0, 1} 22 | }; 23 | vector > v; 24 | arr_of_arr_to_vec(v, a[0], 3, 4); 25 | 26 | cout << solution.minPathSum(v) << endl; 27 | } 28 | 29 | //Error test cases from leetcode.com 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /minimum_window_substring/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /minimum_window_substring/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "minimum_window_substring.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | string s = "ADOBECODEBANC"; 19 | string t = "ABC"; 20 | 21 | cout << solution.minWindow(s, t) << endl; 22 | } 23 | 24 | { 25 | string s = "ADOBECODEBANC"; 26 | string t = "ABCF"; 27 | 28 | cout << solution.minWindow(s, t) << endl; 29 | } 30 | 31 | { 32 | string s = "ADOBECOAEBANC"; 33 | string t = "ABC"; 34 | 35 | cout << solution.minWindow(s, t) << endl; 36 | } 37 | 38 | //Error test cases from leetcode.com 39 | { 40 | string s = "a"; 41 | string t = "a"; 42 | 43 | cout << solution.minWindow(s, t) << endl; 44 | } 45 | 46 | { 47 | // misunderstand the question. I thought the letters in "aa" are just 'a'. 48 | string s = "a"; 49 | string t = "aa"; 50 | 51 | cout << solution.minWindow(s, t) << endl; 52 | } 53 | 54 | { 55 | string s = "acbbaca"; 56 | string t = "aba"; 57 | 58 | cout << solution.minWindow(s, t) << endl; 59 | } 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /multiply_strings/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /multiply_strings/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "multiply_strings.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | string s1 = "22"; 19 | string s2 = "22"; 20 | 21 | cout << solution.multiply(s1, s2) << endl; 22 | } 23 | 24 | { 25 | string s1 = "99"; 26 | string s2 = "8888"; 27 | 28 | cout << solution.multiply(s1, s2) << endl; 29 | } 30 | 31 | //Error test cases from leetcode.com 32 | { 33 | string s1 = "98"; 34 | string s2 = "9"; 35 | 36 | cout << solution.multiply(s1, s2) << endl; 37 | } 38 | 39 | { 40 | string s1 = "9133"; 41 | string s2 = "0"; 42 | 43 | cout << solution.multiply(s1, s2) << endl; 44 | } 45 | 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /n-Queens/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /n-Queens/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "n-Queens.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int n = 4; 19 | 20 | print_vector_vector(solution.solveNQueens(n)); 21 | } 22 | 23 | //Error test cases from leetcode.com 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /n-Queens_2/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /n-Queens_2/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "n-Queens_2.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int n = 4; 19 | 20 | cout << solution.totalNQueens(n) << endl; 21 | } 22 | 23 | { 24 | int n = 12; 25 | 26 | cout << solution.totalNQueens(n) << endl; 27 | } 28 | 29 | //Error test cases from leetcode.com 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /next_permutation/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /next_permutation/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "next_permutation.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {1, 2, 3}; 19 | vector v; 20 | INT_ARR_TO_VEC(v, a); 21 | 22 | solution.nextPermutation(v); 23 | print_vector(v); 24 | } 25 | 26 | { 27 | int a[] = {3, 2, 1}; 28 | vector v; 29 | INT_ARR_TO_VEC(v, a); 30 | 31 | solution.nextPermutation(v); 32 | print_vector(v); 33 | } 34 | 35 | { 36 | int a[] = {1, 1, 5}; 37 | vector v; 38 | INT_ARR_TO_VEC(v, a); 39 | 40 | solution.nextPermutation(v); 41 | print_vector(v); 42 | } 43 | 44 | { 45 | int a[] = {1, 5, 1}; 46 | vector v; 47 | INT_ARR_TO_VEC(v, a); 48 | 49 | solution.nextPermutation(v); 50 | print_vector(v); 51 | } 52 | 53 | //Error test cases from leetcode.com 54 | { 55 | int a[] = {1, 3, 2}; 56 | vector v; 57 | INT_ARR_TO_VEC(v, a); 58 | 59 | solution.nextPermutation(v); 60 | print_vector(v); 61 | } 62 | 63 | { 64 | int a[] = {4, 2, 0, 2, 3, 2, 0}; 65 | vector v; 66 | INT_ARR_TO_VEC(v, a); 67 | 68 | solution.nextPermutation(v); 69 | print_vector(v); 70 | } 71 | 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /palindrome_number/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /palindrome_number/palindrome_number.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | class Solution { 15 | public: 16 | bool isPalindrome(int x) { 17 | if(x < 0) 18 | return false; 19 | 20 | int divisor = 1; 21 | for(int tmp = x; tmp >= 10; tmp = tmp / 10){ 22 | divisor *= 10; 23 | } 24 | 25 | int n1, n2; 26 | n1 = n2 = x; 27 | while(n1 && n2){ 28 | if((n1 / divisor) != (n2 % 10)) 29 | return false; 30 | 31 | n1 = n1 % divisor; 32 | divisor = divisor / 10; 33 | n2 = n2 / 10; 34 | } 35 | 36 | return n1 == n2; 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /palindrome_number/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "palindrome_number.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int n = -12321; 19 | cout << solution.isPalindrome(n) << endl; 20 | } 21 | 22 | { 23 | int n = 12321; 24 | cout << solution.isPalindrome(n) << endl; 25 | } 26 | 27 | { 28 | int n = 12341; 29 | cout << solution.isPalindrome(n) << endl; 30 | } 31 | 32 | //Error test cases from leetcode.com 33 | { 34 | int n = 1001; 35 | cout << solution.isPalindrome(n) << endl; 36 | } 37 | 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /palindrome_partitioning/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /palindrome_partitioning/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "palindrome_partitioning.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | string str = "aab"; 18 | print_vector_vector(solution.partition(str)); 19 | 20 | str = "a"; 21 | print_vector_vector(solution.partition(str)); 22 | 23 | str = ""; 24 | print_vector_vector(solution.partition(str)); 25 | 26 | //Error test cases 27 | str = "bb"; 28 | print_vector_vector(solution.partition(str)); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /palindrome_partitioning_2/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /palindrome_partitioning_2/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "palindrome_partitioning_2.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | string s = "aab"; 18 | cout << solution.minCut(s) << endl; 19 | 20 | s = ""; 21 | cout << solution.minCut(s) << endl; 22 | 23 | s = "a"; 24 | cout << solution.minCut(s) << endl; 25 | 26 | s = "abba"; 27 | cout << solution.minCut(s) << endl; 28 | 29 | s = "abbacdc"; 30 | cout << solution.minCut(s) << endl; 31 | 32 | //Error test cases from leetcode.com 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /partition_list/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /partition_list/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "partition_list.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | ListNode n1(1), n2(4), n3(3), n4(2), n5(5), n6(2); 19 | int x = 3; 20 | n1.next = &n2; 21 | n2.next = &n3; 22 | n3.next = &n4; 23 | n4.next = &n5; 24 | n5.next = &n6; 25 | print_list_node(solution.partition(&n1, x)); 26 | } 27 | 28 | { 29 | ListNode n1(1), n2(4), n3(3), n4(2), n5(5), n6(2); 30 | int x = 7; 31 | n1.next = &n2; 32 | n2.next = &n3; 33 | n3.next = &n4; 34 | n4.next = &n5; 35 | n5.next = &n6; 36 | print_list_node(solution.partition(&n1, x)); 37 | } 38 | 39 | { 40 | ListNode n1(1), n2(4), n3(3), n4(2), n5(5), n6(2); 41 | int x = 0; 42 | n1.next = &n2; 43 | n2.next = &n3; 44 | n3.next = &n4; 45 | n4.next = &n5; 46 | n5.next = &n6; 47 | print_list_node(solution.partition(&n1, x)); 48 | } 49 | 50 | //Error test cases from leetcode.com 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /pascal_triangle/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /pascal_triangle/pascal_triangle.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "test.h" 16 | using namespace std; 17 | 18 | class Solution { 19 | public: 20 | vector > generate(int numRows) { 21 | vector > triangle; 22 | 23 | if(numRows <= 0) 24 | return triangle; 25 | 26 | vector tmp1,tmp2; 27 | for(int i = 1; i <= numRows; i ++){ 28 | tmp2.resize(i); 29 | tmp2[0] = tmp2[i - 1] = 1; 30 | 31 | for(int j = 1; j <= (i - 1) / 2; j++) 32 | tmp2[j] = tmp2[i - 1 - j] = tmp1[j - 1] + tmp1[j]; 33 | 34 | triangle.push_back(tmp2); 35 | tmp2.swap(tmp1); 36 | } 37 | 38 | return triangle; 39 | } 40 | }; 41 | -------------------------------------------------------------------------------- /pascal_triangle/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "pascal_triangle.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | 18 | //Error test cases from leetcode.com 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /pascal_triangle_2/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /pascal_triangle_2/pascal_triangle_2.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "test.h" 16 | using namespace std; 17 | 18 | class Solution { 19 | public: 20 | vector getRow(int rowIndex) { 21 | vector tmp1, tmp2; 22 | 23 | for(int i = 1; i <= rowIndex + 1; i ++){ 24 | tmp2.resize(i); 25 | tmp2[0] = tmp2[i - 1] = 1; 26 | 27 | for(int j = 1; j <= (i - 1) / 2; j ++) 28 | tmp2[j] = tmp2[i - 1 - j] = tmp1[j - 1] + tmp1[j]; 29 | 30 | tmp2.swap(tmp1); 31 | } 32 | 33 | return tmp1; 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /pascal_triangle_2/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "pascal_triangle_2.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | 18 | //Error test cases from leetcode.com 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /path_sum/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /path_sum/path_sum.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | struct TreeNode { 15 | int val; 16 | TreeNode *left; 17 | TreeNode *right; 18 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 19 | }; 20 | 21 | class Solution { 22 | public: 23 | bool hasPathSum(TreeNode *root, int sum) { 24 | if(root == NULL) 25 | return false; 26 | 27 | if(root->left == NULL && root->right == NULL){ 28 | if(sum == root->val) 29 | return true; 30 | else 31 | return false; 32 | } 33 | 34 | int remain = sum - root->val; 35 | 36 | if(root->left){ 37 | bool left = hasPathSum(root->left, remain); 38 | if(left) 39 | return true; 40 | } 41 | 42 | if(root->right){ 43 | bool right = hasPathSum(root->right, remain); 44 | if(right) 45 | return true; 46 | } 47 | 48 | return false; 49 | } 50 | }; 51 | -------------------------------------------------------------------------------- /path_sum/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "path_sum.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | TreeNode t1(5); 18 | TreeNode t2(4); 19 | TreeNode t3(8); 20 | t1.left = &t2; 21 | t1.right = &t3; 22 | 23 | TreeNode t4(11); 24 | t2.left = &t4; 25 | 26 | TreeNode t5(13); 27 | TreeNode t6(4); 28 | t3.left = &t5; 29 | t3.right = &t6; 30 | 31 | TreeNode t7(7); 32 | TreeNode t8(2); 33 | t4.left = &t7; 34 | t4.right = &t8; 35 | 36 | TreeNode t9(1); 37 | t6.right = &t9; 38 | 39 | cout << solution.hasPathSum(&t1, 22) << endl; 40 | cout << solution.hasPathSum(&t1, 80) << endl; 41 | 42 | //Error test cases from leetcode.com 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /path_sum_2/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /path_sum_2/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "path_sum_2.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | TreeNode t1(5); 18 | TreeNode t2(4); 19 | TreeNode t3(8); 20 | t1.left = &t2; 21 | t1.right = &t3; 22 | 23 | TreeNode t4(11); 24 | t2.left = &t4; 25 | 26 | TreeNode t5(13); 27 | TreeNode t6(4); 28 | t3.left = &t5; 29 | t3.right = &t6; 30 | 31 | TreeNode t7(7); 32 | TreeNode t8(2); 33 | t4.left = &t7; 34 | t4.right = &t8; 35 | 36 | TreeNode t9(5); 37 | TreeNode t10(1); 38 | t6.left = &t9; 39 | t6.right = &t10; 40 | 41 | print_vector_vector(solution.pathSum(&t1, 22)); 42 | print_vector_vector(solution.pathSum(&t1, 80)); 43 | 44 | //Error test cases from leetcode.com 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /permutation_sequence/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /permutation_sequence/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "permutation_sequence.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int n = 3; 19 | int k = 5; 20 | cout << solution.getPermutation(n, k) << endl; 21 | } 22 | 23 | //Error test cases from leetcode.com 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /permutations/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /permutations/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "permutations.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {1, 2, 3}; 19 | vector v; 20 | INT_ARR_TO_VEC(v, a); 21 | 22 | print_vector_vector(solution.permute(v)); 23 | } 24 | 25 | { 26 | vector v; 27 | 28 | print_vector_vector(solution.permute(v)); 29 | } 30 | 31 | //Error test cases from leetcode.com 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /permutations_2/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /permutations_2/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "permutations_2.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {1, 2, 3}; 19 | vector v; 20 | INT_ARR_TO_VEC(v, a); 21 | 22 | print_vector_vector(solution.permuteUnique(v)); 23 | } 24 | 25 | { 26 | int a[] = {1, 1, 2}; 27 | vector v; 28 | INT_ARR_TO_VEC(v, a); 29 | 30 | print_vector_vector(solution.permuteUnique(v)); 31 | } 32 | 33 | { 34 | vector v; 35 | 36 | print_vector_vector(solution.permuteUnique(v)); 37 | } 38 | 39 | //Error test cases from leetcode.com 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /plus_one/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /plus_one/plus_one.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "test.h" 13 | using namespace std; 14 | 15 | class Solution { 16 | int add(int a, int &addtion_carry){ 17 | int result = a + addtion_carry; 18 | 19 | if(result >= 10){ 20 | result = result - 10; 21 | addtion_carry = 1; 22 | }else 23 | addtion_carry = 0; 24 | 25 | return result; 26 | } 27 | 28 | public: 29 | vector plusOne(vector &digits) { 30 | vector addtion_results(digits); 31 | 32 | int addtion_carry = 1; 33 | for(int i = digits.size() - 1; i >= 0; i --){ 34 | addtion_results[i] = add(digits[i], addtion_carry); 35 | 36 | if(addtion_carry == 0) 37 | break; 38 | } 39 | 40 | if(addtion_carry) 41 | addtion_results.insert(addtion_results.begin(), 1); 42 | 43 | return addtion_results; 44 | } 45 | }; 46 | -------------------------------------------------------------------------------- /plus_one/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "plus_one.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = { 9, 9, 9, 9 }; 19 | vector v; 20 | INT_ARR_TO_VEC(v, a); 21 | 22 | print_vector(solution.plusOne(v)); 23 | } 24 | 25 | { 26 | int a[] = { 9, 9, 9, 3 }; 27 | vector v; 28 | INT_ARR_TO_VEC(v, a); 29 | 30 | print_vector(solution.plusOne(v)); 31 | } 32 | 33 | //Error test cases from leetcode.com 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /populating_next_right_pointers_in_each_node/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /populating_next_right_pointers_in_each_node/populating_next_right_pointers_in_each_node.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | /** 15 | * Definition for binary tree with next pointer. 16 | * struct TreeLinkNode { 17 | * int val; 18 | * TreeLinkNode *left, *right, *next; 19 | * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} 20 | * }; 21 | */ 22 | class Solution { 23 | public: 24 | void connect(TreeLinkNode *root) { 25 | // That 'left' is NULL indicates 'right'. 26 | if(root == NULL || root->left == NULL) 27 | return; 28 | 29 | TreeLinkNode *left = root->left; 30 | TreeLinkNode *right = root->right; 31 | while(left != NULL && right != NULL){ 32 | left->next = right; 33 | 34 | left = left->right; 35 | right = right->left; 36 | } 37 | 38 | connect(root->left); 39 | connect(root->right); 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /populating_next_right_pointers_in_each_node/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "populating_next_right_pointers_in_each_node.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | TreeLinkNode n1(1), n2(2), n3(3), n4(4), n5(5), n6(6), n7(7); 19 | n1.left = &n2; 20 | n1.right = &n3; 21 | n2.left = &n4; 22 | n2.right = &n5; 23 | n3.left = &n6; 24 | n3.right = &n7; 25 | 26 | solution.connect(&n1); 27 | draw_tree(&n1, "1.dot"); 28 | } 29 | 30 | //Error test cases from leetcode.com 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /populating_next_right_pointers_in_each_node_2/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /populating_next_right_pointers_in_each_node_2/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "populating_next_right_pointers_in_each_node_2.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | TreeLinkNode n1(1), n2(2), n3(3), n4(4), n5(5), n6(6), n7(7); 19 | n1.left = &n2; 20 | n1.right = &n3; 21 | n2.left = &n4; 22 | n2.right = &n5; 23 | n3.left = &n6; 24 | n3.right = &n7; 25 | 26 | solution.connect(&n1); 27 | draw_tree(&n1, "1.dot"); 28 | } 29 | 30 | { 31 | TreeLinkNode n1(1), n2(2), n3(3), n4(4), n5(5), n7(7); 32 | n1.left = &n2; 33 | n1.right = &n3; 34 | n2.left = &n4; 35 | n2.right = &n5; 36 | n3.right = &n7; 37 | 38 | solution.connect(&n1); 39 | draw_tree(&n1, "2.dot"); 40 | } 41 | 42 | //Error test cases from leetcode.com 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /pow/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /pow/pow.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "test.h" 13 | using namespace std; 14 | 15 | class Solution { 16 | map record; 17 | 18 | double get_pow(double x, int n){ 19 | if(n == 1) 20 | return x; 21 | else if(n == 0) 22 | return 1; 23 | 24 | if(record.count(n)) 25 | return record[n]; 26 | 27 | double half = get_pow(x, n / 2); 28 | double ret = half * half; 29 | if(n % 2) 30 | ret = ret * x; 31 | 32 | record[n] = ret; 33 | 34 | return ret; 35 | } 36 | public: 37 | double pow(double x, int n) { 38 | record.clear(); 39 | 40 | if(n >= 0){ 41 | return get_pow(x, n); 42 | }else{ 43 | // 'n' might be the smallest negative '-2147483648' 44 | double ret = get_pow(x, -(n + 1)) * x; 45 | return 1/ret; 46 | } 47 | } 48 | }; 49 | -------------------------------------------------------------------------------- /pow/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "pow.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | double x = 2.5; 19 | int n = 2; 20 | cout << solution.pow(x, n) << endl; 21 | } 22 | 23 | { 24 | double x = 2.5; 25 | int n = 0; 26 | cout << solution.pow(x, n) << endl; 27 | } 28 | 29 | { 30 | double x = 2.5; 31 | int n = 1; 32 | cout << solution.pow(x, n) << endl; 33 | } 34 | 35 | { 36 | double x = 2.5; 37 | int n = 5; 38 | cout << solution.pow(x, n) << endl; 39 | } 40 | 41 | //Error test cases from leetcode.com 42 | { 43 | double x = 1.0; 44 | int n = -1; 45 | cout << solution.pow(x, n) << endl; 46 | } 47 | 48 | 49 | { 50 | double x = 1.0; 51 | int n = -2147483648; 52 | cout << solution.pow(x, n) << endl; 53 | } 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /recover_binary_search_tree/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /recover_binary_search_tree/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "recover_binary_search_tree.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | TreeNode n1(4), n2(3), n3(5), n5(2), n6(6); 19 | n1.left = &n2; 20 | n1.right = &n3; 21 | n2.right = &n5; 22 | n3.right = &n6; 23 | 24 | solution.recoverTree(&n1); 25 | draw_tree(&n1, "1.dot"); 26 | } 27 | 28 | { 29 | TreeNode n1(4), n2(3), n3(2), n5(5), n6(6); 30 | n1.left = &n2; 31 | n1.right = &n3; 32 | n2.left = &n5; 33 | n3.left = &n6; 34 | 35 | solution.recoverTree(&n1); 36 | draw_tree(&n1, "2.dot"); 37 | } 38 | 39 | //Error test cases from leetcode.com 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /regular_expression_matching/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /remove_duplicates_from_sorted_array/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /remove_duplicates_from_sorted_array/remove_duplicates_from_sorted_array.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | class Solution { 15 | public: 16 | int removeDuplicates(int A[], int n) { 17 | int total = 0; 18 | 19 | int cur = 0, next; 20 | while(cur < n){ 21 | A[total ++] = A[cur]; 22 | 23 | next = cur + 1; 24 | while(next < n && A[next] == A[cur]) 25 | next ++; 26 | 27 | cur = next; 28 | } 29 | 30 | return total; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /remove_duplicates_from_sorted_array/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "remove_duplicates_from_sorted_array.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {1, 1, 2}; 19 | int n = solution.removeDuplicates(a, sizeof(a) / sizeof(a[0])); 20 | 21 | print_arr(a, n); 22 | } 23 | 24 | { 25 | int a[] = {1, 3, 2}; 26 | int n = solution.removeDuplicates(a, sizeof(a) / sizeof(a[0])); 27 | 28 | print_arr(a, n); 29 | } 30 | 31 | { 32 | int a[] = {}; 33 | int n = solution.removeDuplicates(a, sizeof(a) / sizeof(a[0])); 34 | 35 | print_arr(a, n); 36 | } 37 | 38 | //Error test cases from leetcode.com 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /remove_duplicates_from_sorted_array_2/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /remove_duplicates_from_sorted_array_2/remove_duplicates_from_sorted_array_2.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | class Solution { 15 | public: 16 | int removeDuplicates(int A[], int n) { 17 | int allowed_times = 2; 18 | int total = 0, cur = 0; 19 | 20 | while(cur < n){ 21 | int i, next; 22 | 23 | for(next = cur, i = 1; next < n && i <= allowed_times; next ++, i ++){ 24 | if(A[next] == A[cur]) 25 | A[total ++] = A[next]; 26 | else 27 | break; 28 | } 29 | 30 | while(next < n && A[next] == A[cur]) 31 | next ++; 32 | 33 | cur = next; 34 | } 35 | 36 | return total; 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /remove_duplicates_from_sorted_array_2/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "remove_duplicates_from_sorted_array_2.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {1, 1, 1, 2, 2, 3}; 19 | int n = solution.removeDuplicates(a, sizeof(a) / sizeof(a[0])); 20 | 21 | print_arr(a, n); 22 | } 23 | 24 | { 25 | int a[] = {1, 1, 3, 2}; 26 | int n = solution.removeDuplicates(a, sizeof(a) / sizeof(a[0])); 27 | 28 | print_arr(a, n); 29 | } 30 | 31 | { 32 | int a[] = {}; 33 | int n = solution.removeDuplicates(a, sizeof(a) / sizeof(a[0])); 34 | 35 | print_arr(a, n); 36 | } 37 | 38 | //Error test cases from leetcode.com 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /remove_duplicates_from_sorted_list/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /remove_duplicates_from_sorted_list/remove_duplicates_from_sorted_list.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | /** 15 | * Definition for singly-linked list. 16 | * struct ListNode { 17 | * int val; 18 | * ListNode *next; 19 | * ListNode(int x) : val(x), next(NULL) {} 20 | * }; 21 | */ 22 | class Solution { 23 | public: 24 | ListNode *deleteDuplicates(ListNode *head) { 25 | ListNode *cur = head, *next; 26 | 27 | while(cur != NULL){ 28 | next = cur->next; 29 | while(next != NULL && next->val == cur->val){ 30 | next = next->next; 31 | } 32 | cur->next = next; 33 | cur = next; 34 | } 35 | 36 | return head; 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /remove_duplicates_from_sorted_list/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "remove_duplicates_from_sorted_list.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | ListNode n1(1), n2(1), n3(2); 19 | n1.next = &n2; 20 | n2.next = &n3; 21 | print_list_node(solution.deleteDuplicates(&n1)); 22 | } 23 | 24 | { 25 | ListNode n1(1), n2(1), n3(2), n4(3), n5(3); 26 | n1.next = &n2; 27 | n2.next = &n3; 28 | n3.next = &n4; 29 | n4.next = &n5; 30 | print_list_node(solution.deleteDuplicates(&n1)); 31 | } 32 | 33 | { 34 | print_list_node(solution.deleteDuplicates(NULL)); 35 | } 36 | 37 | { 38 | ListNode n1(1), n2(2), n3(3); 39 | n1.next = &n2; 40 | n2.next = &n3; 41 | print_list_node(solution.deleteDuplicates(&n1)); 42 | } 43 | 44 | //Error test cases from leetcode.com 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /remove_duplicates_from_sorted_list_2/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /remove_duplicates_from_sorted_list_2/remove_duplicates_from_sorted_list_2.h: -------------------------------------------------------------------------------- 1 | /* Notes: It is a problem that I came across when interviewing one year ago. 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | /** 15 | * Definition for singly-linked list. 16 | * struct ListNode { 17 | * int val; 18 | * ListNode *next; 19 | * ListNode(int x) : val(x), next(NULL) {} 20 | * }; 21 | */ 22 | class Solution { 23 | public: 24 | ListNode *deleteDuplicates(ListNode *head) { 25 | ListNode dummy_head(0); 26 | dummy_head.next = head; 27 | 28 | ListNode *pre = &dummy_head, *cur = head, *next; 29 | 30 | while(cur != NULL){ 31 | next = cur->next; 32 | while(next != NULL && next->val == cur->val){ 33 | next = next->next; 34 | } 35 | 36 | // The value of nodes in the range [cur, next) are same as cur's. 37 | if(cur->next != next){ 38 | pre->next = next; 39 | }else{ 40 | pre = cur; 41 | } 42 | 43 | cur = next; 44 | } 45 | 46 | return dummy_head.next; 47 | } 48 | }; 49 | -------------------------------------------------------------------------------- /remove_duplicates_from_sorted_list_2/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "remove_duplicates_from_sorted_list_2.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | ListNode n1(1), n2(2), n3(3), n4(3), n5(4), n6(4), n7(5); 19 | n1.next = &n2; 20 | n2.next = &n3; 21 | n3.next = &n4; 22 | n4.next = &n5; 23 | n5.next = &n6; 24 | n6.next = &n7; 25 | print_list_node(solution.deleteDuplicates(&n1)); 26 | } 27 | 28 | { 29 | ListNode n1(1), n2(1), n3(1), n4(2), n5(3); 30 | n1.next = &n2; 31 | n2.next = &n3; 32 | n3.next = &n4; 33 | n4.next = &n5; 34 | print_list_node(solution.deleteDuplicates(&n1)); 35 | } 36 | 37 | { 38 | ListNode n1(1), n2(1), n3(1), n4(2), n5(2); 39 | n1.next = &n2; 40 | n2.next = &n3; 41 | n3.next = &n4; 42 | n4.next = &n5; 43 | print_list_node(solution.deleteDuplicates(&n1)); 44 | } 45 | 46 | { 47 | ListNode n1(1), n2(2), n3(3), n4(4), n5(5); 48 | n1.next = &n2; 49 | n2.next = &n3; 50 | n3.next = &n4; 51 | n4.next = &n5; 52 | print_list_node(solution.deleteDuplicates(&n1)); 53 | } 54 | 55 | { 56 | print_list_node(solution.deleteDuplicates(NULL)); 57 | } 58 | 59 | //Error test cases from leetcode.com 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /remove_element/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /remove_element/remove_element.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | class Solution { 15 | public: 16 | int removeElement(int A[], int n, int elem) { 17 | int total = n - 1; 18 | 19 | for(int i = 0; i <= total; i ++){ 20 | if(A[i] != elem) 21 | continue; 22 | 23 | while(total >= i && A[total] == elem) 24 | total --; 25 | 26 | if(total >= i){ 27 | A[i] = A[total]; 28 | total --; 29 | } 30 | } 31 | 32 | return total + 1; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /remove_element/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "remove_element.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {1, 2, 2, 3, 2, 5, 3}; 19 | int element = 2; 20 | int n = solution.removeElement(a, sizeof(a) / sizeof(a[0]), element); 21 | print_arr(a, n); 22 | } 23 | 24 | { 25 | int a[] = {1, 2, 2, 3, 2}; 26 | int element = 2; 27 | int n = solution.removeElement(a, sizeof(a) / sizeof(a[0]), element); 28 | print_arr(a, n); 29 | } 30 | 31 | { 32 | int a[] = {1, 2, 2, 3, 2, 5, 3}; 33 | int element = 6; 34 | int n = solution.removeElement(a, sizeof(a) / sizeof(a[0]), element); 35 | print_arr(a, n); 36 | } 37 | 38 | //Error test cases from leetcode.com 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /remove_nth_node_from_end_of_list/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /remove_nth_node_from_end_of_list/remove_nth_node_from_end_of_list.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | /** 15 | * Definition for singly-linked list. 16 | * struct ListNode { 17 | * int val; 18 | * ListNode *next; 19 | * ListNode(int x) : val(x), next(NULL) {} 20 | * }; 21 | */ 22 | class Solution { 23 | public: 24 | ListNode *removeNthFromEnd(ListNode *head, int n) { 25 | if(head == NULL) 26 | return NULL; 27 | 28 | ListNode *cur = head, *next = head; 29 | 30 | for(int i = 0; i < n; i ++) 31 | next = next->next; 32 | 33 | // remove the first node 34 | if(next == NULL) 35 | return head->next; 36 | 37 | while(next->next != NULL){ 38 | next = next->next; 39 | cur = cur->next; 40 | } 41 | 42 | cur->next = cur->next->next; 43 | 44 | return head; 45 | } 46 | }; 47 | -------------------------------------------------------------------------------- /remove_nth_node_from_end_of_list/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "remove_nth_node_from_end_of_list.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | ListNode n1(1), n2(2), n3(3), n4(4), n5(5); 19 | n1.next = &n2; 20 | n2.next = &n3; 21 | n3.next = &n4; 22 | n4.next = &n5; 23 | int n = 2; 24 | print_list_node(solution.removeNthFromEnd(&n1, n)); 25 | } 26 | 27 | { 28 | ListNode n1(1), n2(2), n3(3), n4(4), n5(5); 29 | n1.next = &n2; 30 | n2.next = &n3; 31 | n3.next = &n4; 32 | n4.next = &n5; 33 | int n = 1; 34 | print_list_node(solution.removeNthFromEnd(&n1, n)); 35 | } 36 | 37 | { 38 | ListNode n1(1), n2(2), n3(3), n4(4), n5(5); 39 | n1.next = &n2; 40 | n2.next = &n3; 41 | n3.next = &n4; 42 | n4.next = &n5; 43 | int n = 5; 44 | print_list_node(solution.removeNthFromEnd(&n1, n)); 45 | } 46 | 47 | //Error test cases from leetcode.com 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /restore_ip_addresses/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /restore_ip_addresses/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "restore_ip_addresses.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | string s = "25525511135"; 18 | print_vector(solution.restoreIpAddresses(s)); 19 | 20 | s = "255255111353333"; 21 | print_vector(solution.restoreIpAddresses(s)); 22 | 23 | s = "2552"; 24 | print_vector(solution.restoreIpAddresses(s)); 25 | 26 | s = "255"; 27 | print_vector(solution.restoreIpAddresses(s)); 28 | 29 | s = "2552002102"; 30 | print_vector(solution.restoreIpAddresses(s)); 31 | 32 | //Error test cases from leetcode.com 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /reverse_integer/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /reverse_integer/reverse_integer.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "test.h" 14 | using namespace std; 15 | 16 | class Solution { 17 | public: 18 | // how to return an error code?! 19 | int reverse(int x) { 20 | unsigned int n = x; 21 | int sign = 1; 22 | 23 | if(x < 0){ 24 | n = -x; 25 | sign = -1; 26 | } 27 | 28 | unsigned int ret = 0; 29 | /* 'limit' is used to judge whether overflow. It is unused now because 30 | * there is no error code I could return. 31 | */ 32 | // unsigned int limit = UINT_MAX / 10; 33 | 34 | while(n){ 35 | ret = ret * 10 + n % 10; 36 | n = n / 10; 37 | } 38 | 39 | return sign * ret; 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /reverse_integer/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "test.h" 9 | #include "reverse_integer.h" 10 | 11 | using namespace std; 12 | 13 | int main() 14 | { 15 | Solution solution; 16 | 17 | //Test cases 18 | { 19 | int n = 123; 20 | cout << solution.reverse(n) << endl; 21 | } 22 | 23 | { 24 | int n = 1200; 25 | cout << solution.reverse(n) << endl; 26 | } 27 | 28 | { 29 | int n = -123; 30 | cout << solution.reverse(n) << endl; 31 | } 32 | 33 | { 34 | int n = INT_MIN; 35 | cout << solution.reverse(n) << endl; 36 | } 37 | 38 | { 39 | int n = 1234567899; 40 | cout << solution.reverse(n) << endl; 41 | } 42 | 43 | //Error test cases from leetcode.com 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /reverse_linked_list_2/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /reverse_linked_list_2/reverse_linked_list_2.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | /** 15 | * Definition for singly-linked list. 16 | * struct ListNode { 17 | * int val; 18 | * ListNode *next; 19 | * ListNode(int x) : val(x), next(NULL) {} 20 | * }; 21 | */ 22 | class Solution { 23 | public: 24 | ListNode *reverseBetween(ListNode *head, int m, int n) { 25 | ListNode dummy_head(0); 26 | dummy_head.next = head; 27 | 28 | ListNode *pre_start = &dummy_head; 29 | for(int i = 1; i < m; i ++) 30 | pre_start = pre_start->next; 31 | 32 | ListNode *start = pre_start->next; 33 | ListNode *end = start; 34 | for(int i = 0; i < n - m; i ++) 35 | end = end->next; 36 | 37 | ListNode *pre = start; 38 | ListNode *mid = pre->next; 39 | ListNode *post; 40 | 41 | pre_start->next = end; 42 | start->next = end->next; 43 | 44 | while(pre != end){ 45 | post = mid->next; 46 | mid->next = pre; 47 | pre = mid; 48 | mid = post; 49 | } 50 | 51 | return dummy_head.next; 52 | } 53 | }; 54 | -------------------------------------------------------------------------------- /reverse_linked_list_2/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "reverse_linked_list_2.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | ListNode n1(1), n2(2), n3(3), n4(4), n5(5); 19 | n1.next = &n2; 20 | n2.next = &n3; 21 | n3.next = &n4; 22 | n4.next = &n5; 23 | int m = 2; 24 | int n = 4; 25 | print_list_node(solution.reverseBetween(&n1, m, n)); 26 | } 27 | 28 | { 29 | ListNode n1(1), n2(2), n3(3), n4(4), n5(5); 30 | n1.next = &n2; 31 | n2.next = &n3; 32 | n3.next = &n4; 33 | n4.next = &n5; 34 | int m = 1; 35 | int n = 2; 36 | print_list_node(solution.reverseBetween(&n1, m, n)); 37 | } 38 | 39 | { 40 | ListNode n1(1), n2(2), n3(3), n4(4), n5(5); 41 | n1.next = &n2; 42 | n2.next = &n3; 43 | n3.next = &n4; 44 | n4.next = &n5; 45 | int m = 1; 46 | int n = 5; 47 | print_list_node(solution.reverseBetween(&n1, m, n)); 48 | } 49 | 50 | { 51 | ListNode n1(1), n2(2), n3(3), n4(4), n5(5); 52 | n1.next = &n2; 53 | n2.next = &n3; 54 | n3.next = &n4; 55 | n4.next = &n5; 56 | int m = 3; 57 | int n = 3; 58 | print_list_node(solution.reverseBetween(&n1, m, n)); 59 | } 60 | 61 | //Error test cases from leetcode.com 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /reverse_nodes_in_k_group/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /roman_to_integer/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /roman_to_integer/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "roman_to_integer.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | // 1994 19 | string s = "MCMXCIV"; 20 | 21 | cout << solution.romanToInt(s) << endl; 22 | } 23 | 24 | //Error test cases from leetcode.com 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /rotate_image/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /rotate_image/rotate_image.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "test.h" 16 | using namespace std; 17 | 18 | class Solution { 19 | public: 20 | void rotate(vector > &matrix) { 21 | int n = matrix.size(); 22 | if(n == 0) 23 | return; 24 | 25 | for(int start = 0, end = n - 1; start < end; start ++, end--){ 26 | for(int i = start; i < end; i ++){ 27 | int offset = i - start; 28 | int tmp = matrix[start][i]; 29 | matrix[start][i] = matrix[end - offset][start]; 30 | matrix[end - offset][start] = matrix[end][end - offset]; 31 | matrix[end][end - offset] = matrix[i][end]; 32 | matrix[i][end] = tmp; 33 | } 34 | } 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /rotate_image/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "rotate_image.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[][3] = { 19 | {1, 2, 3}, 20 | {4, 5, 6}, 21 | {7, 8, 9} 22 | }; 23 | vector > v; 24 | arr_of_arr_to_vec(v, a[0], 3, 3); 25 | 26 | print_vector_vector(v); 27 | solution.rotate(v); 28 | cout << "=>" << endl; 29 | print_vector_vector(v); 30 | } 31 | 32 | { 33 | int a[][4] = { 34 | {1, 2, 3, 4}, 35 | {5, 6, 7, 8}, 36 | {9, 10, 11, 12}, 37 | {13, 14, 15, 16} 38 | }; 39 | vector > v; 40 | arr_of_arr_to_vec(v, a[0], 4, 4); 41 | 42 | print_vector_vector(v); 43 | solution.rotate(v); 44 | cout << "=>" << endl; 45 | print_vector_vector(v); 46 | } 47 | 48 | //Error test cases from leetcode.com 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /rotate_list/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /rotate_list/rotate_list.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | /** 15 | * Definition for singly-linked list. 16 | * struct ListNode { 17 | * int val; 18 | * ListNode *next; 19 | * ListNode(int x) : val(x), next(NULL) {} 20 | * }; 21 | */ 22 | class Solution { 23 | public: 24 | ListNode *rotateRight(ListNode *head, int k) { 25 | ListNode *cur = head, *next = cur; 26 | 27 | if(head == NULL) 28 | return NULL; 29 | 30 | for(int i = 0; i < k ; i ++){ 31 | next = next->next; 32 | // Case: K >= 'the size of the link list'. 33 | if(next == NULL){ 34 | next = cur; 35 | } 36 | } 37 | 38 | // Case: cur and next point to the same node. 39 | if(cur == next) 40 | return head; 41 | 42 | while(next->next != NULL){ 43 | next = next->next; 44 | cur = cur->next; 45 | } 46 | 47 | next->next = head; 48 | head = cur->next; 49 | cur->next = NULL; 50 | 51 | return head; 52 | } 53 | }; 54 | -------------------------------------------------------------------------------- /rotate_list/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "rotate_list.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | ListNode n1(1), n2(2), n3(3), n4(4), n5(5); 19 | n1.next = &n2; 20 | n2.next = &n3; 21 | n3.next = &n4; 22 | n4.next = &n5; 23 | print_list_node(solution.rotateRight(&n1, 2)); 24 | } 25 | 26 | { 27 | ListNode n1(1), n2(2), n3(3), n4(4), n5(5); 28 | n1.next = &n2; 29 | n2.next = &n3; 30 | n3.next = &n4; 31 | n4.next = &n5; 32 | print_list_node(solution.rotateRight(&n1, 5)); 33 | } 34 | 35 | { 36 | ListNode n1(1), n2(2), n3(3), n4(4), n5(5); 37 | n1.next = &n2; 38 | n2.next = &n3; 39 | n3.next = &n4; 40 | n4.next = &n5; 41 | print_list_node(solution.rotateRight(&n1, 0)); 42 | } 43 | 44 | { 45 | ListNode n1(1), n2(2), n3(3), n4(4), n5(5); 46 | n1.next = &n2; 47 | n2.next = &n3; 48 | n3.next = &n4; 49 | n4.next = &n5; 50 | print_list_node(solution.rotateRight(&n1, 7)); 51 | } 52 | 53 | //Error test cases from leetcode.com 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /same_tree/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /same_tree/same_tree.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | /** 15 | * Definition for binary tree 16 | * struct TreeNode { 17 | * int val; 18 | * TreeNode *left; 19 | * TreeNode *right; 20 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 21 | * }; 22 | */ 23 | class Solution { 24 | bool is_same_structure(TreeNode *p, TreeNode *q){ 25 | int n1 = 0; 26 | if(p->left) 27 | n1 += 1; 28 | if(p->right) 29 | n1 += 2; 30 | 31 | int n2 = 0; 32 | if(q->left) 33 | n2 += 1; 34 | if(q->right) 35 | n2 += 2; 36 | 37 | return n1 == n2; 38 | } 39 | 40 | public: 41 | bool isSameTree(TreeNode *p, TreeNode *q) { 42 | if(p == NULL) 43 | return q == NULL; 44 | 45 | if(q == NULL) 46 | return p == NULL; 47 | 48 | if(p->val != q->val) 49 | return false; 50 | 51 | if(is_same_structure(p, q) == false) 52 | return false; 53 | 54 | if(isSameTree(p->left, q->left) == false) 55 | return false; 56 | 57 | if(isSameTree(p->right, q->right) == false) 58 | return false; 59 | 60 | return true; 61 | } 62 | }; 63 | -------------------------------------------------------------------------------- /same_tree/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "same_tree.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | // true 19 | TreeNode n1(1), n2(2), n3(3), n4(4); 20 | n1.left = &n2; 21 | n1.right = &n3; 22 | n2.right = &n4; 23 | 24 | 25 | TreeNode n5(1), n6(2), n7(3), n8(4); 26 | n5.left = &n6; 27 | n5.right = &n7; 28 | n6.right = &n8; 29 | 30 | cout << solution.isSameTree(&n1, &n5) << endl; 31 | } 32 | 33 | { 34 | // false 35 | TreeNode n1(1), n2(2), n3(3), n4(4); 36 | n1.left = &n2; 37 | n1.right = &n3; 38 | n2.right = &n4; 39 | 40 | 41 | TreeNode n5(1), n6(2), n7(3), n8(4); 42 | n5.left = &n6; 43 | n5.right = &n7; 44 | n6.left = &n8; 45 | 46 | cout << solution.isSameTree(&n1, &n5) << endl; 47 | } 48 | 49 | //Error test cases from leetcode.com 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /scramble_string/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /scramble_string/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "scramble_string.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | string s1 = "great"; 19 | string s2 = "rgtae"; 20 | 21 | cout << solution.isScramble(s1, s2) << endl; 22 | } 23 | 24 | { 25 | string s1 = "great"; 26 | string s2 = "rgtam"; 27 | 28 | cout << solution.isScramble(s1, s2) << endl; 29 | } 30 | 31 | //Error test cases from leetcode.com 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /search_a_2D_matrix/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /search_a_2D_matrix/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "search_a_2D_matrix.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[][4] = { 19 | {1, 3, 5, 7}, 20 | {10, 11, 16, 20}, 21 | {23, 30, 34, 50} 22 | }; 23 | 24 | vector > v; 25 | arr_of_arr_to_vec(v, a[0], 3, 4); 26 | 27 | int target = 3; 28 | 29 | cout << solution.searchMatrix(v, target) << endl; 30 | } 31 | 32 | { 33 | int a[][4] = { 34 | {1, 3, 5, 7}, 35 | {10, 11, 16, 20}, 36 | {23, 30, 34, 50} 37 | }; 38 | 39 | vector > v; 40 | arr_of_arr_to_vec(v, a[0], 3, 4); 41 | 42 | int target = 21; 43 | 44 | cout << solution.searchMatrix(v, target) << endl; 45 | } 46 | 47 | //Error test cases from leetcode.com 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /search_for_a_range/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /search_for_a_range/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "search_for_a_range.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {5, 7, 7, 8, 8, 10}; 19 | int target = 8; 20 | 21 | print_vector(solution.searchRange(a, sizeof(a) / sizeof(a[0]), target)); 22 | } 23 | 24 | { 25 | int a[] = {5, 7, 7, 8, 8, 10}; 26 | int target = 9; 27 | 28 | print_vector(solution.searchRange(a, sizeof(a) / sizeof(a[0]), target)); 29 | } 30 | 31 | { 32 | int a[] = {5}; 33 | int target = 9; 34 | 35 | print_vector(solution.searchRange(a, sizeof(a) / sizeof(a[0]), target)); 36 | } 37 | 38 | { 39 | int a[] = {5}; 40 | int target = 3; 41 | 42 | print_vector(solution.searchRange(a, sizeof(a) / sizeof(a[0]), target)); 43 | } 44 | 45 | //Error test cases from leetcode.com 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /search_in_rotated_sorted_array/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /search_in_rotated_sorted_array/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "search_in_rotated_sorted_array.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {4, 5, 6, 7, 0, 1, 2}; 19 | int target = 5; 20 | cout << solution.search(a, sizeof(a) / sizeof(a[0]), target) << endl; 21 | } 22 | 23 | { 24 | int a[] = {4, 5, 6, 7, 8, 9, 10}; 25 | int target = 8; 26 | cout << solution.search(a, sizeof(a) / sizeof(a[0]), target) << endl; 27 | } 28 | 29 | { 30 | int a[] = {4, 5, 6, 7, 0, 1, 2}; 31 | int target = 3; 32 | cout << solution.search(a, sizeof(a) / sizeof(a[0]), target) << endl; 33 | } 34 | 35 | //Error test cases from leetcode.com 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /search_in_rotated_sorted_array_2/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /search_in_rotated_sorted_array_2/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "search_in_rotated_sorted_array_2.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {4, 5, 5, 7, 0, 1, 2}; 19 | int target = 5; 20 | cout << solution.search(a, sizeof(a) / sizeof(a[0]), target) << endl; 21 | } 22 | 23 | { 24 | int a[] = {4, 5, 6, 7, 8, 8, 10}; 25 | int target = 8; 26 | cout << solution.search(a, sizeof(a) / sizeof(a[0]), target) << endl; 27 | } 28 | 29 | { 30 | int a[] = {4, 5, 6, 7, 0, 1, 2}; 31 | int target = 3; 32 | cout << solution.search(a, sizeof(a) / sizeof(a[0]), target) << endl; 33 | } 34 | 35 | //Error test cases from leetcode.com 36 | { 37 | int a[] = {3, 1}; 38 | int target = 3; 39 | cout << solution.search(a, sizeof(a) / sizeof(a[0]), target) << endl; 40 | } 41 | 42 | { 43 | int a[] = {3, 1}; 44 | int target = 3; 45 | cout << solution.search(a, sizeof(a) / sizeof(a[0]), target) << endl; 46 | } 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /search_insert_position/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /search_insert_position/search_insert_position.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "test.h" 13 | 14 | using namespace std; 15 | 16 | class Solution { 17 | public: 18 | int searchInsert(int A[], int n, int target) { 19 | int low = 0, high = n - 1; 20 | 21 | while(low <= high){ 22 | int mid = low + (high - low) / 2; 23 | 24 | if(A[mid] < target) 25 | low = mid + 1; 26 | else 27 | high = mid - 1; 28 | } 29 | 30 | return low; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /search_insert_position/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "search_insert_position.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {1, 3, 5, 6}; 19 | int target = 5; 20 | 21 | cout << solution.searchInsert(a, sizeof(a) / sizeof(a[0]), target) << endl; 22 | } 23 | 24 | { 25 | int a[] = {1, 3, 5, 6}; 26 | int target = 2; 27 | 28 | cout << solution.searchInsert(a, sizeof(a) / sizeof(a[0]), target) << endl; 29 | } 30 | 31 | { 32 | int a[] = {1, 3, 5, 6}; 33 | int target = 7; 34 | 35 | cout << solution.searchInsert(a, sizeof(a) / sizeof(a[0]), target) << endl; 36 | } 37 | 38 | { 39 | int a[] = {1, 3, 5, 6}; 40 | int target = 0; 41 | 42 | cout << solution.searchInsert(a, sizeof(a) / sizeof(a[0]), target) << endl; 43 | } 44 | 45 | //Error test cases from leetcode.com 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /set_matrix_zeroes/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /set_matrix_zeroes/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "set_matrix_zeroes.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[][4] = { 19 | {1, 0, 0, 1}, 20 | {0, 1, 1, 1}, 21 | {1, 1, 1, 1} 22 | }; 23 | vector > v; 24 | arr_of_arr_to_vec(v, a[0], 3, 4); 25 | 26 | solution.setZeroes(v); 27 | print_vector_vector(v); 28 | } 29 | 30 | //Error test cases from leetcode.com 31 | { 32 | // column_record is not clear at the beginning. 33 | int a[][3] = { 34 | {2147483647, 2, 9}, 35 | {2, 6, 7}, 36 | {1, 8, 8}, 37 | {5, 0, 1}, 38 | {9, 6, 0}, 39 | }; 40 | vector > v; 41 | arr_of_arr_to_vec(v, a[0], 5, 3); 42 | 43 | solution.setZeroes(v); 44 | print_vector_vector(v); 45 | } 46 | 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /simplify_path/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /simplify_path/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "simplify_path.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | string s = "/home/"; 19 | 20 | cout << solution.simplifyPath(s) << endl; 21 | } 22 | 23 | { 24 | string s = "/a/./b/../../c/"; 25 | 26 | cout << solution.simplifyPath(s) << endl; 27 | } 28 | 29 | //Error test cases from leetcode.com 30 | { 31 | string s = "/a/.b/c/"; 32 | 33 | cout << solution.simplifyPath(s) << endl; 34 | } 35 | 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /sort_colors/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /sort_colors/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "sort_colors.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {1, 2, 0, 1, 2, 0, 0, 1, 1, 2}; 19 | int size = sizeof(a) / sizeof(a[0]); 20 | 21 | solution.sortColors(a, size); 22 | 23 | print_arr(a, size); 24 | } 25 | 26 | //Error test cases from leetcode.com 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /spiral_matrix/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /spiral_matrix/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "spiral_matrix.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[][3] = { 19 | {1, 2, 3}, 20 | {4, 5, 6}, 21 | {7, 8, 9} 22 | }; 23 | vector > v; 24 | arr_of_arr_to_vec(v, a[0], 3, 3); 25 | 26 | print_vector(solution.spiralOrder(v)); 27 | } 28 | 29 | //Error test cases from leetcode.com 30 | { 31 | int a[][2] = { 32 | {2, 3} 33 | }; 34 | vector > v; 35 | arr_of_arr_to_vec(v, a[0], 1, 2); 36 | 37 | print_vector(solution.spiralOrder(v)); 38 | } 39 | 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /spiral_matrix_2/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /spiral_matrix_2/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "spiral_matrix_2.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int n = 3; 19 | 20 | print_vector_vector(solution.generateMatrix(n)); 21 | } 22 | 23 | //Error test cases from leetcode.com 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /sqrt/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /sqrt/sqrt.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "test.h" 16 | using namespace std; 17 | 18 | class Solution { 19 | public: 20 | int sqrt(int x) { 21 | double old_guess = -1; 22 | double guess = 1; 23 | 24 | while(abs(guess - old_guess) > 0.1){ 25 | old_guess = guess; 26 | guess = (guess + x / guess ) / 2; 27 | } 28 | 29 | return guess; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /sqrt/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "sqrt.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int x = 9; 19 | 20 | cout << solution.sqrt(x) << endl; 21 | } 22 | 23 | { 24 | int x = 15; 25 | 26 | cout << solution.sqrt(x) << endl; 27 | } 28 | 29 | //Error test cases from leetcode.com 30 | { 31 | int x = 3; 32 | 33 | cout << solution.sqrt(x) << endl; 34 | } 35 | 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /string_to_integer/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /string_to_integer/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "string_to_integer.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | char *str = "1234"; 19 | cout << solution.atoi(str) << endl; 20 | } 21 | 22 | { 23 | char *str = "-1234"; 24 | cout << solution.atoi(str) << endl; 25 | } 26 | 27 | { 28 | char *str = " -1234ab12"; 29 | cout << solution.atoi(str) << endl; 30 | } 31 | 32 | { 33 | char *str = "-2147483648"; 34 | cout << solution.atoi(str) << endl; 35 | } 36 | 37 | { 38 | char *str = "2147483647"; 39 | cout << solution.atoi(str) << endl; 40 | } 41 | 42 | //Error test cases from leetcode.com 43 | { 44 | char *str = "2147483648"; 45 | cout << solution.atoi(str) << endl; 46 | } 47 | 48 | { 49 | char *str = " 10522545459"; 50 | cout << solution.atoi(str) << endl; 51 | } 52 | 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /subsets/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /subsets/subsets.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include "test.h" 15 | using namespace std; 16 | 17 | class Solution { 18 | vector > get_sets(vector &S, int start, int end, 19 | vector pre_record){ 20 | vector > sets; 21 | 22 | if(start > end){ 23 | sets.push_back(pre_record); 24 | return sets; 25 | } 26 | 27 | // do not include S[start] 28 | sets = get_sets(S, start + 1, end, pre_record); 29 | 30 | // include S[start] 31 | pre_record.push_back(S[start]); 32 | vector > tmp = get_sets(S, start + 1, end, pre_record); 33 | sets.insert(sets.end(), tmp.begin(), tmp.end()); 34 | 35 | return sets; 36 | } 37 | 38 | public: 39 | vector > subsets(vector &S) { 40 | vector pre_record; 41 | 42 | sort(S.begin(), S.end()); 43 | 44 | return get_sets(S, 0, S.size() - 1, pre_record); 45 | } 46 | }; 47 | -------------------------------------------------------------------------------- /subsets/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "subsets.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {1, 2, 3}; 19 | vector v; 20 | INT_ARR_TO_VEC(v, a); 21 | 22 | print_vector_vector(solution.subsets(v)); 23 | } 24 | 25 | //Error test cases from leetcode.com 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /subsets_2/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /subsets_2/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "subsets_2.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {3, 2, 1}; 19 | vector v; 20 | INT_ARR_TO_VEC(v, a); 21 | 22 | print_vector_vector(solution.subsetsWithDup(v)); 23 | } 24 | 25 | { 26 | int a[] = {2, 2, 1}; 27 | vector v; 28 | INT_ARR_TO_VEC(v, a); 29 | 30 | print_vector_vector(solution.subsetsWithDup(v)); 31 | } 32 | 33 | //Error test cases from leetcode.com 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /substring_with_concatenation_of_all_words/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /substring_with_concatenation_of_all_words/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "substring_with_concatenation_of_all_words.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | string s = "barfoothefoobarman"; 19 | vector v; 20 | v.push_back("foo"); 21 | v.push_back("bar"); 22 | 23 | print_vector(solution.findSubstring(s, v)); 24 | } 25 | 26 | //Error test cases from leetcode.com 27 | { 28 | string s = "aaa"; 29 | vector v; 30 | v.push_back("a"); 31 | v.push_back("a"); 32 | 33 | print_vector(solution.findSubstring(s, v)); 34 | } 35 | 36 | { 37 | string s="abababababababababababababababababababababababab"; 38 | vector v; 39 | v.push_back("ab"); 40 | v.push_back("ba"); 41 | 42 | print_vector(solution.findSubstring(s, v)); 43 | } 44 | 45 | { 46 | string s="abababab"; 47 | vector v; 48 | v.push_back("a"); 49 | v.push_back("b"); 50 | v.push_back("a"); 51 | 52 | print_vector(solution.findSubstring(s, v)); 53 | } 54 | 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /sudoku_solver/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /sudoku_solver/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "sudoku_solver.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | // true 19 | char arr[][9] = { 20 | {'5', '3', '.', '.', '7', '.', '.', '.', '.'}, 21 | {'6', '.', '.', '1', '9', '5', '.', '.', '.'}, 22 | {'.', '9', '8', '.', '.', '.', '.', '6', '.'}, 23 | {'8', '.', '.', '.', '6', '.', '.', '.', '3'}, 24 | {'4', '.', '.', '8', '.', '3', '.', '.', '1'}, 25 | {'7', '.', '.', '.', '2', '.', '.', '.', '6'}, 26 | {'.', '6', '.', '.', '.', '.', '2', '8', '.'}, 27 | {'.', '.', '.', '4', '1', '9', '.', '.', '5'}, 28 | {'.', '.', '.', '.', '8', '.', '.', '7', '9'}, 29 | }; 30 | vector > v; 31 | arr_of_arr_to_vec(v, arr[0], 9, 9); 32 | 33 | solution.solveSudoku(v); 34 | print_vector_vector(v); 35 | } 36 | 37 | //Error test cases from leetcode.com 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /sum_root_to_leaf_numbers/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /sum_root_to_leaf_numbers/sum_root_to_leaf_numbers.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "test.h" 16 | using namespace std; 17 | 18 | /** 19 | * Definition for binary tree 20 | * struct TreeNode { 21 | * int val; 22 | * TreeNode *left; 23 | * TreeNode *right; 24 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 25 | * }; 26 | */ 27 | class Solution { 28 | int sumTree(TreeNode *root, int sum){ 29 | if(root == NULL) 30 | return 0; 31 | 32 | sum = sum * 10 + root->val; 33 | 34 | if(root->left == NULL && root->right == NULL){ 35 | return sum; 36 | } 37 | 38 | int tree_sum = 0; 39 | if(root->left) 40 | tree_sum += sumTree(root->left, sum); 41 | 42 | if(root->right) 43 | tree_sum += sumTree(root->right, sum); 44 | 45 | return tree_sum; 46 | } 47 | 48 | public: 49 | int sumNumbers(TreeNode *root) { 50 | return sumTree(root, 0); 51 | } 52 | }; 53 | -------------------------------------------------------------------------------- /sum_root_to_leaf_numbers/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "sum_root_to_leaf_numbers.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | 18 | //Error test cases from leetcode.com 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /surrounded_regions/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /surrounded_regions/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "surrounded_regions.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | char a[][4] = { 19 | {'X', 'X', 'X', 'X'}, 20 | {'X', 'O', 'O', 'X'}, 21 | {'X', 'X', 'O', 'X'}, 22 | {'X', 'O', 'X', 'X'}, 23 | }; 24 | 25 | vector > v; 26 | arr_of_arr_to_vec(v, a[0], 4, 4); 27 | 28 | solution.solve(v); 29 | print_vector_vector(v); 30 | } 31 | 32 | //Error test cases from leetcode.com 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /swap_nodes_in_pairs/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /swap_nodes_in_pairs/swap_nodes_in_pairs.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "test.h" 13 | using namespace std; 14 | 15 | /** 16 | * Definition for singly-linked list. 17 | * struct ListNode { 18 | * int val; 19 | * ListNode *next; 20 | * ListNode(int x) : val(x), next(NULL) {} 21 | * }; 22 | */ 23 | class Solution { 24 | public: 25 | ListNode *swapPairs(ListNode *head) { 26 | ListNode dummy_head(0); 27 | dummy_head.next = head; 28 | 29 | ListNode *pre = head, *pre_tail = &dummy_head; 30 | ListNode *cur, *next; 31 | while(pre && pre->next){ 32 | cur = pre->next; 33 | next = cur->next; 34 | 35 | cur->next = pre; 36 | pre_tail->next = cur; 37 | pre_tail = pre; 38 | 39 | pre = next; 40 | } 41 | pre_tail->next = pre; 42 | 43 | return dummy_head.next; 44 | } 45 | }; 46 | -------------------------------------------------------------------------------- /swap_nodes_in_pairs/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "swap_nodes_in_pairs.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | ListNode n1(1), n2(2), n3(3), n4(4); 19 | n1.next = &n2; 20 | n2.next = &n3; 21 | n3.next = &n4; 22 | 23 | print_list_node(solution.swapPairs(&n1)); 24 | } 25 | 26 | { 27 | ListNode n1(1), n2(2), n3(3); 28 | n1.next = &n2; 29 | n2.next = &n3; 30 | 31 | print_list_node(solution.swapPairs(&n1)); 32 | } 33 | 34 | //Error test cases from leetcode.com 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /symmetric_tree/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /symmetric_tree/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "symmetric_tree.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | // true 19 | TreeNode n1(1), n2(2), n3(2), n4(3), n5(4), n6(4), n7(3); 20 | n1.left = &n2; 21 | n1.right = &n3; 22 | n2.left = &n4; 23 | n2.right = &n5; 24 | n3.left = &n6; 25 | n3.right = &n7; 26 | 27 | cout << solution.isSymmetric(&n1) << endl; 28 | } 29 | 30 | { 31 | // false 32 | TreeNode n1(1), n2(2), n3(2), n4(3), n6(3); 33 | n1.left = &n2; 34 | n1.right = &n3; 35 | n2.right = &n4; 36 | n3.right = &n6; 37 | 38 | cout << solution.isSymmetric(&n1) << endl; 39 | } 40 | 41 | //Error test cases from leetcode.com 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /template/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /template/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "TEST_FILE_H" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | 18 | //Error test cases from leetcode.com 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /template/test_file.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "test.h" 16 | using namespace std; 17 | -------------------------------------------------------------------------------- /text_justification/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /text_justification/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "text_justification.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | char *a[7] = { 19 | "This", "is", "an", "example", "of", "text", "justification." 20 | }; 21 | vector v; 22 | for(int i = 0; i < 7; i ++) 23 | v.push_back(a[i]); 24 | 25 | int L = 16; 26 | 27 | print_vector(solution.fullJustify(v, L)); 28 | } 29 | 30 | //Error test cases from leetcode.com 31 | { 32 | vector v; 33 | v.push_back(""); 34 | 35 | int L = 0; 36 | 37 | print_vector(solution.fullJustify(v, L)); 38 | } 39 | 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /trapping_rain_water/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /trapping_rain_water/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "trapping_rain_water.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[] = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}; 19 | 20 | cout << solution.trap(a, sizeof(a) / sizeof(a[0])) << endl; 21 | } 22 | 23 | //Error test cases from leetcode.com 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /triangle/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /triangle/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "triangle.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | 18 | //Error test cases from leetcode.com 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /triangle/triangle.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "test.h" 16 | using namespace std; 17 | 18 | class Solution { 19 | public: 20 | int minimumTotal(vector > &triangle) { 21 | int row = triangle.size(); 22 | 23 | if(row <= 0) 24 | return 0; 25 | 26 | vector min(triangle[row - 1]); 27 | 28 | for(int i = row - 2; i >= 0; i --){ 29 | for(int j = 0; j <= i; j ++){ 30 | if(min[j] < min[j + 1]) 31 | min[j] = min[j] + triangle[i][j]; 32 | else 33 | min[j] = min[j + 1] + triangle[i][j]; 34 | } 35 | } 36 | 37 | return min[0]; 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /two_sum/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /two_sum/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "two_sum.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | vector numbers; 18 | 19 | int a[] = {2, 7, 11, 15}; 20 | INT_ARR_TO_VEC(numbers, a); 21 | int target = 9; 22 | print_vector(solution.twoSum(numbers, target)); 23 | 24 | int b[] = {9, 2, 5, 3, 4, 7}; 25 | INT_ARR_TO_VEC(numbers, b); 26 | target = 9; 27 | print_vector(solution.twoSum(numbers, target)); 28 | 29 | //Error test cases from leetcode.com 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /two_sum/two_sum.h: -------------------------------------------------------------------------------- 1 | /* Notes: Map/Hash is very very useful. 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "test.h" 12 | using namespace std; 13 | 14 | class Solution { 15 | public: 16 | vector twoSum(vector &numbers, int target) { 17 | map record; 18 | vector indexs; 19 | 20 | for(size_t i = 0; i < numbers.size(); i ++){ 21 | int n = numbers[i]; 22 | 23 | if(record.count(n)){ 24 | indexs.push_back(record[n]); 25 | indexs.push_back(i + 1); 26 | 27 | return indexs; 28 | }else{ 29 | int remain = target - n; 30 | record[remain] = i + 1; 31 | } 32 | } 33 | 34 | return indexs; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /unique_binary_search_trees/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /unique_binary_search_trees/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "unique_binary_search_trees.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int n = 3; 19 | 20 | cout << solution.numTrees(n) << endl; 21 | } 22 | 23 | //Error test cases from leetcode.com 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /unique_binary_search_trees/unique_binary_search_trees.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "test.h" 14 | using namespace std; 15 | 16 | class Solution { 17 | vector record; 18 | 19 | void init(int n){ 20 | record.clear(); 21 | record.resize(n + 1, -1); 22 | 23 | record[0] = 1; 24 | record[1] = 1; 25 | } 26 | 27 | int get_num(int n){ 28 | if(record[n] != -1) 29 | return record[n]; 30 | 31 | /* pick up the i-th number as the root, 0~i-1 as left tree, i + 1 ~ n - 1 32 | * as right tree. 33 | */ 34 | int num = 0; 35 | for(int i = 0; i <= n - 1; i ++){ 36 | num += get_num(i) * get_num(n - 1 - i); 37 | } 38 | 39 | record[n] = num; 40 | 41 | return num; 42 | } 43 | public: 44 | int numTrees(int n) { 45 | if(n <= 0) 46 | return 1; 47 | 48 | init(n); 49 | 50 | return get_num(n); 51 | } 52 | }; 53 | -------------------------------------------------------------------------------- /unique_binary_search_trees_2/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /unique_binary_search_trees_2/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "unique_binary_search_trees_2.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int n = 3; 19 | vector trees = solution.generateTrees(n); 20 | 21 | for(size_t i = 0; i < trees.size(); i ++){ 22 | char tmp[40]; 23 | sprintf(tmp, "%u", i); 24 | string s = string(tmp); 25 | draw_tree(trees[i], s + ".dot"); 26 | } 27 | } 28 | 29 | //Error test cases from leetcode.com 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /unique_paths/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /unique_paths/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "unique_paths.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int m = 3, n = 7; 19 | 20 | cout << solution.uniquePaths(m, n) << endl; 21 | } 22 | 23 | { 24 | int m = 99, n = 100; 25 | 26 | cout << solution.uniquePaths(m, n) << endl; 27 | } 28 | 29 | { 30 | int m = 100, n = 100; 31 | 32 | cout << solution.uniquePaths(m, n) << endl; 33 | } 34 | 35 | //Error test cases from leetcode.com 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /unique_paths_2/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /unique_paths_2/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "unique_paths_2.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | int a[][3] = { 19 | {0, 0, 0}, 20 | {0, 1, 0}, 21 | {0, 0, 0} 22 | }; 23 | vector > v; 24 | arr_of_arr_to_vec(v, a[0], 3, 3); 25 | 26 | cout << solution.uniquePathsWithObstacles(v) << endl; 27 | } 28 | 29 | //Error test cases from leetcode.com 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /valid_number/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /valid_number/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "valid_number.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | char a[] = "0"; 19 | 20 | cout << solution.isNumber(a) << endl; 21 | } 22 | 23 | { 24 | char a[] = " 0.1"; 25 | 26 | cout << solution.isNumber(a) << endl; 27 | } 28 | 29 | { 30 | char a[] = "abc"; 31 | 32 | cout << solution.isNumber(a) << endl; 33 | } 34 | 35 | { 36 | char a[] = "1 a"; 37 | 38 | cout << solution.isNumber(a) << endl; 39 | } 40 | 41 | { 42 | char a[] = "2e10"; 43 | 44 | cout << solution.isNumber(a) << endl; 45 | } 46 | 47 | //Error test cases from leetcode.com 48 | { 49 | char a[] = ""; 50 | 51 | cout << solution.isNumber(a) << endl; 52 | } 53 | 54 | { 55 | char a[] = ".34"; 56 | 57 | cout << solution.isNumber(a) << endl; 58 | } 59 | 60 | { 61 | char a[] = "1e"; 62 | 63 | cout << solution.isNumber(a) << endl; 64 | } 65 | 66 | { 67 | char a[] = "2e+0"; 68 | 69 | cout << solution.isNumber(a) << endl; 70 | } 71 | 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /valid_palindrome/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /valid_palindrome/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "valid_palindrome.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | 18 | //Error test cases from leetcode.com 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /valid_palindrome/valid_palindrome.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "test.h" 16 | using namespace std; 17 | 18 | class Solution { 19 | public: 20 | bool isPalindrome(string s) { 21 | int len = s.size(); 22 | 23 | if(len <= 1) 24 | return true; 25 | 26 | for(int i = 0, j = len - 1; i < j; i ++, j --){ 27 | while(i < j && !isalnum(s[i])) 28 | i ++; 29 | if(i >= j) 30 | return true; 31 | 32 | while(j > i && !isalnum(s[j])) 33 | j --; 34 | 35 | if(j <= i) 36 | return true; 37 | 38 | if(tolower(s[i]) != tolower(s[j])) 39 | return false; 40 | } 41 | 42 | return true; 43 | } 44 | }; 45 | -------------------------------------------------------------------------------- /valid_parentheses/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /valid_parentheses/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "valid_parentheses.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | string s = "[](){}"; 19 | cout << solution.isValid(s) << endl; 20 | } 21 | 22 | { 23 | string s = "[(){}]"; 24 | cout << solution.isValid(s) << endl; 25 | } 26 | 27 | { 28 | string s = "[](){{}"; 29 | cout << solution.isValid(s) << endl; 30 | } 31 | 32 | { 33 | string s = "[(]){}"; 34 | cout << solution.isValid(s) << endl; 35 | } 36 | 37 | { 38 | string s = "[()]]{}"; 39 | cout << solution.isValid(s) << endl; 40 | } 41 | 42 | //Error test cases from leetcode.com 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /valid_parentheses/valid_parentheses.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "test.h" 13 | using namespace std; 14 | 15 | class Solution { 16 | public: 17 | bool isValid(string s) { 18 | stack st; 19 | 20 | for(size_t i = 0; i < s.size(); i ++){ 21 | char c = s[i]; 22 | 23 | if(c == '(' || c == '{' || c == '[') 24 | st.push(c); 25 | else{ 26 | if(st.empty()) 27 | return false; 28 | 29 | char pre = st.top(); 30 | st.pop(); 31 | 32 | if((c == ')' && pre != '(') || (c == '}' && pre != '{') 33 | || (c == ']' && pre != '[')) 34 | return false; 35 | } 36 | } 37 | 38 | return st.empty(); 39 | } 40 | }; 41 | -------------------------------------------------------------------------------- /valid_sudoku/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /validate_binary_search_tree/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /validate_binary_search_tree/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "validate_binary_search_tree.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | // true 19 | TreeNode n1(4), n2(2), n3(5), n4(3), n5(6); 20 | n1.left = &n2; 21 | n1.right = &n3; 22 | n2.right = &n4; 23 | n3.right = &n5; 24 | 25 | cout << solution.isValidBST(&n1) << endl; 26 | } 27 | 28 | { 29 | // true 30 | TreeNode n1(4), n2(6), n3(5), n4(3), n5(6); 31 | n1.left = &n2; 32 | n1.right = &n3; 33 | n2.right = &n4; 34 | n3.right = &n5; 35 | 36 | cout << solution.isValidBST(&n1) << endl; 37 | } 38 | 39 | //Error test cases from leetcode.com 40 | { 41 | // false 42 | TreeNode n1(1), n2(1); 43 | n1.left = &n2; 44 | 45 | cout << solution.isValidBST(&n1) << endl; 46 | } 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /validate_binary_search_tree/validate_binary_search_tree.h: -------------------------------------------------------------------------------- 1 | /* Notes: 2 | * 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "test.h" 14 | using namespace std; 15 | 16 | /** 17 | * Definition for binary tree 18 | * struct TreeNode { 19 | * int val; 20 | * TreeNode *left; 21 | * TreeNode *right; 22 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 23 | * }; 24 | */ 25 | class Solution { 26 | TreeNode *dummy_node; 27 | 28 | // in-order traversal 29 | bool is_valid_bst(TreeNode *root, TreeNode *&pre){ 30 | if(root == NULL) 31 | return true; 32 | 33 | if(is_valid_bst(root->left, pre) == false) 34 | return false; 35 | 36 | if(root->val <= pre->val && pre != dummy_node) 37 | return false; 38 | 39 | pre = root; 40 | 41 | if(is_valid_bst(root->right, pre) == false) 42 | return false; 43 | 44 | return true; 45 | } 46 | 47 | public: 48 | bool isValidBST(TreeNode *root) { 49 | TreeNode *pre = new TreeNode(INT_MIN); 50 | dummy_node = pre; 51 | 52 | return is_valid_bst(root, pre); 53 | } 54 | }; 55 | -------------------------------------------------------------------------------- /wildcard_matching/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /wildcard_matching/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "wildcard_matching.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | char *s = (char *)"ab"; 19 | char *p = (char *)"ab"; 20 | cout << solution.isMatch(s, p) << endl; 21 | } 22 | 23 | { 24 | char *s = (char *)"abc"; 25 | char *p = (char *)"ab?"; 26 | cout << solution.isMatch(s, p) << endl; 27 | } 28 | 29 | { 30 | char *s = (char *)"abcdefh"; 31 | char *p = (char *)"ab*f?"; 32 | cout << solution.isMatch(s, p) << endl; 33 | } 34 | 35 | { 36 | char *s = (char *)"a"; 37 | char *p = (char *)"?"; 38 | cout << solution.isMatch(s, p) << endl; 39 | } 40 | 41 | { 42 | char *s = (char *)"a"; 43 | char *p = (char *)"a**"; 44 | cout << solution.isMatch(s, p) << endl; 45 | } 46 | 47 | { 48 | char *s = (char *)"abc"; 49 | char *p = (char *)"a*b"; 50 | cout << solution.isMatch(s, p) << endl; 51 | } 52 | 53 | //Error test cases from leetcode.com 54 | { 55 | char *s = (char *)"c"; 56 | char *p = (char *)"*?*"; 57 | cout << solution.isMatch(s, p) << endl; 58 | } 59 | 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /word_ladder/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /word_ladder/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "word_ladder.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | 18 | //Error test cases from leetcode.com 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /word_ladder_2/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /word_ladder_2/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "word_ladder_2.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | 18 | //Error test cases from leetcode.com 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /word_search/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /word_search/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "word_search.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | char a[][4] = { 19 | {'A', 'B', 'C', 'E'}, 20 | {'S', 'F', 'C', 'S'}, 21 | {'A', 'D', 'E', 'E'} 22 | }; 23 | 24 | vector > v; 25 | arr_of_arr_to_vec(v, a[0], 3, 4); 26 | 27 | cout << solution.exist(v, "ABCCED") << endl; 28 | cout << solution.exist(v, "SEE") << endl; 29 | cout << solution.exist(v, "ABCB") << endl; 30 | } 31 | 32 | //Error test cases from leetcode.com 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /zigZag_conversion/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS=-g -Wall 3 | INCLUDE="../include" 4 | 5 | all: 6 | $(CC) $(CFLAGS) -I $(INCLUDE) -o test test.cpp 7 | 8 | clean: 9 | rm test 10 | 11 | -------------------------------------------------------------------------------- /zigZag_conversion/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test.h" 8 | #include "zigZag_conversion.h" 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | Solution solution; 15 | 16 | //Test cases 17 | { 18 | string s = "PAYPALISHIRING"; 19 | int n = 3; 20 | 21 | cout << solution.convert(s, n) << endl; 22 | } 23 | 24 | { 25 | string s = "abcdefghigklmasdfasdfasdfasdfasdf"; 26 | int n = 5; 27 | 28 | cout << solution.convert(s, n) << endl; 29 | } 30 | 31 | //Error test cases from leetcode.com 32 | { 33 | string s = "PAYPALISHIRING"; 34 | int n = 2; 35 | 36 | cout << solution.convert(s, n) << endl; 37 | } 38 | 39 | return 0; 40 | } 41 | --------------------------------------------------------------------------------