From a177043d3f6bc55048d3a4c32e751df06cf2f9ac Mon Sep 17 00:00:00 2001 From: lincongcong Date: Mon, 19 Jul 2021 14:33:01 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E4=BA=8C=E5=88=86=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E8=AF=A6=E8=A7=A3code=20in=20Go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../二分查找及其变种/二分查找详解.md | 114 +++++++++++++----- 1 file changed, 83 insertions(+), 31 deletions(-) diff --git a/animation-simulation/二分查找及其变种/二分查找详解.md b/animation-simulation/二分查找及其变种/二分查找详解.md index 74398a9..2fe2925 100644 --- a/animation-simulation/二分查找及其变种/二分查找详解.md +++ b/animation-simulation/二分查找及其变种/二分查找详解.md @@ -116,29 +116,56 @@ target < nums[mid] 则在左半区间继续进行搜索,即 right = mid -1; ![二分查找2](https://img-blog.csdnimg.cn/img_convert/eb648f86b4ada5b32afc7a52e78d9953.gif) -下面我们来看一下二分查找的代码,可以认真思考一下 if 语句的条件,每个都没有简写。 +下面我们来看一下二分查找的代码,可以认真思考一下 if 语句的条件,每个都没有简写。 + +Java Code: ```java - public static int binarySearch(int[] nums,int target,int left, int right) { - //这里需要注意,循环条件 - while (left <= right) { - //这里需要注意,计算mid - int mid = left + ((right - left) >> 1); - if (nums[mid] == target) { - return mid; - }else if (nums[mid] < target) { - //这里需要注意,移动左指针 - left = mid + 1; - }else if (nums[mid] > target) { - //这里需要注意,移动右指针 - right = mid - 1; - } +public static int binarySearch(int[] nums,int target,int left, int right) { + //这里需要注意,循环条件 + while (left <= right) { + //这里需要注意,计算mid + int mid = left + ((right - left) >> 1); + if (nums[mid] == target) { + return mid; + }else if (nums[mid] < target) { + //这里需要注意,移动左指针 + left = mid + 1; + }else if (nums[mid] > target) { + //这里需要注意,移动右指针 + right = mid - 1; } - //没有找到该元素,返回 -1 - return -1; } + //没有找到该元素,返回 -1 + return -1; +} ``` +Go Code: + +```go +func binarySearch(nums []int, target, left, right int) int { + //这里需要注意,循环条件 + for left <= right { + //这里需要注意,计算mid + mid := left + ((right - left) >> 1) + if nums[mid] == target { + return mid + } else if nums[mid] < target { + //这里需要注意,移动左指针 + left = mid + 1 + } else if nums[mid] > target { + //这里需要注意,移动右指针 + right = mid - 1 + } + } + //没有找到该元素,返回 -1 + return -1 +} +``` + + + 二分查找的思路及代码已经理解了,那么我们来看一下实现时容易出错的地方 @@ -155,25 +182,50 @@ target < nums[mid] 则在左半区间继续进行搜索,即 right = mid -1; 下面我们来看一下二分查找的递归写法 +Java Code: + ```java public static int binarySearch(int[] nums,int target,int left, int right) { - if (left <= right) { - int mid = left + ((right - left) >> 1); - if (nums[mid] == target) { - //查找成功 - return mid; - }else if (nums[mid] > target) { - //新的区间,左半区间 - return binarySearch(nums,target,left,mid-1); - }else if (nums[mid] < target) { - //新的区间,右半区间 - return binarySearch(nums,target,mid+1,right); - } + if (left <= right) { + int mid = left + ((right - left) >> 1); + if (nums[mid] == target) { + //查找成功 + return mid; + }else if (nums[mid] > target) { + //新的区间,左半区间 + return binarySearch(nums,target,left,mid-1); + }else if (nums[mid] < target) { + //新的区间,右半区间 + return binarySearch(nums,target,mid+1,right); } - //不存在返回-1 - return -1; } + //不存在返回-1 + return -1; +} +``` + +Go Code: + +```go +func binarySearch(nums []int, target, left, right int) int { + + if left <= right { + mid := left + ((right - left) >> 1) + if nums[mid] == target { + //查找成功 + return mid + } else if nums[mid] > target { + //新的区间,左半区间 + return binarySearch(nums, target, left, mid-1) + } else if nums[mid] < target { + //新的区间,右半区间 + return binarySearch(nums, target, mid+1, right) + } + } + //不存在返回-1 + return -1 +} ```