1. 原题链接

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

2. 题目要求

给定一个按升序排列的整型数组nums[ ]和目标值target(int类型),如果数组中存在目标值,返回目标值在数组中的起始位置和结束位置,[start, end]。不存在返回 [-1, -1]。

3. 解题思路

思路一:暴力解决,遍历数组进行匹配,时间复杂度O( n )

4. 代码实现

 package com.huiAlex;

 public class SearchForARange34 {
public static void main(String[] args) {
int[] nums = {5, 7, 7, 8, 8, 8, 8, 10};
int[] res = searchRange(nums, 8);
for (int x : res)
System.out.println(x);
} public static int[] searchRange(int[] nums, int target) {
int start, end, count;
start = 0;
end = 0;
count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target) {
start = i;
count++;
}
if (nums[i] > target) break;
} if (count == 0) {
start = -1;
end = -1;
} else {
end = start;
start -= count - 1;
}
return new int[]{start, end};
}
}

LeetCode:34. Search for a Range(Medium)的更多相关文章

  1. LeetCode:24. Swap Nodes in Pairs(Medium)

    1. 原题链接 https://leetcode.com/problems/swap-nodes-in-pairs/description/ 2. 题目要求 给定一个链表,交换相邻的两个结点.已经交换 ...

  2. [Leetcode][Python]34: Search for a Range

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...

  3. 【一天一道LeetCode】#34. Search for a Range

    一天一道LeetCode系列 (一)题目 Given a sorted array of integers, find the starting and ending position of a gi ...

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

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

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

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

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

  7. LeetCode OJ 34. Search for a Range

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

  8. 【leetcode】Bitwise AND of Numbers Range(middle)

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  9. LeetCode:21. Merge Two Sorted Lists(Easy)

    1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...

随机推荐

  1. mongoDB 创建数据库、删除数据库

    创建数据库 use 命令 MongoDB 用 use + 数据库名称 的方式来创建数据库.use 会创建一个新的数据库,如果该数据库存在,则返回这个数据库. 语法格式 use 语句的基本格式如下: u ...

  2. 跨域问题时的Filter无效

    我页面用Web Uploader进行图片上传,后台使用一个过滤器解决跨域的options问题,然后我给入口类加上了这个过滤器注解配置,但是无效页面代码: <body> <div id ...

  3. hdu-3074 Multiply game---线段树+单点更新

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3074 题目大意: 给一些数,进行点更新或者是区间计算乘积 解题思路: 裸的线段树,注意空间开大点 # ...

  4. [objc explain]: Non-fragile ivars

    [objc explain]: Non-fragile ivars   (2009-01-27 09:30 PM)   Non-fragile instance variables are a hea ...

  5. 【[AHOI2005]航线规划】

    树剖维护边双 首先我们看到在整个过程中图是保证连通的,于是我们并不需要LCT来维护连通性 而这些询问询问的是两个点之间关键路径的数量,也就是无论怎么走都必须走的数量,显然这就是两点之间的割边的数量 由 ...

  6. docker-2-安装

    安装之前确定Centos的相关问题: CentOS Docker 安装 Docker支持以下的CentOS版本: CentOS 7 (64-bit) CentOS 6.5 (64-bit) 或更高的版 ...

  7. windows快捷命令修炼

    Description Windows Key combination Open/Close the Start Menu Windows key Open the Action center. Wi ...

  8. 提交文件到ng-pages分支

    一.提交dist文件夹到ng-pages分支 git subtree push --prefix=dist origin gh-pages 二.提交所有文件到ng-pages分支 text git:( ...

  9. SVN工具使用总结

    SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Subversion ...

  10. springboot多数据源的配置与使用

    转自:https://www.jianshu.com/p/34730e595a8c 之前在介绍使用JdbcTemplate和Spring-data-jpa时,都使用了单数据源.在单数据源的情况下,Sp ...