Leetcode[java] 35. Search Insert Position
来源:互联网 发布:苍空的解放者 知乎 编辑:程序博客网 时间:2024/05/17 03:04
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
public class Solution { public int searchInsert(int[] nums, int target) { int res=-1; int len = nums.length; if(nums==null||len==0) return 0; if(target<=nums[0]) return 0; else if(target>nums[len-1]) return len; return binarySearch(target,nums,0,len-1); } private int binarySearch(int target,int[] nums, int begin, int end){ if(begin+1==end){ if(target<=nums[begin]){ return begin; } else if(target<=nums[end]){ return begin+1; } } int mid = begin + (end-begin)/2; System.out.println(mid); if(target>nums[mid]){ return binarySearch(target,nums,mid,end); } else if(target<nums[mid]){ return binarySearch(target,nums,begin,mid); } else{ return mid; } }}
0 0
- (Java)LeetCode-35. Search Insert Position
- Leetcode[java] 35. Search Insert Position
- Leetcode 35. Search Insert Position (Medium) (java)
- [LeetCode-Java]35. Search Insert Position
- [leetcode]35. Search Insert Position(java)
- [leetcode]35. Search Insert Position(Java)
- [Leetcode] Search Insert Position (Java)
- [LeetCode][Java] Search Insert Position
- [LeetCode][Java] Search Insert Position
- leetcode:Search Insert Position 【Java 】
- LeetCode : Search Insert Position [java]
- Search Insert Position leetcode java
- [LeetCode]35.Search Insert Position
- LeetCode --- 35. Search Insert Position
- LeetCode 35.Search Insert Position
- [Leetcode] 35. Search Insert Position
- LeetCode 35. Search Insert Position
- [leetcode] 35.Search Insert Position
- Linux下程序开机自启动方法
- Git基础篇之远程仓库的基本操作以及如何给github的ReadMe文件添加图片
- 异常:No result defined for action
- 数据结构之栈与队列数学表达式的求值
- java 类继承两个父类,同名变量 public 比protected具有优先访问权限
- Leetcode[java] 35. Search Insert Position
- 欢迎使用CSDN-markdown编辑器
- vmware workstation 下安装的linux解决VMware Tools安装失败的问题
- Struts2标签OGNL简单应用
- 文件的保存和读取
- Java学习
- EL表达式 (详解)
- C错题练习
- ORA-03113:通信通道的文件结尾 解决办法