给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。 你可以假设数组中无重复元素。
示例1
输入
[1,3,5,6],5
输出
2
示例2
输入
[1,3,5,6],2
输出
1
示例3
输入
[1,3,5,6],7
输出
4
示例4
输入
[1,3,5,6],0
输出
0
加载中...
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param nums int整型一维数组 * @param target int整型 * @return int整型 */ public int searchInsertPosition (int[] nums, int target) { // write code here } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param nums int整型一维数组 * @param numsLen int nums数组长度 * @param target int整型 * @return int整型 */ int searchInsertPosition(int* nums, int numsLen, int target) { // write code here } };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # @param nums int整型一维数组 # @param target int整型 # @return int整型 # class Solution: def searchInsertPosition(self , nums , target ): # write code here
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param nums int整型一维数组 * @param target int整型 * @return int整型 */ function searchInsertPosition( nums , target ) { // write code here } module.exports = { searchInsertPosition : searchInsertPosition };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # @param nums int整型一维数组 # @param target int整型 # @return int整型 # class Solution: def searchInsertPosition(self , nums , target ): # write code here
package main /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param nums int整型一维数组 * @param target int整型 * @return int整型 */ func searchInsertPosition( nums []int , target int ) int { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param nums int整型一维数组 * @param numsLen int nums数组长度 * @param target int整型 * @return int整型 */ int searchInsertPosition(int* nums, int numsLen, int target ) { // write code here }
[1,3,5,6],5
2
[1,3,5,6],2
1
[1,3,5,6],7
4
[1,3,5,6],0
0