LeetCode_35---Search Insert Position
2015-06-16 14:03
489 查看
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.
</pre><p></p><p style="margin-top:0px; margin-bottom:10px">You may assume no duplicates in the array.</p><p style="margin-top:0px; margin-bottom:10px">Here are few examples.<br style="" /><code style="font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:13px; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">[1,3,5,6]</code>, 5 → 2<br style="" /><code style="font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:13px; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">[1,3,5,6]</code>, 2 → 1<br style="" /><code style="font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:13px; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">[1,3,5,6]</code>, 7 → 4<br style="" /><code style="font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:13px; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">[1,3,5,6]</code>, 0 → 0</p><p style="margin-top:0px; margin-bottom:10px"></p><div id="tags" class="btn btn-xs btn-warning" style="">Hide Tags</div> <span style=""><a target=_blank target="_blank" class="btn btn-xs btn-primary animated fadeIn" href="https://leetcode.com/tag/array/" style="">Array</a> <a target=_blank target="_blank" class="btn btn-xs btn-primary animated fadeIn" href="https://leetcode.com/tag/binary-search/" style="">Binary Search</a></span></div><div class="question-content" style="margin-left:20px; margin-top:20px; margin-bottom:5px; line-height:30px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(221,221,221); padding-left:20px; padding-bottom:2px">翻译:</div></div></div></div></div><div id="interviewed_div" style="padding-left:20px; padding-top:12px; padding-bottom:2px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif"><form id="interviewed_form" method="post"></form><div id="survey" style=""><div id="general_questions" style=""></div></div></div>Code:<p class="action" style="margin-top:12px; margin-bottom:10px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:20px"><pre name="code" class="java">/** * */ package From21; import java.util.Arrays; /** * @author MohnSnow * @time 2015年6月16日 下午2:02:52 * */ public class LeetCode35 { /** * @param argsmengdx * -fnst */ //372msA --- 一次性编写通过-- 删掉判断i相等之后---384msA public static int searchInsert(int[] nums, int target) { int len = nums.length; int i = 0; int j = nums.length - 1; if (nums[0] >= target || len == 0) return 0; if (nums[j] < target) return len; while (i <= j) { if (i + 1 < len && nums[i + 1] < target) { i++; } if (i + 1 < len && nums[i + 1] >= target) { return i + 1; } if (j - 1 >= 0 && nums[j - 1] < target) { return j; } if (j - 1 >= 0 && nums[j - 1] >= target) { j--; } } return 0; } public static void main(String[] args) { //int[] nums = { 3, 5, 5, 7, 7, 8, 8, 8, 10 }; int[] nums = { 1, 3, 5, 6 }; int target = 5; System.out.println("寻找插入:" + searchInsert(nums, target)); } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统