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 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的更多相关文章
- LeetCode练题——35. Search Insert Position
1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...
- 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 ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- 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 ...
- Leetcode 题目整理 Sqrt && Search Insert Position
Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...
- 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] 35. Search Insert Position 搜索插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
随机推荐
- c++原型模式(Prototype)
原型模式是通过已经存在的对象的接口快速方便的创建新的对象. #include <iostream> #include <string> using namespace std; ...
- 598. Range Addition II 矩阵的范围叠加
[抄题]: Given an m * n matrix M initialized with all 0's and several update operations. Operations are ...
- c++ 指向类成员函数的函数指针
// ConsoleApplication34.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...
- FreeMarker 的空值处理 , 简单理解 , 不用TMD就会忘记
NO.1 而对于FreeMarker来说,null值和不存在的变量是完全一样的 NO.2 ! 指定缺失变量的默认值 返回String NO.3 ?? 判断变量是否存在 返回boolean NO.4 $ ...
- Python的split()函数
手册中关于split()用法如下: str.split(sep=None, maxsplit=-1) Return a list of the words in the string, usi ...
- css总结9:内边距(padding)和外边距(margin)
1 css总结9:内边距和外边距 通过css总结8:盒子模型可知:内边距(padding),外边距(margin).可以影响盒子在浏览器的位置. 1.1 padding使用:{padding:上 右 ...
- Java web 三层架构 模拟图
- 编写javascript的基本技巧一
自己从事前端编码也有两年有余啦,时间总是比想象中流逝的快.岁月啊,请给我把时间的 脚步停下吧.不过,这是不可能的,我在这里不是抒发时间流逝的感慨.而是想在这分享两 年来码农生活的一些javascrip ...
- (转)Asp.Net底层原理(三、Asp.Net请求响应过程)
原文地址:http://www.cnblogs.com/liuhf939/archive/2013/09/16/3324753.html 在之前,我们写了自己的Asp.Net框架,对整个流程有了一个大 ...
- 数组Byte [] 和 string 相互转换
using System; using System.Collections.Generic; using System.Text; namespace NET.MST.Fourth.StringBy ...