[LeetCode]35. Search Insert Position
2016-09-04 15:02
309 查看
35. Search Insert PositionGiven 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.
程序说明:如果数组为空,则发挥位置0,若数组中无大于等于的数,则返回数组长度即可。
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0
程序说明:如果数组为空,则发挥位置0,若数组中无大于等于的数,则返回数组长度即可。
int searchInsert(int* nums, int numsSize, int target) { if ( numsSize == 0 ) { return 0; } int cnt; for ( cnt = 0; cnt < numsSize; cnt++ ) { if ( *(nums + cnt) >= target ) { return cnt; } } return numsSize; }由于数组已经是排序的了,故只需逐个开始比较大小,找到相应的位置返回即可
相关文章推荐
- RPC failed; result=22, HTTP code = 411
- gosyd/go1.6.slide at master · davecheney/gosyd · GitHub
- Windows Server 2008 Code "Longhorn" Beta 3 提供下载
- dedecms v5.1 WriteBookText() code injection vul注入漏洞
- 在C#中生成与PHP一样的MD5 Hash Code的方法
- PHP通过微信跳转的Code参数获取用户的openid(关键代码)
- Live Write 的代码高亮插件 Paste Code
- ERROR CODE: 1175 YOU ARE USING SAFE UPDATE MODE AN
- IIS7.5 Error Code 0x8007007e HTTP 错误 500.19的解决方法
- Microsoft Windows Server Code Name"Longhorn"Beta 3 正式发布! 下载
- Microsfot .NET Framework4.0框架 安装失败的解决方法
- Windows 下Spark 快速搭建Spark源码阅读环境
- Leanote集成Ace代码编辑器, 程序员的最爱
- 好代码是廉价的代码
- Optimizing Java Code(Pro Android Apps Performance Optimization)
- data,bdata,idata,pdata,xdata,code存储类型与存储区bit
- Writing Efficient Android Code(转)
- 常见HTTP状态(304,200等)
- 转:关于keil中data,idata,xdata,pdata,code的问题