国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[LeetCode] Search Insert Position

2019-11-15 01:07:35
字體:
來源:轉載
供稿:網友
[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;    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 浮梁县| 舞钢市| 曲阜市| 逊克县| 永城市| 涪陵区| 宁远县| 青田县| 普兰县| 通城县| 曲阳县| 始兴县| 麻栗坡县| 兰坪| 凤山县| 志丹县| 万全县| 卓尼县| 阜新市| 固镇县| 望谟县| 牡丹江市| 中山市| 温泉县| 溆浦县| 灵寿县| 晋州市| 林西县| 鱼台县| 普洱| 江华| 微博| 阿拉善左旗| 米易县| 连平县| 遂昌县| 巢湖市| 江安县| 延庆县| 保靖县| 全南县|