[leedcode 35] Search Insert Position
2015-07-09 18:25
363 查看
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.
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
public class Solution { public int searchInsert(int[] nums, int target) { //二分法的变形,没有找到时,返回的是应该插入的下标 return find(nums,target,0,nums.length-1); } public int find(int []nums,int target,int start,int end){ if(start>end) return start;//二分法唯一的不同 int mid=(start+end)/2; if(nums[mid]==target) return mid; if(nums[mid]>target){ return find(nums,target,start,mid-1); }else{ return find(nums,target,mid+1,end); } } }
相关文章推荐
- Word Ladder
- JavaScript-ajax学习
- 第一个Django项目及部署到Sina App Engine
- 开发学习网页
- 关于Canvas的旋转和平移
- Java中的String的replace方法和replaceAll方法
- 50款经典 iPhone 开源应用(附代码下载)
- 小胖说事26------cocoaPods安装以及问题:Setting up CocoaPods master repo
- mongodb - 前端form表单数据传输,在保存和清除的数据格式的处理程序的 - 非递归
- 50款经典 iPhone 开源应用(附代码下载) 分类: ios 2015-07-09 18:23 31人阅读 评论(0) 收藏
- SCCM 2012报表之——查看局域网中哪些电脑安装了游戏类软件
- 编译db2 clt文件
- 【贪心】【TOJ4107】【A simple problem】
- 【贪心】【TOJ4107】【A simple problem】
- Javascript模块化编程(三):require.js的用法
- B. Two Buttons
- 黑马程序员——Java 面向对象(上)
- 链表分割
- Javascript模块化编程(二):AMD规范
- For多重循环 break continue