[LeetCode][Java] 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
算法分析:
二分搜索就可以。
直接上代码
AC代码:
- <span style="font-size:12px;">public class Solution
- {
- public int searchInsert(int[] A, int target)
- {
- int i = 0;
- int j = A.length - 1;
- while (i <= j)
- {
- int mid = (int)((i + j)/2);
- if (A[mid] == target)
- return mid;
- else if (A[mid] > target)
- j = mid - 1;
- else
- i = mid + 1;
- }
- return i;
- }
- }</span>
[LeetCode][Java] Search Insert Position的更多相关文章
- [LeetCode] 035. Search Insert Position (Medium) (C++)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- LeetCode 035 Search Insert Position
题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target ...
- [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 ...
- Java for LeetCode 035 Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- Java [leetcode 35]Search Insert Position
题目描述: Given a sorted array and a target value, return the index if the target is found. If not, retu ...
- 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 ...
- LeetCode 35 Search Insert Position(查找插入位置)
题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入 ...
- [leetcode 35] Search Insert Position
1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...
随机推荐
- Internal Server Error: /favicon.ico 的解决方法
项目大致环境:Linux + Apache + Django + Python 因为项目的需求重写了Python Logger.error 方法,每当记录错误日志的时候就发送一封邮件到指定的邮箱.项目 ...
- wepy - 入手
wepy官方文档: https://tencent.github.io/wepy/document.html#/ ESLint:一般用来校验JavaScript代码是否符合规范,不符合预期就报错(程序 ...
- Java从零开始学三十二(正则表达式)
一.为什么要有正则 正则表达式可以方便的对数据进行匹配,可以执行更加复杂的字符串验证.拆份.替换功能. 例如:现在要求判断一个字符串是否由数字组成,则可以有以下的两种做法: 不使用正则完成 使用正则完 ...
- 动态IP或无公网IP时外网訪问内网ORACLE数据库
ORACLE数据库是应用最多的一个数据库.一般项目应用.将ORACLE部署在内网,内网调用,及运维都仅仅能是内网完毕. 假设ORACLE主机或所在局域网没有固定公网IP,又想在外网对ORACLE进行訪 ...
- ERROR 1227 (42000): Access denied; you need (at least one of) the SUPER privilege(s) for this operation
开启super权限: 1. update user set Super_priv=‘Y’ where User=‘root’2. flush privileges
- raise语句
# -*- coding: utf-8 -*- #python 27 #xiaodeng #Python学习手册 868 #raise语句 res=[IndexError,TypeError] #ra ...
- spring aop的两种写法aspect和advisor
本文转自:https://www.cnblogs.com/leiOOlei/p/3709607.html 首先看个例子,如下 接口代码: package com.lei.demo.aop.schema ...
- centos7 卸载 gitlab
标黑的就是关键命令,先停止gitlab服务,然后rpm -e卸载,然后查看剩余gitlab进程,然后杀死主进程,然后删除所有相关目录 1 [liuyx@MiWiFi-R3L-srv ~]$ sudo ...
- Linux链接文件ln
链接命令:ln 生成链接文件 ln [源文件] [目标目录] : 创建硬链接 -s:创建软链接 硬链接特征:(ln )1.拥有相同的i节点和存储block快,可以看做是同一个文件2.可通过i ...
- IBM InfoSphere DataStage 8.1 DataStage Job 开发具体解释
简单介绍 DataStage 使用了 Client-Server 架构,server端存储全部的项目和元数据,client DataStage Designer 为整个 ETL 过程提供了一个图形化的 ...