LeetCode problem solving Thirteen: Search Insert location

topic

Given a sorted array and a target, find the object in the array, and returns its index. If the target is not present in the array, it will be returned in sequence inserted position.

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

solve

This question is relatively simple, direct cycle just fine, the title has been ordered and no repeat array, find below the target value of the array index is the subject of the answer.

class Solution {
    public int searchInsert(int[] nums, int target) {
        int i;
        for( i = 0; i < nums.length; i++){
        	if(target <= nums[i]){
        		 break;
        	}
        }
       return i;

    }
}

Published 88 original articles · won praise 49 · Views 100,000 +

Guess you like

Origin blog.csdn.net/Diamond_Tao/article/details/102529710