【LeetCode OJ 34】Search for a Range
题目链接:https://leetcode.com/problems/search-for-a-range/
题目: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].
解题思路:题意为在一个有序数组中寻找指定值的起始位置和结束位置,由于题目要求时间复杂度为O(log n)。故而想到使用二分查找法。
演示样例代码:
public class Solution
{
public int[] searchRange(int[] nums, int target)
{
int[] result=new int[]{-1,-1};
int start=0;
int end=nums.length-1;
while(start<=end)
{
int middle=(start+end)>>1;
int middleValue=nums[middle];
if(middleValue==target)
{
//找到目标值后,先搜索其左边有没有与目标值相等的数,取其最左边的索引值放入result中。同理再搜索其最右边
int i=middle;
while((i-1)>=0&&nums[i-1]==target)
{
i--;
}
result[0]=i;
int j=middle;
while((j+1)<nums.length&&nums[j+1]==target)
{
j++;
}
result[1]=j;
return result;
}
else if(target<middleValue)
{
//小于中值时在中值前面找
end=middle-1;
}
else
{
//大于中值在中值后面找
start=middle+1;
} }
return result;
}
}
【LeetCode OJ 34】Search for a Range的更多相关文章
- 【LeetCode OJ 016】3Sum Closest
题目链接:https://leetcode.com/problems/3sum-closest/ 题目:Given an array S of n integers, find three integ ...
- 【LeetCode OJ 232】Implement Queue using Stacks
题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operatio ...
- 【LeetCode OJ 136】Single Number
题目链接:https://leetcode.com/problems/single-number/ 题目:Given an array of integers, every element appea ...
- 【LeetCode OJ 268】Missing Number
题目链接:https://leetcode.com/problems/missing-number/ 题目:Given an array containing n distinct numbers t ...
- 【LeetCode OJ 14】Longest Common Prefix
题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...
- LeetCode(34)Search for a Range
题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...
- 【leetcode 字符串处理】Compare Version Numbers
[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...
- 【LeetCode算法-27】Remove Element
LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
随机推荐
- ./configure --prefix /?/? 解释
-- ./configure :编译源码 --prefix :把所有资源文件放在 之后的路径之后
- NYIST 1107 最高的奖励
最高的奖励 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 请问:挖掘机技术哪家强?AC了告诉你! 给你N(N<=3*10^4)个任务,每个任务有一个截止完成时 ...
- WinServer-IIS-svg/woff/woff2字体 404错误
问题:最近在IIS上部署web项目的时候,发现浏览器总是报找不到woff.woff2字体的错误.导致浏览器加载字体报404错误,白白消耗了100-200毫秒的加载时间. 原因:因为服务器IIS不认SV ...
- debian mysql 定时自己主动备份的脚本
#!/bin/sh LOG=/var/log/mysql-backup.log # mysql db info USER_ROOT=XXXXXX USER_PWD=XXXXXXX # mysql da ...
- hdu4390-Number Sequence(容斥计算)
题意:给定b数列.计算有多少种数列 a1,a2,...,an 满足条件 a1*a2*...*an=b1*b2*-*bn (ai>1). 解法:处理出b数列中出现的全部质因子的数量记录在map中, ...
- Android 多分辨率自适应总结
这周的工作对Android项目多分辨率自适应进行调整.故对这方面知识进行不断的尝试学习.Android项目刚開始做的时候一定养成编程习惯,全部资源调用放在value中.统一命名以及管理.总结了下面内容 ...
- No connection could be made because the target machine actively refused it [::1]:808
No connection could be made because the target machine actively refused it [::1]:808 1.首先查看端口占用情况, 在 ...
- Authentication in asp.net
https://docs.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-security/introduction/an-o ...
- legend---十一、thinkphp事务中if($ans1&&$ans2){}else{}方式和try{}catch{}方式事务操作的区别在哪里
legend---十一.thinkphp事务中if($ans1&&$ans2){}else{}方式和try{}catch{}方式事务操作的区别在哪里 一.总结 一句话总结:执行的条件其 ...
- 10.ref regex unordered_set smartpoint
ref引用不可以复制的对象 void print(std::ostream &os, int i) { os << i << endl; } //ref引用不可以复制的 ...