星空网 > 软件开发 > Java

[LeetCode] Search Insert Position

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

     这道题还是很简单的哈。考虑下特殊情况。然后写一个简单的Loop来比较大小就可以了。

     代码如下。

public class Solution {  public int searchInsert(int[] nums, int target) {    //special case    if(nums==null||nums.length==0){      return 0;    }    //also special case    if(target<=nums[0]){      return 0;    }    for(int i=0;i<nums.length-1;i++){      if(nums[i+1]>=target&&target>=nums[i]){        return i+1;      }    }    return nums.length;  }}

 




原标题:[LeetCode] Search Insert Position

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

赛狐ERP | 亚马逊选品没有灵感怎么办?:https://www.kjdsnews.com/a/1417566.html
e汇丰跨境电商收款方案全新上线:https://www.kjdsnews.com/a/1417567.html
fb广告账户的时间怎么调:https://www.kjdsnews.com/a/1417568.html
2023全球海运、空运货代50强榜单出炉;Temu再上线欧洲五国站点:https://www.kjdsnews.com/a/1417569.html
TikTok广告又又又审核不通过?别再盲目踩雷了!:https://www.kjdsnews.com/a/1417570.html
2023年Bing广告优化策略来了!记得收藏哦!:https://www.kjdsnews.com/a/1417571.html
字节跳动辟谣!TikTok收紧美国开店政策为不实信息:https://www.goluckyvip.com/news/188212.html
2024北京庞各庄镇梨花节开幕时间是几号?:https://www.vstour.cn/a/365179.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流