[LeetCode-Java Exercise] 35. Search for the insertion position (simple)

1. Title description

Insert picture description here

2. Problem solving ideas

Brute force method, directly traverse the sorted array, find the subscript of the target value in the array or the subscript of the insertion position.

3. Code implementation

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

Guess you like

Origin blog.csdn.net/weixin_48683410/article/details/113816662