My 0ms solution by java


  • 0
    S
    public class Solution {
        public int searchInsert(int[] nums, int target) {
            
    		int start = 0;
    		int end = nums.length;
    		int mid = 0;
    		int flag = 0;
    		while(start<end){
    			mid = start + (end - start)/2;
    			if(nums[mid]>target){
    				end = mid;
    				flag = 0;
    			}
    			else if(nums[mid]<target){
    				start=mid+1;
    				flag = 1;
    			}
    			else
    				return mid;
    		}
    		if(flag==0)
    			return mid;
    		else
    			return mid+1;
    		
    	
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.