示例1:
输入: nums = [-1,0,3,5,9,12], target = 9
输出: 4
解释: 9 出现在 nums 中并且下标为 4
示例2:
输入: nums = [-1,0,3,5,9,12], target = 9
输出: 4
解释: 9 出现在 nums 中并且下标为 4
图解部分:
这个题目,用暴力求解也可以,不过时间时间复杂度为O(N),效率不算太高。这个题目主要是教我们二分查找(或折半查找)方式,数组已经按从小到大的顺序排列,这非常复合二分查找的特点。
目标值nums[i]与target 进行比较
nums[i]==target 则返回i
nums[i]<target 目标值可能在其右侧
nums[i]>target 目标值可能在其左侧
在图中右侧部分的代码,已经定义了[low,high],每次查找取其mid中间值进行比较,相等则返回其mid,不等则继续缩小范围进行执行。
整体的时间复杂度为O(logN).
代码部分:
时间复杂度为:O(logN)
空间复杂度为:O(1)
public int testSearch(int nums[], int target) {
int low = 0;
int high = nums.length - 1;
while (low <=high) {
int mid = (high - low) / 2 + low;
int num = nums[mid];
if (num == target) {
return mid;
} else if (target > num) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
完整的测试代码:
package com.coderpwh.leetcode;
/***
*
* 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。
*
*
* 示例 1:
*
* 输入: nums = [-1,0,3,5,9,12], target = 9
* 输出: 4
* 解释: 9 出现在 nums 中并且下标为 4
* 示例 2:
*
* 输入: nums = [-1,0,3,5,9,12], target = 2
* 输出: -1
* 解释: 2 不存在 nums 中因此返回 -1
*
*/
public class Searchs {
public static void main(String[] args) {
int nums[] = {-1, 0, 3, 5, 9, 12};
// int nums[] ={5};
int target = 9;
Searchs search = new Searchs();
// 普通的for循环
// System.out.println(search.search(nums, target));
// 二分查找方式实现
System.out.println(search.testSearch(nums, target));
}
/***
* 思路:
* 1. 时间复杂度为O(N)
* 2. 空间复杂度为O(1)
* 3. 算法效率不高
*
*
* @param nums
* @param target
* @return
*/
public int search(int[] nums, int target) {
if (nums == null || nums.length <= 0) {
return -1;
}
int count = -1;
for (int i = 0; i < nums.length; i++) {
if (target == nums[i]) {
count = i;
break;
}
}
return count;
}
/**
* 思路:
* 1. 时间复杂度为O(LogN)
* 2. 空间复杂度为O(1)
* 3.利用二分查找方式实现
*
* @param nums
* @param target
* @return
*/
public int testSearch(int nums[], int target) {
int low = 0;
int high = nums.length - 1;
int count = 0;
while (low <=high) {
count++;
System.out.println("count:"+count);
int mid = (high - low) / 2 + low;
System.out.println("mid:"+mid);
int num = nums[mid];
if (num == target) {
return mid;
} else if (target > num) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
}