索引:[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. 线程 (detach的作用)

      线程状态在一个线程的生存期内,可以在多种状态之间转换.不同操作系统可以实现不同的线程模型,定义许多不同的线程状态,每个状 态还可以包含多个子状态.但大体说来,如下几种状态是通用的:       就 ...

  2. Chapter 4: Spring and AOP:Spring's AOP Framework -- draft

    Spring's AOP Framework Let's begin by looking at Spring's own AOP framework - a proxy-based framewor ...

  3. linux加入windows域之完美方案(转载)

    概念理解:1.kdc:可信任的密钥分发中心(KDC, Key Distribution Center).2.Winbind是Samba套件的功能之一.它允许Unix系统利用Windows NT的用户帐 ...

  4. unbantu相关笔记

    很多项目使用的系统是centos或者redhat,最近有一个项目使用的系统竟然是阿里云unbantu,不知道他们负责人怎么想的,明明有centos,非要用unbantu.抱怨到此,unbantu的学习 ...

  5. AspNetPager

    AspNetPager使用方法引入dll <%@ Register assembly="AspNetPager" namespace="Wuqi.Webdiyer& ...

  6. FineUI布局应用(二)

    一.FineUI页面布局分为 1.Fit布局 <f:Panel ID="Panel1" Title="布局Fit(Layout=Fit)" runat=& ...

  7. Oracle存储过程procedure

    --给plsql块设置一个名称保存下来便于调用 /* Create or replace procedure 名称 As 声明变量 Begin //代码块 End; --plsql块 Declare ...

  8. Trident内核中取验证码图片的几种方法

    程序中用了IE的内核,想取出网站中的验证码图片,单独显示出来,调研了以下几路方法 1.枚举所有缓存文件,进行处理,找到想要的,核心代码 )//这段代码可以枚举所有缓存资源,然后对应做处理 { LPIN ...

  9. (原)10-folder交叉验证

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6069731.html 参考网址: https://github.com/cmusatyalab/ope ...

  10. centos中的配置文件

    /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置. /etc/bashrc:为每一个 ...