您的位置:首页 > 编程语言 > Java开发

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));

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 LeetCode java