35 Search Insertion position

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

以上是自己写的

参考九章答案:用二分

find the first position >= target

public class Solution {
    public int searchInsert(int[] nums, int target) {
        int start = 0;
        int end = nums.length -1;

        while (start + 1 < end) {
            int mid = start + (end - start)/2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] < target) {
                start = mid;
            } else {
                end = mid;
            }
        }

        if (nums[start] >= target) {
            return start;
        } else if (nums[end] >= target) {
            return end;
        } else {
            return end+1;
        }
    }
}

find the last position <= target

public class Solution {
    public int searchInsert(int[] A, int target) {
        int start = 0;
        int end = A.length - 1;
        int mid;

        if (target < A[0]) {
            return 0;
        }
        // find the last number less than target
        while (start + 1 < end) {
            mid = start + (end - start) / 2;
            if (A[mid] == target) {
                return mid;
            } else if (A[mid] < target) {
                start = mid;
            } else {
                end = mid;
            }
        }

        if (A[end] == target) {
            return end;
        }
        if (A[end] < target) {
            return end + 1;
        }
        if (A[start] == target) {
            return start;
        }
        return start + 1;
    }
}
时间: 2024-10-26 14:54:50

35 Search Insertion position的相关文章

LeetCode练题——35. Search Insert Position

1.题目 35. Search Insert Position Easy 1781214Add to ListShare 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 th

[Leetcode][Python]35: Search Insert Position

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 35: Search Insert Positionhttps://oj.leetcode.com/problems/search-insert-position/ Given a sorted array and a target value, return the index if the target is found.If not, return the index whe

leetCode 35.Search Insert Position (搜索插入位置) 解题思路和方法

Search Insert Position 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

[LeetCode] 35. Search Insert Position 解决思路

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 →

35. Search Insert Position【leetcode】

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 →

LeetCode 35. Search Insert Position (Easy)

上班无聊就在leetCode刷刷题目,有点遗憾的是到现在才去刷算法题,大学那会好好利用该多好啊. 第35道简单算法题,一次性通过有点开心,分享自己的代码. 问题描述 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 assu

LeetCode 35 Search Insert Position

翻译 给定一个已排序的数组和一个目标值,如果这个目标值能够在数组中找到则返回索引.如果不能,返回它应该被插入的位置的索引. 你可以假设数组中没有重复项. 以下是一些示例. 原文 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 ma

35. Search Insert Position(C++)

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 →

35. Search Insert Position

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 →