mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-12-27 04:46:17 +00:00
add a code in go
This commit is contained in:
parent
02aaf283e1
commit
e63c35b170
@ -38,31 +38,56 @@
|
|||||||
|
|
||||||
OK!我们到这一步就能把这个变种给整的明明白白的了,下面我们看一哈程序代码吧,也是非常简单的。
|
OK!我们到这一步就能把这个变种给整的明明白白的了,下面我们看一哈程序代码吧,也是非常简单的。
|
||||||
|
|
||||||
|
Java Code:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public static int lowBoundnum(int[] nums,int target,int left, int right) {
|
public static int lowBoundnum(int[] nums,int target,int left, int right) {
|
||||||
|
|
||||||
while (left <= right) {
|
while (left <= right) {
|
||||||
//求中间值
|
//求中间值
|
||||||
int mid = left + ((right - left) >> 1);
|
int mid = left + ((right - left) >> 1);
|
||||||
//大于目标值的情况
|
//大于目标值的情况
|
||||||
if (nums[mid] > target) {
|
if (nums[mid] > target) {
|
||||||
//返回 mid
|
//返回 mid
|
||||||
if (mid == 0 || nums[mid-1] <= target) {
|
if (mid == 0 || nums[mid-1] <= target) {
|
||||||
return mid;
|
return mid;
|
||||||
}
|
|
||||||
else{
|
|
||||||
right = mid -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (nums[mid] <= target){
|
|
||||||
left = mid + 1;
|
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
right = mid -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (nums[mid] <= target){
|
||||||
|
left = mid + 1;
|
||||||
}
|
}
|
||||||
//所有元素都小于目标元素
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
//所有元素都小于目标元素
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Go Code:
|
||||||
|
```go
|
||||||
|
func lowBoundnum(nums []int, target, left, right int) int {
|
||||||
|
|
||||||
|
for (left <= right) {
|
||||||
|
//求中间值
|
||||||
|
mid := left + ((right - left) >> 1);
|
||||||
|
//大于目标值的情况
|
||||||
|
if (nums[mid] > target) {
|
||||||
|
//返回 mid
|
||||||
|
if (mid == 0 || nums[mid-1] <= target) {
|
||||||
|
return mid
|
||||||
|
}else{
|
||||||
|
right = mid -1
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (nums[mid] <= target){
|
||||||
|
left = mid + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//所有元素都小于目标元素
|
||||||
|
return -1
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## **找出最后一个小于目标元素的索引**
|
## **找出最后一个小于目标元素的索引**
|
||||||
@ -73,6 +98,8 @@ public static int lowBoundnum(int[] nums,int target,int left, int right) {
|
|||||||
|
|
||||||
查找最后一个小于目标数的元素,比如我们的目标数为 7 ,此时他前面的数为 6,最后一个 6 的索引为 5,此时我们返回 5 即可,如果目标数元素为 12,那么我们最后一个元素为 11,仍小于目标数,那么我们此时返回 8,即可。这个变种其实算是上面变种的相反情况,上面的会了,这个也完全可以搞定了,下面我们看一下代码吧。
|
查找最后一个小于目标数的元素,比如我们的目标数为 7 ,此时他前面的数为 6,最后一个 6 的索引为 5,此时我们返回 5 即可,如果目标数元素为 12,那么我们最后一个元素为 11,仍小于目标数,那么我们此时返回 8,即可。这个变种其实算是上面变种的相反情况,上面的会了,这个也完全可以搞定了,下面我们看一下代码吧。
|
||||||
|
|
||||||
|
Java Code:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public static int upperBoundnum(int[] nums,int target,int left, int right) {
|
public static int upperBoundnum(int[] nums,int target,int left, int right) {
|
||||||
|
|
||||||
@ -99,6 +126,7 @@ public static int upperBoundnum(int[] nums,int target,int left, int right) {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func upperBoundnum(nums []int, target, left, right int) int {
|
func upperBoundnum(nums []int, target, left, right int) int {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user