博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Search Insert Position
阅读量:7034 次
发布时间:2019-06-28

本文共 955 字,大约阅读时间需要 3 分钟。

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

二分搜索的修改版

1 class Solution { 2 public: 3     int bsearch(int a[], int left, int right, int key) 4     { 5         if (left > right) 6             return left; 7              8         int mid = left + (right - left) / 2; 9         10         if (a[mid] == key)11             return mid;12         else if (a[mid] < key)13             return bsearch(a, mid + 1, right, key);14         else15             return bsearch(a, left, mid - 1, key);16     }17     18     int searchInsert(int A[], int n, int target) {19         // Start typing your C/C++ solution below20         // DO NOT write int main() function21         return bsearch(A, 0, n - 1, target);22     }23 };

转载地址:http://gpyal.baihongyu.com/

你可能感兴趣的文章
nginx错误解决方法个人总结
查看>>
利用Solid Converter PDF与Office优化处理文档信息
查看>>
spring boot 1.3.5 PUT方法接收参数
查看>>
Java 并发之 CountDownLatch、CyclicBarrier 和 Semaphore
查看>>
./ source 以及 exec的区别
查看>>
vsftpd虚拟配置增加用户脚本
查看>>
linux下安装DNS服务器
查看>>
Ext2文件系统 inode
查看>>
Java中四种XML解析技术
查看>>
Oracle Goldengate在HP平台裸设备文件系统OGG-01028处理
查看>>
Windows Server 2008 R2之高可用管理系列之(添加共享磁盘)
查看>>
linux下的/dev/shm/ 以及与swap目录的区别
查看>>
【android学习笔记】理解android.intent.action.MAIN 与 android.intent.category.LAUNCHER
查看>>
[阅读笔记]年过40岁的雷军致已逝去的青春
查看>>
我的友情链接
查看>>
远程使用sudo 执行命令,慎用!
查看>>
TMD我可知道[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]神马意思了
查看>>
《飞机大战》安卓游戏开发源码(三)
查看>>
JMS - JMS应用领域 应用场景
查看>>
[转]一步一步教你做ios推送
查看>>