搜索插入位置
来源:互联网 发布:烈焰遮天 源码 双端 编辑:程序博客网 时间:2024/06/05 17:39
给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。
你可以假设在数组中无重复元素。
样例
[1,3,5,6],5 → 2
[1,3,5,6],2 → 1
[1,3,5,6], 7 → 4
你可以假设在数组中无重复元素。
样例
[1,3,5,6],5 → 2
[1,3,5,6],2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6],0 → 0
import java.util.Scanner;/** * 给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。你可以假设在数组中无重复元素。样例[1,3,5,6],5 → 2[1,3,5,6],2 → 1[1,3,5,6], 7 → 4[1,3,5,6],0 → 0 * * @author Dell * */public class Test60 { public static int searchInsert(int[] A, int target) { if(A.length==0) return 0; int index=-1; for(int i=0;i<A.length;i++) { if(target>A[i]) continue; else { index=i; return index; } } if(target>A[A.length-1]) return A.length; return index; }public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] a=new int[n]; for(int i=0;i<a.length;i++) { a[i]=sc.nextInt(); } int target=sc.nextInt(); System.out.println(searchInsert(a,target)); }}
阅读全文
0 0
- 题目:搜索插入位置
- LintCode : 搜索插入位置
- 搜索插入位置
- 搜索插入位置
- 搜索插入位置
- lintcode ----搜索插入位置
- LintCode : 插入搜索位置
- 搜索插入位置
- Lintcode 搜索插入位置
- 搜索插入位置
- 搜索插入位置
- 搜索插入位置
- lintcode:搜索插入位置
- Lintcode搜索插入位置
- lintCode 搜索插入位置
- LintCode_060_搜索插入位置
- 60. 搜索插入位置
- 【lintcode笔记】搜索插入位置
- leveldb学习---0
- 纯虚函数与抽象类
- python decode和encode
- Oracle那些事(9)-启动与关闭
- HDU 5517 Triple (线段树)
- 搜索插入位置
- 【Gradle】eclipse导入Gradle项目
- 机器学习实战-使用朴素贝叶斯分类器来做垃圾邮件分类
- 我的网络编程学习之路——getsockname 和 getpeername 函数
- Codeforces Round #419 (Div. 2)_Karen and Coffee
- windows的grep命令Select-String
- SQL server中设置自增字段
- storm示例之DRPC
- 欢迎使用CSDN-markdown编辑器