LeetCode--Search Insert Position
来源:互联网 发布:宁波知乎 编辑:程序博客网 时间:2024/05/21 15:50
Search Insert Position
Total Accepted: 14091 Total Submissions: 41005My SubmissionsGiven 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(int A[], int n, int target) { //if() /* int m; int x=0; int y=n; while(x<y) { m=x+(y-x)/2; if(A[m]==target)return m; else if(A[m]>target)y=m; else x=m+1; } return m+1; */ int m; int x=0; int y=n; while(x<y) { m=x+(y-x)/2; if(A[m]>=target)y=m; else x=m+1; } return x; }};二分找查求下界
int lower_bound(int *A,int x,int y,int v){ int m; while(x<y) { m=x+(y-x)/2; if(A[m]>=v)y=m; else x=m+1; } return x;}求上界时
只需要将 if(A[m]>=v)y=m;else x=m+1; 改成if(A[m]<=v)x=m+1;else y=m;
0 0
- LeetCode: Search Insert Position
- LeetCode Search Insert Position
- [Leetcode] Search Insert Position
- LeetCode : Search Insert Position
- [LeetCode] Search Insert Position
- [Leetcode] Search Insert Position
- [LeetCode]Search Insert Position
- [LeetCode] Search Insert Position
- LeetCode-Search Insert Position
- LeetCode - Search Insert Position
- LeetCode | Search Insert Position
- Leetcode: Search Insert Position
- 【LeetCode】Search Insert Position
- Leetcode Search Insert Position
- Leetcode: Search Insert Position
- LeetCode:Search Insert Position
- leetcode Search Insert Position
- LeetCode Search Insert Position
- linux下使用openssl命令行加密文件
- 我们一起去了神圣的爱情金地——香巴林卡
- 仓央嘉措灵魂驻足的地方——香巴林卡
- 转自百度经验:editplus编译执行java的配置方法 确实不错
- Debian安装Apache2+MySQL5+PHP5
- LeetCode--Search Insert Position
- tiny6410裸机实验第4章--------------UART(原理分析)
- 模拟重写数组类
- 一个函数名后面加const
- 简单线段树专辑(未完待续 poj3468+hdu1698+hdu4325+hdu1394+poj2777
- 数据库专题
- 初学linux 常用指令
- html中的js调用c#的winform程序
- 精通安卓性能优化(Pro Android Apps Performance Optimization)-前言