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 ...
随机推荐
- CAD2015 安装出错
Autodesk安装失败后回滚连带把在D盘创建的安装目录都给删除掉了. 把.net 4.6卸载干净之后就可以成功安装CAD2015了.只安装.net 4.5就行了.
- 最新Eclipse Photon安装tomcat
发现最新版的eclipse竟然没有tomcat配置项,可能是因为spring boot很火,所以server默认就不包含tomcat,需要手动安装组件, Version: Photon Releas ...
- 217. Contains Duplicate数组重复元素 123
[抄题]: Given an array of integers, find if the array contains any duplicates. Your function should re ...
- ZendStudio 代码调试
F5.单步调试进入函数内部(单步进入)F6.单步调试不进入函数内部(跳过)F7.由函数内部返回到调用处(跳出) F8.一直执行到下一个断点Ctrl+F2:结束调试
- 现代C++学习笔记之二入门篇1
现代 C++ 强调: 基于堆栈的范围,而非堆或静态全局范围. 自动类型推理,而非显式类型名称. 智能指针而不是原始指针. std::string 和 std::wstring 类型(请参见 <s ...
- VC维的来龙去脉(转)
本文转自VC维的来龙去脉 本文为直接复制原文内容,建议阅读原文,原文排版更清晰,且原网站有很多有意思的文章. 阅读总结: 文章几乎为台大林老师网课“机器学习可行性”部分串联总结,是一个很好的总结. H ...
- DataType--时间类型
SQL SERVER 存储时间的方式和存放浮点数的方式类似,存放时按照一定公式算出一个数值,存放到页面,在读取时按照公式求算出时间值,再按照默认或指定的时间格式展示给用户. 如果存放DATETIME数 ...
- 手动给kvm虚机挂载lvm卷
1.查看计算节点上虚机挂载的卷 [root@xgto01n010243186070 ~]# virsh domblklist instance- Target Source ------------- ...
- C语言字符串拼接
1.使用strcat进行字符串拼接 #include <stdio.h> #include <stdlib.h> #include <string.h> int m ...
- day1学python Hello Python
Hello Python 本人使用的是Pycharm编译器 ----------------------------------------------- 1.输出 2.赋值 3.‘’‘/“”“ 多行 ...