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. 题目要求 给定一个已经排好序的数组和一个目标值 ...
随机推荐
- Java Web servlet中的cookie
点击submit后: 点击查看Cookies: 在C:\Documents and Settings\Administrator\Cookies目录下面会有一个 hongten@webproj ...
- SpringBoot2.x整合Shiro(一)
一:什么是ACL和RBAC: ACL: Access Control List 访问控制列表 以前盛行的一种权限设计,它的核心在于用户直接和权限挂钩 优点:简单易用,开发便捷 缺点:用户和权限直接挂钩 ...
- System.Net.Mail.SmtpException:不允许使用邮箱名称.
使用SmtpClient发送邮件的时候,出现了如题错误. 解决方案: 将 SmtpClient.UseDefaultCredentials 属性设置为 true . 官方文档说明: Some SM ...
- SQL数据库—<6-001> 常用系统存储过程大全 --摘录网
-- 来源于网络 -- 更详细的介结参考联机帮助文档 xp_cmdshell --*执行DOS各种命令,结果以文本行返回. xp_fixeddrives --*查询各磁盘/分区可用空间 xp_logi ...
- create-react-app创建项目后,运行npm run eject报错解决方法
运行npm run eject报错解决方法 主要问题是脚手架添加.gitgnore文件,但是却没有本地仓库,使用以下命令操作以下就可以了 git init git add . git commit - ...
- rabbitmq的管理软件以及PHP7安装rabbitmq的
rabbitmq-server管理软件的安装 1.mac下载rabbitmq-server brew install rabbitmq 2.启动服务rabbitmq-server rabbitmq-s ...
- Node的优点和缺点
(优点)因为Node是基于事件驱动和无阻塞的,所以非常适合处理并发请求, 因此构建在Node上的代理服务器相比其他技术实现(如Ruby)的服务器表现要好得多. 此外,与Node代理服务器交互的客户端代 ...
- 树形查询sql
DROP FUNCTION IF EXISTS PersonName; CREATE FUNCTION PersonName(parent_id smallint) RETURNS VARCHAR(2 ...
- One Switch for Mac 一键切换系统各项功能
One Switch 是火球工作室推出的最新 Mac效率软件,它在 Menubar 菜单里集成了隐藏桌面(图标).切换 Dark Mode.保持亮屏.开启屏保的一键切换按钮,将以往这些以独立小 ...
- 【Linux】【sendmail】利用sendmail发送带附件的邮件及解决邮件中文标题乱码
#收件邮箱列表 TO_LIST=$1 #邮件标题 MAIL_TITLE=$2 #附件地址 LOG_PATH=$3 fromAdd="=?UTF-8?B?`echo $MAIL_TITLE | ...