LeetCode Arrary Easy 35. 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.
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 题解的更多相关文章
- LeetCode练题——35. Search Insert Position
1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...
- LeetCode记录之35——Search Insert Position
这道题难度较低,没有必要作说明. Given a sorted array and a target value, return the index if the target is found. I ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- 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导致的溢 ...
- 【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 ...
- 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 ...
- Leetcode 题目整理 Sqrt && Search Insert Position
Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...
- LeetCode:35. Search Insert Position(Easy)
1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...
随机推荐
- QProcess
QT应用在windows系统下调用bat脚本,居然报错了.该BAT脚本,是用来检查svn.exe这个命令行工具,是否在当前系统里存在.在cmd终端里,一直是可正常执行的.但是在windows7家庭中文 ...
- Solr添加索引
发送请求: http://localhost:8080/solr/update/?stream.body= <delete><id>id值</id></del ...
- 转帖 java使用poi.3.10读取excel 2010
package poi; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; ...
- 【leetcode】937. Reorder Log Files
题目如下: You have an array of logs. Each log is a space delimited string of words. For each log, the f ...
- jdbcTemplate查询结果为对象list
RowMapper<WmsExpensesSettleEntity> rowMapper1=new BeanPropertyRowMapper<WmsExpensesSettleEn ...
- openwrt 编译支持sqlite3
编译版本加载lib库 ------------------------------Libraries----------------------------------- Filesystem -- ...
- 【JS】js引擎执行过程
概述 js引擎执行过程主要分为三个阶段,分别是语法分析,预编译和执行阶段,上篇文章我们介绍了语法分析和预编译阶段,那么我们先做个简单概括,如下: 语法分析: 分别对加载完成的代码块进行语法检验,语法正 ...
- select 1 from ... sql语句中的1解读
摘自:http://blog.csdn.net/zengcong2013/article/details/48224509 select 1 from ..., sql语句中的1代表什么意思?查出来 ...
- Python深度学习
序言 目的驱动型学习 概念解释 资料 https://www.tensorflow.org/ https://www.imooc.com/video/17186 https://www.cnblogs ...
- 「NOI2016」区间 解题报告
「NOI2016」区间 最近思维好僵硬啊... 一上来就觉得先把区间拆成两个端点进行差分,然后扫描位置序列,在每个位置维护答案,用数据结构维护当前位置的区间序列,但是不会维护. 于是想研究性质,想到为 ...