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 创建auto increment 自增函数

    计数器表  > db.counters.insert({_id: "userid",seq: 0}  ); > db.counters.find();  { " ...

  2. 使用ant进行邮件发送,ant发送已存在的html文件

    Jenkins上使用发送邮件功能一直有问题,放弃Jenkins配置,使用ant的进行发送邮件,参考文档可以: https://www.jianshu.com/p/04cfce59890a 我这里是要发 ...

  3. PhoneGap API 之多媒体

    一. MediaApi 简单介绍 PhoneGap API Media 对象提供录制和回放设备上的音频文件的能力 参数: var media = new Media(src, mediaSuccess ...

  4. Java连接MQ的实例, 测试类

    package cjf.mq.mqclient; import com.ibm.mq.MQC; import com.ibm.mq.MQEnvironment; import com.ibm.mq.M ...

  5. MySQL语法三:数据控制语句

    数据控制语句MCL(GRANT,REVOKE,COMMIT,ROLLBACK)

  6. Hadoop-2.2.0中文文档—— Common - 超级用户模拟别的用户

    简单介绍 此文档描写叙述了一个超级用户怎样在安全的方式下以还有一用户的名义提交作业或訪问hdfs. Use Case 下一部分描写叙述的的代码演示样例对此用户用例是可用的. 一个username为's ...

  7. 轻松排查线上Node内存泄漏问题

    I. 三种比较典型的内存泄漏 一. 闭包引用导致的泄漏 这段代码已经在很多讲解内存泄漏的地方引用了,非常经典,所以拿出来作为第一个例子,以下是泄漏代码: 'use strict'; const exp ...

  8. ASP.NET Core 程序发布到Linux(Centos7)爬坑实战

    前言 前阶段弄了个Linux系统想倒腾倒腾.NET Core,结果看了下网上的资料,大部分都是过期的,走了不少弯路,不过还好,今下午总算捣鼓出来了.Linux命令太多了,唉.血的教训:安装一定要看官网 ...

  9. ASP.NET SingalR + MongoDB 实现简单聊天室(三):实现用户群聊,总结完善

    前两篇已经介绍的差不多了,本篇就作为收尾. 使用hub方法初始化聊天室的基本步骤和注意事项 首先确保页面已经引用了jquery和singalR.js还有对应的hubs文件,注意,MVC框架有时会将jq ...

  10. 决策树在sklearn中的实现

    1 概述 1.1 决策树是如何工作的 1.2 构建决策树 1.2.1 ID3算法构建决策树 1.2.2 简单实例 1.2.3 ID3的局限性 1.3 C4.5算法 & CART算法 1.3.1 ...