ITPub博客

首页 > 应用开发 > IT综合 > 算法Search Insert Position

算法Search Insert Position

IT综合 作者:dongyu2013 时间:2014-05-04 09:27:40 0 删除 编辑

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[] A, int target) {        if(A==null) return 0;         if(target <= A[0]) return 0;         for(int i=0; i A[i] && target <= A[i+1]){               return i+1;           }        }        return A.length;     } }

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29012686/viewspace-1153251/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: MySQL 面试题
请登录后发表评论 登录
全部评论

注册时间:2013-12-25

  • 博文量
    263
  • 访问量
    238063