- [Day12]35. Search Insert Positio
- 35 Search Insert Position
- Leetcode PHP题解--D138 35. Search
- LeetCode 35. Search Insert Posit
- 35. Search Insert Position(二分查找)
- leetcode:35. Search Insert Posit
- LeetCode #35 #34 #69 #74 #240 20
- Leetcode-35Search Insert Positio
- [LeetCode] Search Insert Positio
- 35. Search Insert Position
A EASY problem, just for accomplish today's task.
Already late
DESCRIPTION:
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
ANALYSIS:
So easy!
SOLUTION:
public static int searchInsert(int[] nums, int target) {
int i=0;
for(i=0;i<nums.length;){
if(target>nums[i]){
i++;
}else if(target==nums[i]||target<nums[i]){
return i;
}
}
return i;
}
网友评论