Search for a Range leetcode java
题目:
Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].
从题目中获取到关键字:sorted array ,find...position, a given target value, O(logn).
这些关键字都在提醒我们这道题的思路应该是二分查找法。
二分查找方法
二分查找经常用来在有序的数列查找某个特定的位置。
因此,应用二分查找法,这个数列必须包含以下特征:
- pos = mid;
high = mid - 1;
low = mid + 1;
res[0] = pos;
res[1] = pos;
}
}
newlow = newmid + 1;
newhigh = newmid - 1;
}
res[1] = newhigh;
newhigh = pos;
newhigh = newmid - 1;
newlow = newmid + 1;
}
res[0] = newlow;
}
Reference:http://blog.csdn.net/linhuanmars/article/details/20593391
Search for a Range leetcode java的更多相关文章
- Search for a Range [LeetCode]
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- Search for a Range ——LeetCode
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- Search a 2D Matrix leetcode java
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- [LeetCode] 034. Search for a Range (Medium) (C++/Java)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku
1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at so ...
- [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)
原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...
- [Leetcode][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- LeetCode: Search for a Range 解题报告
Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...
随机推荐
- 快递鸟电子面单打印功能基于java
之前的后天管理系统的电子面单打印使用的是灵通打单. 使用相对比较麻烦,需要到处Excel之后再导入,麻烦. 快递鸟有电子面单api,后台系统直接对接很是方便,不过也遇到了好些问题. 不难是不难,但是遇 ...
- MySQL 事物的隔离级别(简要)
事务的隔离级别 为什么 引入了 事务隔离级别?? 在数据库操作中,为了有效保证并发读取数据的正确性,提出的事务隔离级别. 更新丢失两个事务都同时更新一行数据,一个事务对数据的更新把另一个事务对数据的 ...
- 表单验证插件validate
http://www.runoob.com/jquery/jquery-plugin-validate.html <!DOCTYPE html> <html lang="e ...
- bzoj 2178
这题调精度真痛苦啊(向管理员要了数据才调出来). 用的是hwd在WC2015上讲的方法,考虑将原图分割,根据每个圆的左右边界和圆与圆交点的横坐标来分割,这样原图就被分成很多竖着的长条,并且每一条中间都 ...
- UVALive 4868 Palindrometer 暴力
F - Palindrometer Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- 批量替换url,指定内容不替换
如果需要批量替换url的某几部分,当然是用正则了比如在CI框架中要把 <img src="pc/baidu/aa.jpg"> 替换成 <img src=" ...
- centos7搭建.netcore运行环境
开发环境介绍 1.操作系统:Windows Server 2008 R2 Enterprise 2.IDE:VisualStudio2017 3..Net Core 2.0 SDK 本文假设你已经满足 ...
- Linux rescue
适用场景: 当误操作修改系统启动文件/etc/fstab, /etc/rc.d/rc.sysinit时,就会造成系统启动时读取磁盘或初始化环境失败,导致linux无法正常启动,此时就可以借助Linux ...
- Java_tomcat thread dump 分析
前言 Java Thread Dump 是一个非常有用的应用诊断工具, 通过thread dump出来的信息, 可以定位到你需要了解的线程, 以及这个线程的调用栈. 如果配合linux的top命令, ...
- linux(系统centos6.5)常用命令总结
ls -al 列出当前目录下的所有文件和子目录 用户在登录Linux时由/etc/passwd文件来决定要使用哪个shell,用户使用的shell被列于每行的末尾(/bin/bash) ls -F在 ...