【LeetCode】- Search Insert Position(查找插入的位置)

[ 问题: ]

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. You may assume no duplicates in the array.

翻译:给你一个排好序的数组和一个目标值,请找出目标值可以插入数组的位置。

[ 分析: ]

Here are few examples.

[1,3,5,6], 5 → 2

[1,3,5,6], 2 → 1

[1,3,5,6], 7 → 4

[1,3,5,6], 0 → 0

注意:一定要考虑一些特殊情况,如数组为null等。

[ 解法: ]

①. 常规解法:从数组索引为0的位置开始找,时间复杂度为O(n),accepted

public class Solution {
	public int searchInsert(int[] A, int target) {
		if (A != null) {
			for (int i = 0; i < A.length; i++) {
				if (target == A[i] || target < A[i]) {
					return i;
				}
			}
			return A.length;
		}
		return -1;
	}

	public static void main(String[] args) {
		int[] arr = { 1, 3, 5, 6 };
		System.out.println(new Solution().searchInsert(arr, 5)); // 5 -> 2
		System.out.println(new Solution().searchInsert(arr, 2)); // 2 -> 1
		System.out.println(new Solution().searchInsert(arr, 7)); // 7 -> 4
		System.out.println(new Solution().searchInsert(arr, 0)); // 0 -> 0
	}
}

②. 二分查找:时间复杂度log2n

前提条件:一定是有序数组。

public class Solution {
	public int searchInsert(int[] A, int target) {
		int mid;
		int low = 0;
		int high = A.length - 1;
		while (low < high) {
			mid = (low + high) / 2;
			if (A[mid] < target) {
				low = mid + 1;
			} else if (A[mid] > target) {
				high = mid - 1;
			} else {
				return mid;
			}
		}

		return target > A[low] ? low + 1 : low;
	}

	public static void main(String[] args) {
		int[] arr = { 1, 3, 5, 6 };
		System.out.println(new Solution().searchInsert(arr, 5)); // 5 -> 2
		System.out.println(new Solution().searchInsert(arr, 2)); // 2 -> 1
		System.out.println(new Solution().searchInsert(arr, 7)); // 7 -> 4
		System.out.println(new Solution().searchInsert(arr, 0)); // 0 -> 0
	}
}

【LeetCode】- Search Insert Position(查找插入的位置),码迷,mamicode.com

时间: 2024-08-08 06:47:54

【LeetCode】- Search Insert Position(查找插入的位置)的相关文章

[LeetCode] Search Insert Position [21]

题目 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. You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,6]

LeetCode: Search Insert Position [034]

[题目] 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. You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,

LeetCode——Search Insert Position

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. You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,6], 2

LeetCode: Search Insert Position 解题报告

Search Insert Position 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. You may assume no duplicates in the array. Here are few examples.[1,3,5,

LeetCode Search Insert Position (二分查找)

题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. 1 class Solution { 2 public: 3 int searchInsert(vector<int>& nums,int target) 4 { 5 int L=0, R=nums.size(); 6 while(L<R) 7 { 8 int mid=R-(R-L+1)/2; 9 if(nums[mid]>=t

Search Insert Position 查找给定元素在数组中的位置,若没有则返回应该在的位置

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. You may assume no duplicates in the array. Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 →

Array——LeetCode——Search Insert Position

[学到的知识点--查找方法]1.二分查找 ----------------------------------------------------------------------------------------------------- private static int searchInsert(int[] nums, int target) { //1.循环这个数组 //2.找到这个位置 //3.如果相同则返回这个值:如果不同则插入,依次后移 /* * [优化] * 1.可以用优化

[leetcode]Search Insert Position @ Python

原题地址:https://oj.leetcode.com/problems/search-insert-position/ 题意: 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. You may assume no duplicates

35 Search Insert Position(找到数的位置Medium)

题目意思:在递增数组中找到目标数的位置,如果目标数不在数组中,返回其应该在的位置. 思路:折半查找,和相邻数比较,注意边界 1 class Solution { 2 public: 3 int searchInsert(vector<int>& nums, int target) { 4 int start=0,end=nums.size()-1; 5 int flag=0; 6 while(start<=end){ 7 flag=(start+end)/2; 8 if(nums

leetcode : Search Insert Position

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. You may assume no duplicates in the array. Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 →