这道题难度较低,没有必要作说明。

 

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

给定一个排序的数组和一个目标值,如果找到目标,返回索引。 如果没有,如果按顺序插入索引,返回索引。

您可以假定阵列中没有重复项。

以下是几个例子。
[1,3,5,6],5→2
[1,3,5,6],2→1
[1,3,5,6],7→4
[1,3,5,6],0→0


 class Solution {
public int searchInsert(int[] nums, int target) {
int k=0;
if(target>nums[nums.length-1])
return nums.length;
if(target<nums[0])
return 0;
else{
for(int i=0;i<nums.length-1;i++){
if(nums[i]==target&&target<nums[i+1]){
k=i;
break;
}
if(nums[i]<target&&target<=nums[i+1]){
k=i+1;
break;
}
}
}
return k;
}
}

LeetCode记录之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 Arrary Easy 35. Search Insert Position 题解

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

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

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

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

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

  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. Leetcode 题目整理 Sqrt && Search Insert Position

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

  8. 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 ...

  9. [LeetCode] 35. Search Insert Position 搜索插入位置

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

随机推荐

  1. instanceof php

    instdnceof php5  的一个新成员 功能: 使用这个关键字可以确定一个对象是否是类的实例,是否是累的子类 ,还是实现了某个特定的接口. <?php class A{} class B ...

  2. 如何学习MySQL

    转自高手的帖子 1.坚持阅读官方手册,看MySQL书籍作用不会特别大:(挑选跟工作相关的内容优先阅读,例如InnoDB存储引擎,MySQL复制,查询优化) 2.阅读官方手册,同时对阅读的内容做对应的测 ...

  3. Linux 下五款出色的流媒体客户端

    数 字流媒体这几天几乎占据了我音乐收听的全部时间.近年来我为了收藏 CD 花费了数量可观的费用:但它们中的大部分现在正静静地躺在满是灰尘的角落里.基本上所有的音乐流媒体服务所提供的的音质都不如 CD ...

  4. 关于前端XSS攻击、短信轰炸等问题

    关于前端的XSS攻击: 这里有一篇Ryf老师的写的关于使用  Content Security Policy (简称 CSP 防御)来防御xss攻击,简单来说就是设置白名单,告诉浏览器哪些链接.内容是 ...

  5. Luogu 4844 LJJ爱数数

    LOJ 6482 设$d = gcd(a, b)$,$xd = a$,$yd = b$,因为$\frac{1}{a} + \frac{1}{b} = \frac{a + b}{ab} = \frac{ ...

  6. 第十二课 Actionlib(1)

    一\Actionlib概念 在ROS系统中,有时需发送请求给某个节点完成相应的任务,同时获得一个一个响应,这种情况下可以通过ROS服务来 完成;然而,在某些情况下,服务需要很长时间才能执行完,如让机器 ...

  7. openvswitch安装

    安装前准备软件包及版本 Gcc pkg-config autoconf automake m4 python2.X 下载OVS软件包 http://openvswitch.org/releases/o ...

  8. 模板方法(Template Method)模式

    /* * 抽象模版(AbstractClass)角色有如下的责任: 定义了一个或多个抽象操作,以便让子类实现.这些抽象操作叫做基本操作,它们是一个顶级逻辑的组成步骤. 定义并实现了一个模版方法.这个模 ...

  9. 温故而知新:什么是wcf

    1.什么是WCF.WCF是Windows Communication Fundation的缩写,是微软在.net 3.0 的时候引进的,用于开发可交互的分布式应用程序,是由微软发展的一组数据通信的应用 ...

  10. 为什么 kubernetes 天然适合微服务 (3)

    此文已由作者刘超授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验 四.Kubernetes 本身就是微服务架构 基于上面这十个设计要点,我们再回来看 Kubernetes,会发现 ...