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

 

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. 回车换行0x0D和0x0A 小谈

    在计算机还没有出现之前,有一种叫做电传打字机(Teletype Model 33)的玩意,每秒钟可以打10个字符.但是它有一个问题,就是打完一行换行的时候,要用去0.2秒,正好可以打两个字符.要是在这 ...

  2. PHP配置数据库XML文件

    <?php $doc=new DOMDocument('1.0','utf-8'); //new一个dom对象 $doc->load("config.xml"); 加载 ...

  3. php常用 随机数

    <?php $number =range(1,50); //shuffle 将数组顺序随即打乱. shuffle($number); print_r($number); echo '<br ...

  4. Openssl genpkey命令

    一.简介 genpkey命令用于产生各种密钥(RSA.DSA.DH.EC等)的私钥值. 二.语法 openssl genpkey [-out filename] [-outform PEM | DER ...

  5. pandas dataframe 满足条件的样本提取

    pandas 的dataframe 对 数据查询可以通过3种方式 . 预备知识: 1. pandas 的索引和label都是从0开始的计数的 2. 时间切片都是左闭右开的. [5:6,:]  只会输出 ...

  6. javascript总结8:JavaScript 类型转换

    1 JavaScript 数据类型转换 1.1 数字类型转字符串 n1 = 10;var n2 =String(n1); 或者 var n3 = n1.toString(n1); 1.2 字符串转数字 ...

  7. portableDFS-可便携的分布式文件系统

    PPT下载(因附件大小有限制,删除了PPT中的隐藏页,如需完整版本,请转到it168文库下载):portableDFS-可便携的分布式文件系统.ppt 完整版本请上这里下载:http://wenku. ...

  8. 【2008nmj】Logistic回归二元分类感知器算法.docx

    给你一堆样本数据(xi,yi),并标上标签[0,1],让你建立模型(分类感知器二元),对于新给的测试数据进行分类. 要将两种数据分开,这是一个分类问题,建立数学模型,(x,y,z),z指示[0,1], ...

  9. 对于网站,APP开发流程的理解

    • 明确产品目标用户,目标市场 • 明确将要开发的产品面世后是要解决什么样的问题 • 梳理产品有哪些功能点,功能点如何按照模块划分 • 站在用户角度思考用户怎样使用这款产品,以故事的情景讲述用户如何使 ...

  10. [Lua快速了解一下]Lua的控制语句

    -Lua中没有++或者--的骚操作 -while loop sum = num = do sum = sum + num num = num + end print("sum =" ...