31 篇文章 0 订阅
14 篇文章 0 订阅
21 篇文章 0 订阅

# SearchInsertPosition

## 前言：

package array;

/**
* @author BlackSugar
* @date 2019/4/16
* Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
* <p>
* You may assume no duplicates in the array.
* <p>
* Example 1:
* <p>
* Input: [1,3,5,6], 5
* Output: 2
* Example 2:
* <p>
* Input: [1,3,5,6], 2
* Output: 1
* Example 3:
* <p>
* Input: [1,3,5,6], 7
* Output: 4
* Example 4:
* <p>
* Input: [1,3,5,6], 0
* Output: 0
*/
public class SearchInsertPosition {
/**
* 查询插入数字的索引
* 思路：
* 1、由于是升序数组，直接进行遍历，直到数组当中的数字对象大于等于目标值，则为目标值索引。O(n)
* 2、二分法，设置中点为(i + j) / 2，目标值于中点比较，左右夹逼
*
* @param nums   数组
* @param target 目标值
* @return
*/
public int searchInsert(int[] nums, int target) {
if (null == nums || nums.length == 0) {
return 0;
}
/*for (int i = 0; i < nums.length; i++) {
if (nums[i] >= target) {
return i;
}
}
return nums.length;*/

int i = 0, j = nums.length - 1;
while (i <= j) {
int mid = (i + j) / 2;
if (target > nums[mid]) {
i = mid + 1;
} else if (target < nums[mid]) {
j = mid - 1;
} else {
return mid;
}
}
return i;
}

public static void main(String[] args) {
System.out.println(new SearchInsertPosition().searchInsert(new int[]{1, 3, 4, 7}, 4));
}
}



1、暴力迭代O(n)
2、二分法O(logn)

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报