索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

Github: https://github.com/illuz/leetcode


035. Search for a Range (Medium)

链接

题目:https://leetcode.com/problems/search-for-a-range/

代码(github):https://github.com/illuz/leetcode

题意

在有序数组中找到一个数的范围。(由于数有反复)

分析

还是二分搜索变形。

  1. (C++)直接用 C++ STL 的 lower_boundupper_bound 偷懒。

  2. (Java)直接从普通的二分改一下即可了。

代码

C++:

class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
int* lower = lower_bound(A, A + n, target);
int* upper = upper_bound(A, A + n, target);
if (*lower != target)
return vector<int> {-1, -1};
else
return vector<int>{lower - A, upper - A - 1};
}
};

Java:

public class Solution {

    public int[] searchRange(int[] A, int target) {
int[] ret = new int[2];
ret[0] = ret[1] = -1;
int left = 0, right = A.length - 1, mid; while (left <= right) {
if (A[left] == target && A[right] == target) {
ret[0] = left;
ret[1] = right;
break;
} mid = (right + left) / 2;
if (A[mid] < target) {
left = mid + 1;
} else if (A[mid] > target) {
right = mid - 1;
} else {
if (A[right] == target) {
++left;
} else {
--right;
}
}
} return ret;
}
}

[LeetCode] 034. Search for a Range (Medium) (C++/Java)的更多相关文章

  1. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

  2. LeetCode 034 Search for a Range

    题目要求:Search for a Range Given a sorted array of integers, find the starting and ending position of a ...

  3. Java for LeetCode 034 Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  4. [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 ...

  5. leetCode 34.Search for a Range (搜索范围) 解题思路和方法

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

  6. leetcode 34 Search for a Range(二分法)

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

  7. 【leetcode】Search for a Range(middle)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  8. LeetCode 34. Search for a Range (找到一个范围)

    Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...

  9. leetcode 【 Search for a Range 】python 实现

    题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...

随机推荐

  1. openstack中glance组件images的全部python API 汇总

    感谢朋友支持本博客,欢迎共同探讨交流.因为能力和时间有限.错误之处在所难免,欢迎指正! 假设转载,请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...

  2. HTML与CSS入门——第十二章  在网页中使用多媒体

    知识点: 1.如何链接多媒体文件 2.如何嵌入多媒体文件 3.使用多媒体的更多技巧 多媒体文件:音频,视频和动画,以及静态的图像和文本. 这里我就直接讲HTML5了…… 此前都是用ojbect来加载或 ...

  3. CSS书写规范、顺序和命名规则

    写了这么久的CSS,但大部分前端er都没有按照良好的CSS书写规范来写CSS代码,这样会影响代码的阅读体验,这里总结一个CSS书写规范.CSS书写顺序供大家参考   这些是参考了国外一些文章以及我的个 ...

  4. web worker使用

    使用postMessage()方法传递信息.来自Worker的数据保存在event.data中.通过message和error事件与页面通信. <script> var data = [4 ...

  5. Gora官方范例

    参考官方文档:http://gora.apache.org/current/tutorial.html 项目代码见:https://code.csdn.net/jediael_lu/mygoradem ...

  6. 简单的thinkPHP3.2运行实例。

    在上一篇的环境基础下. 我们用zendstudio12.5版本编写我们的代码.具体的下载方式在这里就不多做注明了.自己百度就可以搞定. 首先我们用zendstudio12.5 导入我们从网上随处都可以 ...

  7. python之pandas模块

    一.pandas模块是基于Numpy模块的,pandas的主要数据结构是Series和DadaFrame,下面引入这样的约定: from pandas import Series,DataFrame ...

  8. Python多线程,threading的用法

    虫师的文章: 需要注意的是: threads = [ ] t1 = threading.Thread(target=music,args=(u'爱情买卖',)) threads.append(t1) ...

  9. FPGA技术的一些基本概念(综合、BlackBox)(转)

    原文:http://blog.sina.com.cn/s/blog_6254a8ca0100i0wr.html 原文也是转的,哈哈,大家多转转,转转更健康.删除了一些Xilinx的东西 前言 综合是将 ...

  10. Blogger建立新文章 - Blog透视镜

    使用Blogger,建立好Blog部落格之后,接着就是建立新文章,它是Blog部落格的灵魂,先从简单开始,来了解建立新文章的标题,文章中如何上传图片,建立卷标,及设定排程日期,定时自动发布等这些功能, ...