【lintcode笔记】搜索插入位置
来源:互联网 发布:蘑菇街宝贝如何优化 编辑:程序博客网 时间:2024/05/20 11:52
给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。
你可以假设在数组中无重复元素。
样例
[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(n)#include <stdio.h>#include <vector>using namespace std;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) { // write your code here int n = A.size(); if(n == 0) return 0; int i; for(i=0;i<=n-1;i++){ if(A[i] == target){ return i; }else if(A[i] < target){ if(i+1 == n) return i+1; }else{ return i; } } }};
0 0
- 【lintcode笔记】搜索插入位置
- LintCode : 搜索插入位置
- lintcode ----搜索插入位置
- LintCode : 插入搜索位置
- Lintcode 搜索插入位置
- lintcode:搜索插入位置
- Lintcode搜索插入位置
- lintCode 搜索插入位置
- LintCode 60 搜索插入位置
- 【LintCode】Search Insert Position 搜索插入位置
- lintcode python 容易 60.搜索插入位置
- LintCode 二分法查找, 搜索插入位置 和 二维矩阵
- lintcode&九章算法——No.60 搜索插入位置 ? 待解决
- 题目:搜索插入位置
- 搜索插入位置
- 搜索插入位置
- 搜索插入位置
- 搜索插入位置
- 我早年在Google学到的10条经验
- 1.揭秘angular2学习 ------- angualr架构总览
- 面向对象的程序设计Java-类和对象
- 选项卡实现
- ARM(A83T )开发环境搭建(一)
- 【lintcode笔记】搜索插入位置
- 八大排序算法_python
- openstack 管理二十八
- 悬浮窗口播放视频等(Android)
- PAT A1044 shopping in mars (25)
- Android6.0获取进程和进程数目的方法:
- mongodb安装
- linux共享内存
- ios直播技术(3)-- 视频解码