弱渣leetcode刷题-Search Insert Position
2015-07-03 14:36
225 查看
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.
int searchInsert(int* nums, int numsSize, int target) {
int i=numsSize;
int tar=target;
int j;
int index=numsSize;
for(j=numsSize-1;j>=0;j--)
{
if(tar<=nums[j]) index--;
else break;
}
return index;
}
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
int searchInsert(int* nums, int numsSize, int target) {
int i=numsSize;
int tar=target;
int j;
int index=numsSize;
for(j=numsSize-1;j>=0;j--)
{
if(tar<=nums[j]) index--;
else break;
}
return index;
}
相关文章推荐
- 学习python
- 深入理解PHP中的Streams工具
- 如何选择开源许可证?
- 一些算法JAVA实现
- iOS点击空白处关闭presentViewController出来的view
- 内容包装:jQuery中wrap、wrapAll和wrapInner用法以及区别
- 用一条SQL语句 查询出每门课程都大于80分的学生姓名" 的实现方式
- 图标字体的使用
- Maven学习第2期---Maven安装配置
- Mac OS Operation - 1
- Vivado_MicroBlaze_问题及解决方法_汇总(不定时更新)
- cookie 保存上次访问url方法
- 在IIS站点中使用数字证书
- HMailServer日记
- linux下多线程编程
- kafka详细解读
- 防止表单重复提交的几种策略
- warning : WINVER not defined. Defaulting to 0x0600(Windows Vista)
- 七个图表解读VC们在种子轮融资的影响
- Mybatis基于注解的方式访问数据库