国产探花免费观看_亚洲丰满少妇自慰呻吟_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;    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄平县| 哈巴河县| 贞丰县| 肇源县| 聊城市| 天门市| 盐山县| 巩义市| 昌宁县| 遵化市| 通海县| 枣强县| 乐清市| 锡林浩特市| 宁津县| 黄石市| 肇东市| 涞水县| 务川| 桑植县| 东明县| 宜川县| 永顺县| 海晏县| 微山县| 辽源市| 博白县| 林芝县| 天镇县| 闸北区| 西华县| 固始县| 如皋市| 博兴县| 临猗县| 西青区| 内乡县| 夏河县| 老河口市| 获嘉县| 获嘉县|