Description

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.

Example 1:

Input: [,,,],
Output:

Example 2:

Input: [,,,],
Output:

Example 3:

Input: [,,,],
Output:

Example 4:

Input: [,,,],
Output:

解题思路:在当前索引的值小于目标值时,索引递增。在while循环中判断索引的值,防止数组索引越界

C#代码:

public class Solution {
public int SearchInsert(int[] nums, int target) {
if(nums.Length == )
return ;
int index = ;
while(nums[index] < target){
index++;
if(index == nums.Length)
break;
}
return index;
}
}

开始渐渐习惯了LeetCode的题目描述和解题规范。继续加油

LeetCode Arrary Easy 35. Search Insert Position 题解的更多相关文章

  1. LeetCode练题——35. Search Insert Position

    1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...

  2. LeetCode记录之35——Search Insert Position

    这道题难度较低,没有必要作说明. Given a sorted array and a target value, return the index if the target is found. I ...

  3. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  4. [Leetcode][Python]35: Search Insert Position

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...

  5. leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version

    704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...

  6. 【LeetCode】35. Search Insert Position (2 solutions)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  7. 35. Search Insert Position@python

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  8. Leetcode 题目整理 Sqrt && Search Insert Position

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...

  9. LeetCode:35. Search Insert Position(Easy)

    1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...

随机推荐

  1. fiddler抓取手机端的数据流量包

    1.首先下载安装fiddler 2.然后打开fiddler,进入到tools-->options-->connections 3.然后进入到https 4.设置完成后,查找本机ip 然后打 ...

  2. js非数值的比较

    /** * 非数值的比较: * 1.对于非数值的比较时,会将其转换成数字然后再比较 * 2.如果符号两端是字符串的值进行比较时,不会将其转换为数字进行比较,而是 * 分别比较字符串中的字符的 unic ...

  3. React 全新的 Context API

    Context API 可以说是 React 中最有趣的一个特性了.一方面很多流行的框架(例如react-redux.mobx-react.react-router等)都在使用它:另一方面官方文档中却 ...

  4. GeneXus笔记本—获取当月的最后一天

    首先获取当前日期 然后赋值为当前年月的第一天  然后加一个月 减去一天 就是当月最后一天 多用于筛选数据时的条件或者区间 我们先随便拉个页面  简单点就好 放入两个textblock 然后点击Even ...

  5. rabbitmq 发送的用户名是什么

    消费的用户名也必须是什么 接收必须单独启动线程——否则mfc会崩溃 Sleep(30000);  //30秒 只要你打开消费队列,只消费1个 队列中所有数据都丢失???????? 断线重连: 知道答案 ...

  6. js序列化----转载

    https://www.cnblogs.com/craftsman-gao/p/5130567.html JSON的全称是”JavaScript Object Notation“——JavaScrip ...

  7. php操作redis常用方法代码示例

     redis 的连接 描述:实例连接到一个Redis. 参数:host: string,port: int 返回值:BOOL 成功返回:TRUE;失败返回:FALSE $redis = new Red ...

  8. C语言集成开发环境使用小记

    时隔6年,我又重操C语言,是什么让我如此再下定决心?就是不想让自己所学过的知识就此荒废了,我重新以一个C语言初学者的身份(当然,稍稍有点基础,以前的知识忘得没这么快^_^)温故C语言,学习了几天,果真 ...

  9. CSP-S2019退役记。。。

    模拟赛的时候题目就比较迷,感觉不像联赛难度的. 考完正式赛才觉得这TM算个P. Day1: 写密码的监考同学的蜜汁字体让我傻了. 0和O是一样的,9和q是一样的,1和l是一样的-- 又没有冷静下来发现 ...

  10. Python--前端之HTML

    html概述和基本结构 HTML概述 HTML是 HyperText Mark-up Language 的首字母简写,意思是超文本标记语言,超文本指的是超链接,标记指的是标签,是一种用来制作网页的语言 ...