【LeetCode】Search Insert Position搜索插入位置[数组、二分法]
来源:互联网 发布:软件数据与金蝶对接 编辑:程序博客网 时间:2024/06/05 15:07
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.
给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。
你可以假设在数组中无重复元素。
样例
[1,3,5,6],5 → 2
[1,3,5,6],2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6],0 → 0
挑战
O(log(n)) time
标签
二分法 数组 排序数组
(1)Java
public class Solution { /* * @param A: an integer sorted array * @param target: an integer to be inserted * @return: An integer */ public int searchInsert(int[] A, int target) { if(A == null || A.length == 0) return 0; int start,end,mid; start = 0; end = A.length - 1; while(start + 1 < end){ mid = start + (end - start) / 2; // avoid Stack Overflow if(A[mid] == target){ return mid; }else if(A[mid] < target){ start = mid; }else{ end = mid; } }//法①:find the first position >= target //若下标 start(end)处的值>=target 则直接将target插入对应的start(end)处即可 if(A[start] >= target){ return start; }else if(A[end] >= target){ return end; }else{// if(A[start] < target && A[end] < target) return end + 1; } }}//Version 2//法②:find the last position < target, return +1, 要特判一下target小于所有数组里面的元素public class Solution { public int searchInsert(int[] A, int target) { if (A == null || A.length == 0) { return 0; } 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; }}
(2)C++
class Solution { /** * param A : an integer sorted array * param target : an integer to be inserted * return : an integer */public: int searchInsert(vector<int> &A, int target) { // find first position >= target if (A.size() == 0) { return 0; } int start = 0, end = A.size() - 1; while (start + 1 < end) { int mid = (end - start) / 2 + start; if (A[mid] >= target) { end = mid; } else { start = mid; } } if (A[start] >= target) { return start; } if (A[end] >= target) { return end; } return A.size(); }};
阅读全文
2 0
- 【LeetCode】Search Insert Position搜索插入位置[数组、二分法]
- leetcode 35. Search Insert Position搜索插入位置(二分查找)
- 【LintCode】Search Insert Position 搜索插入位置
- 搜索插入位置:Search Insert Position
- LeetCode Search Insert Position查找插入位置
- 【二分法】LeetCode-Search Insert Position
- leetCode 35.Search Insert Position (搜索插入位置) 解题思路和方法
- 【LeetCode-面试算法经典-Java实现】【035-Search Insert Position(搜索插入位置)】
- Search Insert Position 插入位置
- 60.Search Insert Position-搜索插入位置(容易题)
- [LeetCode]—Search Insert Position 有序数组中找目标插入的位置
- Search Insert Position 找适合插入的位置@LeetCode
- 【LeetCode】- Search Insert Position(查找插入的位置)
- LeetCode | Search Insert Position(查找插入位置)
- [Leetcode] Search Insert Position 寻找插入的位置
- LeetCode OJ 之 Search Insert Position (查找插入位置)
- Leetcode #35. Search Insert Position 搜寻插入位置 解题报告
- LeetCode---35. Search Insert Position(查找插入位置)
- 蒙特卡洛方法的应用——解决“彩色砖块”问题
- HTML5 标题h 段落p 下划线hr 轻松搞定这种页面
- python遍历指定类型文件
- 【tomcat】日志、连接限制、Tomcat Users
- 回顾IT职业历程
- 【LeetCode】Search Insert Position搜索插入位置[数组、二分法]
- HDU-1874-畅通工程续【SPFA】
- 职称论文发表流程
- Android 端基于 OpenCV 的边框识别功能
- STL-adjacent_find算法
- java实现url编码与中文的互相转换
- NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第二轮Day1
- Beyond Compare过期后怎么接着使用
- ios-自动布局的基本原理