索引:[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. 使用Win32::OLE操作Excel——Excel对象模型

    像VBA操作Excel一样,Win32::OLE模块也是通过对象操作来控制Excel. 如果想自动化操作和控制Excel应用程序,则必须要与Excel对象模型所提供的对象进行交互.理解和熟悉Excel ...

  2. Fedora24安装常用软件方法

    # 添加chrome源 cd /etc/yum.repos.d/ # 下载google-chrome.repo并保存# wget  http://repo.fdzh.org/chrome/google ...

  3. bit、byte、位、字节、字符串等概念

    原始文章:http://djt.qq.com/article/view/658 1.古代送信:马车,烽火,信鸽 2.1837年,世界第一条电报诞生, 美国科学家莫尔斯尝试用一些“点”和“划”来表示不同 ...

  4. HTML与CSS入门——第十四章  使用边距、填充、对齐和浮动

    知识点: 1.在元素周围添加边距的方法 2.在元素中添加填充的方法 3.对齐的方法 4.float属性的使用 这里提到了CSS禅意花园,这块有时间可以玩玩~ margin和padding:用于添加元素 ...

  5. 附加到IIS调试出现不会命中断点

    当项目附加到IIS进行调试时,如果在IIS中没有配置该项目则在设置断点是会出现:当前不会命中断点 还没有为该文档加载任何符号

  6. linux删除ORACLE【weber出品必属精品】

    关闭数据库 sqlplus / as sysdba shutdown abort 清除oracle软件 su - oracle cd $ORACLE_BASE rm -rf * rm -rf /etc ...

  7. linux tar使用

    Linux  tar指令简单使用 -c:创建包,-x:解压或解包(-c和-x可理解为互逆运算),-t:查看包 -f:后加处理文件,必须放在参数组合的最后一位(tar  -cf  a.tar  1.tx ...

  8. hdu 5305Friends

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5305 Problem Description There are n people and m pai ...

  9. python作业day3修改配置文件

    思维还有点乱,撸代码到深夜,先上代码吧.(我是跟着武sir的思路的) 流程图: 代码(有注释): #!/usr/bin/env python # -*- coding:utf-8 -*- import ...

  10. IOS响应式编程框架ReactiveCocoa(RAC)使用示例-备

    ReactiveCocoa是响应式编程(FRP)在IOS中的一个实现框架,它的开源地址为:https://github.com/ReactiveCocoa/ReactiveCocoa# :在网上看了几 ...