From c5cb2d92ad82af000bab0a4cc660c68d5922f673 Mon Sep 17 00:00:00 2001 From: Zhaowei Date: Thu, 20 Dec 2018 16:15:39 +0800 Subject: [PATCH 1/4] sqrt x solved --- README.md | 1 + src/0069_sqrtx/sqrtx.go | 41 ++++++++++++++++++++++++++++++++++++ src/0069_sqrtx/sqrtx_test.go | 14 ++++++++++++ src/README.md | 1 + 4 files changed, 57 insertions(+) create mode 100644 src/0069_sqrtx/sqrtx.go create mode 100644 src/0069_sqrtx/sqrtx_test.go diff --git a/README.md b/README.md index 915fb14..d2a8573 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ continually updating 😃. ### Binary Search * [33. Search in Rotated Sorted Array](./src/0033_search_in_rotated_sorted_array/search_in_rotated_sorted_array.go)   *`array;`*  *`binary search`* * [34. Find First and Last Position of Element in Sorted Array](./src/0034_find_first_and_last_position_of_element_in_sorted_array/find_first_and_last_position_of_element_in_sorted_array.go)   *`array;`*  *`binary search`* +* [69. Sqrt(x)](./src/0069_sqrtx/sqrtx.go)   *`math`* * [704. Binary Search](./src/0704_binary_search/binary_search.go)
diff --git a/src/0069_sqrtx/sqrtx.go b/src/0069_sqrtx/sqrtx.go new file mode 100644 index 0000000..d858829 --- /dev/null +++ b/src/0069_sqrtx/sqrtx.go @@ -0,0 +1,41 @@ +/* +69. Sqrt(x) +https://leetcode.com/problems/sqrtx/ + +Implement int sqrt(int x). + +Compute and return the square root of x, where x is guaranteed to be a non-negative integer. + +Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned. +*/ + +// time: 2018-12-20 + +package sqrtx + +// binary search +// time complexity: O(logn) +// space complexity: O(1) +func mySqrt(x int) int { + var ( + l int + r = x // 在0~x范围内寻找平凡根 + ) + + for l <= r { + mid := l + (r-l)>>1 //位运算更高效 + if mid*mid == x { + return mid + } + + if mid*mid < x { + if (mid+1)*(mid+1) > x { + return mid // 返回整数部分 + } + l = mid + 1 + } else { + r = mid - 1 + } + } + return l +} diff --git a/src/0069_sqrtx/sqrtx_test.go b/src/0069_sqrtx/sqrtx_test.go new file mode 100644 index 0000000..4f0dc7b --- /dev/null +++ b/src/0069_sqrtx/sqrtx_test.go @@ -0,0 +1,14 @@ +package sqrtx + +import "testing" + +func TestMySqrt(t *testing.T) { + testCases := []int{66, 99, 9} + expected := []int{8, 9, 3} + + for index, data := range testCases { + if res := mySqrt(data); res != expected[index] { + t.Errorf("expected %d, got %d", expected[index], res) + } + } +} diff --git a/src/README.md b/src/README.md index dd0b73b..125ea35 100644 --- a/src/README.md +++ b/src/README.md @@ -16,6 +16,7 @@ |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`*| |0067|[add Binary](./0067_add_binary/add_binary.go)|Easy|| +|0069|[Sqrt(x)](0069_sqrtx/sqrtx.go)|Easy|*`binary search`*| |0070|[Climbing Stairs](./0070_climbing_stairs/climbing_stairs.go)|Easy|*`dynamic programming`*| |0076|[Minimum Window Substring](./0076_minimum_window_substring/minimum_window_substring.go)|Hard|*`sliding window`*| |0094|[Binary Tree Inorder Traversal](./0094_binary_tree_inorder_traversal/binary_tree_inorder_traversal.go)|Medium|*`binary tree`*| From c8c3769b05ad2b016d66360aa95b2fbc9dde68d6 Mon Sep 17 00:00:00 2001 From: Zhaowei Date: Thu, 20 Dec 2018 16:17:34 +0800 Subject: [PATCH 2/4] gofmt -s -w --- ..._last_position_of_element_in_sorted_array_test.go | 12 ++++++------ src/0112_path_sum/path_sum_test.go | 4 ++-- .../invert_binary_tree_test.go | 2 +- .../all_anagrams_in_a_string_test.go | 6 +++--- src/0455_assign_cookies/assign_cookies_test.go | 4 ++-- src/0704_binary_search/binary_search_test.go | 4 ++-- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/0034_find_first_and_last_position_of_element_in_sorted_array/find_first_and_last_position_of_element_in_sorted_array_test.go b/src/0034_find_first_and_last_position_of_element_in_sorted_array/find_first_and_last_position_of_element_in_sorted_array_test.go index fe8bdef..31b9211 100644 --- a/src/0034_find_first_and_last_position_of_element_in_sorted_array/find_first_and_last_position_of_element_in_sorted_array_test.go +++ b/src/0034_find_first_and_last_position_of_element_in_sorted_array/find_first_and_last_position_of_element_in_sorted_array_test.go @@ -13,27 +13,27 @@ func TestSearchRange(t *testing.T) { } testCases := []arg{ - arg{ + { nums: []int{5, 7, 7, 8, 8, 10}, target: 8, }, - arg{ + { nums: []int{5, 7, 7, 8, 8, 10}, target: 6, }, - arg{ + { nums: []int{1}, target: 1, }, - arg{ + { nums: []int{}, target: 0, }, - arg{ + { nums: []int{2, 2}, target: 2, }, - arg{ + { nums: []int{1}, target: 0, }, diff --git a/src/0112_path_sum/path_sum_test.go b/src/0112_path_sum/path_sum_test.go index 48c4b01..cd0a544 100644 --- a/src/0112_path_sum/path_sum_test.go +++ b/src/0112_path_sum/path_sum_test.go @@ -30,8 +30,8 @@ type arg struct { func TestHasPathSum(t *testing.T) { testData := []arg{ - arg{nums: []interface{}{5, 4, 8, 11, nil, 13, 4, 7, 2, nil, nil, nil, 1}, sum: 22}, - arg{nums: []interface{}{}, sum: 0}, + {nums: []interface{}{5, 4, 8, 11, nil, 13, 4, 7, 2, nil, nil, nil, 1}, sum: 22}, + {nums: []interface{}{}, sum: 0}, } expectedData := []bool{true, false} for index, data := range testData { diff --git a/src/0226_invert_binary_tree/invert_binary_tree_test.go b/src/0226_invert_binary_tree/invert_binary_tree_test.go index a84f72d..a8d4d73 100644 --- a/src/0226_invert_binary_tree/invert_binary_tree_test.go +++ b/src/0226_invert_binary_tree/invert_binary_tree_test.go @@ -47,7 +47,7 @@ func TestInvertTree(t *testing.T) { } expectedData := [][]interface{}{ - []interface{}{4, 7, 2, 9, 6, 3, 1}, + {4, 7, 2, 9, 6, 3, 1}, } for index, data := range testData { diff --git a/src/0438_all_anagrams_in_a_string/all_anagrams_in_a_string_test.go b/src/0438_all_anagrams_in_a_string/all_anagrams_in_a_string_test.go index 4e75dc1..2c07ebf 100644 --- a/src/0438_all_anagrams_in_a_string/all_anagrams_in_a_string_test.go +++ b/src/0438_all_anagrams_in_a_string/all_anagrams_in_a_string_test.go @@ -11,15 +11,15 @@ func TestFindAnagrams(t *testing.T) { p string } cases := []arg{ - arg{ + { s: "cbaebabacd", p: "abc", }, - arg{ + { s: "abab", p: "ab", }, - arg{ + { p: "abab", s: "ab", }, diff --git a/src/0455_assign_cookies/assign_cookies_test.go b/src/0455_assign_cookies/assign_cookies_test.go index 3dfa60f..48121a6 100644 --- a/src/0455_assign_cookies/assign_cookies_test.go +++ b/src/0455_assign_cookies/assign_cookies_test.go @@ -9,11 +9,11 @@ type args struct { func TestAssignCookies(t *testing.T) { testData := []args{ - args{ + { g: []int{1, 2, 3}, s: []int{1, 1}, }, - args{ + { g: []int{1, 2}, s: []int{1, 2, 3}, }, diff --git a/src/0704_binary_search/binary_search_test.go b/src/0704_binary_search/binary_search_test.go index 4a019d0..4c3480f 100644 --- a/src/0704_binary_search/binary_search_test.go +++ b/src/0704_binary_search/binary_search_test.go @@ -9,11 +9,11 @@ func TestSearch(t *testing.T) { } testCases := []arg{ - arg{ + { nums: []int{-1, 0, 3, 5, 9, 12}, target: 9, }, - arg{ + { nums: []int{-1, 0, 3, 5, 9, 12}, target: -3, }, From 99604dac70c836bcb8ac2e6e87e4d6fb301297fd Mon Sep 17 00:00:00 2001 From: Zhaowei Date: Thu, 20 Dec 2018 16:19:55 +0800 Subject: [PATCH 3/4] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d2a8573..4b344ea 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ continually updating 😃. ### Binary Search * [33. Search in Rotated Sorted Array](./src/0033_search_in_rotated_sorted_array/search_in_rotated_sorted_array.go)   *`array;`*  *`binary search`* * [34. Find First and Last Position of Element in Sorted Array](./src/0034_find_first_and_last_position_of_element_in_sorted_array/find_first_and_last_position_of_element_in_sorted_array.go)   *`array;`*  *`binary search`* -* [69. Sqrt(x)](./src/0069_sqrtx/sqrtx.go)   *`math`* +* [69. Sqrt(x)](./src/0069_sqrtx/sqrtx.go)   *`math;`*  *`binary search`* * [704. Binary Search](./src/0704_binary_search/binary_search.go)
From 68137a724c3cea69e54a7123efc5a5ca834169b8 Mon Sep 17 00:00:00 2001 From: Zhaowei Date: Thu, 20 Dec 2018 16:25:11 +0800 Subject: [PATCH 4/4] add annotation for sqrtx.go --- src/0069_sqrtx/sqrtx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/0069_sqrtx/sqrtx.go b/src/0069_sqrtx/sqrtx.go index d858829..8195cd5 100644 --- a/src/0069_sqrtx/sqrtx.go +++ b/src/0069_sqrtx/sqrtx.go @@ -37,5 +37,5 @@ func mySqrt(x int) int { r = mid - 1 } } - return l + return x // 不会执行,如果x为负数,会执行到此处,但是不符合题目要求。 }