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

给定一个排好序的数组和一个目标值,如果在数组中找到这个值则返回其索引,未找到的话,返回其应该插入的位置的索引。

你可以假设数组中没有重复元素。

思路就是遍历,并用当前值与目标值比较。第一次循环看是否有与目标值相同的元素,第二次循环找插入位置。

	public static int searchInsert(int[] A, int target) {
		int index = -1;
		for(int i=0;i<A.length;i++){
			if(A[i] == target)
				return i;
		}
		for(int i=0;i<A.length;i++){
			if(A[0] > target)
				return 0;
			else if(A[A.length -1] < target)
				return A.length;
			else if(A[i] < target && A[i+1] > target)
				return i+1;
		}
		return index;
	}

LeetCode——Search Insert Position,布布扣,bubuko.com

时间: 2024-10-16 19:38:28

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 解题报告

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

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 二分搜索

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 @ 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

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

leetcode Search Insert Position(easy) /java

写easy,心情也很顺畅. 题目的意思是,如果有val,那么返回val的下标.如果没有val,那么找出val应该放在哪里. import java.io.*; import java.util.*; public class Solution { public static int searchInsert(int[] nums, int target) { int r=0; int len=nums.length; if(len==0) return 0; int i,j; i=0; whil

[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 →