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. EOF及相关函数

    结论:EOF是在头文件stdio.h中预定义的一个宏,而eof(end of file)是一个与标准输入/输出流相关联的标志位.当文件指针已经指向文件尾且再次尝试读取时,eof标志会被设置.同时,某些 ...

  2. Angular4.0--创建类实例

    src/app/hero.ts文件: export class Hero { constructor( public id: number, public name: string) { } } sr ...

  3. 修改WSAD的默认工作区(转)

    如果要一直用某个目录作为工作区,可以在启动WSAD的时候将该目录设为默认工作区,这样启动时直接启动,方便.快捷.但是如果设置后在想换工作区就比较麻烦了,因为没有可供选择的界面了,跟网上搜了一下总结共有 ...

  4. IBM websphere MQ远程队列的简单配置

    原理: 1.远程队列分发送方和接收方 2.接收方配置: 接收方配置要先拿到对方的发送通道配置,接收方的队列名称必须和远程发送方的队列名称一致,告诉远程发送方,你的地址,队列管理器名称等信息,在通道中建 ...

  5. Git 初始化全局user.name 和 user.email

    git config --global user.name "username" git config --global user.email "email"

  6. PHP------关于字符串的处理

    每一种语言对,字符串都是比较重要的,因为字符串牵扯到输出. 尤其是在网页里面,所有的内容输出,都要以字符串的形式展示在页面上.比如,输出换行.输出一段话或者输出一个标签,都是以字符串来输出的:有时用数 ...

  7. C#读取信息备份

    class Program { static void Main(string[] args) { var wc = new WebClient(); var html = wc.DownloadSt ...

  8. js 实现加入收藏/加入首页功能

    加入收藏夹实现: html代码如下: <a href="javascript:;" onclick="AddFavorite(window.location.hre ...

  9. QGis 利用Python Console编写脚本进行批量处理

    前言 这篇文章里,我们要完成一些数据的合并,计算等操作. 准备工作 首先要了解Qgis的编程模型,具体参考文章<QGIS里的编程模型>及<Qgis里的查询过滤>.了解了Qgis ...

  10. Multicast Routing

    Multicasting Source S sends packets to multicast group G1 (and minimize the number of copies) Revers ...