35. Search Insert Position (Array; Divide-and-Conquer)
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
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
if(nums.size()==) return ;
return binarySearch(nums,,nums.size()-,target);
}
int binarySearch(vector<int>& nums, int start, int end, int target){
if(start==end){
if(target <= nums[start]) return start;
else return start+;
}
int mid = start + ((end-start)>>);
if(target <= nums[mid]) return binarySearch(nums,start,mid,target);
else return binarySearch(nums,mid+,end,target);
}
};
35. Search Insert Position (Array; Divide-and-Conquer)的更多相关文章
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- LeetCode练题——35. Search Insert Position
1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...
- leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version
704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...
- 【LeetCode】35. Search Insert Position (2 solutions)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- 35. Search Insert Position@python
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [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 ...
- [leetcode 35] Search Insert Position
1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...
- [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 ...
随机推荐
- 使用 lego生成 Let's Encrypt 证书
1. 工具 https://github.com/xenolf/lego 2. 使用 命令生成新的 lego --email="foo@bar.com" --domains=& ...
- Android中执行的错误:java.lang.UnsatisfiedLinkError: Couldn't load locSDK3: findLibrary returned null.
今天在使用百度地图的时候执行发现报错: 明明已经增加了liblocSDK3.so.但总是无法定位.提示错误java.lang.UnsatisfiedLinkError: Couldn't load l ...
- Python 3 利用 Dlib 实现人脸检测和剪切
0. 引言 利用 Python 开发,借助 Dlib 库进行人脸检测 / face detection 和剪切: 1. crop_faces_show.py : 将检测到的人脸剪切下来,依次排序平 ...
- Oracle安装-------实例化EM 配置文件时出错问题 ( 转 )
命令行中执行: C:\Users\Administrator>emca -repos drop EMCA 开始于 2013-4-23 20:13:58 EM Configuration As ...
- jQuery 性能优化技巧
原文地址:jQuery 性能优化技巧 博客地址:http://www.extlight.com 一.使用最新版本 jQuery 类库 二.合理使用选择器 # 推荐使用 $("#id" ...
- Android JNI访问Java成员
在 JNI 调用中,不仅仅 Java 可以调用本地方法,本地方法也可以调用 Java 中的方法和成员变量. Java 中的类封装了属性和方法,想要访问 Java 中的属性和方法,首先要获得 Java ...
- RK3288 error: undefined reference to 'LOGD'
HAL层和JNI层中的打印都必须包含下面的宏和头文件. 比如:LOGD.LOGE等等. #define LOG_TAG "TEST_LED" #include <utils/ ...
- eclipse Mars(4.5.2)安装pydev后不出现
一.环境 windows 7 64bit eclipse 4.5.2 pydev jdk7u55 二.安装步骤 1. 安装JDK eclipse依赖于java环境,所以需要安装java运行环境JRE. ...
- Hibernate学习2—Hibernate4 CRUD体验初步
接着上一节,工程结构: jar包没有变化: 一.HibernateUtil 封装: com.cy.util.HibernateUtil.java: package com.cy.util; impor ...
- Java中的强制类型转换
例如,当程序中需要将 double 型变量的值赋给一个 int 型变量,该如何实现呢? 显然,这种转换是不会自动进行的!因为 int 型的存储范围比 double 型的小.此时就需要通过强制类型转换来 ...