35. Search Insert Position【搜索插入位置】_i++

package LeetCode;
public class Test {
public static void main(String[] args) {
int[] nums1 = {1,3,5,6};
int target1 = 5;
int target2 = 2;
int target3 = 7;
int target4 = 0;
System.out.println(searchInsert(nums1, target1));
System.out.println(searchInsert(nums1, target2));
System.out.println(searchInsert(nums1, target3));
System.out.println(searchInsert(nums1, target4));
}

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

return 0;
}
}
}