From 95ee3c2a624a1edba14b7b8fc536e9a28e960c90 Mon Sep 17 00:00:00 2001 From: Zhaowei Date: Wed, 19 Dec 2018 20:17:40 +0800 Subject: [PATCH 1/4] binary search --- Makefile | 2 +- README.md | 3 ++ src/0704_binary_search/binary_search.go | 39 ++++++++++++++++++++ src/0704_binary_search/binary_search_test.go | 13 +++++++ src/README.md | 1 + 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/0704_binary_search/binary_search.go create mode 100644 src/0704_binary_search/binary_search_test.go diff --git a/Makefile b/Makefile index 7054ecb..bda50bc 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ GOGET=$(GOCMD) get test: @echo "unit test" - $(GOTEST) -v ./... + $(GOTEST) ./... clean: @echo "clean test cache" diff --git a/README.md b/README.md index 2130648..cda47ad 100644 --- a/README.md +++ b/README.md @@ -39,5 +39,8 @@ continually updating 😃. * [435. Non-overlapping Intervals](./src/0435_non_overlapping_intervals/greedy_solution.go) * [455. Assign Cookies](./src/0455_assign_cookies/assign_cookies.go) +### Binary Search +* [704. Binary Search](./src/0704_binary_search/binary_search.go) +
diff --git a/src/0704_binary_search/binary_search.go b/src/0704_binary_search/binary_search.go new file mode 100644 index 0000000..342e16e --- /dev/null +++ b/src/0704_binary_search/binary_search.go @@ -0,0 +1,39 @@ +/* +704. Binary Search +https://leetcode.com/problems/binary-search/ + +Given a sorted (in ascending order) integer array nums of n elements and a target value, +write a function to search target in nums. If target exists, then return its index, otherwise return -1. + +Note: + 1. You may assume that all elements in nums are unique. + 2. n will be in the range [1, 10000]. + 3. The value of each element in nums will be in the range [-9999, 9999]. +*/ + +// time: 2018-12-19 + +package binarysearch + +// iterative +// Time complexity: O(log(n)) +// Space complexity: O(1) +func search(nums []int, target int) int { + var ( + l int + r = len(nums) - 1 + ) // 在[l...r]区间寻找target + + for l <= r { // 当l==r时,区间依然是有效的。 + mid := l + (r-l)/2 // (l+r)/2 可能会整型益处 + if nums[mid] == target { + return mid + } + if nums[mid] > target { + r = mid - 1 + } else { + l = mid + 1 + } + } + return -1 +} diff --git a/src/0704_binary_search/binary_search_test.go b/src/0704_binary_search/binary_search_test.go new file mode 100644 index 0000000..f827348 --- /dev/null +++ b/src/0704_binary_search/binary_search_test.go @@ -0,0 +1,13 @@ +package binarysearch + +import "testing" + +func TestSearch(t *testing.T) { + nums := []int{-1, 0, 3, 5, 9, 12} + target := 9 + expected := 4 + + if res := search(nums, target); res != expected { + t.Errorf("expected %d, got %d", expected, res) + } +} diff --git a/src/README.md b/src/README.md index 9f058a4..46a7bb7 100644 --- a/src/README.md +++ b/src/README.md @@ -40,5 +40,6 @@ |0447|[Number of Boomerangs](./0447_number_of_boomerangs/number_of_boomerangs.go)|Easy|| |0454|[4Sum II](./0454_4sum2/4sum2.go)|Medium|| |0455|[Assign Cookies](./0455_assign_cookies/assign_cookies.go)|Easy|*`greedy algorithm`*| +|0704|[Binary Search](0704_binary_search/binary_search.go)|Easy|*`binary search`*| |0728|[Self Dividing Numbers](./0728_self_dividing_numbers/self_dividing_numbers.go)|Easy|| |0747|[Largest Number At Least Twice of Others](./0455_assign_cookies/largest_number_at_least_twice_of_others.go)|Easy|| From d90b99a9d348d46d078df6a34b27bd70ef44857d Mon Sep 17 00:00:00 2001 From: Zhaowei Date: Wed, 19 Dec 2018 20:26:20 +0800 Subject: [PATCH 2/4] add more test case for binary search --- src/0704_binary_search/binary_search_test.go | 26 ++++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/0704_binary_search/binary_search_test.go b/src/0704_binary_search/binary_search_test.go index f827348..28575f7 100644 --- a/src/0704_binary_search/binary_search_test.go +++ b/src/0704_binary_search/binary_search_test.go @@ -3,11 +3,27 @@ package binarysearch import "testing" func TestSearch(t *testing.T) { - nums := []int{-1, 0, 3, 5, 9, 12} - target := 9 - expected := 4 + type arg struct { + nums []int + target int + } + + testCases := []arg{ + arg{ + nums: []int{-1, 0, 3, 5, 9, 12}, + target: 9, + }, + arg{ + nums: []int{-1, 0, 3, 5, 9, 12}, + target: 20, + }, + } + + expected := []int{4, -1} - if res := search(nums, target); res != expected { - t.Errorf("expected %d, got %d", expected, res) + for index, data := range testCases { + if res := search(data.nums, data.target); res != expected[index] { + t.Errorf("expected %d, got %d", expected[index], res) + } } } From 60750d0c2a7467ff08487b5c5bac50791d6fc5a4 Mon Sep 17 00:00:00 2001 From: Zhaowei Date: Wed, 19 Dec 2018 20:29:10 +0800 Subject: [PATCH 3/4] add more test case for binary search --- src/0704_binary_search/binary_search_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/0704_binary_search/binary_search_test.go b/src/0704_binary_search/binary_search_test.go index 28575f7..4a019d0 100644 --- a/src/0704_binary_search/binary_search_test.go +++ b/src/0704_binary_search/binary_search_test.go @@ -15,7 +15,7 @@ func TestSearch(t *testing.T) { }, arg{ nums: []int{-1, 0, 3, 5, 9, 12}, - target: 20, + target: -3, }, } From 5f0386c2afbfc0e7111c17ae77645567bfafe847 Mon Sep 17 00:00:00 2001 From: Zhaowei Date: Wed, 19 Dec 2018 21:19:54 +0800 Subject: [PATCH 4/4] update readme --- README.md | 24 ++++++++++++++++++- .../all_anagrams_in_a_string.go | 2 ++ src/README.md | 10 ++++---- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index cda47ad..73171b5 100644 --- a/README.md +++ b/README.md @@ -14,15 +14,25 @@ continually updating 😃. * [209. Minimum Size Subarray Sum](./src/0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)   *`sliding window`* * [283. Move Zeroes(solution1)](./src/0283_move_zeroes/move_zeroes.go)   *`sliding window`* * [283. Move Zeroes(solution2)](./src/0283_move_zeroes/move_zeroes2.go)   *`sliding window`* +* [349. Intersection of Two Arrays](./src/0349_intersection_of_2_arrays/intersection_of_two_arrays.go)   *`set`* +* [350. Intersection of Two Arrays II](./src/0350_intersection_of_two_arrays2/intersection_of_two_arrays2.go)   *`hash table`* +* [447. Number of Boomerangs](./src/0447_number_of_boomerangs/number_of_boomerangs.go)   *`hash table`* +* [454. 4Sum II](./src/0454_4sum2/4sum2.go)   *`hash table`* +* [747. Largest Number At Least Twice of Others](./src/0747_largest_number_at_least_twice_of_others/largest_number_at_least_twice_of_others.go) ### String * [3. Longest Substring Without Repeating Characters](./src/0003_longest_substring_without_repeating_characters/longest_substring_without_repeating_characters.go)   *`sliding window;`*  *`hash table`* * [17. Letter Combinations of a Phone Number](./src/0017_letter_combination_of_a_phone_number/letter_combination_of_phone_number.go)   *`tree`* +* [20. Valid Parentheses](./src/0020_valid_parentheses/valid_parentheses.go)   *`stack`* * [67. Add Binary](./src/0067_add_binary/add_binary.go)   *`brute force`* * [76. Minimum Window Substring](./src/0076_minimum_window_substring/minimum_window_substring.go)    *`sliding window`* +* [438. Find All Anagrams in a String](./src/0438_all_anagrams_in_a_string/all_anagrams_in_a_string.go)   *`sliding window`* ### Linked List * [2. Add Two Numbers](./src/0002_add_two_numbers/add_two_numbers.go)   *`recursion;`*  *`math`* +* [21. Merge Two Sorted Lists](./src/0021_merge_two_sorted_lists/mergeTwoLists.go) +* [25. Reverse Nodes in k-Group](./src/0025_reverse_nodes_in_k_group/reverse_node_k_group.go) +* [61. Rotate List](./src/0061_rotate_list/rotate_list.go) ### Dynamic Programming * [62. Unique Paths](./src/0062_unique_paths/unique_paths.go)   *`array`* @@ -33,12 +43,24 @@ continually updating 😃. * [198. House Robber](./src/0198_house_robber/house_robber.go) * [300. Longest Increasing Subsequence](./src/0300_longest_increasing_subsequence/lis.go) * [343. Integer Break](./src/0343_integer_break/integer_break.go)   *`math`* +* [376. Wiggle Subsequence](./src/0376_wiggle_subsequence/wiggle_subsequence.go)   *`greedy;`*  *`dynamic programming`* +* [416. Partition Equal Subset Sum](./src/0416_partition_equal_subset_sum/partition_equal_subset_sum.go)  *`0-1 knapsack problem`* +* [435. Non-overlapping Intervals](./src/0435_non_overlapping_intervals/dp_solution.go)   *`greedy;`*  *`dynamic programming`*  *`0-1 knapsack problem`* ### Greedy * [392. Is Subsequence](./src/0392_is_subsequence/is_subsequence.go) -* [435. Non-overlapping Intervals](./src/0435_non_overlapping_intervals/greedy_solution.go) +* [435. Non-overlapping Intervals](./src/0435_non_overlapping_intervals/greedy_solution.go)   *`greedy;`*  *`dynamic programming`* * [455. Assign Cookies](./src/0455_assign_cookies/assign_cookies.go) +### Binary Tree +* [94. Binary Tree Inorder Traversal](./src/0094_binary_tree_inorder_traversal/binary_tree_inorder_traversal.go)   *`stack`* +* [100. Same Tree](./src/0100_same_tree/same_tree.go)   *`dfs`* +* [101. Symmetric Tree](./src/0101_symmetric_tree/symmetric_tree.go)   *`dfs;`*  *`bfs;`* +* [107. Binary Tree Level Order Traversal II](./src/0107_binary_tree_level_order_traversal_2/binary_tree_level_order_traversal2.go)   *`bfs`* +* [111. Minimum Depth of Binary Tree](./src/0111_minimum_depth_of_binary_tree/minimum_depth_of_binary_tree.go)   *`dfs`* +* [112. Path Sum](./src/0112_path_sum/path_sum.go)   *`dfs`* +* [226. Invert Binary Tree](./src/0226_invert_binary_tree/invert_binary_tree.go) + ### Binary Search * [704. Binary Search](./src/0704_binary_search/binary_search.go) diff --git a/src/0438_all_anagrams_in_a_string/all_anagrams_in_a_string.go b/src/0438_all_anagrams_in_a_string/all_anagrams_in_a_string.go index d71b451..e3c3d0e 100644 --- a/src/0438_all_anagrams_in_a_string/all_anagrams_in_a_string.go +++ b/src/0438_all_anagrams_in_a_string/all_anagrams_in_a_string.go @@ -1,4 +1,6 @@ /* +438. Find All Anagrams in a String + Source: https://leetcode.com/problems/find-all-anagrams-in-a-string/submissions/ Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. diff --git a/src/README.md b/src/README.md index 46a7bb7..f8aaff4 100644 --- a/src/README.md +++ b/src/README.md @@ -6,10 +6,10 @@ |0002|[Add Two Numbers](0002_add_two_numbers/add_two_numbers.go)|Medium|*`linked list`*| |0003|[Longest Substring Without Repeating Characters](0003_longest_substring_without_repeating_characters/longest_substring_without_repeating_characters.go)|Medium|*`sliding window`*| |0017|[Letter Combinations of a Phone Number](0017_letter_combination_of_a_phone_number/letter_combination_of_phone_number.go)|Medium|*`tree`*| -|0020|[Valid Parentheses](0020_valid_parentheses/valid_parentheses.go)|Easy|| -|0021|[Merge Two Sorted Lists](0021_merge_two_sorted_lists/mergeTwoLists.go)|Easy|| -|0025|[Reverse Nodes in k-Group](./0025_reverse_nodes_in_k_group/reverse_node_k_group.go)|Hard|| -|0061|[Rotate List](./0061_rotate_list/rotate_list.go)|Medium|| +|0020|[Valid Parentheses](0020_valid_parentheses/valid_parentheses.go)|Easy|*`string;`* *`stack`*| +|0021|[Merge Two Sorted Lists](0021_merge_two_sorted_lists/mergeTwoLists.go)|Easy|*`linked list`*| +|0025|[Reverse Nodes in k-Group](./0025_reverse_nodes_in_k_group/reverse_node_k_group.go)|Hard|*`linked list`*| +|0061|[Rotate List](./0061_rotate_list/rotate_list.go)|Medium|*`linked list`*| |0062|[Unique Paths](./0062_unique_paths/unique_paths.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*| |0063|[Unique Paths 2](./0063_unique_paths_2/unique_paths2.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*| |0064|[Minimum Path Sum](./0064_minimum_path_sum/minimum_path_sum.go)|Medium|*`dynamic programming;`* *` dfs`*| @@ -42,4 +42,4 @@ |0455|[Assign Cookies](./0455_assign_cookies/assign_cookies.go)|Easy|*`greedy algorithm`*| |0704|[Binary Search](0704_binary_search/binary_search.go)|Easy|*`binary search`*| |0728|[Self Dividing Numbers](./0728_self_dividing_numbers/self_dividing_numbers.go)|Easy|| -|0747|[Largest Number At Least Twice of Others](./0455_assign_cookies/largest_number_at_least_twice_of_others.go)|Easy|| +|0747|[Largest Number At Least Twice of Others](./0747_largest_number_at_least_twice_of_others/largest_number_at_least_twice_of_others.go)|Easy||