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 ...
随机推荐
- 第二波分析:德国是2018世界杯夺冠最大热门? Python数据分析来揭开神秘面纱… (附源代码)
2018年,世界杯小组赛已经在如火如荼的进行中.在上篇文章的基础上[2018世界杯:用Python分析热门夺冠球队],我们继续分析世界杯32强的实力情况,以期能够更进一步分析本次世界杯的夺冠热门球队. ...
- 【基础知识】.Net基础加强第三天
一. 里氏替换原则--类型转换 1. 里氏替换原则:当需要一个父类类型对象的时候,可以给一个子类类型的对象. 2. 里氏替换原则实际也就是发生了隐身转换 3. a.>把子类类型赋值给父类类型, ...
- luoguP2303 [SDOI2012]Longge的问题 化式子
求\(\sum \limits_{i = 1}^n gcd(i, n)\) \(\sum \limits_{i = 1}^n gcd(i, n)\) \(=\sum \limits_{i = 1}^n ...
- wdcp(WDlinux Control Panel) 快速安装RPM包,lanmp一件安装
lanmp一键安装包是wdlinux官网2010年开始推出的lamp,lnmp,lnamp(apache,nginx,php,mysql,zend,eAccelerator,pureftpd)应用环境 ...
- Problem B: 七龙珠II
Description 小王去找了个算命先生算算这辈子是有钱还是没钱.他在纸上写下“性命”两个字,问哪个字重要. 小王想了想说当然是命比较重要. 他摇摇头:“你,没钱” “为什么?” “有钱,任性.没 ...
- 二值化方法:Kittler:Minimum Error Thresholding
Kittler二值化方法,是一种经典的基于直方图的二值化方法.由J. Kittler在1986年发表的论文“Minimum Error Thresholding”提出.论文是对贝叶斯最小错误阈值的准则 ...
- .net正则提取手机号码,并替换带有手机号码的a标签
//用正则查找字符串中的手机号码,最后替换成带a标签的可打电话的字符串 var str = "收件人:江苏省苏州市工业园区屌丝大叔收,电话:18688888888"; var re ...
- [置顶] 解决EXTJS文本框长度验证在ORACLE数据库下不正确的问题
由于ORACLE数据库里面一个汉字和符号占2 个字节,数字和英文占1个字节,所以用EXTJS的文本框MaxLenght去限制输入的长度是不正确的,因为EXTJS只限制了输入的字数量,而不是字节数量. ...
- 通过Roslyn构建自己的C#脚本(更新版)
之前写过文章介绍过如何通过Roslyn构建自己的C#脚本,但那篇文章是参考自Roslyn CTP版的,记得本来想等到Roslyn正式版出来重新更新一下文档的,不过记得后来Roslyn是跳票了的,Scr ...
- Turtelizer 2 provide JTAG Flash programming and debugging of ARM based boards via USB
http://www.ethernut.de/en/hardware/turtelizer/ Introducing Turtelizer 2 Overview Turtelizer 2 had be ...