├── .gitignore ├── LICENSE ├── README.md ├── algorithms.dev ├── attribute-test ├── argc_argv.cpp ├── array_addr.cpp ├── assert.cpp ├── bit_field.cpp ├── char_init_style.cpp ├── class_type_conversion.cpp ├── constructor.cpp ├── deque.cpp ├── explicit_constructor.cpp ├── for_loop.cpp ├── instantiate_class.cpp ├── list_pair.cpp ├── map.cpp ├── nth_element.cpp ├── printf_gcc.cpp ├── rand.cpp ├── sizeof_strlen.cpp ├── static_and_array.cpp ├── vector.cpp ├── virtual.cpp ├── virtual_derived.cpp └── void_class.cpp ├── boost ├── 01_basic_thread.cpp ├── 02_interrupt_thread.cpp ├── 03_mutex.cpp ├── 04_lock.cpp ├── 05_condition_variable.cpp ├── 06_cooperation.cpp ├── Makefile └── README.md ├── compile.sh ├── leetcode ├── README.md ├── array │ ├── 0015_3sum.cpp │ ├── 0016_3sum_closest.cpp │ ├── 0031_next_permutation.cpp │ └── README.md ├── bfs │ └── README.md ├── bit │ ├── 67_add_binary.cpp │ └── README.md ├── bm │ └── README.md ├── brainteaser │ └── README.md ├── bs │ ├── 0033_search_in_rotated_sorted_array.cpp │ └── README.md ├── bst │ └── README.md ├── bt │ ├── 0017_letter_combinations_of_a_phone_numbe.cpp │ ├── 0022_generate_parentheses.cpp │ └── README.md ├── dc │ └── README.md ├── design │ └── README.md ├── dfs │ └── README.md ├── difficulty │ ├── easy.md │ ├── hard.md │ └── medium.md ├── dp │ ├── 0416-partition-equal-subset-sum.cpp │ └── README.md ├── geometry │ └── README.md ├── graph │ ├── 0399_evaluate_division.cpp │ └── README.md ├── greedy │ ├── 0011_container_with_most_water.cpp │ ├── 0406-queue-reconstruction-by-height.cpp │ └── README.md ├── hash │ ├── 0003_longest_substring_without_repeating_characters.cpp │ └── README.md ├── heap │ └── README.md ├── ll │ └── README.md ├── lw │ └── README.md ├── math │ └── README.md ├── mem │ └── README.md ├── minimax │ └── README.md ├── month │ ├── 2019-10-12.md │ ├── 2019-10-19.md │ ├── 2019-10-26.md │ ├── 2019-10-31.md │ ├── 2019-11-09.md │ ├── 2019-11-16.md │ ├── 2019-11-23.md │ ├── 2019-12-22.md │ └── 2020.md ├── om │ └── README.md ├── queue │ └── README.md ├── random │ └── README.md ├── recursion │ └── README.md ├── rejs │ └── README.md ├── ress │ └── README.md ├── sort │ └── README.md ├── src │ ├── 0001_two_sum.cpp │ ├── 0007_reverse_integer.cpp │ ├── 0009_palindrome_number.cpp │ ├── 0013_roman_to_integer.cpp │ ├── 0014_longest_common_prefix.cpp │ ├── 0020_valid_parentheses.cpp │ ├── 0021_merge_two_sorted_lists.cpp │ ├── 0026_remove_duplicates_from_sorted_array.cpp │ ├── 0027_remove_element.cpp │ ├── 0028_implement_strstr.cpp │ ├── 0035_search_insert_position.cpp │ ├── 0038_count_and_say.cpp │ ├── 0045_jump_game_II.cpp │ ├── 0053_maximum_subarray.cpp │ ├── 0055_jump_game.cpp │ ├── 0058_length_of_last_word.cpp │ ├── 0066_plus_one.cpp │ ├── 0067_add_binary.cpp │ ├── 0069_sqrtx.cpp │ ├── 0070_climbing_stairs.cpp │ ├── 0083_remove_duplicates_from_sorted_list.cpp │ ├── 0088_merge_sorted_array.cpp │ ├── 0094__binary_tree_inorder_traversal.cpp │ ├── 0100_same_tree.cpp │ ├── 0101_symmetric_tree.cpp │ ├── 0104_maximum_depth_of_binary_tree.cpp │ ├── 0106_binary_tree_postorder_traversal.cpp │ ├── 0107_binary_tree_level_order_traversal_ii.cpp │ ├── 0108_convert_sorted_array_to_binary_search_tree.cpp │ ├── 0110_balanced_binary_tree.cpp │ ├── 0111_minimum_depth_of_binary_tree.cpp │ ├── 0112_path_sum.cpp │ ├── 0118_pascals_triangle.cpp │ ├── 0119_pascals_triangle_ii.cpp │ ├── 0121_best_time_to_buy_and_sell_stock.cpp │ ├── 0122_best_time_to_buy_and_sell_stock_ii.cpp │ ├── 0125_valid_palindrome.cpp │ ├── 0136_single_number.cpp │ ├── 0141_linked_list_cycle.cpp │ ├── 0144_binary_tree_preorder_traversal.cpp │ ├── 0147_insertion_sort_list.cpp │ ├── 0148_sort_list.cpp │ ├── 0149_max_points_on_a_line.cpp │ ├── 0150_evaluate_reverse_polish_notation.cpp │ ├── 0155_min_stack.cpp │ ├── 0160_intersection_of_two_linked_lists.cpp │ ├── 0167_two_sum_ii.cpp │ ├── 0168_excel_sheet_column_title.cpp │ ├── 0169_majority_element.cpp │ ├── 0172_factorial_trailling_zeros.cpp │ ├── 0189_rotate_array.cpp │ ├── 0190_reverse_bits.cpp │ ├── 0191_number_of_1_bits.cpp │ ├── 0198_house_robber.cpp │ ├── 0202_happy_numbers.cpp │ ├── 0203_remove_linked_list_elements.cpp │ ├── 0204_count_primes.cpp │ ├── 0205_isomorphic_strings.cpp │ ├── 0206_reverse_linked_list.cpp │ ├── 0217_contains_duplicate.cpp │ ├── 0219_contains_duplicate_ii.cpp │ ├── 0222_count_complete_tree_nodes.cpp │ ├── 0225_implement_stack_using_queues.cpp │ ├── 0226_invert_binary_tree.cpp │ ├── 0231_power_of_two.cpp │ ├── 0232_implement_queue_using_stacks.cpp │ ├── 0234_palindrome_linked_list.cpp │ ├── 0235_lowest_common_ancestor_of_bst.cpp │ ├── 0237_delete_node_in_a_linked_list.cpp │ ├── 0239_sliding_window_maximum.cpp │ ├── 0242_valid_anagram.cpp │ ├── 0257_binary_tree_paths.cpp │ ├── 0258_add_digits.cpp │ ├── 0263_ugly_number.cpp │ ├── 0268_missing_number.cpp │ ├── 0278_first_bad_version.cpp │ ├── 0283_move_zeroes.cpp │ ├── 0290_word_pattern.cpp │ ├── 0292_nim_game.cpp │ ├── 0299_bulls_and_cows.cpp │ ├── 0300_longest_increasing_subsequence.cpp │ ├── 0303_range_sum_query_immutable.cpp │ ├── 0326_power_of_three.cpp │ ├── 0342_power_of_four.cpp │ ├── 0344_reverse_string.cpp │ ├── 0345_reverse_vowels_of_a_string.cpp │ ├── 0349_intersection_of_two_arrays.cpp │ ├── 0350_intersection_of_two_arrays_ii.cpp │ ├── 0367_valid_perfect_square.cpp │ ├── 0371_sum_of_two_integers.cpp │ ├── 0374_guess_number_higher_or_lower.cpp │ ├── 0383_ransom_note.cpp │ ├── 0387_first_unique_character_in_a_string.cpp │ ├── 0389_find_the_difference.cpp │ ├── 0392_is_subsequence.cpp │ ├── 0401_binary_watch.cpp │ ├── 0404_sum_of_left_leaves.cpp │ ├── 0405_convert_a_number_to_hex.cpp │ ├── 0409_longest_palindrome.cpp │ ├── 0412_fizz_buzz.cpp │ ├── 0414_third_maximum_number.cpp │ ├── 0415_add_strings.cpp │ ├── 0434_number_of_segments_in_a_string.cpp │ ├── 0437_path_sum_iii.cpp │ ├── 0441_arranging_coins.cpp │ ├── 0443_string_compression.cpp │ ├── 0447_number_of_boomerangs.cpp │ ├── 0448_find_all_numbers_disappeared_in_an_array.cpp │ ├── 0453_minimum_moves_to_equal_array_elements.cpp │ ├── 0455_assign_cookies.cpp │ ├── 0459_repeated_substring_pattern.cpp │ ├── 0461_hamming_distance.cpp │ ├── 0463_island_perimeter.cpp │ ├── 0475_heaters.cpp │ ├── 0476_number_complement.cpp │ ├── 0482_license_key_formatting.cpp │ ├── 0485_max_consecutive_ones.cpp │ ├── 0492_construct_the_rectangle.cpp │ ├── 0496_next_greater_element_i.cpp │ ├── 0500_keyboard_row.cpp │ ├── 0501_find_mode_in_binary_search_tree.cpp │ ├── 0504_base_7.cpp │ ├── 0506_relative_ranks.cpp │ ├── 0507_perfect_number.cpp │ ├── 0509_fibonacci_number.cpp │ ├── 0520_detect_capital.cpp │ ├── 0530_minimum_absolute_difference_in_bst.cpp │ ├── 0532-k-diff-pairs-in-an-array.cpp │ ├── 0538-convert-bst-to-greater-tree.cpp │ ├── 0541-reverse-string-ii.cpp │ └── 0617_merge_two_binary_trees.cpp ├── st │ └── README.md ├── stack │ └── README.md ├── string │ └── README.md ├── sw │ └── README.md ├── tps │ └── README.md ├── tree │ ├── 0437-path-sum-iii.cpp │ └── README.md ├── trie │ └── README.md ├── ts │ └── README.md └── uf │ └── README.md ├── log ├── C++.txt └── compile-error.txt ├── python ├── decorator │ └── decorator.py ├── leetcode │ ├── 0530_minimum_absolute_difference_in_bst.py │ ├── 0532-k-diff-pairs-in-an-array.py │ ├── 0538-convert-bst-to-greater-tree.py │ └── 0541-reverse-string-ii.py └── thread │ ├── 01_hello_thread.py │ ├── 02_thread_lock.py │ └── 03_condition_objects.py ├── scripts ├── add-license-test-1.cpp ├── add-license-test-2.cpp ├── add-license.sh ├── git-commit-count.sh ├── install-opencv-python.sh └── license.txt └── src ├── 01_find_num.cpp ├── 02_replace_space.cpp ├── 03_print_list_tail_to_head.cpp ├── 04_construct_binary_tree.cpp ├── 05_queue_with_two_stack.cpp ├── 06_find_minimum_in_rotated_array.cpp ├── 07_fibonacci.cpp ├── 08_jump_floor_i.cpp ├── 09_jump_floor_ii.cpp ├── 10_rect_cover.cpp ├── 11_number_of_1.cpp ├── 12_power.cpp ├── 13_reorder_list.cpp ├── 14_find_kth_to_tail.cpp ├── 15_reverse_list.cpp ├── 16_merge_sorted_lists.cpp ├── 17_has_subtree.cpp ├── 18_mirror_of_binary_tree.cpp ├── 19_print_matrix.cpp ├── 20_min_in_stack.cpp ├── 21_stack_push_pop_order.cpp ├── 22_print_from_top_to_bottom.cpp ├── 23_sequence_of_bst.cpp ├── 26_convert_bst.cpp ├── 27_permutation.cpp ├── 29_more_than_half_num.cpp ├── Fibonacci.cpp ├── accumulate.cpp ├── add_two_numbers.cpp ├── assignment_operator.cpp ├── bfs.hpp ├── common_parent_in_tree.cpp ├── construct_binary_tree.cpp ├── continous_cards.cpp ├── continues_squence_with_sum.cpp ├── convert_binary_search_tree.cpp ├── copy_complex_list.cpp ├── delete_kth_node_from_end.cpp ├── delete_node_in_list.cpp ├── dfs.hpp ├── dices_probability.cpp ├── find_minimum_in_rotated_array.cpp ├── find_num.cpp ├── first_common_node.cpp ├── first_not_repeat_char.cpp ├── greatest_sum_of_subarray.cpp ├── inverse_pairs.cpp ├── k_least_numbers.cpp ├── kmp └── 1_kmp.cpp ├── knapsack ├── 1_knapsack.cpp ├── 1_knapsack_acwing.cpp ├── 2_knapsack.cpp ├── 2_knapsack_acwing.cpp ├── 3_knapsack_acwing_i.cpp ├── 4_knapscak_acwing_ii.cpp ├── 5_knapsack_acwing_iii.cpp ├── 6_knapsack_acwing.cpp ├── 7_knapsack_acwing.cpp └── 8_knapsack_acwing.cpp ├── last_num_in_circle.cpp ├── left_rotate_string.cpp ├── merge_sorted_lists.cpp ├── min_in_stack.cpp ├── mirror_of_binary_tree.cpp ├── more_than_half_number.cpp ├── number_of_1.cpp ├── number_of_1_in_binary.cpp ├── number_of_k.cpp ├── numbers_appear_once.cpp ├── path_in_tree.cpp ├── power.cpp ├── print_1_to_maximum_value.cpp ├── print_from_top_to_bottom.cpp ├── print_list_tail_to_head.cpp ├── print_matrix.cpp ├── queue_with_two_stack.cpp ├── quick_sort.cpp ├── quick_sort.hpp ├── reorder_array.cpp ├── replace_blank.cpp ├── reverse_list.cpp ├── reverse_words_in_sentence.cpp ├── sealed_class.cpp ├── selection_sort.hpp ├── sequence_of_bst.cpp ├── singleton.cpp ├── sort.cpp ├── sort ├── bubble.hpp ├── heap.hpp ├── heap_sort_link.hpp ├── main.cpp ├── merge.hpp ├── quick.hpp ├── shell.hpp ├── sort.dev ├── sort_link.hpp ├── straight_insert.hpp └── straight_select.hpp ├── sort_array_for_min_number.cpp ├── stack_push_pop_order.cpp ├── str_to_int.cpp ├── string_permutation.cpp ├── substructure_in_tree.cpp ├── tree_depth.cpp ├── two_numbers_with_sum.cpp ├── ugly_number.cpp ├── xfs.cpp └── zzz.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | *.layout 2 | *.exe 3 | *.o 4 | *.win -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 剑指 Offer 2 | - [背包问题](src/knapsack) 3 | - [排序](src/sort) 4 | - [MORE...](src) 5 | 6 | ## LeetCode 7 | - [数组](leetcode/array) 8 | - [动态规划](leetcode/dp) 9 | - [树](leetcode/tree) 10 | - [哈希表](leetcode/hash) 11 | - [深度优先搜素](leetcode/dfs) 12 | - [二分查找](leetcode/bs) 13 | - [贪心法](leetcode/greedy) 14 | - [双指针](leetcode/tps) 15 | - [宽度优先搜索](leetcode/bfs) 16 | - [栈](leetcode/stack) 17 | - [链表](leetcode/ll) 18 | - [排序](leetcode/sort) 19 | - [分治法](leetcode/dc) 20 | - [递归](leetcode/recursion) 21 | - [二叉搜索树](leetcode/bst) 22 | - [MORE...](leetcode) 23 | 24 | ## Boost 多线程编程 25 | - [基本线程](boost/01_basic_thread.cpp) 26 | - [中断线程](boostt/02_interrupt_thread.cpp) 27 | - [互斥信号量](boost/03_mutex.cpp) 28 | - [锁](boost/04_lock.cpp) 29 | - [条件变量](boost/05_condition_variable.cpp) 30 | - [MORE...](boost) 31 | 32 | ## 后记 33 | 本开源代码主要是记录个人刷题过程中的解题思路之用,也作为自己后续快速查找、翻阅、回忆解题之用, 34 | 可能方法并不是尽善尽美,会逐渐改进,也希望有心之人能够提出宝贵的改进意见, 35 | 我后续的解题思路、源码等也会持续更新到该仓库中, 36 | 更多详情[点击该链接](https://github.com/jiauzhang/algorithms)。 37 | -------------------------------------------------------------------------------- /algorithms.dev: -------------------------------------------------------------------------------- 1 | [Project] 2 | FileName=algorithms.dev 3 | Name=algorithms 4 | Type=1 5 | Ver=2 6 | ObjFiles= 7 | Includes= 8 | Libs= 9 | PrivateResource= 10 | ResourceIncludes= 11 | MakeIncludes= 12 | Compiler= 13 | CppCompiler= 14 | Linker= 15 | IsCpp=1 16 | Icon= 17 | ExeOutput= 18 | ObjectOutput= 19 | LogOutput= 20 | LogOutputEnabled=0 21 | OverrideOutput=0 22 | OverrideOutputName=algorithms.exe 23 | HostApplication= 24 | UseCustomMakefile=0 25 | CustomMakefile= 26 | CommandLine= 27 | Folders= 28 | IncludeVersionInfo=0 29 | SupportXPThemes=0 30 | CompilerSet=0 31 | CompilerSettings=0000000000000000001000000 32 | UnitCount=1 33 | 34 | [VersionInfo] 35 | Major=1 36 | Minor=0 37 | Release=0 38 | Build=0 39 | LanguageID=1033 40 | CharsetID=1252 41 | CompanyName= 42 | FileVersion=1.0.0.0 43 | FileDescription=Developed using the Dev-C++ IDE 44 | InternalName= 45 | LegalCopyright= 46 | LegalTrademarks= 47 | OriginalFilename= 48 | ProductName= 49 | ProductVersion=1.0.0.0 50 | AutoIncBuildNr=0 51 | SyncProduct=1 52 | 53 | [Unit2] 54 | FileName=src\knapsack\2_knapsack_acwing.cpp 55 | CompileCpp=1 56 | Folder= 57 | Compile=1 58 | Link=1 59 | Priority=1000 60 | OverrideBuildCmd=0 61 | BuildCmd= 62 | 63 | [Unit1] 64 | FileName=src\kmp\1_kmp.cpp 65 | CompileCpp=1 66 | Folder= 67 | Compile=1 68 | Link=1 69 | Priority=1000 70 | OverrideBuildCmd=0 71 | BuildCmd= 72 | 73 | -------------------------------------------------------------------------------- /attribute-test/argc_argv.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void print_string(char *str) 7 | { 8 | if (str) { 9 | while (*str != '\0') 10 | cout << *str++; 11 | 12 | cout << endl; 13 | } 14 | } 15 | 16 | int main(int argc, char **argv) 17 | { 18 | cout << "argc: " << argc << endl; 19 | 20 | if (argc == 1) { 21 | cout << "usage: PATH_TO_EXE param_1 param_2 ... param_n" << endl; 22 | return 1; 23 | } 24 | 25 | for (int i=0; i 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | char str[] = { 8 | "string 1;" 9 | "string 2;" 10 | "string 3;" 11 | }; 12 | 13 | cout << str << endl; 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /attribute-test/class_type_conversion.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/attribute-test/class_type_conversion.cpp -------------------------------------------------------------------------------- /attribute-test/constructor.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/attribute-test/constructor.cpp -------------------------------------------------------------------------------- /attribute-test/deque.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(int argc, char** argv) 7 | { 8 | deque dq; 9 | dq.push_back(1); 10 | dq.push_back(2); 11 | dq.push_back(3); 12 | dq.push_back(4); 13 | dq.push_back(5); 14 | 15 | deque dq_test; 16 | for (int i=0; i 2 | #include 3 | 4 | int main(int argc, char **argv) 5 | { 6 | char str[20] = "string test\n"; 7 | printf("%s", str); 8 | std::cout << "cout test\n" << std::endl; 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /attribute-test/rand.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/attribute-test/rand.cpp -------------------------------------------------------------------------------- /attribute-test/sizeof_strlen.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | char c1[] = {'a', 'b', '\0', 'd', 'e'}; 9 | char c2[] = {'a', 'b', '\0', 'd', 'e'}; 10 | 11 | char *c3 = "ab\0decvzvzxv"; 12 | char *c4 = "abcde"; 13 | 14 | cout << "sizeof(c1): " << sizeof(c1) << endl; 15 | cout << "strlen(c2): " << strlen(c2) << endl; 16 | 17 | cout << "sizeof(c3): " << sizeof(c3) << endl; 18 | cout << "strlen(c4): " << strlen(c4) << endl; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /attribute-test/static_and_array.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/attribute-test/static_and_array.cpp -------------------------------------------------------------------------------- /attribute-test/vector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | /* 7 | vector::reserve is used to alloc memory 8 | vector::resize is reset the occpied memory pointer 9 | */ 10 | 11 | int main(int argc, char** argv) { 12 | vector test; 13 | cout << "test size: " << test.size() << endl; 14 | cout << "test cap: " << test.capacity() << endl; 15 | 16 | test.reserve(10); 17 | 18 | cout << "test size: " << test.size() << endl; 19 | cout << "test cap: " << test.capacity() << endl; 20 | 21 | test.push_back(10); 22 | test.push_back(11); 23 | 24 | cout << "test size: " << test.size() << endl; 25 | cout << "test cap: " << test.capacity() << endl; 26 | 27 | test.resize(6); 28 | test.push_back(66); 29 | 30 | cout << "test size: " << test.size() << endl; 31 | cout << "test cap: " << test.capacity() << endl; 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /attribute-test/virtual.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/attribute-test/virtual.cpp -------------------------------------------------------------------------------- /attribute-test/virtual_derived.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/attribute-test/virtual_derived.cpp -------------------------------------------------------------------------------- /attribute-test/void_class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class VoidClass { 6 | // nothing 7 | }; 8 | 9 | class OneFuncClass { 10 | void foo() { cout << "something" << endl; } 11 | }; 12 | 13 | class VirtualClass { 14 | virtual foo() {} 15 | }; 16 | 17 | class NormalClass{ 18 | void foo() {} 19 | virtual vfoo() {} 20 | int num; 21 | }; 22 | 23 | int main() 24 | { 25 | cout << "VoidClass: " << sizeof(VoidClass) << endl; 26 | cout << "OneFuncClass: " << sizeof(OneFuncClass) << endl; 27 | cout << "VirtualClass: " << sizeof(VirtualClass) << endl; 28 | cout << "NormalClass: " << sizeof(NormalClass) << endl; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /boost/03_mutex.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | #include 19 | #include 20 | 21 | void wait(int seconds) 22 | { 23 | boost::this_thread::sleep(boost::posix_time::seconds(seconds)); 24 | } 25 | 26 | void show_info() 27 | { 28 | std::cout << "thread id: " 29 | << boost::this_thread::get_id() << std::endl; 30 | std::cout << "hardware cores: " 31 | << boost::thread::hardware_concurrency() << std::endl; 32 | } 33 | 34 | boost::mutex mutex; 35 | 36 | void thread() 37 | { 38 | show_info(); 39 | 40 | for (int i = 0; i < 5; ++i) 41 | { 42 | wait(1); 43 | mutex.lock(); 44 | std::cout << "Thread " << boost::this_thread::get_id() << ": " << i << std::endl; 45 | mutex.unlock(); 46 | } 47 | } 48 | 49 | int main() 50 | { 51 | show_info(); 52 | 53 | boost::thread t1(thread); 54 | boost::thread t2(thread); 55 | t1.join(); 56 | t2.join(); 57 | 58 | return 0; 59 | } -------------------------------------------------------------------------------- /boost/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | @echo "please specity the specific target" 3 | 4 | BOOST_LIB_COMMON = -lboost_system -lboost_thread 5 | 6 | 01_basic_thread: 01_basic_thread.cpp 7 | g++ -o 01_basic_thread.o $^ $(BOOST_LIB_COMMON) 8 | 9 | 02_interrupt_thread: 02_interrupt_thread.cpp 10 | g++ -o 02_interrupt_thread.o $^ $(BOOST_LIB_COMMON) 11 | 12 | 03_mutex: 03_mutex.cpp 13 | g++ -o 03_mutex.o $^ $(BOOST_LIB_COMMON) 14 | 15 | # -lpthread used for boost::timed_mutex 16 | LOCK_LIB = -lpthread $(BOOST_LIB_COMMON) 17 | 18 | 04_lock: 04_lock.cpp 19 | g++ -o 04_lock.o $^ $(LOCK_LIB) 20 | 21 | 05_condition_variable: 05_condition_variable.cpp 22 | g++ -o 05_condition_variable.o $^ $(BOOST_LIB_COMMON) 23 | 24 | 06_cooperation: 06_cooperation.cpp 25 | g++ -o 06_cooperation.o $^ $(BOOST_LIB_COMMON) -------------------------------------------------------------------------------- /compile.sh: -------------------------------------------------------------------------------- 1 | # ./compile.sh YOUR_SOURCE_FILE 2 | g++ -o build/algorithm.o $1 3 | -------------------------------------------------------------------------------- /leetcode/README.md: -------------------------------------------------------------------------------- 1 | ### LeetCode Solution 2 | - [Array](array) 3 | - [Dynamic Programming](dp) 4 | - [Math](math) 5 | - [String](string) 6 | - [Tree](tree) 7 | - [Hash Table](hash) 8 | - [Depth First Search](dfs) 9 | - [Binary Search](bs) 10 | - [Greedy](greedy) 11 | - [Two Pointers](tps) 12 | - [Breadth First Search](bfs) 13 | - [Stack](stack) 14 | - [Backtracking](bt) 15 | - [Design](design) 16 | - [Linked List](ll) 17 | - [Sort](sort) 18 | - [Bit Manipulation](bm) 19 | - [Graph](graph) 20 | - [Heap](heap) 21 | - [Union Find](uf) 22 | - [Sliding Window](sw) 23 | - [Divide and Conquer](dc) 24 | - [Trie](trie) 25 | - [Recursion](recursion) 26 | - [Ordered Map](om) 27 | - [Segment Tree](st) 28 | - [Queue](queue) 29 | - [Minimax](minimax) 30 | - [Binary Indexed Tree](bit) 31 | - [Random](random) 32 | - [Topological Sort](ts) 33 | - [Brainteaser](brainteaser) 34 | - [Geometry](geometry) 35 | - [Line Sweep](lw) 36 | - [Binary Search Tree](bst) 37 | - [Rejection Sampling](rejs) 38 | - [Reservoir Sampling](ress) 39 | - [Memoization](mem) -------------------------------------------------------------------------------- /leetcode/array/README.md: -------------------------------------------------------------------------------- 1 | ### Array 2 | - [0001_two_sum](../src/0001_two_sum.cpp) 3 | - [0015_3sum](./0015_3sum.cpp) 4 | - [0016_3sum_closest](./0016_3sum_closest.cpp) 5 | - [0026_remove_duplicates_from_sorted_array](../src/0026_remove_duplicates_from_sorted_array.cpp) 6 | - [0027_remove_element](../src/0027_remove_element.cpp) 7 | - [0031_next_permutation](./0031_next_permutation.cpp) 8 | - [0035_search_insert_position](../src/0035_search_insert_position.cpp) 9 | - [0045_jump_game_II](../src/0045_jump_game_II.cpp) 10 | - [0053_maximum_subarray](../src/0053_maximum_subarray.cpp) 11 | - [0055_jump_game](../src/0055_jump_game.cpp) 12 | - [0066_plus_one](../src/0066_plus_one.cpp) 13 | - [0088_merge_sorted_array](../src/0088_merge_sorted_array.cpp) 14 | - [0106_binary_tree_postorder_traversal](../src/0106_binary_tree_postorder_traversal.cpp) 15 | - [0118_pascals_triangle](../src/0118_pascals_triangle.cpp) 16 | - [0119_pascals_triangle_ii](../src/0119_pascals_triangle_ii.cpp) 17 | - [0121_best_time_to_buy_and_sell_stock](../src/0121_best_time_to_buy_and_sell_stock.cpp) 18 | - [0122_best_time_to_buy_and_sell_stock_ii](../src/0122_best_time_to_buy_and_sell_stock_ii.cpp) 19 | - [0167_two_sum_ii](../src/0167_two_sum_ii.cpp) 20 | - [0169_majority_element](../src/0169_majority_element.cpp) 21 | - [0189_rotate_array](../src/0189_rotate_array.cpp) 22 | - [0217_contains_duplicate](../src/0217_contains_duplicate.cpp) 23 | - [0219_contains_duplicate_ii](../src/0219_contains_duplicate_ii.cpp) 24 | - [0268_missing_number](../src/0268_missing_number.cpp) 25 | - [0283_move_zeroes](../src/0283_move_zeroes.cpp) 26 | - [0414_third_maximum_number](../src/0414_third_maximum_number.cpp) 27 | - [0448_find_all_numbers_disappeared_in_an_array](../src/0448_find_all_numbers_disappeared_in_an_array.cpp) 28 | - [0485_max_consecutive_ones](../src/0485_max_consecutive_ones.cpp) 29 | - [0509_fibonacci_number](../src/0509_fibonacci_number.cpp) -------------------------------------------------------------------------------- /leetcode/bfs/README.md: -------------------------------------------------------------------------------- 1 | ### Breadth First Search 2 | - [0101_symmetric_tree](../src/0101_symmetric_tree.cpp) 3 | - [0107_binary_tree_level_order_traversal_ii](../src/0107_binary_tree_level_order_traversal_ii.cpp) 4 | - [0111_minimum_depth_of_binary_tree](../src/0111_minimum_depth_of_binary_tree.cpp) -------------------------------------------------------------------------------- /leetcode/bit/README.md: -------------------------------------------------------------------------------- 1 | ### Binary Indexed Tree -------------------------------------------------------------------------------- /leetcode/bm/README.md: -------------------------------------------------------------------------------- 1 | ### Bit Manipulation 2 | - [0136_single_number](../src/0136_single_number.cpp) 3 | - [0169_majority_element](../src/0169_majority_element.cpp) 4 | - [0190_reverse_bits](../src/0190_reverse_bits.cpp) 5 | - [0191_number_of_1_bits](../src/0191_number_of_1_bits.cpp) 6 | - [0231_power_of_two](../src/0231_power_of_two.cpp) 7 | - [0268_missing_number](../src/0268_missing_number.cpp) 8 | - [0342_power_of_four](../src/0342_power_of_four.cpp) 9 | - [0371_sum_of_two_integers](../src/0371_sum_of_two_integers.cpp) 10 | - [0389_find_the_difference](../src/0389_find_the_difference.cpp) 11 | - [0401_binary_watch](../src/0401_binary_watch.cpp) 12 | - [0405_convert_a_number_to_hex](../src/0405_convert_a_number_to_hex.cpp) 13 | - [0461_hamming_distance](../src/0461_hamming_distance.cpp) 14 | - [0476_number_complement](../src/0476_number_complement.cpp) -------------------------------------------------------------------------------- /leetcode/brainteaser/README.md: -------------------------------------------------------------------------------- 1 | ### Brainteaser 2 | - [0292_nim_game](../src/0292_nim_game.cpp) -------------------------------------------------------------------------------- /leetcode/bs/README.md: -------------------------------------------------------------------------------- 1 | ### Binary Search 2 | - [0033_search_in_rotated_sorted_array](./0033_search_in_rotated_sorted_array.cpp) 3 | - [0035_search_insert_position](../src/0035_search_insert_position.cpp) 4 | - [0069_sqrtx](../src/0069_sqrtx.cpp) 5 | - [0167_two_sum_ii](../src/0167_two_sum_ii.cpp) 6 | - [0222_count_complete_tree_nodes](../src/0222_count_complete_tree_nodes.cpp) 7 | - [0278_first_bad_version](../src/0278_first_bad_version.cpp) 8 | - [0300_longest_increasing_subsequence](../src/0300_longest_increasing_subsequence.cpp) 9 | - [0349_intersection_of_two_arrays](../src/0349_intersection_of_two_arrays.cpp) 10 | - [0350_intersection_of_two_arrays_ii](../src/0350_intersection_of_two_arrays_ii.cpp) 11 | - [0367_valid_perfect_square](../src/0367_valid_perfect_square.cpp) 12 | - [0374_guess_number_higher_or_lower](../src/0374_guess_number_higher_or_lower.cpp) 13 | - [0392_is_subsequence](../src/0392_is_subsequence.cpp) 14 | - [0441_arranging_coins](../src/0441_arranging_coins.cpp) 15 | - [0475_heaters](../src/0475_heaters.cpp) -------------------------------------------------------------------------------- /leetcode/bst/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/leetcode/bst/README.md -------------------------------------------------------------------------------- /leetcode/bt/README.md: -------------------------------------------------------------------------------- 1 | ### Backtracking 2 | - [0017_letter_combinations_of_a_phone_numbe](./0017_letter_combinations_of_a_phone_numbe.cpp) 3 | - [0022_generate_parentheses](./0022_generate_parentheses.cpp) 4 | - [0401_binary_watch](../src/0401_binary_watch.cpp) -------------------------------------------------------------------------------- /leetcode/dc/README.md: -------------------------------------------------------------------------------- 1 | ### Divide and Conquer 2 | - [0053_maximum_subarray](../src/0053_maximum_subarray.cpp) 3 | - [0169_majority_element](../src/0169_majority_element.cpp) -------------------------------------------------------------------------------- /leetcode/design/README.md: -------------------------------------------------------------------------------- 1 | ### Design 2 | - [0155_min_stack](../src/0155_min_stack.cpp) 3 | - [0225_implement_stack_using_queues](../src/0225_implement_stack_using_queues.cpp) 4 | - [0232_implement_queue_using_stacks](../src/0232_implement_queue_using_stacks.cpp) -------------------------------------------------------------------------------- /leetcode/dfs/README.md: -------------------------------------------------------------------------------- 1 | ### Depth First Search 2 | - [0100_same_tree](../src/0100_same_tree.cpp) 3 | - [0101_symmetric_tree](../src/0101_symmetric_tree.cpp) 4 | - [0104_maximum_depth_of_binary_tree](../src/0104_maximum_depth_of_binary_tree.cpp) 5 | - [0106_binary_tree_postorder_traversal](../src/0106_binary_tree_postorder_traversal.cpp) 6 | - [0108_convert_sorted_array_to_binary_search_tree](../src/0108_convert_sorted_array_to_binary_search_tree.cpp) 7 | - [0110_balanced_binary_tree](../src/0110_balanced_binary_tree.cpp) 8 | - [0111_minimum_depth_of_binary_tree](../src/0111_minimum_depth_of_binary_tree.cpp) 9 | - [0112_path_sum](../src/0112_path_sum.cpp) 10 | - [0257_binary_tree_paths](../src/0257_binary_tree_paths.cpp) -------------------------------------------------------------------------------- /leetcode/difficulty/easy.md: -------------------------------------------------------------------------------- 1 | ## Easy 2 | 3 | ## 二零一九 4 | 5 | ### 十月(59道) 6 | - [第一周(13道)](../month/2019-10-12.md) 7 | - [第二周(18道)](../month/2019-10-19.md) 8 | - [第三周(13道)](../month/2019-10-26.md) 9 | - [第四周(15道)](../month/2019-10-31.md) 10 | 11 | ### 十一月(34道) 12 | - [第一周(20道)](../month/2019-11-09.md) 13 | - [第二周(06道)](../month/2019-11-16.md) 14 | - [第三周(08道)](../month/2019-11-23.md) 15 | 16 | ### 十二月(07道) 17 | - [第三周(07道)](../month/2019-12-22.md) 18 | 19 | ## 二零二零 20 | 21 | - [第X周(00道)](../month/2020.md) -------------------------------------------------------------------------------- /leetcode/difficulty/hard.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/leetcode/difficulty/hard.md -------------------------------------------------------------------------------- /leetcode/difficulty/medium.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/leetcode/difficulty/medium.md -------------------------------------------------------------------------------- /leetcode/dp/0416-partition-equal-subset-sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2021 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * 解题思路: 20 | * 本题的核心思路是把该问题转化为 0-1 背包问题,题目目标是把有限数组分成两份 21 | * 使得两个组合各自的和相等,所以如果有可能存在,那么只需要找到一个组合就行 22 | * 即在有限的可选物品中选择或不选择某个物品达到和为某个数,即背包问题 23 | */ 24 | 25 | class Solution { 26 | public: 27 | bool canPartition(vector& nums) { 28 | int sum = 0, max_num = INT_MIN; 29 | for (auto n: nums) { 30 | sum += n; 31 | max_num = max(max_num, n); 32 | } 33 | 34 | int half = sum / 2; 35 | if ((sum & 0x01) || (max_num > half)) 36 | return false; 37 | 38 | if (max_num == half) 39 | return true; 40 | 41 | vector buffer(half+1, false); 42 | 43 | if (nums[0] <= half) 44 | buffer[nums[0]] = true; 45 | 46 | for (int i=1; i=nums[i]; j--) { 48 | if (!buffer[j]) 49 | buffer[j] = buffer[j-nums[i]]; 50 | } 51 | } 52 | 53 | return buffer[half]; 54 | } 55 | }; -------------------------------------------------------------------------------- /leetcode/dp/README.md: -------------------------------------------------------------------------------- 1 | ### Dynamic Programming 2 | - [0053_maximum_subarray](../src/0053_maximum_subarray.cpp) 3 | - [0070_climbing_stairs](../src/0070_climbing_stairs.cpp) 4 | - [0121_best_time_to_buy_and_sell_stock](../src/0121_best_time_to_buy_and_sell_stock.cpp) 5 | - [0198_house_robber](../src/0198_house_robber.cpp) 6 | - [0300_longest_increasing_subsequence](../src/0300_longest_increasing_subsequence.cpp) 7 | - [0303_range_sum_query_immutable](../src/0303_range_sum_query_immutable.cpp) 8 | - [0392_is_subsequence](../src/0392_is_subsequence.cpp) 9 | - [0416-partition-equal-subset-sum](./0416-partition-equal-subset-sum.cpp) -------------------------------------------------------------------------------- /leetcode/geometry/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/leetcode/geometry/README.md -------------------------------------------------------------------------------- /leetcode/graph/README.md: -------------------------------------------------------------------------------- 1 | ### Graph 2 | - [0399_evaluate_division](./0399_evaluate_division.cpp) -------------------------------------------------------------------------------- /leetcode/greedy/0011_container_with_most_water.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2021 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | https://leetcode-cn.com/problems/container-with-most-water 20 | 题目描述: 21 | 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 22 | 在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) 23 | 找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水 24 | 25 | 解题思路: 26 | 根据题意,容器大小由最小边和两个边的距离共同决定,于是从两边开始查找 27 | 即先把两边的距离做的最大,然后再移动短的边,因为假如移动长的边 28 | 那么最终的容积必定会减小,而移动短的边是肯能减小也可能增加 29 | 这就是贪婪算法! 30 | */ 31 | 32 | class Solution { 33 | public: 34 | int maxArea(vector& height) { 35 | int left = 0, right = height.size() - 1; 36 | int max, temp; 37 | 38 | max = min(height[left], height[right]) * right; 39 | 40 | while (left < right) { 41 | if (height[right] > height[left]) { 42 | temp = height[left] * (right - left); 43 | left++; 44 | } else { 45 | temp = height[right] * (right - left); 46 | right--; 47 | } 48 | 49 | if (max < temp) 50 | max = temp; 51 | } 52 | 53 | return max; 54 | } 55 | }; -------------------------------------------------------------------------------- /leetcode/greedy/README.md: -------------------------------------------------------------------------------- 1 | ### Greedy 2 | - [0011_container_with_most_water](./0011_container_with_most_water.cpp) 3 | - [0045_jump_game_II](../src/0045_jump_game_II.cpp) 4 | - [0055_jump_game](../src/0055_jump_game.cpp) 5 | - [0122_best_time_to_buy_and_sell_stock_ii](../src/0122_best_time_to_buy_and_sell_stock_ii.cpp) 6 | - [0392_is_subsequence](../src/0392_is_subsequence.cpp) 7 | - [0406-queue-reconstruction-by-height](./0406-queue-reconstruction-by-height.cpp) 8 | - [0455_assign_cookies](../src/0455_assign_cookies.cpp) -------------------------------------------------------------------------------- /leetcode/hash/0003_longest_substring_without_repeating_characters.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2021 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | https://leetcode-cn.com/problems/longest-substring-without-repeating-characters 20 | 题目描述: 21 | 给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。 22 | 23 | 示例 1: 24 | 输入: s = "abcabcbb" 25 | 输出: 3 26 | 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3 27 | 28 | 解题思路: 29 | 因为要查找不重复的子串,所以需要能快速查找一个字符是否出现过,又因为字符有限多 30 | 故使用固定长度数组来做哈希表,然后就是用两个指针来指示出字串的位置 31 | 如果快指针对应的字符出现过,那么慢指针直接跳到上次出现的位置的下一个位置即可 32 | */ 33 | 34 | class Solution { 35 | public: 36 | int lengthOfLongestSubstring(string s) { 37 | if (s.size() == 0) 38 | return 0; 39 | 40 | int next[256] = {0}; 41 | int left = 0, right = 1; 42 | int ml = 1; 43 | 44 | next[s[0]] = 0; 45 | while (right < s.size()) { 46 | char offset = s[right]; 47 | if (next[offset] >= left && s[right] == s[next[offset]]) { 48 | left = next[offset] + 1; 49 | } 50 | next[offset] = right; 51 | right++; 52 | if (right - left > ml) 53 | ml = right - left; 54 | } 55 | 56 | return ml; 57 | } 58 | }; -------------------------------------------------------------------------------- /leetcode/hash/README.md: -------------------------------------------------------------------------------- 1 | ### Hash Table 2 | - [0001_two_sum](../src/0001_two_sum.cpp) 3 | - [0003_longest_substring_without_repeating_characters](./0003_longest_substring_without_repeating_characters.cpp) 4 | - [0136_single_number](../src/0136_single_number.cpp) 5 | - [0149_max_points_on_a_line](../src/0149_max_points_on_a_line.cpp) 6 | - [0202_happy_numbers](../src/0202_happy_numbers.cpp) 7 | - [0204_count_primes](../src/0204_count_primes.cpp) 8 | - [0205_isomorphic_strings](../src/0205_isomorphic_strings.cpp) 9 | - [0217_contains_duplicate](../src/0217_contains_duplicate.cpp) 10 | - [0219_contains_duplicate_ii](../src/0219_contains_duplicate_ii.cpp) 11 | - [0242_valid_anagram](../src/0242_valid_anagram.cpp) 12 | - [0290_word_pattern](../src/0290_word_pattern.cpp) 13 | - [0299_bulls_and_cows](../src/0299_bulls_and_cows.cpp) 14 | - [0349_intersection_of_two_arrays](../src/0349_intersection_of_two_arrays.cpp) 15 | - [0350_intersection_of_two_arrays_ii](../src/0350_intersection_of_two_arrays_ii.cpp) 16 | - [0387_first_unique_character_in_a_string](../src/0387_first_unique_character_in_a_string.cpp) 17 | - [0389_find_the_difference](../src/0389_find_the_difference.cpp) 18 | - [0409_longest_palindrome](../src/0409_longest_palindrome.cpp) 19 | - [0447_number_of_boomerangs](../src/0447_number_of_boomerangs.cpp) 20 | - [0463_island_perimeter](../src/0463_island_perimeter.cpp) 21 | - [0500_keyboard_row](../src/0500_keyboard_row.cpp) 22 | - [0506_relative_ranks](../src/0506_relative_ranks.cpp) -------------------------------------------------------------------------------- /leetcode/heap/README.md: -------------------------------------------------------------------------------- 1 | ### Heap 2 | - [0239_sliding_window_maximum](../src/0239_sliding_window_maximum.cpp) -------------------------------------------------------------------------------- /leetcode/ll/README.md: -------------------------------------------------------------------------------- 1 | ### Linked List 2 | - [0021_merge_two_sorted_lists](../src/0021_merge_two_sorted_lists.cpp) 3 | - [0083_remove_duplicates_from_sorted_list](../src/0083_remove_duplicates_from_sorted_list.cpp) 4 | - [0141_linked_list_cycle](../src/0141_linked_list_cycle.cpp) 5 | - [0147_insertion_sort_list](../src/0147_insertion_sort_list.cpp) 6 | - [0148_sort_list](../src/0148_sort_list.cpp) 7 | - [0160_intersection_of_two_linked_lists](../src/0160_intersection_of_two_linked_lists.cpp) 8 | - [0203_remove_linked_list_elements](../src/0203_remove_linked_list_elements.cpp) 9 | - [0206_reverse_linked_list](../src/0206_reverse_linked_list.cpp) 10 | - [0234_palindrome_linked_list](../src/0234_palindrome_linked_list.cpp) 11 | - [0237_delete_node_in_a_linked_list](../src/0237_delete_node_in_a_linked_list.cpp) -------------------------------------------------------------------------------- /leetcode/lw/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/leetcode/lw/README.md -------------------------------------------------------------------------------- /leetcode/math/README.md: -------------------------------------------------------------------------------- 1 | ### Math 2 | - [0007_reverse_integer](../src/0007_reverse_integer.cpp) 3 | - [0009_palindrome_number](../src/0009_palindrome_number.cpp) 4 | - [0013_roman_to_integer](../src/0013_roman_to_integer.cpp) 5 | - [0067_add_binary](../src/0067_add_binary.cpp) 6 | - [0069_sqrtx](../src/0069_sqrtx.cpp) 7 | - [0149_max_points_on_a_line](../src/0149_max_points_on_a_line.cpp) 8 | - [0168_excel_sheet_column_title](../src/0168_excel_sheet_column_title.cpp) 9 | - [0172_factorial_trailling_zeros](../src/0172_factorial_trailling_zeros.cpp) 10 | - [0202_happy_numbers](../src/0202_happy_numbers.cpp) 11 | - [0204_count_primes](../src/0204_count_primes.cpp) 12 | - [0231_power_of_two](../src/0231_power_of_two.cpp) 13 | - [0258_add_digits](../src/0258_add_digits.cpp) 14 | - [0263_ugly_number](../src/0263_ugly_number.cpp) 15 | - [0268_missing_number](../src/0268_missing_number.cpp) 16 | - [0326_power_of_three](../src/0326_power_of_three.cpp) 17 | - [0367_valid_perfect_square](../src/0367_valid_perfect_square.cpp) 18 | - [0441_arranging_coins](../src/0441_arranging_coins.cpp) 19 | - [0453_minimum_moves_to_equal_array_elements](../src/0453_minimum_moves_to_equal_array_elements.cpp) 20 | - [0507_perfect_number](../src/0507_perfect_number.cpp) -------------------------------------------------------------------------------- /leetcode/mem/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/leetcode/mem/README.md -------------------------------------------------------------------------------- /leetcode/minimax/README.md: -------------------------------------------------------------------------------- 1 | ### Minimax 2 | - [0292_nim_game](../src/0292_nim_game.cpp) -------------------------------------------------------------------------------- /leetcode/month/2019-10-12.md: -------------------------------------------------------------------------------- 1 | ## First Week 2 | - [0001_two_sum](../src/0001_two_sum.cpp) 3 | - [0007_reverse_integer](../src/0007_reverse_integer.cpp) 4 | - [0009_palindrome_number](../src/0009_palindrome_number.cpp) 5 | - [0013_roman_to_integer](../src/0013_roman_to_integer.cpp) 6 | - [0014_longest_common_prefix](../src/0014_longest_common_prefix.cpp) 7 | - [0021_merge_two_sorted_lists](../src/0021_merge_two_sorted_lists.cpp) 8 | - [0020_valid_parentheses](../src/0020_valid_parentheses.cpp) 9 | - [0026_remove_duplicates_from_sorted_array](../src/0026_remove_duplicates_from_sorted_array.cpp) 10 | - [0027_remove_element](../src/0027_remove_element.cpp) 11 | - [0028_implement_strstr](../src/0028_implement_strstr.cpp) 12 | - [0038_count_and_say](../src/0038_count_and_say.cpp) 13 | - [0035_search_insert_position](../src/0035_search_insert_position.cpp) 14 | - [0053_maximum_subarray](../src/0053_maximum_subarray.cpp) -------------------------------------------------------------------------------- /leetcode/month/2019-10-19.md: -------------------------------------------------------------------------------- 1 | ## Second Week 2 | - [0058_length_of_last_word](../src/0058_length_of_last_word.cpp) 3 | - [0066_plus_one](../src/0066_plus_one.cpp) 4 | - [0067_add_binary](../src/0067_add_binary.cpp) 5 | - [0069_sqrtx](../src/0069_sqrtx.cpp) 6 | - [0070_climbing_stairs](../src/0070_climbing_stairs.cpp) 7 | - [0083_remove_duplicates_from_sorted_list](../src/0083_remove_duplicates_from_sorted_list.cpp) 8 | - [0088_merge_sorted_array](../src/0088_merge_sorted_array.cpp) 9 | - [0100_same_tree](../src/0100_same_tree.cpp) 10 | - [0101_symmetric_tree](../src/0101_symmetric_tree.cpp) 11 | - [0104_maximum_depth_of_binary_tree](../src/0104_maximum_depth_of_binary_tree.cpp) 12 | - [0107_binary_tree_level_order_traversal_ii](../src/0107_binary_tree_level_order_traversal_ii.cpp) 13 | - [0108_convert_sorted_array_to_binary_search_tree](../src/0108_convert_sorted_array_to_binary_search_tree.cpp) 14 | - [0110_balanced_binary_tree](../src/0110_balanced_binary_tree.cpp) 15 | - [0111_minimum_depth_of_binary_tree](../src/0111_minimum_depth_of_binary_tree.cpp) 16 | - [0112_path_sum](../src/0112_path_sum.cpp) 17 | - [0118_pascals_triangle](../src/0118_pascals_triangle.cpp) 18 | - [0119_pascals_triangle_ii](../src/0119_pascals_triangle_ii.cpp) 19 | - [0121_best_time_to_buy_and_sell_stock](../src/0121_best_time_to_buy_and_sell_stock.cpp) -------------------------------------------------------------------------------- /leetcode/month/2019-10-26.md: -------------------------------------------------------------------------------- 1 | ## Third Week 2 | - [0122_best_time_to_buy_and_sell_stock_ii](../src/0122_best_time_to_buy_and_sell_stock_ii.cpp) 3 | - [0125_valid_palindrome](../src/0125_valid_palindrome.cpp) 4 | - [0136_single_number](../src/0136_single_number.cpp) 5 | - [0141_linked_list_cycle](../src/0141_linked_list_cycle.cpp) 6 | - [0155_min_stack](../src/0155_min_stack.cpp) 7 | - [0160_intersection_of_two_linked_lists](../src/0160_intersection_of_two_linked_lists.cpp) 8 | - [0167_two_sum_ii](../src/0167_two_sum_ii.cpp) 9 | - [0168_excel_sheet_column_title](../src/0168_excel_sheet_column_title.cpp) 10 | - [0169_majority_element](../src/0169_majority_element.cpp) 11 | - [0172_factorial_trailling_zeros](../src/0172_factorial_trailling_zeros.cpp) 12 | - [0189_rotate_array](../src/0189_rotate_array.cpp) 13 | - [0190_reverse_bits](../src/0190_reverse_bits.cpp) 14 | - [0191_number_of_1_bits](../src/0191_number_of_1_bits.cpp) -------------------------------------------------------------------------------- /leetcode/month/2019-10-31.md: -------------------------------------------------------------------------------- 1 | ## Fourth Week 2 | - [0198_house_robber](../src/0198_house_robber.cpp) 3 | - [0202_happy_numbers](../src/0202_happy_numbers.cpp) 4 | - [0203_remove_linked_list_elements](../src/0203_remove_linked_list_elements.cpp) 5 | - [0204_count_primes](../src/0204_count_primes.cpp) 6 | - [0205_isomorphic_strings](../src/0205_isomorphic_strings.cpp) 7 | - [0206_reverse_linked_list](../src/0206_reverse_linked_list.cpp) 8 | - [0217_contains_duplicate](../src/0217_contains_duplicate.cpp) 9 | - [0219_contains_duplicate_ii](../src/0219_contains_duplicate_ii.cpp) 10 | - [0225_implement_stack_using_queues](../src/0225_implement_stack_using_queues.cpp) 11 | - [0226_invert_binary_tree](../src/0226_invert_binary_tree.cpp) 12 | - [0231_power_of_two](../src/0231_power_of_two.cpp) 13 | - [0232_implement_queue_using_stacks](../src/0232_implement_queue_using_stacks.cpp) 14 | - [0234_palindrome_linked_list](../src/0234_palindrome_linked_list.cpp) 15 | - [0235_lowest_common_ancestor_of_a_binary_search_tree](../src/0235_lowest_common_ancestor_of_bst.cpp) 16 | - [0237_delete_node_in_a_linked_list](../src/0237_delete_node_in_a_linked_list.cpp) -------------------------------------------------------------------------------- /leetcode/month/2019-11-09.md: -------------------------------------------------------------------------------- 1 | ## First Week 2 | - [0242_valid_anagram](../src/0242_valid_anagram.cpp) 3 | - [0257_binary_tree_paths](../src/0257_binary_tree_paths.cpp) 4 | - [0258_add_digits](../src/0258_add_digits.cpp) 5 | - [0263_ugly_number](../src/0263_ugly_number.cpp) 6 | - [0268_missing_number](../src/0268_missing_number.cpp) 7 | - [0278_first_bad_version](../src/0278_first_bad_version.cpp) 8 | - [0290_word_pattern](../src/0290_word_pattern.cpp) 9 | - [0292_nim_game](../src/0292_nim_game.cpp) 10 | - [0299_bulls_and_cows](../src/0299_bulls_and_cows.cpp) 11 | - [0303_range_sum_query_immutable](../src/0303_range_sum_query_immutable.cpp) 12 | - [0326_power_of_three](../src/0326_power_of_three.cpp) 13 | - [0342_power_of_four](../src/0342_power_of_four.cpp) 14 | - [0344_reverse_string](../src/0344_reverse_string.cpp) 15 | - [0345_reverse_vowels_of_a_string](../src/0345_reverse_vowels_of_a_string.cpp) 16 | - [0349_intersection_of_two_arrays](../src/0349_intersection_of_two_arrays.cpp) 17 | - [0350_intersection_of_two_arrays_ii](../src/0350_intersection_of_two_arrays_ii.cpp) 18 | - [0367_valid_perfect_square](../src/0367_valid_perfect_square.cpp) 19 | - [0371_sum_of_two_integers](../src/0371_sum_of_two_integers.cpp) 20 | - [0374_guess_number_higher_or_lower](../src/0374_guess_number_higher_or_lower.cpp) 21 | - [0383_ransom_note](../src/0383_ransom_note.cpp) -------------------------------------------------------------------------------- /leetcode/month/2019-11-16.md: -------------------------------------------------------------------------------- 1 | ## Second Week 2 | - [0387_first_unique_character_in_a_string](../src/0387_first_unique_character_in_a_string.cpp) 3 | - [0389_find_the_difference](../src/0389_find_the_difference.cpp) 4 | - [0392_is_subsequence](../src/0392_is_subsequence.cpp) 5 | - [0401_binary_watch](../src/0401_binary_watch.cpp) 6 | - [0404_sum_of_left_leaves](../src/0404_sum_of_left_leaves.cpp) 7 | - [0405_convert_a_number_to_hex](../src/0405_convert_a_number_to_hex.cpp) -------------------------------------------------------------------------------- /leetcode/month/2019-11-23.md: -------------------------------------------------------------------------------- 1 | ### Third Week 2 | - [0409_longest_palindrome](../src/0409_longest_palindrome.cpp) 3 | - [0412_fizz_buzz](../src/0412_fizz_buzz.cpp) 4 | - [0414_third_maximum_number](../src/0414_third_maximum_number.cpp) 5 | - [0415_add_strings](../src/0415_add_strings.cpp) 6 | - [0434_number_of_segments_in_a_string](../src/0434_number_of_segments_in_a_string.cpp) 7 | - [0437_path_sum_iii](../src/0437_path_sum_iii.cpp) 8 | - [0441_arranging_coins](../src/0441_arranging_coins.cpp) 9 | - [0443_string_compression](../src/0443_string_compression.cpp) -------------------------------------------------------------------------------- /leetcode/month/2019-12-22.md: -------------------------------------------------------------------------------- 1 | - [0447_number_of_boomerangs](../src/0447_number_of_boomerangs.cpp) 2 | - [0448_find_all_numbers_disappeared_in_an_array](../src/0448_find_all_numbers_disappeared_in_an_array.cpp) 3 | - [0453_minimum_moves_to_equal_array_elements](../src/0453_minimum_moves_to_equal_array_elements.cpp) 4 | - [0455_assign_cookies](../src/0455_assign_cookies.cpp) 5 | - [0459_repeated_substring_pattern](../src/0459_repeated_substring_pattern.cpp) 6 | - [0461_hamming_distance](../src/0461_hamming_distance.cpp) 7 | - [0463_island_perimeter](../src/0463_island_perimeter.cpp) -------------------------------------------------------------------------------- /leetcode/month/2020.md: -------------------------------------------------------------------------------- 1 | - [0475_heaters](../src/0475_heaters.cpp) 2 | - [0476_number_complement](../src/0476_number_complement.cpp) 3 | - [0482_license_key_formatting](../src/0482_license_key_formatting.cpp) 4 | - [0485_max_consecutive_ones](../src/0485_max_consecutive_ones.cpp) 5 | - [0492_construct_the_rectangle](../src/0492_construct_the_rectangle.cpp) 6 | - [0501_find_mode_in_binary_search_tree](../src/0501_find_mode_in_binary_search_tree.cpp) 7 | - [0504_base_7](../src/0504_base_7.cpp) 8 | - [0506_relative_ranks](../src/0506_relative_ranks.cpp) 9 | - [0507_perfect_number](../src/0507_perfect_number.cpp) 10 | - [0509_fibonacci_number](../src/0509_fibonacci_number.cpp) 11 | - [0520_detect_capital](../src/0520_detect_capital.cpp) 12 | - [0530_minimum_absolute_difference_in_bst](../src/0530_minimum_absolute_difference_in_bst.cpp) 13 | - [0532-k-diff-pairs-in-an-array](../src/0532-k-diff-pairs-in-an-array.cpp) 14 | - [0538-convert-bst-to-greater-tree](../src/0538-convert-bst-to-greater-tree.cpp) 15 | - [0541-reverse-string-ii](../src/0541-reverse-string-ii.cpp) -------------------------------------------------------------------------------- /leetcode/om/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/leetcode/om/README.md -------------------------------------------------------------------------------- /leetcode/queue/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/leetcode/queue/README.md -------------------------------------------------------------------------------- /leetcode/random/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/leetcode/random/README.md -------------------------------------------------------------------------------- /leetcode/recursion/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/leetcode/recursion/README.md -------------------------------------------------------------------------------- /leetcode/rejs/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/leetcode/rejs/README.md -------------------------------------------------------------------------------- /leetcode/ress/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/leetcode/ress/README.md -------------------------------------------------------------------------------- /leetcode/sort/README.md: -------------------------------------------------------------------------------- 1 | ### Sort 2 | - [0147_insertion_sort_list](../src/0147_insertion_sort_list.cpp) 3 | - [0148_sort_list](../src/0148_sort_list.cpp) 4 | - [0242_valid_anagram](../src/0242_valid_anagram.cpp) 5 | - [0349_intersection_of_two_arrays](../src/0349_intersection_of_two_arrays.cpp) 6 | - [0350_intersection_of_two_arrays_ii](../src/0350_intersection_of_two_arrays_ii.cpp) -------------------------------------------------------------------------------- /leetcode/src/0007_reverse_integer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/reverse-integer 20 | * 题目描述: 21 | * 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。 22 | * 23 | * 示例 1: 24 | * 输入: 123 25 | * 输出: 321 26 | * 27 | * 示例 2: 28 | * 输入: -123 29 | * 输出: -321 30 | * 31 | * 示例 3: 32 | * 输入: 120 33 | * 输出: 21 34 | * 35 | * 注意: 36 | * 假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231, 231 − 1]。 37 | * 请根据这个假设,如果反转后整数溢出那么就返回 0。 38 | * 39 | * 解题思路: 40 | * 1. 逐个整数位取出,然后构造新的数即可 41 | * 2. 注意事项是检查是否会超出最大最小值限制 42 | */ 43 | 44 | class Solution { 45 | public: 46 | int reverse(int x) { 47 | int res = 0; 48 | while (x) { 49 | int remainder = x; 50 | x /= 10; 51 | remainder -= x * 10; 52 | /* 边界条件 */ 53 | if (res > INT_MAX / 10 || res < INT_MIN / 10) { 54 | res = 0; 55 | break; 56 | } 57 | 58 | res *= 10; 59 | res += remainder; 60 | } 61 | 62 | return res; 63 | } 64 | }; -------------------------------------------------------------------------------- /leetcode/src/0053_maximum_subarray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/maximum-subarray 20 | * 题目描述: 21 | * 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素) 22 | * 返回其最大和 23 | * 24 | * 示例: 25 | * 输入: [-2,1,-3,4,-1,2,1,-5,4], 26 | * 输出: 6 27 | * 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 28 | * 29 | * 进阶: 30 | * 如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。 31 | * 32 | * 解题思路: 33 | * 1. 这里使用的方法不是分治法,分治法时间复杂度反而变高了 34 | * 2. 直接一次性扫描数组即可,每当当前和变为负数时,就重新计数即可 35 | * 同时记录最大值 36 | */ 37 | 38 | class Solution { 39 | public: 40 | int maxSubArray(vector& nums) { 41 | int max = INT_MIN, sum = 0; 42 | for (int i=0; i max) 50 | max = sum; 51 | } 52 | 53 | return max; 54 | } 55 | }; -------------------------------------------------------------------------------- /leetcode/src/0058_length_of_last_word.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/length-of-last-word 20 | * 题目描述: 21 | * 给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度。 22 | * 如果不存在最后一个单词,请返回 0 。 23 | * 24 | * 说明: 25 | * 一个单词是指由字母组成,但不包含任何空格的字符串。 26 | * 27 | * 示例: 28 | * 输入: "Hello World" 29 | * 输出: 5 30 | * 31 | * 解题思路: 32 | * 只需要注意末尾会有空格的情况即可 33 | */ 34 | 35 | class Solution { 36 | public: 37 | int lengthOfLastWord(string s) { 38 | if (s.size() == 0) 39 | return 0; 40 | /* 41 | int end = s.size() - 1; 42 | for (int i=end; i>=0; i--) { 43 | if (s[i] != ' ') 44 | break; 45 | else 46 | end--; 47 | } 48 | */ 49 | int count = 0; 50 | for (int i=s.size()-1; i>=0; i--) { 51 | if (s[i] != ' ') 52 | count++; 53 | else if (count) 54 | break; 55 | } 56 | 57 | return count; 58 | } 59 | }; -------------------------------------------------------------------------------- /leetcode/src/0066_plus_one.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/plus-one 20 | * 题目描述: 21 | * 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。 22 | * 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。 23 | * 你可以假设除了整数 0 之外,这个整数不会以零开头。 24 | * 25 | * 示例 1: 26 | * 输入: [1,2,3] 27 | * 输出: [1,2,4] 28 | * 解释: 输入数组表示数字 123。 29 | * 30 | * 示例 2: 31 | * 输入: [4,3,2,1] 32 | * 输出: [4,3,2,2] 33 | * 解释: 输入数组表示数字 4321。 34 | * 35 | * 解题思路: 36 | * 1. 由于是加一操作,所以只需要考虑当前位是否需要进位即可 37 | * 即当前位是否为 9,如果不是直接加一结束 38 | * 2. 如果是就把当前位置零,继续下一个加一操作,如此循环 39 | */ 40 | 41 | class Solution { 42 | public: 43 | vector plusOne(vector& digits) { 44 | int head = digits[0]; 45 | for (int i=digits.size()-1; i>=0; i--) { 46 | if (digits[i] == 9) { 47 | digits[i] = 0; 48 | } else { 49 | digits[i]++; 50 | break; 51 | } 52 | } 53 | 54 | if (head > digits[0]) { 55 | vector res = {1}; 56 | res.insert(res.end(), digits.begin(), digits.end()); 57 | return res; 58 | } else { 59 | return digits; 60 | } 61 | } 62 | }; -------------------------------------------------------------------------------- /leetcode/src/0069_sqrtx.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/sqrtx 20 | * 题目描述: 21 | * 实现 int sqrt(int x) 函数。 22 | * 计算并返回 x 的平方根,其中 x 是非负整数。 23 | * 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。 24 | * 25 | * 解题思路: 26 | * 使用二分法,需要注意的问题就是数据溢出问题,解释见代码注释 27 | */ 28 | 29 | class Solution { 30 | public: 31 | int mySqrt(int x) { 32 | if (x == 0) 33 | return 0; 34 | 35 | if (x <= 3) 36 | return 1; 37 | 38 | unsigned int head = 2, end = x / 2; 39 | while (head < end) { 40 | /* signed type can detect the overflow */ 41 | /* but the multiple will be restricted */ 42 | /* however unsigned cant detect overflow, so div is used! */ 43 | unsigned int mid = head + (end - head) / 2; 44 | unsigned int res = x / mid; 45 | if (mid > res) { 46 | end = mid - 1; 47 | } else if (mid < res) { 48 | head = mid + 1; 49 | } else { 50 | return mid; 51 | } 52 | } 53 | 54 | if (head * head > x) 55 | return head - 1; 56 | return head; 57 | } 58 | }; -------------------------------------------------------------------------------- /leetcode/src/0070_climbing_stairs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/climbing-stairs 20 | * 题目描述: 21 | * 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 22 | * 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢? 23 | * 24 | * 解题思路: 25 | * 斐波那契数列 26 | */ 27 | 28 | class Solution { 29 | public: 30 | int climbStairs(int n) { 31 | if (n == 1) 32 | return 1; 33 | 34 | if (n == 2) 35 | return 2; 36 | 37 | int n_1 = 2, n_2 = 1; 38 | int nn = 3, step; 39 | while (nn <= n) { 40 | step = n_1 + n_2; 41 | n_2 = n_1; 42 | n_1 = step; 43 | nn++; 44 | } 45 | 46 | return step; 47 | } 48 | }; -------------------------------------------------------------------------------- /leetcode/src/0083_remove_duplicates_from_sorted_list.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list 20 | * 题目描述: 21 | * 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。 22 | * 23 | * 示例: 24 | * 输入: 1->1->2->3->3 25 | * 输出: 1->2->3 26 | * 27 | * 解题思路: 28 | * 直接从头遍历,删除重复的即可 29 | */ 30 | 31 | /** 32 | * Definition for singly-linked list. 33 | * struct ListNode { 34 | * int val; 35 | * ListNode *next; 36 | * ListNode(int x) : val(x), next(NULL) {} 37 | * }; 38 | */ 39 | class Solution { 40 | public: 41 | ListNode* deleteDuplicates(ListNode* head) { 42 | ListNode *res = head; 43 | while (res) { 44 | ListNode *next = res->next; 45 | if (next && next->val == res->val) { 46 | res->next = next->next; 47 | delete next; 48 | } else { 49 | res = next; 50 | } 51 | } 52 | 53 | return head; 54 | } 55 | }; -------------------------------------------------------------------------------- /leetcode/src/0088_merge_sorted_array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/merge-sorted-array 20 | * 题目描述: 21 | * 给定两个有序整数数组 nums1 和 nums2, 22 | * 将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组。 23 | * 24 | * 说明: 25 | * 初始化 nums1 和 nums2 的元素数量分别为 m 和 n。 26 | * 你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n) 27 | * 来保存 nums2 中的元素。 28 | * 29 | * 示例: 30 | * 输入: 31 | * nums1 = [1,2,3,0,0,0], m = 3 32 | * nums2 = [2,5,6], n = 3 33 | * 输出: [1,2,2,3,5,6]、 34 | * 35 | * 解题思路: 36 | * 该题合并两个有序数组思路其实很简单的,按大小取数即可 37 | * 稍微需要注意的就是该题要求需要使用原始数组存储排好序 38 | * 的数据,所以需要从后往前进行合并! 39 | */ 40 | 41 | class Solution { 42 | public: 43 | void merge(vector& nums1, int m, vector& nums2, int n) { 44 | int i = m-1, j = n-1; 45 | int index = m+n-1; 46 | while (i >= 0 && j >= 0) { 47 | if (nums1[i] > nums2[j]) { 48 | nums1[index--] = nums1[i--]; 49 | } else { 50 | nums1[index--] = nums2[j--]; 51 | } 52 | } 53 | 54 | while (j >= 0) { 55 | nums1[index--] = nums2[j--]; 56 | } 57 | } 58 | }; -------------------------------------------------------------------------------- /leetcode/src/0094__binary_tree_inorder_traversal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2021 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/binary-tree-inorder-traversal/ 20 | * 题目描述: 21 | * 给定一个二叉树的根节点 root ,返回它的中序遍历 22 | * 解题思路: 23 | * 1. 最直接的就是递归 24 | * 2. 用栈模拟递归,先 left 全部入栈,然后取栈顶,再操作 right 25 | */ 26 | 27 | class Solution { 28 | public: 29 | vector inorderTraversal(TreeNode* root) { 30 | vector res; 31 | // do_work(res, root); 32 | stack stk; 33 | 34 | while (root != nullptr || !stk.empty()) { 35 | while (root) { 36 | stk.push(root); 37 | root = root->left; 38 | } 39 | 40 | root = stk.top(); 41 | stk.pop(); 42 | res.push_back(root->val); 43 | root = root->right; 44 | } 45 | 46 | return res; 47 | } 48 | 49 | void do_work(vector &res, TreeNode *root) { 50 | if (root) { 51 | if (root->left) 52 | do_work(res, root->left); 53 | res.push_back(root->val); 54 | if (root->right) 55 | do_work(res, root->right); 56 | } 57 | } 58 | }; -------------------------------------------------------------------------------- /leetcode/src/0104_maximum_depth_of_binary_tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/maximum-depth-of-binary-tree 20 | * 题目描述: 21 | * 给定一个二叉树,找出其最大深度。 22 | * 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。 23 | * 24 | * 说明: 25 | * 叶子节点是指没有子节点的节点。 26 | * 27 | * 示例: 28 | * 给定二叉树 [3,9,20,null,null,15,7], 29 | * 30 | * 3 31 | * / \ 32 | * 9 20 33 | * / \ 34 | * 15 7 35 | * 返回它的最大深度 3 。 36 | */ 37 | 38 | /** 39 | * Definition for a binary tree node. 40 | * struct TreeNode { 41 | * int val; 42 | * TreeNode *left; 43 | * TreeNode *right; 44 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 45 | * }; 46 | */ 47 | class Solution { 48 | public: 49 | int maxDepth(TreeNode* root) { 50 | if (root == NULL) 51 | return 0; 52 | 53 | int left = maxDepth(root->left); 54 | int right = maxDepth(root->right); 55 | 56 | return max(left, right) + 1; 57 | } 58 | }; -------------------------------------------------------------------------------- /leetcode/src/0118_pascals_triangle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/pascals-triangle 20 | * 示例: 21 | * 输入: 5 22 | * 输出: 23 | * [ 24 | * [1], 25 | * [1,1], 26 | * [1,2,1], 27 | * [1,3,3,1], 28 | * [1,4,6,4,1] 29 | * ] 30 | */ 31 | 32 | class Solution { 33 | public: 34 | vector> generate(int numRows) { 35 | if (numRows == 0) 36 | return {}; 37 | 38 | vector> res; 39 | res.resize(numRows); 40 | 41 | vector one; 42 | one.push_back(1); 43 | res[0] = one; 44 | 45 | if (numRows == 1) { 46 | return res; 47 | } 48 | 49 | vector two; 50 | two.push_back(1); 51 | two.push_back(1); 52 | res[1] = two; 53 | 54 | for (int i=2; i 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/pascals-triangle-ii 20 | * 题目描述: 21 | * 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行 22 | * 23 | * 0 1 24 | * 1 1 1 25 | * 2 1 2 1 26 | * 3 1 3 3 1 27 | * 4 1 4 6 4 1 28 | * 5 1 5 10 10 5 1 29 | * 6 1 6 15 20 15 6 1 30 | */ 31 | 32 | class Solution { 33 | public: 34 | vector getRow(int rowIndex) { 35 | if (rowIndex == 0) 36 | return {1}; 37 | 38 | vector rows[2]; 39 | bool flag = true; 40 | rows[0] = {1, 1}; 41 | 42 | if (rowIndex == 1) 43 | return rows[0]; 44 | 45 | for (int i=2; i<=rowIndex; i++) { 46 | rows[flag].push_back(1); 47 | for (int j=1; j 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/single-number 20 | * 题目描述: 21 | * 给定一个非空整数数组,除了某个元素只出现一次以外 22 | * 其余每个元素均出现两次。找出那个只出现了一次的元素 23 | * 24 | * 说明: 25 | * 你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗? 26 | * 27 | * 示例 1: 28 | * 输入: [2,2,1] 29 | * 输出: 1 30 | * 31 | * 示例 2: 32 | * 输入: [4,1,2,1,2] 33 | * 输出: 4 34 | * 35 | * 解题思路: 36 | * 由于只有一个数只出现一次,且其他数都出现两次 37 | * 则可以通过 异或 操作来消除出现两次的数,从而保留只出现一次的数 38 | */ 39 | 40 | class Solution { 41 | public: 42 | int singleNumber(vector& nums) { 43 | int res = nums[0]; 44 | for (int i=1; i 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/linked-list-cycle 20 | * 题目描述: 21 | * 给定一个链表,判断链表中是否有环。 22 | * 23 | * 解题思路: 24 | * 两种方法:1. 快慢指针;2. 哈希表 25 | * 代码给出的是使用快慢指针判断的 26 | */ 27 | 28 | /** 29 | * Definition for singly-linked list. 30 | * struct ListNode { 31 | * int val; 32 | * ListNode *next; 33 | * ListNode(int x) : val(x), next(NULL) {} 34 | * }; 35 | */ 36 | class Solution { 37 | public: 38 | bool hasCycle(ListNode *head) { 39 | if (head == nullptr) 40 | return false; 41 | 42 | ListNode *temp = head->next; 43 | while (head && temp) { 44 | if (head == temp) 45 | return true; 46 | else { 47 | head = head->next; 48 | temp = temp->next; 49 | if (temp) 50 | temp = temp->next; 51 | } 52 | } 53 | 54 | return false; 55 | } 56 | }; -------------------------------------------------------------------------------- /leetcode/src/0167_two_sum_ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted 20 | * 题目描述: 21 | * 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。 22 | * 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。 23 | * 24 | * 说明: 25 | * 返回的下标值(index1 和 index2)不是从零开始的。 26 | * 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。 27 | * 28 | * 示例: 29 | * 输入: numbers = [2, 7, 11, 15], target = 9 30 | * 输出: [1,2] 31 | * 解释: 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。 32 | * 33 | * 解题思路: 34 | * 因为数组是有序的,so...看代码理解吧~ 35 | * 另一种方法就是二分法 36 | */ 37 | 38 | class Solution { 39 | public: 40 | vector twoSum(vector& numbers, int target) { 41 | if (numbers.size() == 0) 42 | return {}; 43 | 44 | int head = 0, tail = numbers.size() - 1; 45 | while (head < tail) { 46 | if (numbers[head] + numbers[tail] > target) 47 | tail--; 48 | else if (numbers[head] + numbers[tail] < target) 49 | head++; 50 | else 51 | return {head+1, tail+1}; 52 | } 53 | 54 | return {}; 55 | } 56 | }; -------------------------------------------------------------------------------- /leetcode/src/0169_majority_element.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/majority-element 20 | * 题目描述: 21 | * 给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊n/2⌋ 的元素。 22 | * 你可以假设数组是非空的,并且给定的数组总是存在众数。 23 | * 24 | * 示例 1: 25 | * 输入: [3,2,3] 26 | * 输出: 3 27 | * 28 | * 示例 2: 29 | * 输入: [2,2,1,1,1,2,2] 30 | * 输出: 2 31 | * 32 | * 解题思路: 33 | * 1. 由于众数是出现次数多余 n/2 的,所以可以使用计数方式来找到它 34 | * 即遇到相同的数就 ++ 操作,遇到不同的数就 -- 操作即可 35 | * 2. 每次计数归零后,重新选取新的数字,因为众数的特性 36 | * 所以最后选择的一定还是众数本身 37 | */ 38 | 39 | class Solution { 40 | public: 41 | int majorityElement(vector& nums) { 42 | if (nums.size() <= 0) 43 | return -1; 44 | 45 | int num = 0, count = 0; 46 | for (int i=0; i 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/reverse-bits 20 | * 题目描述: 21 | * 颠倒给定的 32 位无符号整数的二进制位 22 | */ 23 | 24 | class Solution { 25 | public: 26 | uint32_t reverseBits(uint32_t n) { 27 | if (n == 0) 28 | return 0; 29 | 30 | uint32_t res = 0x00; 31 | for (int i=0; i<32; i++) { 32 | if (n & (0x01 << i)) { 33 | res |= (0x01 << (31-i)); 34 | } 35 | } 36 | 37 | return res; 38 | } 39 | }; -------------------------------------------------------------------------------- /leetcode/src/0191_number_of_1_bits.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/number-of-1-bits 20 | * 题目描述: 21 | * 编写一个函数,输入是一个无符号整数 22 | * 返回其二进制表达式中数字位数为 ‘1’ 的个数 23 | */ 24 | 25 | class Solution { 26 | public: 27 | int hammingWeight(uint32_t n) { 28 | int res = 0; 29 | while (n) { 30 | res++; 31 | int temp = n - 1; 32 | n &= temp; 33 | } 34 | 35 | return res; 36 | } 37 | }; -------------------------------------------------------------------------------- /leetcode/src/0203_remove_linked_list_elements.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/remove-linked-list-elements 20 | * 题目描述: 21 | * 删除链表中等于给定值 val 的所有节点。 22 | * 23 | * 示例: 24 | * 输入: 1->2->6->3->4->5->6, val = 6 25 | * 输出: 1->2->3->4->5 26 | * 27 | * 解题思路: 28 | * 典型的双指针法,需要注意的是头结点等于目标值的情况 29 | * 该特例可以通过引入一个额外的节点消除,如代码所示 30 | */ 31 | 32 | /** 33 | * Definition for singly-linked list. 34 | * struct ListNode { 35 | * int val; 36 | * ListNode *next; 37 | * ListNode(int x) : val(x), next(NULL) {} 38 | * }; 39 | */ 40 | class Solution { 41 | public: 42 | ListNode* removeElements(ListNode* head, int val) { 43 | if (head == nullptr) 44 | return nullptr; 45 | 46 | ListNode res(val+1); 47 | res.next = head; 48 | ListNode *left = &res; 49 | ListNode *right = head; 50 | while (right) { 51 | if (right->val == val) { 52 | left->next = right->next; 53 | delete right; 54 | right = left->next; 55 | } else { 56 | left = right; 57 | right = right->next; 58 | } 59 | } 60 | 61 | return res.next; 62 | } 63 | }; -------------------------------------------------------------------------------- /leetcode/src/0204_count_primes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/count-primes 20 | * 题目描述: 21 | * 统计所有小于非负整数 n 的质数的数量。 22 | * 23 | * 示例: 24 | * 输入: 10 25 | * 输出: 4 26 | * 解释: 27 | * 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。 28 | * 29 | * 解题思路: 30 | * 非质数一定由质数因子构成,所以可以通过某个质数的 2倍、3倍... 31 | * 来排除非质数,由于合数至少含有两个数,故只需要遍历到 i*i flags(n); 41 | int res = n-2; 42 | for (int i=2; i 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/contains-duplicate 20 | * 题目描述: 21 | * 给定一个整数数组,判断是否存在重复元素 22 | * 23 | * 如果任何值在数组中出现至少两次,函数返回 true 24 | * 如果数组中每个元素都不相同,则返回 false 25 | * 26 | * 示例 1: 27 | * 输入: [1,2,3,1] 28 | * 输出: true 29 | * 30 | * 示例 2: 31 | * 输入: [1,2,3,4] 32 | * 输出: false 33 | * 34 | * 示例 3: 35 | * 输入: [1,1,1,3,3,4,3,2,4,2] 36 | * 输出: true 37 | * 38 | * 解题思路: 39 | * 第一种:暴力法 O(n) 40 | * 第二种:先排序,然后检查相邻的数是否相等即可 O(nlogn) 41 | * 第三种:哈希表,如代码所示 42 | */ 43 | 44 | class Solution { 45 | public: 46 | bool containsDuplicate(vector& nums) { 47 | unordered_map hash; 48 | for (int i=0; i 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/contains-duplicate-ii 20 | * 题目描述: 21 | * 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j 22 | * 使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k 23 | * 24 | * 示例 1: 25 | * 输入: nums = [1,2,3,1], k = 3 26 | * 输出: true 27 | * 28 | * 示例 2: 29 | * 输入: nums = [1,0,1,1], k = 1 30 | * 输出: true 31 | * 32 | * 示例 3: 33 | * 输入: nums = [1,2,3,1,2,3], k = 2 34 | * 输出: false 35 | */ 36 | 37 | class Solution { 38 | public: 39 | bool containsNearbyDuplicate(vector& nums, int k) { 40 | unordered_map hash; 41 | for (int i=0; i 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | class MyStack { 19 | public: 20 | deque dq; 21 | /** Initialize your data structure here. */ 22 | MyStack() { 23 | while (dq.size()) 24 | dq.pop_back(); 25 | } 26 | 27 | /** Push element x onto stack. */ 28 | void push(int x) { 29 | dq.push_back(x); 30 | } 31 | 32 | /** Removes the element on top of the stack and returns that element. */ 33 | int pop() { 34 | int res = dq.back(); 35 | dq.pop_back(); 36 | return res; 37 | } 38 | 39 | /** Get the top element. */ 40 | int top() { 41 | return dq.back(); 42 | } 43 | 44 | /** Returns whether the stack is empty. */ 45 | bool empty() { 46 | return dq.empty(); 47 | } 48 | }; 49 | 50 | /** 51 | * Your MyStack object will be instantiated and called as such: 52 | * MyStack* obj = new MyStack(); 53 | * obj->push(x); 54 | * int param_2 = obj->pop(); 55 | * int param_3 = obj->top(); 56 | * bool param_4 = obj->empty(); 57 | */ -------------------------------------------------------------------------------- /leetcode/src/0226_invert_binary_tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/invert-binary-tree 20 | * 题目描述: 21 | * 翻转一棵二叉树 22 | * 23 | * 示例: 24 | * 输入: 25 | * 4 26 | * / \ 27 | * 2 7 28 | * / \ / \ 29 | * 1 3 6 9 30 | * 31 | * 输出: 32 | * 4 33 | * / \ 34 | * 7 2 35 | * / \ / \ 36 | * 9 6 3 1 37 | */ 38 | 39 | /** 40 | * Definition for a binary tree node. 41 | * struct TreeNode { 42 | * int val; 43 | * TreeNode *left; 44 | * TreeNode *right; 45 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 46 | * }; 47 | */ 48 | class Solution { 49 | public: 50 | TreeNode* invertTree(TreeNode* root) { 51 | if (root == nullptr) 52 | return root; 53 | 54 | TreeNode *temp = root->left; 55 | root->left = root->right; 56 | root->right = temp; 57 | 58 | invertTree(root->left); 59 | invertTree(root->right); 60 | 61 | return root; 62 | } 63 | }; -------------------------------------------------------------------------------- /leetcode/src/0231_power_of_two.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/power-of-two 20 | * 题目描述: 21 | * 给定一个整数,编写一个函数来判断它是否是 2 的幂次方 22 | * 23 | * 示例 1: 24 | * 输入: 1 25 | * 输出: true 26 | * 解释: 20 = 1 27 | * 28 | * 示例 2: 29 | * 输入: 16 30 | * 输出: true 31 | * 解释: 24 = 16 32 | * 33 | * 示例 3: 34 | * 输入: 218 35 | * 输出: false 36 | * 37 | * 解题思路: 38 | * 2的幂次方其实就是该数的二进制中只含有一个 1 39 | */ 40 | 41 | class Solution { 42 | public: 43 | bool isPowerOfTwo(int n) { 44 | if (n == INT_MIN || n == INT_MAX || n == 0) 45 | return false; 46 | 47 | return (n & (n - 1)) == 0; 48 | } 49 | }; -------------------------------------------------------------------------------- /leetcode/src/0235_lowest_common_ancestor_of_bst.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree 20 | * 题目描述: 21 | * 给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先 22 | * 23 | * 解题思路: 24 | * 要么左子树,要么右子树,否则当前节点就是最低公共祖先 25 | */ 26 | 27 | /** 28 | * Definition for a binary tree node. 29 | * struct TreeNode { 30 | * int val; 31 | * TreeNode *left; 32 | * TreeNode *right; 33 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 34 | * }; 35 | */ 36 | class Solution { 37 | public: 38 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 39 | if (root == nullptr) 40 | return root; 41 | 42 | if (p->val > root->val && q->val > root->val) 43 | return lowestCommonAncestor(root->right, p, q); 44 | else if (p->val < root->val && q->val < root->val) 45 | return lowestCommonAncestor(root->left, p, q); 46 | else 47 | return root; 48 | } 49 | }; -------------------------------------------------------------------------------- /leetcode/src/0237_delete_node_in_a_linked_list.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/delete-node-in-a-linked-list 20 | * 题目描述: 21 | * 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点 22 | * 你将只被给定要求被删除的节点 23 | * 24 | * 说明: 25 | * 链表至少包含两个节点 26 | * 链表中所有节点的值都是唯一的 27 | * 给定的节点为非末尾节点并且一定是链表中的一个有效节点 28 | * 不要从你的函数中返回任何结果 29 | * 30 | * 解题思路: 31 | * 直接 O(1) 操作,删除需要删除的节点的后一个节点即可 32 | */ 33 | 34 | /** 35 | * Definition for singly-linked list. 36 | * struct ListNode { 37 | * int val; 38 | * ListNode *next; 39 | * ListNode(int x) : val(x), next(NULL) {} 40 | * }; 41 | */ 42 | class Solution { 43 | public: 44 | void deleteNode(ListNode* node) { 45 | if (node == nullptr) 46 | return; 47 | 48 | ListNode *temp = node->next; 49 | node->val = temp->val; 50 | node->next = temp->next; 51 | 52 | delete temp; 53 | } 54 | }; -------------------------------------------------------------------------------- /leetcode/src/0239_sliding_window_maximum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | class Solution { 19 | public: 20 | vector maxInWindows(vector& nums, int k) { 21 | vector res; 22 | deque dq; 23 | 24 | for (int i=0; i=k) 26 | dq.pop_front(); 27 | 28 | while (!dq.empty() && nums[dq.back()]<=nums[i]) 29 | dq.pop_back(); 30 | 31 | dq.push_back(i); 32 | 33 | if (i>=k-1) 34 | res.push_back(nums[dq.front()]); 35 | } 36 | 37 | return res; 38 | } 39 | }; -------------------------------------------------------------------------------- /leetcode/src/0242_valid_anagram.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/valid-anagram 20 | * 题目描述: 21 | * 给定两个字符串 s 和 t 22 | * 编写一个函数来判断 t 是否是 s 的字母异位词 23 | * 24 | * 示例 1: 25 | * 输入: s = "anagram", t = "nagaram" 26 | * 输出: true 27 | * 28 | * 示例 2: 29 | * 输入: s = "rat", t = "car" 30 | * 输出: false 31 | * 32 | * 说明: 33 | * 你可以假设字符串只包含小写字母 34 | * 35 | * 进阶: 36 | * 如果输入字符串包含 unicode 字符怎么办? 37 | * 你能否调整你的解法来应对这种情况? 38 | * 39 | * 解题思路: 40 | * 这类题目直接 哈希表 即可解决 O(n) 41 | * 另一种方法就是先排序,再逐个字符比较 O(nlogn) 42 | */ 43 | 44 | class Solution { 45 | public: 46 | bool isAnagram(string s, string t) { 47 | if (s.size() != t.size()) 48 | return false; 49 | 50 | char shash[256] = {0}; 51 | char thash[256] = {0}; 52 | for (int i=0; i 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/add-digits 20 | * 题目描述: 21 | * 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数 22 | * 23 | * 示例: 24 | * 输入: 38 25 | * 输出: 2 26 | * 解释: 27 | * 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2。由于 2 是一位数,所以返回 2 28 | * 29 | * 进阶: 30 | * 你可以不使用循环或者递归,且在 O(1) 时间复杂度内解决这个问题吗? 31 | */ 32 | 33 | class Solution { 34 | public: 35 | int addDigits(int num) { 36 | if (num == 0) 37 | return 0; 38 | 39 | int res = num % 9; 40 | if (res == 0) 41 | res = 9; 42 | return res; 43 | } 44 | }; -------------------------------------------------------------------------------- /leetcode/src/0263_ugly_number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/ugly-number 20 | * 题目描述: 21 | * 编写一个程序判断给定的数是否为丑数。 22 | * 丑数就是只包含质因数 2, 3, 5 的正整数。 23 | * 24 | * 示例 1: 25 | * 输入: 6 26 | * 输出: true 27 | * 解释: 6 = 2 × 3 28 | * 29 | * 示例 2: 30 | * 输入: 8 31 | * 输出: true 32 | * 解释: 8 = 2 × 2 × 2 33 | * 34 | * 示例 3: 35 | * 输入: 14 36 | * 输出: false 37 | * 解释: 14 不是丑数,因为它包含了另外一个质因数 7 38 | * 39 | * 说明: 40 | * 1 是丑数。 41 | * 输入不会超过 32 位有符号整数的范围: [−231, 231 − 1] 42 | */ 43 | 44 | class Solution { 45 | public: 46 | bool isUgly(int num) { 47 | if (num < 1) 48 | return false; 49 | 50 | factor(num, 2); 51 | factor(num, 3); 52 | factor(num, 5); 53 | 54 | return num == 1; 55 | } 56 | 57 | void factor(int &num, int f) { 58 | while (num % f == 0) 59 | num /= f; 60 | } 61 | }; -------------------------------------------------------------------------------- /leetcode/src/0268_missing_number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/missing-number 20 | * 题目描述: 21 | * 给定一个包含 0, 1, 2, ..., n 中 n 个数的序列 22 | * 找出 0 .. n 中没有出现在序列中的那个数。 23 | * 24 | * 示例 1: 25 | * 输入: [3,0,1] 26 | * 输出: 2 27 | * 28 | * 示例 2: 29 | * 输入: [9,6,4,2,3,5,7,0,1] 30 | * 输出: 8 31 | * 32 | * 说明: 33 | * 你的算法应具有线性时间复杂度。你能否仅使用额外常数空间来实现? 34 | * 35 | * 解题思路: 36 | * 1. 递增数组求和公式 37 | * 2. 官方给出的异或运算方法,方法很新颖,比较有意思 38 | */ 39 | 40 | class Solution { 41 | public: 42 | int missingNumber(vector& nums) { 43 | int res = (nums.size() + 1) * nums.size() / 2; 44 | int sum = 0; 45 | for (int i=0; i 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/first-bad-version 20 | * 题目描述: 21 | * 你是产品经理,目前正在带领一个团队开发新的产品 22 | * 不幸的是,你的产品的最新版本没有通过质量检测 23 | * 由于每个版本都是基于之前的版本开发的 24 | * 所以错误的版本之后的所有版本都是错的 25 | * 假设你有 n 个版本 [1, 2, ..., n] 26 | * 你想找出导致之后所有版本出错的第一个错误的版本 27 | * 你可以通过调用 bool isBadVersion(version) 接口 28 | * 来判断版本号 version 是否在单元测试中出错 29 | * 实现一个函数来查找第一个错误的版本 30 | * 你应该尽量减少对调用 API 的次数 31 | * 32 | * 示例: 33 | * 给定 n = 5,并且 version = 4 是第一个错误的版本 34 | * 调用 isBadVersion(3) -> false 35 | * 调用 isBadVersion(5) -> true 36 | * 调用 isBadVersion(4) -> true 37 | * 所以,4 是第一个错误的版本 38 | * 39 | * 解题思路: 40 | * 二分法即可 41 | */ 42 | 43 | // Forward declaration of isBadVersion API. 44 | bool isBadVersion(int version); 45 | 46 | class Solution { 47 | public: 48 | int firstBadVersion(int n) { 49 | int left = 1, right = n; 50 | while (left < right) { 51 | int mid = (right - left) / 2 + left; 52 | bool bad = isBadVersion(mid); 53 | if (bad) 54 | right = mid; 55 | else 56 | left = mid + 1; 57 | } 58 | 59 | return right; 60 | } 61 | }; -------------------------------------------------------------------------------- /leetcode/src/0283_move_zeroes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/move-zeroes 20 | * 题目描述: 21 | * 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾 22 | * 同时保持非零元素的相对顺序 23 | * 24 | * 示例: 25 | * 输入: [0,1,0,3,12] 26 | * 输出: [1,3,12,0,0] 27 | * 28 | * 说明: 29 | * 必须在原数组上操作,不能拷贝额外的数组。 30 | * 尽量减少操作次数。 31 | * 32 | * 解题思路: 33 | * 把不同类型的数组分离的题,典型做法就是双指针 34 | * 但此题如果用双指针操作就不符合要求了,题目要求保持 35 | * 非零数原来的顺序,所以必须对双指针方法进行修改 36 | * 由于最后操作完成后的结果是末尾全是零,所以我们可以 37 | * 不必关心零的存在,只需要把非零数字向左紧密排列即可 38 | * 最后再把数组中剩余的元素全部填充为零即可 39 | */ 40 | 41 | class Solution { 42 | public: 43 | void moveZeroes(vector& nums) { 44 | int head = 0, tail = 0; 45 | for (int i=0; i 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/nim-game 20 | * 题目描述: 21 | * 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头 22 | * 每次你们轮流拿掉 1 - 3 块石头。 拿掉最后一块石头的人就是获胜者 23 | * 你作为先手,你们是聪明人,每一步都是最优解 24 | * 编写一个函数,来判断你是否可以在给定石头数量的情况下赢得游戏。 25 | * 26 | * 示例: 27 | * 输入: 4 28 | * 输出: false 29 | * 解释: 30 | * 如果堆中有 4 块石头,那么你永远不会赢得比赛 31 | * 因为无论你拿走 1 块、2 块 还是 3 块石头 32 | * 最后一块石头总是会被你的朋友拿走 33 | */ 34 | 35 | class Solution { 36 | public: 37 | bool canWinNim(int n) { 38 | return (n % 4 != 0); 39 | } 40 | }; -------------------------------------------------------------------------------- /leetcode/src/0300_longest_increasing_subsequence.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/leetcode/src/0300_longest_increasing_subsequence.cpp -------------------------------------------------------------------------------- /leetcode/src/0303_range_sum_query_immutable.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/range-sum-query-immutable 20 | * 题目描述: 21 | * 给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 22 | * 范围内元素的总和,包含 i, j 两点。 23 | * 24 | * 示例: 25 | * 给定 nums = [-2, 0, 3, -5, 2, -1],求和函数为 sumRange() 26 | * sumRange(0, 2) -> 1 27 | * sumRange(2, 5) -> -1 28 | * sumRange(0, 5) -> -3 29 | * 30 | * 说明: 31 | * 你可以假设数组不可变。 32 | * 会多次调用 sumRange 方法。 33 | * 34 | * 解题思路: 35 | * 计算一个积分表即可,了解图像处理的都知道有图像积分这个概念 36 | */ 37 | 38 | class NumArray { 39 | public: 40 | vector sum; 41 | NumArray(vector& nums) { 42 | if (nums.empty()) 43 | return; 44 | 45 | sum.resize(nums.size()); 46 | sum[0] = nums[0]; 47 | for (int i=1; isumRange(i,j); 63 | */ -------------------------------------------------------------------------------- /leetcode/src/0326_power_of_three.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/power-of-three 20 | * 题目描述: 21 | * 给定一个整数,写一个函数来判断它是否是 3 的幂次方 22 | * 23 | * 示例 1: 24 | * 输入: 27 25 | * 输出: true 26 | * 27 | * 示例 2: 28 | * 输入: 0 29 | * 输出: false 30 | * 31 | * 示例 3: 32 | * 输入: 9 33 | * 输出: true 34 | * 35 | * 示例 4: 36 | * 输入: 45 37 | * 输出: false 38 | * 39 | * 进阶: 40 | * 你能不使用循环或者递归来完成本题吗? 41 | * 42 | * 解题思路: 43 | * 1. 逐个除 3 判断是否整除 44 | * 2. 将原数转化为 3 进制,如果是 3 的幂,则只含有一个 1 45 | * 但该方法操作比方法 1 还复杂,但方法比较新颖 46 | * 3. 找到 3 的幂的最大值,如果给定的数是 3 的幂,那么 47 | * 一定可以被最大数整除,这个方法也有点意思,代码如下 48 | */ 49 | 50 | class Solution { 51 | public: 52 | bool isPowerOfThree(int n) { 53 | if (n <= 0) 54 | return false; 55 | /* 56 | int max = log(INT_MAX) / log(3); 57 | max = pow(3, max); 58 | */ 59 | int max = 1162261467; 60 | 61 | return max % n == 0; 62 | } 63 | }; -------------------------------------------------------------------------------- /leetcode/src/0342_power_of_four.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/power-of-four 20 | * 题目描述: 21 | * 给定一个整数 (32 位有符号整数) 22 | * 请编写一个函数来判断它是否是 4 的幂次方 23 | * 24 | * 示例 1: 25 | * 输入: 16 26 | * 输出: true 27 | * 28 | * 示例 2: 29 | * 输入: 5 30 | * 输出: false 31 | * 32 | * 进阶: 33 | * 使用循环或者递归来完成本题吗? 34 | * 35 | * 解题思路: 36 | * 根据 4 的幂的规律,先判断是否是 2 的幂,然后判断是否是 4 的幂 37 | */ 38 | 39 | class Solution { 40 | public: 41 | bool isPowerOfFour(int num) { 42 | if (num <= 0) 43 | return false; 44 | 45 | if ((num & (num-1)) != 0) 46 | return false; 47 | 48 | const int check = 0x55555555; 49 | if ((num | check) != check) 50 | return false; 51 | 52 | return true; 53 | } 54 | }; -------------------------------------------------------------------------------- /leetcode/src/0344_reverse_string.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/reverse-string 20 | * 题目描述: 21 | * 编写一个函数,其作用是将输入的字符串反转过来 22 | * 输入字符串以字符数组 char[] 的形式给出 23 | * 不要给另外的数组分配额外的空间 24 | * 你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题 25 | * 你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符 26 | * 27 | * 示例 1: 28 | * 输入:["h","e","l","l","o"] 29 | * 输出:["o","l","l","e","h"] 30 | * 31 | * 示例 2: 32 | * 输入:["H","a","n","n","a","h"] 33 | * 输出:["h","a","n","n","a","H"] 34 | * 35 | * 解题思路: 36 | * 双指针即可 37 | */ 38 | 39 | class Solution { 40 | public: 41 | void reverseString(vector& s) { 42 | int left = 0, right = s.size() - 1; 43 | while (left < right) { 44 | char temp = s[left]; 45 | s[left++] = s[right]; 46 | s[right--] = temp; 47 | } 48 | } 49 | }; -------------------------------------------------------------------------------- /leetcode/src/0371_sum_of_two_integers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/sum-of-two-integers 20 | * 题目描述: 21 | * 不使用运算符 + 和 - ​​​​​​​,计算两整数 ​​​​​​​a 、b ​​​​​​​之和 22 | * 23 | * 示例 1: 24 | * 输入: a = 1, b = 2 25 | * 输出: 3 26 | * 27 | * 示例 2: 28 | * 输入: a = -2, b = 3 29 | * 输出: 1 30 | * 31 | * 解题思路: 32 | * 根据二进制加法原则,使用 异或 实现 加 法 33 | * 使用 与 运算实现 进位 34 | */ 35 | 36 | class Solution { 37 | public: 38 | int getSum(int a, int b) { 39 | /* carray is unsinged int */ 40 | int carry = (unsigned int)(a & b) << 1; 41 | int sum = a ^ b; 42 | while (carry) { 43 | int temp = sum & carry; 44 | sum ^= carry; 45 | carry = (unsigned int)temp << 1; 46 | } 47 | 48 | return sum; 49 | } 50 | }; -------------------------------------------------------------------------------- /leetcode/src/0374_guess_number_higher_or_lower.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/guess-number-higher-or-lower 20 | * 题目描述: 21 | * 我们正在玩一个猜数字游戏。 游戏规则如下: 22 | * 我从 1 到 n 选择一个数字。 你需要猜我选择了哪个数字 23 | * 每次你猜错了,我会告诉你这个数字是大了还是小了 24 | * 你调用一个预先定义好的接口 guess(int num) 25 | * 它会返回 3 个可能的结果(-1,1 或 0): 26 | * -1 : 我的数字比较小 27 | * 1 : 我的数字比较大 28 | * 0 : 恭喜!你猜对了! 29 | * 30 | * 示例 : 31 | * 输入: n = 10, pick = 6 32 | * 输出: 6 33 | * 34 | * 解题思路:直接二分法搞定 35 | */ 36 | 37 | // Forward declaration of guess API. 38 | // @param num, your guess 39 | // @return -1 if my number is lower, 1 if my number is higher, otherwise return 0 40 | int guess(int num); 41 | 42 | class Solution { 43 | public: 44 | int guessNumber(int n) { 45 | int left = 1, right = n; 46 | while (left < right) { 47 | int mid = left + (right-left) / 2; 48 | int g = guess(mid); 49 | if (g == -1) 50 | right = mid - 1; 51 | else if (g == 1) 52 | left = mid + 1; 53 | else 54 | return mid; 55 | } 56 | 57 | return left; 58 | } 59 | }; -------------------------------------------------------------------------------- /leetcode/src/0387_first_unique_character_in_a_string.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/first-unique-character-in-a-string 20 | * 题目描述: 21 | * 给定一个字符串,找到它的第一个不重复的字符 22 | * 并返回它的索引。如果不存在,则返回 -1 23 | * 24 | * 案例: 25 | * s = "leetcode" 26 | * 返回 0 27 | * s = "loveleetcode" 28 | * 返回 2 29 | * 30 | * 注意事项: 31 | * 您可以假定该字符串只包含小写字母 32 | */ 33 | 34 | class Solution { 35 | public: 36 | int firstUniqChar(string s) { 37 | if (s.size() == 0) 38 | return -1; 39 | 40 | const char base = 'a'; 41 | unsigned int hash[26] = {0}; 42 | for (int i=0; i 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/find-the-difference 20 | * 题目描述: 21 | * 给定两个字符串 s 和 t,它们只包含小写字母 22 | * 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母 23 | * 请找出在 t 中被添加的字母 24 | * 25 | * 26 | * 示例: 27 | * 输入: 28 | * s = "abcd" 29 | * t = "abcde" 30 | * 输出: 31 | * e 32 | * 33 | * 解释: 34 | * 'e' 是那个被添加的字母。 35 | * 36 | * 解题思路: 37 | * 1. 可以使用映射统计每个字符的数量,然后相减,剩下的一个就是 38 | * 要找的字符 39 | * 2. 由于两个字符串只有一个不同,所以可以通过异或找出唯一的 40 | * 那一个 41 | */ 42 | 43 | class Solution { 44 | public: 45 | char findTheDifference(string s, string t) { 46 | char res = 0; 47 | for (int i=0; i 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/is-subsequence 20 | * 题目描述: 21 | * 给定字符串 s 和 t ,判断 s 是否为 t 的子序列 22 | * 你可以认为 s 和 t 中仅包含英文小写字母 23 | * 字符串 t 可能会很长(长度 ~= 500,000) 24 | * 而 s 是个短字符串(长度 <=100) 25 | * 字符串的一个子序列是原始字符串删除一些(也可以不删除)字符 26 | * 而不改变剩余字符相对位置形成的新字符串 27 | * 例如,"ace"是"abcde"的一个子序列,而"aec"不是 28 | * 29 | * 示例 1: 30 | * s = "abc", t = "ahbgdc" 31 | * 返回 true. 32 | * 33 | * 示例 2: 34 | * s = "axc", t = "ahbgdc" 35 | * 返回 false. 36 | * 37 | * 后续挑战 : 38 | * 如果有大量输入的 S,称作S1, S2, ... , Sk 其中 k >= 10亿 39 | * 你需要依次检查它们是否为 T 的子序列 40 | * 在这种情况下, 你会怎样改变代码 41 | */ 42 | 43 | class Solution { 44 | public: 45 | bool isSubsequence(string s, string t) { 46 | unsigned int si = 0, ti = 0; 47 | while (si 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | class Solution { 19 | public: 20 | string toHex(int num) { 21 | if (num == 0) 22 | return "0"; 23 | 24 | const char codes[16] = { 25 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' 26 | }; 27 | unsigned int n = num; 28 | string res; 29 | while (n) { 30 | res = codes[n & 0x0f] + res; 31 | n >>= 4; 32 | } 33 | 34 | return res; 35 | } 36 | }; -------------------------------------------------------------------------------- /leetcode/src/0434_number_of_segments_in_a_string.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/number-of-segments-in-a-string 20 | * 题目描述: 21 | * 统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符 22 | * 请注意,你可以假定字符串里不包括任何不可打印的字符 23 | * 24 | * 示例: 25 | * 输入: "Hello, my name is John" 26 | * 输出: 5 27 | * 28 | * 解题思路: 29 | * 三步解决: 30 | * 跳过前导空格,跳过一个非空格字符串,跳过空格直到下一个字符 31 | */ 32 | 33 | class Solution { 34 | public: 35 | int countSegments(string s) { 36 | int count = 0, index = 0; 37 | /* skip the blank */ 38 | while (index < s.size() && s[index] == ' ') 39 | index++; 40 | while (index < s.size()) { 41 | count++; 42 | /* skip the word */ 43 | while (index < s.size() && s[index] != ' ') 44 | index++; 45 | /* find the next word */ 46 | while (index < s.size() && s[index] == ' ') 47 | index++; 48 | } 49 | 50 | return count; 51 | } 52 | }; -------------------------------------------------------------------------------- /leetcode/src/0441_arranging_coins.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/arranging-coins 20 | * 题目描述: 21 | * 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状 22 | * 第 k 行就必须正好有 k 枚硬币 23 | * 给定一个数字 n,找出可形成完整阶梯行的总行数 24 | * n 是一个非负整数,并且在32位有符号整型的范围内 25 | * 26 | * 示例 1: 27 | * n = 5 28 | * 硬币可排列成以下几行: 29 | * ¤ 30 | * ¤ ¤ 31 | * ¤ ¤ 32 | * 因为第三行不完整,所以返回2 33 | * 34 | * 示例 2: 35 | * n = 8 36 | * 硬币可排列成以下几行: 37 | * ¤ 38 | * ¤ ¤ 39 | * ¤ ¤ ¤ 40 | * ¤ ¤ 41 | * 因为第四行不完整,所以返回3 42 | * 43 | * 解题思路: 44 | * 等差数组求和公式即可 45 | */ 46 | 47 | class Solution { 48 | public: 49 | int arrangeCoins(int n) { 50 | unsigned int un = n; 51 | double s = sqrt(2*un+0.25) - 0.5; 52 | int k = (int)s; 53 | 54 | return k; 55 | } 56 | }; -------------------------------------------------------------------------------- /leetcode/src/0459_repeated_substring_pattern.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/repeated-substring-pattern 20 | * 题目描述: 21 | * 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成 22 | * 给定的字符串只含有小写英文字母,并且长度不超过10000 23 | * 24 | * 示例 1: 25 | * 输入: "abab" 26 | * 输出: True 27 | * 解释: 28 | * 可由子字符串 "ab" 重复两次构成。 29 | * 30 | * 示例 2: 31 | * 输入: "aba" 32 | * 输出: False 33 | * 34 | * 示例 3: 35 | * 输入: "abcabcabcabc" 36 | * 输出: True 37 | * 解释: 38 | * 可由子字符串 "abc" 重复四次构成 39 | * 或者子字符串 "abcabc" 重复两次构成 40 | */ 41 | 42 | class Solution { 43 | public: 44 | bool repeatedSubstringPattern(string s) { 45 | if (s.size() <= 1) 46 | return false; 47 | 48 | for (int i=1; i<=s.size()/2; i++) { 49 | if (s.size() % i) 50 | continue; 51 | int j; 52 | for (j=i; j 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/hamming-distance 20 | * 题目描述: 21 | * 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目 22 | * 给出两个整数 x 和 y,计算它们之间的汉明距离 23 | * 24 | * 注意: 25 | * 0 ≤ x, y < 231 26 | * 27 | * 示例: 28 | * 输入: x = 1, y = 4 29 | * 输出: 2 30 | * 解释: 31 | * 1 (0 0 0 1) 32 | * 4 (0 1 0 0) 33 | * ↑ ↑ 34 | * 上面的箭头指出了对应二进制位不同的位置 35 | * 36 | * 解题思路: 37 | * 先对两个数异或操作,这样结果中就记录了不同的位数,然后就是计算 38 | * 二进制数值 1 的个数的问题了,具体如代码所示 39 | */ 40 | 41 | class Solution { 42 | public: 43 | int hammingDistance(int x, int y) { 44 | int xy = x^y; 45 | int res = 0; 46 | while (xy) { 47 | res += 1; 48 | xy &= (xy - 1); 49 | } 50 | 51 | return res; 52 | } 53 | }; -------------------------------------------------------------------------------- /leetcode/src/0476_number_complement.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2020 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/number-complement 20 | * 题目描述: 21 | * 给定一个正整数,输出它的补数。补数是对该数的二进制表示取反 22 | * 23 | * 注意: 24 | * 给定的整数保证在32位带符号整数的范围内。 25 | * 你可以假定二进制数不包含前导零位。 26 | * 27 | * 示例 1: 28 | * 输入: 5 29 | * 输出: 2 30 | * 解释: 31 | * 5的二进制表示为101(没有前导零位),其补数为010。所以你需要输出2 32 | * 33 | * 示例 2: 34 | * 输入: 1 35 | * 输出: 0 36 | * 解释: 37 | * 1的二进制表示为1(没有前导零位),其补数为0。所以你需要输出0 38 | * 39 | * 解题思路: 40 | * 通过示例可以看出,只对左边第一个 1 及其之后的二进制位进行取反 41 | * 因此需要对第一个 1 之前的数保持不变,即保持为零 42 | */ 43 | 44 | class Solution { 45 | public: 46 | int findComplement(int num) { 47 | unsigned int temp = num; 48 | int mask = 0x00; 49 | while (temp) { 50 | mask = (mask << 1) | 0x01; 51 | temp >>= 1; 52 | } 53 | 54 | return (~num) & mask; 55 | } 56 | }; -------------------------------------------------------------------------------- /leetcode/src/0485_max_consecutive_ones.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2020 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/max-consecutive-ones 20 | * 题目描述: 21 | * 给定一个二进制数组, 计算其中最大连续1的个数 22 | * 23 | * 示例 1: 24 | * 输入: [1,1,0,1,1,1] 25 | * 输出: 3 26 | * 解释: 27 | * 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3 28 | * 29 | * 注意: 30 | * 输入的数组只包含 0 和1 31 | * 输入数组的长度是正整数,且不超过 10,000 32 | */ 33 | 34 | class Solution { 35 | public: 36 | int findMaxConsecutiveOnes(vector& nums) { 37 | int res = 0, temp = 0; 38 | for (auto n: nums) { 39 | if (n) { 40 | temp++; 41 | } else if (temp) { 42 | res = max(res, temp); 43 | temp = 0; 44 | } 45 | } 46 | res = max(res, temp); 47 | 48 | return res; 49 | } 50 | }; -------------------------------------------------------------------------------- /leetcode/src/0492_construct_the_rectangle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2020 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/construct-the-rectangle 20 | * 题目描述: 21 | * 作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的 22 | * 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 23 | * 且满足以下要求的矩形的页面。要求: 24 | * 1. 你设计的矩形页面必须等于给定的目标面积 25 | * 2. 宽度 W 不应大于长度 L,换言之,要求 L >= W 26 | * 3. 长度 L 和宽度 W 之间的差距应当尽可能小 27 | * 你需要按顺序输出你设计的页面的长度 L 和宽度 W 28 | * 29 | * 示例: 30 | * 输入: 4 31 | * 输出: [2, 2] 32 | * 解释: 33 | * 目标面积是 4, 所有可能的构造方案有 [1,4], [2,2], [4,1] 34 | * 但是根据要求2,[1,4] 不符合要求; 根据要求3,[2,2] 比 [4,1] 更能符合要求 35 | * 所以输出长度 L 为 2, 宽度 W 为 2 36 | * 37 | * 解题思路: 38 | * 从示例可以看出,最优解应该尽量接近正方形,因此从面积的平方根开始遍历 39 | */ 40 | 41 | class Solution { 42 | public: 43 | vector constructRectangle(int area) { 44 | int width = sqrt(area); 45 | 46 | while (width >= 1) { 47 | if (area % width == 0) 48 | break; 49 | 50 | width--; 51 | } 52 | 53 | return {area/width, width}; 54 | } 55 | }; -------------------------------------------------------------------------------- /leetcode/src/0504_base_7.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2020 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/base-7 20 | * 题目描述: 21 | * 给定一个整数,将其转化为7进制,并以字符串形式输出。 22 | * 23 | * 示例 1: 24 | * 输入: 100 25 | * 输出: "202" 26 | * 27 | * 示例 2: 28 | * 输入: -7 29 | * 输出: "-10" 30 | */ 31 | 32 | class Solution { 33 | public: 34 | string convertToBase7(int num) { 35 | if (!num) 36 | return "0"; 37 | 38 | unsigned n; 39 | string res; 40 | if (num < 0) 41 | n = -num; 42 | else 43 | n = num; 44 | 45 | char char_num[10] = {'0','1','2','3','4','5','6','7','8','9'}; 46 | while (n) { 47 | unsigned int temp = n / 7; 48 | res = char_num[n-7*temp] + res; 49 | n = temp; 50 | } 51 | if (num < 0) 52 | res = "-" + res; 53 | 54 | return res; 55 | } 56 | }; 57 | -------------------------------------------------------------------------------- /leetcode/src/0507_perfect_number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2020 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/perfect-number 20 | * 题目描述: 21 | * 对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等 22 | * 我们称它为“完美数”,给定一个 整数 n, 如果他是完美数 23 | * 返回 True,否则返回 False 24 | * 25 | * 示例: 26 | * 输入: 28 27 | * 输出: True 28 | * 解释: 28 = 1 + 2 + 4 + 7 + 14 29 | */ 30 | 31 | class Solution { 32 | public: 33 | bool checkPerfectNumber(int num) { 34 | // 负数及 1 直接返回 false 35 | if (num <= 1) 36 | return false; 37 | 38 | bool res = true; 39 | int sum = 1, loop = 2; 40 | // 求各个因子的和 41 | while (loop <= sqrt(num)) { 42 | if (num % loop == 0) { 43 | sum += loop; 44 | sum += num / loop; 45 | } 46 | loop++; 47 | } 48 | // 如果恰好能够开方,则上一步多加了一个因子 49 | int s = sqrt(num); 50 | if (s*s == num) 51 | sum -= s; 52 | 53 | if (sum != num) 54 | res = false; 55 | 56 | return res; 57 | } 58 | }; -------------------------------------------------------------------------------- /leetcode/src/0509_fibonacci_number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2020 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/fibonacci-number 20 | * 斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列 21 | * 该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和 22 | * 也就是: 23 | * F(0) = 0, F(1) = 1 24 | * F(N) = F(N - 1) + F(N - 2), 其中 N > 1 25 | * 给定 N,计算 F(N)。 26 | * 27 | * 示例 1: 28 | * 输入:2 29 | * 输出:1 30 | * 解释: 31 | * F(2) = F(1) + F(0) = 1 + 0 = 1 32 | * 33 | * 示例 3: 34 | * 输入:4 35 | * 输出:3 36 | * 解释: 37 | * F(4) = F(3) + F(2) = 2 + 1 = 3 38 | */ 39 | 40 | class Solution { 41 | public: 42 | int fib(int N) { 43 | if (N <= 0) 44 | return 0; 45 | 46 | if (N == 1) 47 | return 1; 48 | 49 | int f_n_1 = 0, f_n = 1; 50 | for (int i=2; i<=N; i++) { 51 | int temp = f_n + f_n_1; 52 | f_n_1 = f_n; 53 | f_n = temp; 54 | } 55 | 56 | return f_n; 57 | } 58 | }; -------------------------------------------------------------------------------- /leetcode/src/0520_detect_capital.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2020 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * https://leetcode-cn.com/problems/detect-capital 20 | * 给定一个单词,你需要判断单词的大写使用是否正确 21 | * 22 | * 我们定义,在以下情况时,单词的大写用法是正确的 23 | * 全部字母都是大写,比如"USA" 24 | * 单词中所有字母都不是大写,比如"leetcode" 25 | * 如果单词不只含有一个字母,只有首字母大写, 比如 "Google" 26 | * 否则,我们定义这个单词没有正确使用大写字母。 27 | * 28 | * 示例 1: 29 | * 输入: "USA" 30 | * 输出: True 31 | * 32 | * 示例 2: 33 | * 输入: "FlaG" 34 | * 输出: False 35 | */ -------------------------------------------------------------------------------- /leetcode/src/0530_minimum_absolute_difference_in_bst.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2020 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | class Solution { 19 | public: 20 | int getMinimumDifference(TreeNode* root) { 21 | if (root == nullptr) 22 | return -1; 23 | 24 | int min = INT_MAX; 25 | TreeNode *prev = nullptr; 26 | 27 | find_min(root, &prev, min); 28 | 29 | return min; 30 | } 31 | 32 | void find_min(TreeNode *root, TreeNode **prev, int &min) { 33 | if (root->left) 34 | find_min(root->left, prev, min); 35 | 36 | if (*prev) { 37 | int tmp = root->val - (*prev)->val; 38 | min = tmp > min ? min : tmp; 39 | } 40 | 41 | *prev = root; 42 | 43 | if (root->right) 44 | find_min(root->right, prev, min); 45 | } 46 | }; -------------------------------------------------------------------------------- /leetcode/src/0532-k-diff-pairs-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2020 Jiau Zhang 3 | * For more information see 4 | * 5 | * SPDX-License-Identifier: GPL 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int findPairs(vector& nums, int k) { 11 | if (k < 0) 12 | return 0; 13 | 14 | unordered_map hash; 15 | for (auto n: nums) 16 | hash[n]++; 17 | 18 | int res = 0; 19 | for (unordered_map::iterator it=hash.begin(); it!=hash.end(); it++) { 20 | if (k) { 21 | int diff = it->first - k; 22 | if (hash.count(diff)) 23 | res += 1; 24 | } else if (it->second > 1){ 25 | res += 1; 26 | } 27 | } 28 | 29 | return res; 30 | } 31 | }; -------------------------------------------------------------------------------- /leetcode/src/0538-convert-bst-to-greater-tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2020 Jiau Zhang 3 | * For more information see 4 | * 5 | * SPDX-License-Identifier: GPL 6 | */ 7 | 8 | /* 9 | * 解题思路:首先二叉搜索树的特点就是右节点比左节点大,再根据题意 10 | * 需要累加比自己大的,所以就从大的往小的累加,即先访问右节点 11 | * 再访问当前节点,最后访问左节点即可 12 | */ 13 | 14 | class Solution { 15 | public: 16 | TreeNode* convertBST(TreeNode* root) { 17 | if (root == nullptr) 18 | return nullptr; 19 | 20 | int sum = 0; 21 | do_convert(root, sum); 22 | 23 | return root; 24 | } 25 | 26 | void do_convert(TreeNode *root, int &sum) { 27 | if (root->right) 28 | do_convert(root->right, sum); 29 | 30 | root->val = root->val + sum; 31 | sum = root->val; 32 | 33 | if (root->left) 34 | do_convert(root->left, sum); 35 | } 36 | }; -------------------------------------------------------------------------------- /leetcode/src/0541-reverse-string-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2020 Jiau Zhang 3 | * For more information see 4 | * 5 | * SPDX-License-Identifier: GPL 6 | */ 7 | 8 | /* 9 | * 解题思路:首先根据题意对数组以 k 为单位进行划分 10 | * 仅对第奇数个 k 的单位内的数组进行翻转 11 | * 另外就是需要检查边界,因为可能最后一个 k 会超过字符串最大长度 12 | */ 13 | 14 | class Solution { 15 | public: 16 | string reverseStr(string s, int k) { 17 | if (k<=0 || s.size()<=1) 18 | return s; 19 | 20 | bool flag = true; 21 | for (int i=0; i s.size() ? s.size() : end; 26 | swap(s, start, end); 27 | } 28 | 29 | flag = !flag; 30 | } 31 | 32 | return s; 33 | } 34 | 35 | void swap(string &s, int start, int end) { 36 | for (int i=0; i<(end-start)/2; i++) { 37 | int left = start+i; 38 | int right = end - 1 - i; 39 | unsigned char c = s[left]; 40 | s[left] = s[right]; 41 | s[right] = c; 42 | } 43 | } 44 | }; -------------------------------------------------------------------------------- /leetcode/st/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/leetcode/st/README.md -------------------------------------------------------------------------------- /leetcode/stack/README.md: -------------------------------------------------------------------------------- 1 | ### Stack 2 | - [0020_valid_parentheses](../src/0020_valid_parentheses.cpp) 3 | - [0094__binary_tree_inorder_traversal.cpp](../src/0094__binary_tree_inorder_traversal.cpp) 4 | - [0144_binary_tree_preorder_traversal](../src/0144_binary_tree_preorder_traversal.cpp) 5 | - [0150_evaluate_reverse_polish_notation](../src/0150_evaluate_reverse_polish_notation.cpp) 6 | - [0155_min_stack](../src/0155_min_stack.cpp) 7 | - [0225_implement_stack_using_queues](../src/0225_implement_stack_using_queues.cpp) 8 | - [0232_implement_queue_using_stacks](../src/0232_implement_queue_using_stacks.cpp) 9 | - [0496_next_greater_element_i](../src/0496_next_greater_element_i.cpp) -------------------------------------------------------------------------------- /leetcode/string/README.md: -------------------------------------------------------------------------------- 1 | ### String 2 | - [0013_roman_to_integer](../src/0013_roman_to_integer.cpp) 3 | - [0014_longest_common_prefix](../src/0014_longest_common_prefix.cpp) 4 | - [0020_valid_parentheses](../src/0020_valid_parentheses.cpp) 5 | - [0028_implement_strstr](../src/0028_implement_strstr.cpp) 6 | - [0038_count_and_say](../src/0038_count_and_say.cpp) 7 | - [0058_length_of_last_word](../src/0058_length_of_last_word.cpp) 8 | - [0067_add_binary](../src/0067_add_binary.cpp) 9 | - [0125_valid_palindrome](../src/0125_valid_palindrome.cpp) 10 | - [0344_reverse_string](../src/0344_reverse_string.cpp) 11 | - [0345_reverse_vowels_of_a_string](../src/0345_reverse_vowels_of_a_string.cpp) 12 | - [0383_ransom_note](../src/0383_ransom_note.cpp) 13 | - [0387_first_unique_character_in_a_string](../src/0387_first_unique_character_in_a_string.cpp) 14 | - [0415_add_strings](../src/0415_add_strings.cpp) 15 | - [0434_number_of_segments_in_a_string](../src/0434_number_of_segments_in_a_string.cpp) 16 | - [0443_string_compression](../src/0443_string_compression.cpp) 17 | - [0459_repeated_substring_pattern](../src/0459_repeated_substring_pattern.cpp) 18 | - [0482_license_key_formatting](../src/0482_license_key_formatting.cpp) 19 | - [0520_detect_capital](../src/0520_detect_capital.cpp) -------------------------------------------------------------------------------- /leetcode/sw/README.md: -------------------------------------------------------------------------------- 1 | ### Sliding Window 2 | - [0239_sliding_window_maximum](../src/0239_sliding_window_maximum.cpp) -------------------------------------------------------------------------------- /leetcode/tps/README.md: -------------------------------------------------------------------------------- 1 | ### Two Pointers 2 | - [0026_remove_duplicates_from_sorted_array](../src/0026_remove_duplicates_from_sorted_array.cpp) 3 | - [0027_remove_element](../src/0027_remove_element.cpp) 4 | - [0028_implement_strstr](../src/0028_implement_strstr.cpp) 5 | - [0088_merge_sorted_array](../src/0088_merge_sorted_array.cpp) 6 | - [0125_valid_palindrome](../src/0125_valid_palindrome.cpp) 7 | - [0141_linked_list_cycle](../src/0141_linked_list_cycle.cpp) 8 | - [0167_two_sum_ii](../src/0167_two_sum_ii.cpp) 9 | - [0234_palindrome_linked_list](../src/0234_palindrome_linked_list.cpp) 10 | - [0283_move_zeroes](../src/0283_move_zeroes.cpp) 11 | - [0344_reverse_string](../src/0344_reverse_string.cpp) 12 | - [0345_reverse_vowels_of_a_string](../src/0345_reverse_vowels_of_a_string.cpp) 13 | - [0349_intersection_of_two_arrays](../src/0349_intersection_of_two_arrays.cpp) 14 | - [0350_intersection_of_two_arrays_ii](../src/0350_intersection_of_two_arrays_ii.cpp) -------------------------------------------------------------------------------- /leetcode/tree/0437-path-sum-iii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2021 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * 解题思路: 20 | * 朴素的做法就是以逐个节点为起点递归统计可能性的综合,但是存在太多的重复计算 21 | * 所以这里引入了前缀和,即记录从根节点到当前节点所有路径的和的中间结果 22 | * 当遍历到当前节点的时候,反向的查找是否存在某些节点的前缀和差值正好等于 target 的 23 | * 这一方向查找就避免了朴素做法中的以每个节点为起点查找的操作,消除了重复计算 24 | */ 25 | 26 | class Solution { 27 | private: 28 | unordered_map buffer; 29 | public: 30 | int pathSum(TreeNode* root, int targetSum) { 31 | buffer[0] = 1; 32 | return do_path(root, 0, targetSum); 33 | } 34 | 35 | int do_path(TreeNode *root, int cur, int target) { 36 | if (root == nullptr) 37 | return 0; 38 | 39 | int ret = 0; 40 | cur += root->val; 41 | if (buffer.count(cur-target)) // 反向查找符合要求的节点 42 | ret = buffer[cur-target]; 43 | 44 | buffer[cur]++; 45 | ret += do_path(root->left, cur, target) + do_path(root->right, cur, target); 46 | buffer[cur]--; 47 | 48 | return ret; 49 | } 50 | }; -------------------------------------------------------------------------------- /leetcode/tree/README.md: -------------------------------------------------------------------------------- 1 | ### Tree 2 | - [0100_same_tree](../src/0100_same_tree.cpp) 3 | - [0101_symmetric_tree](../src/0101_symmetric_tree.cpp) 4 | - [0104_maximum_depth_of_binary_tree](../src/0104_maximum_depth_of_binary_tree.cpp) 5 | - [0106_binary_tree_postorder_traversal](../src/0106_binary_tree_postorder_traversal.cpp) 6 | - [0107_binary_tree_level_order_traversal_ii](../src/0107_binary_tree_level_order_traversal_ii.cpp) 7 | - [0108_convert_sorted_array_to_binary_search_tree](../src/0108_convert_sorted_array_to_binary_search_tree.cpp) 8 | - [0110_balanced_binary_tree](../src/0110_balanced_binary_tree.cpp) 9 | - [0111_minimum_depth_of_binary_tree](../src/0111_minimum_depth_of_binary_tree.cpp) 10 | - [0112_path_sum](../src/0112_path_sum.cpp) 11 | - [0144_binary_tree_preorder_traversal](../src/0144_binary_tree_preorder_traversal.cpp) 12 | - [0222_count_complete_tree_nodes](../src/0222_count_complete_tree_nodes.cpp) 13 | - [0226_invert_binary_tree](../src/0226_invert_binary_tree.cpp) 14 | - [0235_lowest_common_ancestor_of_a_binary_search_tree](../src/0235_lowest_common_ancestor_of_bst.cpp) 15 | - [0617_merge_two_binary_trees](../src/0617_merge_two_binary_trees.cpp) 16 | - [0257_binary_tree_paths](../src/0257_binary_tree_paths.cpp) 17 | - [0404_sum_of_left_leaves](../src/0404_sum_of_left_leaves.cpp) 18 | - [0437_path_sum_iii](../src/0437_path_sum_iii.cpp) [v2](./0437-path-sum-iii.cpp) 19 | - [0501_find_mode_in_binary_search_tree](../src/0501_find_mode_in_binary_search_tree.cpp) -------------------------------------------------------------------------------- /leetcode/trie/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/leetcode/trie/README.md -------------------------------------------------------------------------------- /leetcode/ts/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/leetcode/ts/README.md -------------------------------------------------------------------------------- /leetcode/uf/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/leetcode/uf/README.md -------------------------------------------------------------------------------- /log/C++.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/log/C++.txt -------------------------------------------------------------------------------- /log/compile-error.txt: -------------------------------------------------------------------------------- 1 | 1. gcc: error trying to exec 'cc1plus': execvp: 没有那个文件或目录 2 | 解决方法:安装g++ 3 | 2. printf_gcc.cpp:(.text+0x41):对‘std::cout’未定义的引用 4 | printf_gcc.cpp:(.text+0x46):对‘std::basic_ostream >& std::operator<< >(std::basic_ostream >&, char const*)’未定义的引用 5 | printf_gcc.cpp:(.text+0x4b):对‘std::basic_ostream >& std::endl >(std::basic_ostream >&)’未定义的引用 6 | printf_gcc.cpp:(.text+0x53):对‘std::ostream::operator<<(std::ostream& (*)(std::ostream&))’未定义的引用 7 | /tmp/ccS0MaAK.o:在函数‘__static_initialization_and_destruction_0(int, int)’中: 8 | printf_gcc.cpp:(.text+0x95):对‘std::ios_base::Init::Init()’未定义的引用 9 | printf_gcc.cpp:(.text+0xa4):对‘std::ios_base::Init::~Init()’未定义的引用 10 | 解决办法:用g++编译程序 11 | -------------------------------------------------------------------------------- /python/decorator/decorator.py: -------------------------------------------------------------------------------- 1 | def decorator_func(func): 2 | def do_decorate(*args, **kargs): 3 | print('decorator_func') 4 | print('args: {}, kargs: {}'.format(args, kargs)) 5 | func(*args, **kargs) 6 | print('do_decorate done!') 7 | 8 | return do_decorate 9 | 10 | class decorator_clazz(): 11 | def __init__(self, func): 12 | print('decorator_clazz') 13 | self._func = func 14 | print('decorator_clazz __init__ done!') 15 | 16 | def __call__(self, *args, **kargs): 17 | print('__call__') 18 | print('args: {}, kargs: {}'.format(args, kargs)) 19 | self._func(*args, **kargs) 20 | print('__call__ done!') 21 | 22 | @decorator_clazz 23 | @decorator_func 24 | def hello(*args, **kargs): 25 | print('original hello func') 26 | print('args: {}, kargs: {}'.format(args, kargs)) 27 | print('original hello func done!') 28 | 29 | hello('xxx') 30 | hello('yyy', key='zzz') 31 | hello('yyy', 'mmm', key='zzz', value='nnn') 32 | -------------------------------------------------------------------------------- /python/leetcode/0530_minimum_absolute_difference_in_bst.py: -------------------------------------------------------------------------------- 1 | ''' 2 | > Base Repo --> https://github.com/JiauZhang/algorithms 3 | ''' 4 | 5 | class Solution: 6 | def getMinimumDifference(self, root: TreeNode) -> int: 7 | res = sys.maxsize 8 | prev = -1 9 | 10 | def LDR(root: TreeNode): 11 | nonlocal res, prev 12 | if root.left: 13 | LDR(root.left) 14 | 15 | if prev != -1: 16 | tmp = root.val - prev 17 | res = min(tmp, res) 18 | 19 | prev = root.val 20 | 21 | if root.right: 22 | LDR(root.right) 23 | 24 | LDR(root) 25 | 26 | return res -------------------------------------------------------------------------------- /python/leetcode/0532-k-diff-pairs-in-an-array.py: -------------------------------------------------------------------------------- 1 | ''' 2 | For more information see 3 | ''' 4 | 5 | class Solution: 6 | def findPairs(self, nums: List[int], k: int) -> int: 7 | if k < 0: 8 | return 0 9 | 10 | c = collections.Counter(nums) 11 | res = 0 12 | 13 | for n in c: 14 | if k > 0: 15 | diff = n - k 16 | if c[diff] != 0: 17 | res += 1 18 | else: 19 | if c[n] > 1: 20 | res += 1 21 | 22 | return res -------------------------------------------------------------------------------- /python/leetcode/0538-convert-bst-to-greater-tree.py: -------------------------------------------------------------------------------- 1 | ''' 2 | For more information see 3 | ''' 4 | 5 | class Solution: 6 | def convertBST(self, root: TreeNode) -> TreeNode: 7 | sum = 0 8 | 9 | def do_convert(root): 10 | nonlocal sum 11 | if root.right: 12 | do_convert(root.right) 13 | 14 | root.val = sum + root.val 15 | sum = root.val 16 | 17 | if root.left: 18 | do_convert(root.left) 19 | 20 | if root: 21 | do_convert(root) 22 | 23 | return root -------------------------------------------------------------------------------- /python/leetcode/0541-reverse-string-ii.py: -------------------------------------------------------------------------------- 1 | ''' 2 | For more information see 3 | ''' 4 | 5 | class Solution: 6 | def reverseStr(self, s: str, k: int) -> str: 7 | if len(s) < 1: 8 | return s 9 | 10 | flag, res = True, "" 11 | for i in range(0, len(s), k): 12 | res += s[i:i+k][::-1] if flag else s[i:i+k] 13 | flag = not flag 14 | 15 | return res 16 | -------------------------------------------------------------------------------- /python/thread/01_hello_thread.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright(c) 2019 Jiau Zhang 3 | For more information see 4 | 5 | This repo is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation 8 | 9 | It is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with THIS repo. If not, see . 16 | """ 17 | 18 | # https://github.com/python/cpython/blob/master/Lib/threading.py 19 | import threading, time 20 | 21 | class HelloThread(threading.Thread): 22 | def __init__(self, thread_name): 23 | threading.Thread.__init__(self, name=thread_name) 24 | self.count = 0 25 | 26 | def run(self): 27 | print ('HelloThread>>> thread name: {} start runing...\n'.format(self.getName())) 28 | 29 | while self.count < 10: 30 | self.count += 1 31 | print ('HelloThread>>> {} >>> {}'.format(self.getName(), self.count)) 32 | time.sleep(0.5) 33 | 34 | print ('HelloThread>>> {} stopped!'.format(self.getName())) 35 | 36 | if __name__ == '__main__': 37 | td = HelloThread("HelloThread") 38 | print ('main Thread>>> start HelloTread...') 39 | td.start() 40 | print ('main Thread>>> wait HelloThread exit...') 41 | td.join() 42 | print ('main Thread>>> HelloTread and the main Thread exit...') 43 | -------------------------------------------------------------------------------- /python/thread/02_thread_lock.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright(c) 2019 Jiau Zhang 3 | For more information see 4 | 5 | This repo is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation 8 | 9 | It is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with THIS repo. If not, see . 16 | """ 17 | 18 | import thread, time 19 | 20 | def thread_lock(ident): 21 | print ('thread {} acquire lock\n'.format(ident)) 22 | lk.acquire() 23 | print ('thread {} get the lock'.format(ident)) 24 | print ('thread {} release the lock'.format(ident)) 25 | lk.release() 26 | 27 | if __name__ == '__main__': 28 | # https://docs.python.org/2/library/thread.html#module-thread 29 | lk = thread.allocate_lock() 30 | for i in xrange(0, 5): 31 | thread.start_new_thread(thread_lock, (i, )) 32 | 33 | time.sleep(1) 34 | print ('main thread exit...') 35 | -------------------------------------------------------------------------------- /python/thread/03_condition_objects.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright(c) 2019 Jiau Zhang 3 | For more information see 4 | 5 | This repo is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation 8 | 9 | It is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with THIS repo. If not, see . 16 | """ 17 | 18 | import threading, time 19 | 20 | def condition_thread(ident): 21 | con.acquire() 22 | print ('thread {} get the lock'.format(ident)) 23 | print ('thread {} notify another thread'.format(ident)) 24 | con.notify() 25 | print ('thread {} wait lock'.format(ident)) 26 | # release the lock and block itself until be notified 27 | con.wait() 28 | 29 | if __name__ == '__main__': 30 | lock = threading.Lock() 31 | con = threading.Condition(lock = lock) 32 | 33 | for i in xrange(0, 5): 34 | threading._start_new_thread(condition_thread, (i, )) 35 | 36 | time.sleep(1) 37 | print ('main thread exit...') 38 | -------------------------------------------------------------------------------- /scripts/add-license-test-1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | # Test #1 19 | 20 | ## Head #1 21 | content #1 22 | 23 | ## Head #2 24 | content #2 25 | 26 | ## Head #3 27 | content #3 -------------------------------------------------------------------------------- /scripts/add-license-test-2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | # Test #2 19 | 20 | ## Head #1 21 | content #1 22 | 23 | ## Head #2 24 | content #2 25 | 26 | ## Head #3 27 | content #3 -------------------------------------------------------------------------------- /scripts/add-license.sh: -------------------------------------------------------------------------------- 1 | # SHELL_SCRIPT /YOUR/FILES/PATH YOUR_FILTER_RULE /YOUR/LICENSE/FILE 2 | 3 | echo "PATH: "$1 4 | echo "FILTER RULE: "$2 5 | echo "LICENSE FILE: "$3 6 | license_file=$3 7 | # blank is illegal! 8 | target_files=`ls $1/$2` 9 | echo "Target files: "$target_files 10 | # fetch source file content 11 | # touch temp_file 12 | 13 | for file in $target_files 14 | do 15 | echo "add license to file: "$file 16 | cat $file > temp_file 17 | # add license 18 | cat $license_file > $file 19 | echo >> $file 20 | echo >> $file 21 | cat temp_file >> $file 22 | done 23 | 24 | rm temp_file -------------------------------------------------------------------------------- /scripts/git-commit-count.sh: -------------------------------------------------------------------------------- 1 | git log | grep -e 'commit [a-zA-Z0-9]*' | wc -l -------------------------------------------------------------------------------- /scripts/install-opencv-python.sh: -------------------------------------------------------------------------------- 1 | pip install opencv-python 2 | pip install --user opencv-contrib-python -------------------------------------------------------------------------------- /scripts/license.txt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ -------------------------------------------------------------------------------- /src/01_find_num.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | *题目: 20 | * 二维数组中的查找 21 | * 22 | *题目描述: 23 | * 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序, 24 | * 每一列都按照从上到下递增的顺序排序。请完成一个函数, 25 | * 输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 26 | * 27 | *解题思路: 28 | * 因为数组是有序的,所以采用对角顺序比较数据大小,这样一次就可以排除一行 29 | * 30 | */ 31 | 32 | class Solution { 33 | public: 34 | bool Find(int target, vector > array) { 35 | int x = array.size(); 36 | int y = array[0].size(); 37 | 38 | for (int i=0; i=0; j--) { 40 | if (target > array[i][j]) 41 | break; 42 | if (target == array[i][j]) 43 | return true; 44 | if (j == 0) 45 | return false; 46 | } 47 | } 48 | return false; 49 | } 50 | }; -------------------------------------------------------------------------------- /src/02_replace_space.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | 题目描述 20 | 请实现一个函数,将一个字符串中的每个空格替换成“%20”。 21 | 例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。 22 | 解题思路: 23 | 为了节省空间,这里假设源字符串后方有足够的空间,这里采用就地替换的方法 24 | 但是如果从前往后替换的话,每遇到一次空格都需要整体移动后方的字符串 25 | 复杂度太高,所以从后方开始替换,为了确定字符串最后的长度,所以需要 26 | 先统计字符串中空格的数量,然后得到尾部指针,遇到空格就替换,没有就复制 27 | */ 28 | class Solution { 29 | public: 30 | void replaceSpace(char *str,int length) { 31 | if (!str || length<0) 32 | return; 33 | 34 | int spaces = 0; 35 | for (int l=0; l=0; l--) { 47 | if (str[l] != ' ') { 48 | str[tail--] = str[l]; 49 | } else { 50 | str[tail--] = '0'; 51 | str[tail--] = '2'; 52 | str[tail--] = '%'; 53 | } 54 | } 55 | } 56 | }; -------------------------------------------------------------------------------- /src/05_queue_with_two_stack.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * 题目描述: 20 | * 用两个栈来实现一个队列,完成队列的Push和Pop操作。队列中的元素为int类型。 21 | * 22 | * 解题思路: 23 | * 队列是先进先出,而栈是先进后出,但是两个栈串在一起操作就是先进先出了 24 | * so.... 25 | * 26 | */ 27 | class Solution 28 | { 29 | public: 30 | void push(int node) { 31 | stack2.push(node); 32 | } 33 | 34 | int pop() { 35 | if (stack1.size() != 0) { 36 | int res = stack1.top(); 37 | stack1.pop(); 38 | return res; 39 | } else { 40 | while (stack2.size()) { 41 | int top = stack2.top(); 42 | stack1.push(top); 43 | stack2.pop(); 44 | } 45 | 46 | if (stack1.size()) { 47 | int res = stack1.top(); 48 | stack1.pop(); 49 | return res; 50 | } 51 | } 52 | 53 | return -1; 54 | } 55 | 56 | private: 57 | stack stack1; 58 | stack stack2; 59 | }; -------------------------------------------------------------------------------- /src/07_fibonacci.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * 题目描述: 20 | * 大家都知道斐波那契数列,现在要求输入一个整数n, 21 | * 请你输出斐波那契数列的第n项(从0开始,第0项为0)。 22 | * 23 | */ 24 | 25 | /* 递归方法----效率低,内存占用大 */ 26 | class Solution { 27 | public: 28 | int Fibonacci(int n) { 29 | if (n <= 0) 30 | return 0; 31 | 32 | if (n == 1) 33 | return 1; 34 | 35 | return Fibonacci(n-1) + Fibonacci(n-2); 36 | } 37 | }; 38 | 39 | /* 循环方法----效率高,避免了递归中的重复计算 */ 40 | class Solution { 41 | public: 42 | int Fibonacci(int n) { 43 | if (n <= 0) 44 | return 0; 45 | 46 | if (n == 1) 47 | return 1; 48 | 49 | int f_n_1 = 1; 50 | int f_n_2 = 0; 51 | int f_n = 0; 52 | for (int i=2; i<=n; i++) { 53 | f_n = f_n_1 + f_n_2; 54 | f_n_2 = f_n_1; 55 | f_n_1 = f_n; 56 | } 57 | 58 | return f_n; 59 | } 60 | }; 61 | 62 | -------------------------------------------------------------------------------- /src/08_jump_floor_i.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * 题目描述: 20 | * 一只青蛙一次可以跳上1级台阶,也可以跳上2级。 21 | * 求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。 22 | * 23 | * 解题思路: 24 | * 当n=1时,仅有 1 中跳法 25 | * 当n=2时,有两种跳法,1+1 或者 2 26 | * 这里只是起始条件和斐波那契数列有点不同而已 27 | * 推广到当有 n 个台阶时,调到当前台阶有两种方式: 28 | * 1. 从上个台阶跳 1 个台阶到达的,即在 n-1 处跳过来的 29 | * 2. 从上个台阶跳 2 个台阶到达的,即在 n-2 处跳过来的 30 | * 因此,调到第 n 个台阶的总方法数为 f(n-1) + f(n-2) 31 | * 32 | */ 33 | 34 | class Solution { 35 | public: 36 | int jumpFloor(int number) { 37 | if (number <= 0) 38 | return 0; 39 | 40 | if (number == 1) 41 | return 1; 42 | 43 | if (number == 2) 44 | return 2; 45 | 46 | int f_n_1 = 2; 47 | int f_n_2 = 1; 48 | int f_n = 0; 49 | for (int i=3; i<=number; i++) { 50 | f_n = f_n_1 + f_n_2; 51 | f_n_2 = f_n_1; 52 | f_n_1 = f_n; 53 | } 54 | 55 | return f_n; 56 | } 57 | }; -------------------------------------------------------------------------------- /src/09_jump_floor_ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * 题目描述: 20 | * 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。 21 | * 求该青蛙跳上一个n级的台阶总共有多少种跳法。 22 | * 23 | * 解题思路: 24 | * n = 1时,跳法为 1 25 | * n = 2时,跳法为 2 26 | * n = 3时,跳法为 f(n-1) + f(n-2) + f(n-3) = 2 + 1 + 1 = 4 = 2^2 27 | * n = 4时,跳法为 f(n-1) + f(n-2) + f(n-3) + f(n-4) = 2^2 + 2 + 1 + 1 = 8 = 2^3 28 | * 推广到 n 29 | * f(n) = f(n-1) + f(n-2) + f(n-3) + ... + f(2) + f(1) + f(0) 30 | * = 2^(n-2) + 2^(n-3) + 2^(n-4) + ...+ 2 + 1 + 1 31 | * = 2^(n-1) 32 | * 为了快速求解 f(n) 可以使用循环快速求解幂运算,但是这里是 2 的幂运算 33 | * 所以可以使用 移位运算 这样只需一步即可完成运算!!! 34 | * 35 | */ 36 | 37 | class Solution { 38 | public: 39 | int jumpFloorII(int number) { 40 | if (number <= 0) 41 | return 0; 42 | 43 | return (1<<(number-1)); 44 | } 45 | }; -------------------------------------------------------------------------------- /src/10_rect_cover.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * 题目描述 20 | * 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。 21 | * 请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 22 | * 23 | * 解题思路: 24 | * 当n=1时,覆盖方法有 1 种,竖着放 25 | * 当n=2时,覆盖方法有 2 种,两个竖着放或者两个横着放 26 | * 当n=3时,覆盖方法有 3 中,当第三个为竖着放时,前边的两个随便放 27 | * 当第三个横着放时,第二个也被覆盖了, 28 | * 所以只剩第一个是自由的了 29 | * 即 f(3) = f(2) + f(1) 30 | * 推广成 n 后变成: 31 | * f(n) = f(n-1) + f(n-2) 32 | * 33 | */ 34 | 35 | class Solution { 36 | public: 37 | int rectCover(int number) { 38 | if (number<=0) 39 | return 0; 40 | 41 | if (number == 1) 42 | return 1; 43 | 44 | if (number == 2) 45 | return 2; 46 | 47 | int f_n_1 = 2; 48 | int f_n_2 = 1; 49 | int f_n = 0; 50 | for (int i=3; i<=number; i++) { 51 | f_n = f_n_1 + f_n_2; 52 | f_n_2 = f_n_1; 53 | f_n_1 = f_n; 54 | } 55 | 56 | return f_n; 57 | } 58 | }; -------------------------------------------------------------------------------- /src/11_number_of_1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * 题目描述: 20 | * 输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。 21 | * 22 | * 解题思路: 23 | * 通过 位与 操作判断是否为 1 24 | * 如果对输入数据进行移位操作,则会出现死循环 25 | * 因为:负数在 右移位 时,负号标志符 1 会往下传递 26 | * 所以只能定义一个无符号整数 1 左移位来完成算法 27 | * 28 | * 另一种方法: 29 | * 01101100 - 1 = 01101011 30 | * 01101100 & 01101011 = 01101000 31 | * 可以看出减 1 操作会把原数中的最后一个 1 变成 0 32 | * 再进行与操作后就会变成把原数最后一个 1 抹去了!!! 33 | * 34 | */ 35 | 36 | /* 方法一 */ 37 | class Solution { 38 | public: 39 | int NumberOf1(int n) { 40 | int count = 0; 41 | 42 | unsigned int flag = 1; 43 | while (flag) { 44 | if (n & flag) 45 | count++; 46 | flag <<= 1; 47 | } 48 | 49 | return count; 50 | } 51 | }; 52 | 53 | /* 方法二 */ 54 | class Solution { 55 | public: 56 | int NumberOf1(int n) { 57 | int count = 0; 58 | 59 | while (n) { 60 | count++; 61 | n &= (n-1); 62 | } 63 | 64 | return count; 65 | } 66 | }; -------------------------------------------------------------------------------- /src/14_find_kth_to_tail.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * 题目描述: 20 | * 输入一个链表,输出该链表中倒数第k个结点。 21 | * 22 | * struct ListNode { 23 | * int val; 24 | * struct ListNode *next; 25 | * ListNode(int x) : 26 | * val(x), next(NULL) { 27 | * } 28 | * }; 29 | * 30 | * 解题思路: 31 | * 使用两个指针,快的比慢的多走 k 步 32 | * 注意事项:当链表长度不足 k 个时返回 NULL 33 | * 1. 一个指针先走 k 步,走k步后,该指针指向下一个 34 | * 2. 同时移动两个指针,直到快指针指向NULL 35 | * 3. 返回慢指针 36 | */ 37 | class Solution { 38 | public: 39 | ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) { 40 | if (pListHead == NULL) 41 | return NULL; 42 | 43 | ListNode *pRight = pListHead; 44 | ListNode *pLeft = pListHead; 45 | for (int i=0; inext; 50 | } 51 | 52 | while (pRight) { 53 | pRight = pRight->next; 54 | pLeft = pLeft->next; 55 | } 56 | 57 | return pLeft; 58 | } 59 | }; -------------------------------------------------------------------------------- /src/15_reverse_list.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * 题目描述 20 | * 输入一个链表,反转链表后,输出新链表的表头。 21 | * 22 | * struct ListNode { 23 | * int val; 24 | * struct ListNode *next; 25 | * ListNode(int x) : 26 | * val(x), next(NULL) { 27 | * } 28 | * }; 29 | * 30 | * 解题思路: 31 | * 由于是单链表,在反转过程中链表肯定会断裂,所以需要临时指针来记录 32 | * 原来的连接关系,步骤如下: 33 | * 1. 确定是否为空链表,空链表则返回,非空则先把第一个和链表断开连接 34 | * 2. 一个指针指向第一个节点,另一个指向剩余的链表的头部 35 | * 3. 循环遍历所有节点,最后返回新的头结点指针 36 | * 37 | */ 38 | class Solution { 39 | public: 40 | ListNode* ReverseList(ListNode* pHead) { 41 | if (pHead == NULL) 42 | return NULL; 43 | 44 | ListNode *pTemp = pHead; 45 | pHead = pHead->next; 46 | pTemp->next = NULL; 47 | while (pHead) { 48 | ListNode *p = pHead->next; 49 | pHead->next = pTemp; 50 | pTemp = pHead; 51 | pHead = p; 52 | } 53 | 54 | return pTemp; 55 | } 56 | }; -------------------------------------------------------------------------------- /src/20_min_in_stack.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | /* 19 | * 题目描述: 20 | * 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数 21 | * (时间复杂度应为O(1))。 22 | * 23 | * 解题思路: 24 | * 使用两个栈,一个栈用作正常的pop和push操作,另一个栈来根据压栈或出栈的数据 25 | * 来更新其中记录的当前栈最小值。 26 | * 27 | */ 28 | 29 | class Solution { 30 | public: 31 | void push(int value) { 32 | if (stack1.size() == 0) { 33 | stack1.push(value); 34 | stack2.push(value); 35 | } else { 36 | stack1.push(value); 37 | int min = stack2.top(); 38 | if (min>=value) 39 | stack2.push(value); 40 | } 41 | } 42 | void pop() { 43 | assert(stack1.size()>0 && stack2.size()>0); 44 | int pop = stack1.top(); 45 | stack1.pop(); 46 | if (pop == stack2.top()) 47 | stack2.pop(); 48 | } 49 | int top() { 50 | assert(stack1.size()>0); 51 | return stack1.top(); 52 | } 53 | int min() { 54 | assert(stack2.size()>0); 55 | return stack2.top(); 56 | } 57 | stack stack1; 58 | stack stack2; 59 | }; -------------------------------------------------------------------------------- /src/Fibonacci.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/Fibonacci.cpp -------------------------------------------------------------------------------- /src/accumulate.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/accumulate.cpp -------------------------------------------------------------------------------- /src/add_two_numbers.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/add_two_numbers.cpp -------------------------------------------------------------------------------- /src/bfs.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/bfs.hpp -------------------------------------------------------------------------------- /src/common_parent_in_tree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/common_parent_in_tree.cpp -------------------------------------------------------------------------------- /src/construct_binary_tree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/construct_binary_tree.cpp -------------------------------------------------------------------------------- /src/continous_cards.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/continous_cards.cpp -------------------------------------------------------------------------------- /src/continues_squence_with_sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | #include 19 | 20 | using namespace std; 21 | 22 | void printRange(int small, int big) 23 | { 24 | cout << "squence: " << endl; 25 | while(small<=big) { 26 | cout << small++ << ' '; 27 | } 28 | cout << endl; 29 | } 30 | 31 | void findNumbersWithSum(int sum) 32 | { 33 | int small = 1; 34 | int big = 2; 35 | int end = (sum + 1) / 2; 36 | int curSum = small + big; 37 | 38 | while (small < end) { 39 | if (curSum == sum) { 40 | printRange(small, big); 41 | big++; 42 | curSum += big; 43 | } 44 | 45 | if (curSum > sum) { 46 | curSum -= small; 47 | small++; 48 | } 49 | 50 | if (curSum < sum) { 51 | big++; 52 | curSum += big; 53 | } 54 | } 55 | 56 | } 57 | 58 | int main(int argc, char **argv) 59 | { 60 | cout << "Test 1: " << endl; 61 | findNumbersWithSum(0); 62 | 63 | cout << "Test 2: " << endl; 64 | findNumbersWithSum(1); 65 | 66 | cout << "Test 3: " << endl; 67 | findNumbersWithSum(3); 68 | 69 | cout << "Test 4: " << endl; 70 | findNumbersWithSum(15); 71 | 72 | cout << "Test 5: " << endl; 73 | findNumbersWithSum(30); 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /src/convert_binary_search_tree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/convert_binary_search_tree.cpp -------------------------------------------------------------------------------- /src/delete_kth_node_from_end.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/delete_kth_node_from_end.cpp -------------------------------------------------------------------------------- /src/delete_node_in_list.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/delete_node_in_list.cpp -------------------------------------------------------------------------------- /src/dfs.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/dfs.hpp -------------------------------------------------------------------------------- /src/dices_probability.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/dices_probability.cpp -------------------------------------------------------------------------------- /src/find_minimum_in_rotated_array.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/find_minimum_in_rotated_array.cpp -------------------------------------------------------------------------------- /src/first_common_node.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/first_common_node.cpp -------------------------------------------------------------------------------- /src/first_not_repeat_char.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | #include 19 | //#include 20 | #include 21 | 22 | using namespace std; 23 | 24 | int getFirstNotRepeatChar(char *str) 25 | { 26 | if (!str) 27 | return -1; 28 | 29 | int chars[256]; 30 | char *pChar = str; 31 | 32 | for (int i=0; i<256; i++) 33 | chars[i] = 0; 34 | //这里的index不用减去 'a',因为字符还可能是数子字符~ 35 | while (*pChar != '0') { 36 | int index = *pChar - 'a'; 37 | chars[index]++; 38 | pChar++; 39 | } 40 | 41 | pChar = str; 42 | while (*pChar != '0') { 43 | int index = *pChar - 'a'; 44 | if (chars[index] == 1) 45 | return *pChar; 46 | pChar++; 47 | } 48 | } 49 | 50 | int main(int argc, char **argv) 51 | { 52 | //string *str = "ddbscagds"; 53 | //string str("ddbscagds"); 54 | char str[] = "dd0bscagds"; 55 | 56 | char firstNotRepeatChar = getFirstNotRepeatChar(str); 57 | cout << firstNotRepeatChar << endl; 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /src/inverse_pairs.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/inverse_pairs.cpp -------------------------------------------------------------------------------- /src/kmp/1_kmp.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/kmp/1_kmp.cpp -------------------------------------------------------------------------------- /src/knapsack/1_knapsack.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/knapsack/1_knapsack.cpp -------------------------------------------------------------------------------- /src/knapsack/1_knapsack_acwing.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/knapsack/1_knapsack_acwing.cpp -------------------------------------------------------------------------------- /src/knapsack/2_knapsack.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/knapsack/2_knapsack.cpp -------------------------------------------------------------------------------- /src/knapsack/2_knapsack_acwing.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/knapsack/2_knapsack_acwing.cpp -------------------------------------------------------------------------------- /src/knapsack/3_knapsack_acwing_i.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | #include 19 | 20 | using namespace std; 21 | 22 | int main() 23 | { 24 | int N, V; 25 | cin>>N>>V; 26 | 27 | int dp[V+1]; 28 | for (int i=0; i<=V; i++) dp[i] = 0; 29 | 30 | for (int i=0; i>v>>w>>s; 33 | 34 | for (int j=V; j>0; j--) { 35 | for (int k=1; k<=s && k*v<=j; k++) { 36 | dp[j] = max(dp[j-k*v]+k*w, dp[j]); 37 | } 38 | } 39 | } 40 | 41 | cout << dp[V]; 42 | 43 | return 0; 44 | } -------------------------------------------------------------------------------- /src/knapsack/4_knapscak_acwing_ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | #include 19 | #include 20 | 21 | using namespace std; 22 | 23 | struct Split { 24 | int v, w; 25 | }; 26 | 27 | int main() 28 | { 29 | int N, V; 30 | cin>>N>>V; 31 | 32 | int dp[V+1]; 33 | for (int i=0; i<=V; i++) dp[i] = 0; 34 | 35 | vector splits; 36 | 37 | for (int i=0; i>v>>w>>s; 40 | 41 | for (unsigned int k=1; k<=s; k = k<<1) { 42 | // for (; k<=s; k*=2) { 43 | s -= k; 44 | splits.push_back({k*v, k*w}); 45 | // cout << k; 46 | } 47 | 48 | if (s>0) { 49 | splits.push_back({s*v, s*w}); 50 | } 51 | } 52 | 53 | // for (int i=0; i=v; j--) { 59 | dp[j] = max(dp[j-v]+w, dp[j]); 60 | } 61 | } 62 | 63 | cout << dp[V]; 64 | 65 | return 0; 66 | } -------------------------------------------------------------------------------- /src/knapsack/7_knapsack_acwing.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | #include 19 | 20 | using namespace std; 21 | 22 | int main() 23 | { 24 | int N, V, M; 25 | cin>>N>>V>>M; 26 | /* 27 | 局部变量、数组必须自己初始化 28 | 否则程序无法正确执行,不想全局变量会被自动初始化 29 | 局部变量都是随机数 30 | */ 31 | int dp[V+1][M+1]; 32 | for (int i=0; i<=V; i++) 33 | for (int j=0; j<=M; j++) 34 | dp[i][j] = 0; 35 | 36 | for (int i=0; i>v>>m>>w; 39 | /* 从大到小枚举 */ 40 | for (int j=V; j>=v; j--) { 41 | for (int k=M; k>=m; k--) { 42 | dp[j][k] = max(dp[j][k], dp[j-v][k-m] + w); 43 | } 44 | } 45 | } 46 | 47 | cout << dp[V][M]; 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /src/last_num_in_circle.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/last_num_in_circle.cpp -------------------------------------------------------------------------------- /src/left_rotate_string.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | #include 19 | 20 | using namespace std; 21 | 22 | void reverse(char *start, char *end) 23 | { 24 | if (!start || !end) 25 | return; 26 | 27 | while (start < end) { 28 | char temp = *start; 29 | *start = *end; 30 | *end = temp; 31 | start++; 32 | end--; 33 | } 34 | } 35 | 36 | void left_rotate(char *data, int offset) 37 | { 38 | if (!data || offset <=0) 39 | return; 40 | 41 | int length = 0; 42 | char *start = data; 43 | //cout << "step 1" << endl; 44 | while (*start != '\0') { 45 | length++; 46 | start++; 47 | } 48 | 49 | if (offset >= length || length < offset) 50 | return; 51 | 52 | start = data; 53 | char *end = data + offset - 1; 54 | //cout << "step 2" << endl; 55 | reverse(start, end); 56 | //cout << "step 3" << endl; 57 | start = end + 1; 58 | end = data + length -1; 59 | reverse(start, end); 60 | //cout << "step 4" << endl; 61 | start = data; 62 | reverse(start, end); 63 | } 64 | 65 | int main(int argc, char **argv) 66 | { 67 | char sentence[] = "sentence"; 68 | left_rotate(sentence, 3); 69 | cout << sentence << endl; 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /src/merge_sorted_lists.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/merge_sorted_lists.cpp -------------------------------------------------------------------------------- /src/min_in_stack.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/min_in_stack.cpp -------------------------------------------------------------------------------- /src/mirror_of_binary_tree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/mirror_of_binary_tree.cpp -------------------------------------------------------------------------------- /src/more_than_half_number.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/more_than_half_number.cpp -------------------------------------------------------------------------------- /src/number_of_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/number_of_1.cpp -------------------------------------------------------------------------------- /src/number_of_1_in_binary.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/number_of_1_in_binary.cpp -------------------------------------------------------------------------------- /src/number_of_k.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/number_of_k.cpp -------------------------------------------------------------------------------- /src/numbers_appear_once.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/numbers_appear_once.cpp -------------------------------------------------------------------------------- /src/path_in_tree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/path_in_tree.cpp -------------------------------------------------------------------------------- /src/power.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/power.cpp -------------------------------------------------------------------------------- /src/print_1_to_maximum_value.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/print_1_to_maximum_value.cpp -------------------------------------------------------------------------------- /src/print_from_top_to_bottom.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/print_from_top_to_bottom.cpp -------------------------------------------------------------------------------- /src/print_list_tail_to_head.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/print_list_tail_to_head.cpp -------------------------------------------------------------------------------- /src/print_matrix.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/print_matrix.cpp -------------------------------------------------------------------------------- /src/queue_with_two_stack.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/queue_with_two_stack.cpp -------------------------------------------------------------------------------- /src/quick_sort.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/quick_sort.cpp -------------------------------------------------------------------------------- /src/quick_sort.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | #ifndef __QUICK_SORT_HPP__ 19 | #define __QUICK_SORT_HPP__ 20 | 21 | /* 22 | O(NlogN) 23 | */ 24 | 25 | void quick_sort(int nums[], int start, int end) 26 | { 27 | if (start>=end) 28 | return; 29 | 30 | int left = start, right = end; 31 | int ref = nums[start]; 32 | while (left=ref) 34 | right--; 35 | nums[left] = nums[right]; 36 | 37 | while (left 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | #ifndef __SELECTION_SORT_HPP__ 19 | #define __SELECTION_SORT_HPP__ 20 | 21 | /* 22 | O(N^2) 23 | */ 24 | 25 | void selection_sort(int nums[], int start, int end) 26 | { 27 | if (start>=end) 28 | return; 29 | 30 | int min, index; 31 | for (int i=0; i<=end; i++) { 32 | min = nums[i]; 33 | index = i; 34 | for (int j=i; j<=end; j++) { 35 | if (nums[j] < min) { 36 | min = nums[j]; 37 | index = j; 38 | } 39 | } 40 | 41 | nums[index] = nums[i]; 42 | nums[i] = min; 43 | } 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /src/sequence_of_bst.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/sequence_of_bst.cpp -------------------------------------------------------------------------------- /src/singleton.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/singleton.cpp -------------------------------------------------------------------------------- /src/sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | #include 19 | 20 | #include "quick_sort.hpp" 21 | #include "selection_sort.hpp" 22 | 23 | using namespace std; 24 | 25 | int main() 26 | { 27 | #define LENGTH 15 28 | 29 | int nums[LENGTH] = {5, 3, 2, 7, 3, 6, 0, 5, 10, 1, -5, 15, -15, 11, 8}; 30 | 31 | //quick_sort(nums, 0, LENGTH-1); 32 | selection_sort(nums, 0, LENGTH-1); 33 | 34 | for (int i=0; i 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | #include 19 | 20 | template 21 | class BubbleSort { 22 | public: 23 | BubbleSort() {} 24 | ~BubbleSort() {} 25 | 26 | static void sort(Dtype *data, int length); 27 | }; 28 | 29 | template 30 | void BubbleSort::sort(Dtype *data, int length) 31 | { 32 | if (data == NULL || length <=0) 33 | return; 34 | 35 | int end = length; 36 | for (int i=0; i data[j]) { 39 | Dtype temp = data[j-1]; 40 | data[j-1] = data[j]; 41 | data[j] = temp; 42 | } 43 | } 44 | end--; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/sort/heap_sort_link.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | Node *parent, *lson, *rson; 6 | void *data; 7 | }; 8 | 9 | struct Data { 10 | int val; 11 | Node *node; 12 | }; 13 | 14 | Node *create_node(void *data) 15 | { 16 | Node *node = new Node(); 17 | node->data = data; 18 | node->parent = 0; 19 | node->lson = 0; 20 | node->rson = 0; 21 | 22 | return node; 23 | } 24 | 25 | Node *insert_node(Node *head, Node *tail, Node *next) 26 | { 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/sort/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/sort/main.cpp -------------------------------------------------------------------------------- /src/sort/merge.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/sort/merge.hpp -------------------------------------------------------------------------------- /src/sort/quick.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/sort/quick.hpp -------------------------------------------------------------------------------- /src/sort/shell.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/sort/shell.hpp -------------------------------------------------------------------------------- /src/sort/straight_insert.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/sort/straight_insert.hpp -------------------------------------------------------------------------------- /src/sort/straight_select.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | #include 19 | 20 | template 21 | class StraightSelectSort { 22 | public: 23 | StraightSelectSort() {} 24 | ~StraightSelectSort() {} 25 | 26 | static void sort(Dtype *data, int length); 27 | }; 28 | 29 | template 30 | void StraightSelectSort::sort(Dtype *data, int length) 31 | { 32 | if (data == NULL || length <=0) 33 | return; 34 | 35 | Dtype min; 36 | int start = 0, min_index; 37 | while (start < length) { 38 | min = data[start]; 39 | min_index = start; 40 | for (int i=start; i data[i]) { 42 | min = data[i]; 43 | min_index = i; 44 | } 45 | } 46 | if (start != min_index) { 47 | Dtype temp = data[start]; 48 | data[start] = min; 49 | data[min_index] = temp; 50 | } 51 | start++; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/sort_array_for_min_number.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/sort_array_for_min_number.cpp -------------------------------------------------------------------------------- /src/stack_push_pop_order.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/stack_push_pop_order.cpp -------------------------------------------------------------------------------- /src/string_permutation.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/string_permutation.cpp -------------------------------------------------------------------------------- /src/substructure_in_tree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiauZhang/algorithms/6770cdfd72a301879a8638a0288aad424bae638f/src/substructure_in_tree.cpp -------------------------------------------------------------------------------- /src/two_numbers_with_sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | #include 19 | 20 | using namespace std; 21 | 22 | bool findNumbersWithSum(int *data, int length, int sum, int *num1, int *num2) 23 | { 24 | int *head = data, *tail = data + length -1; 25 | 26 | while (head < tail) { 27 | if (*head + *tail < sum) 28 | head++; 29 | else if (*head + *tail > sum) 30 | tail--; 31 | else if (*head + *tail == sum) { 32 | *num1 = *head; 33 | *num2 = *tail; 34 | return true; 35 | } 36 | } 37 | 38 | return false; 39 | } 40 | 41 | int main(int argc, char **argv) 42 | { 43 | int data[] = {1, 2, 4, 6, 8, 9, 11}; 44 | int num1, num2; 45 | bool success = findNumbersWithSum(data, 7, 14, &num1, &num2); 46 | cout << "success: " << success << endl; 47 | if (success) 48 | cout << "num1: " << num1 << " num2: " << num2 << endl; 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /src/xfs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright(c) 2019 Jiau Zhang 3 | * For more information see 4 | * 5 | * This repo is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation 8 | * 9 | * It is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with THIS repo. If not, see . 16 | */ 17 | 18 | #include 19 | #include 20 | 21 | #include "dfs.hpp" 22 | #include "bfs.hpp" 23 | 24 | using namespace std; 25 | 26 | void show_matrix(MatInt &matrix) 27 | { 28 | for (auto x: matrix) { 29 | for (auto y: x) { 30 | cout << y << ' '; 31 | } 32 | cout << endl; 33 | } 34 | } 35 | 36 | int main() 37 | { 38 | MatInt matrix = { 39 | { 0, 0, 0, 0, 0, 0, 0, 0 }, 40 | { 0, 0, 0, 1, 1, 1, 1, 0 }, 41 | { 0, 0, 0, 1, 0, 0, 1, 0 }, 42 | { 1, 1, 0, 1, 0, 1, 1, 0 }, 43 | { 0, 0, 0, 1, 0, 0, 1, 1 }, 44 | { 0, 0, 1, 1, 0, 0, 0, 0 }, 45 | { 0, 0, 0, 0, 0, 1, 0, 0 }, 46 | { 0, 0, 0, 0, 0, 1, 0, 0 } 47 | }; 48 | 49 | show_matrix(matrix); 50 | 51 | cout << "dfs:" << endl; 52 | dfs(matrix, 2, matrix[0].size()-1, matrix.size()-1, matrix[0].size()-1); 53 | 54 | //cout << "bfs:" << endl; 55 | //bfs(matrix, 2, matrix[0].size()-1, matrix.size()-1, matrix[0].size()-1); 56 | //show_matrix(matrix); 57 | 58 | cout << "bfs_v2:" << endl; 59 | bfs_v2(matrix, 2, matrix[0].size()-1, matrix.size()-1, matrix[0].size()-1); 60 | show_matrix(matrix); 61 | 62 | return 0; 63 | } 64 | --------------------------------------------------------------------------------