0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Leetcode 35. Search Insert Position

Posted at

アプローチ

二分探索

class Solution {
    public int searchInsert(int[] nums, int target) {
        if(nums[nums.length-1]< target){
            return nums.length;
        }
        int low = 0;
        int high = nums.length - 1;
        int result = 0;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (nums[mid] == target) {
                result = mid;
                break;
            }
            if (nums[mid] < target) {
                low = mid + 1;

            } else {
                high = mid - 1;
                result = mid;               
            }

        }
        return result;
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?