【LeetCode-面试算法经典-Java实现】【035-Search Insert Position(搜索插入位置)】

【035-Search Insert Position(搜索插入位置)】


【LeetCode-面试算法经典-Java实现】【所有题目目录索引】

原题

  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

题目大意

  给定一个排序数组,和一个指定的值,如果找到这个值,返回这个值位置,如果没有找到,返回这个值在数组中的插入位置。

  假设数组中没有重复的元素。

解题思路

  一、最直接的查找算法,从左向右搜索。

  二、使用二分查找算法。

代码实现

算法实现类(直接查找)

public class Solution {
    public int searchInsert(int[] A, int target) {

        if (A == null) {
            return -1;
        }

        int i;
        for (i = 0; i < A.length; i++) {
            if (A[i] >= target) {
                return i;
            }
        }

        return i;
    }
}

算法实现类(二分查找)

public class Solution {
    public int searchInsert(int[] A, int target) {

        int mid;
        int lo = 0;
        int hi = A.length - 1;

        while (lo <= hi) {
            mid = lo + (hi - lo)/ 2;

            if (A[mid] == target) {
                return mid;
            } else if (A[mid] < target){
                lo = mid + 1;
            } else {
                hi = mid - 1;
            }
        }

        return lo;
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

直接算法

二分查找算法

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47079367

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-09 16:35:17

【LeetCode-面试算法经典-Java实现】【035-Search Insert Position(搜索插入位置)】的相关文章

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] 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 →

[LeetCode] 035. Search Insert Position (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Search Insert Position (Medium) 链接: 题目:https://leetcode.com/problems/search-insert-position/ 代码(github):https://github.com/illuz/leetcode 题意: 要把一个数有序插入到一

LeetCode 035 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

Java for LeetCode 035 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

035. Search Insert Position

1 class Solution { 2 public: 3 int searchInsert(vector<int>& nums, int target) { 4 int left = 0, right = nums.size() - 1; 5 int mid = 0; 6 while (left <= right) { 7 mid = left + (right - left) / 2; 8 if (nums[mid] > target) right = mid - 1

[Leetcode] 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 →

Search insert position, 查找插入位置

问题描述:给定一个有序序列,如果找到target,返回下标,如果找不到,返回插入位置. 算法分析:依旧利用二分查找算法. 1 public int searchInsert(int[] nums, int target) 2 { 3 return binarySearch(nums, 0, nums.length - 1, target); 4 } 5 public int binarySearch(int[] nums, int left, int right, int target) 6 {

【LeetCode-面试算法经典-Java实现】【079-Word Search(单词搜索)】

[079-Word Search(单词搜索)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally

【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】

[096-Unique Binary Search Trees(唯一二叉搜索树)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For example, Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / /