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. 如何在两个月的时间内发表一篇EI/SCI论文-我的时间管理心得

    在松松垮垮的三年研究生时期,要说有点像样的成果,也只有我的小论文可以谈谈了.可能有些厉害的角色研究生是丰富而多彩的,而大多数的同学在研究生阶段可能同我一样,是慢悠悠的渡过的,而且可能有的还不如我,我还 ...

  2. [译] 怎样(以及为什么要)保持你的 Git 提交记录的整洁

    最近在掘金翻译了一篇文章,主要讲的是 Git 提交记录的维护,确实很有用,感兴趣的同学可以去看一下.链接如下: [译] 怎样(以及为什么要)保持你的 Git 提交记录的整洁 截图:

  3. 七.部署war包到Tomcat(基于Centos安装)

    1.把war包上传至tomcat的webapps目录下面 2.启动Tomcat,在Tomcat的bin目录下面,运行startup.sh 3.访问项目,如下成功打开项目

  4. wampserver 最新版本 mysql修改数据库密码

    由于wampsever版本更新就导致以前版本的密码修改造成失败,主要是密码字段改变造成的! 第一步 进入MySQL 控制台  wamp安装,数据库是没有密码 进入控制台直接回车就可以了 第二步 使用 ...

  5. MySQL 8.0.13的使用心得

    今天在阿里云上安装了最新版的MySQL,把碰到的一些问题总结下 1.导入从另一台服务器dump的.sql,出现如下提示: ERROR at line xxx: Unknown command '\\' ...

  6. CQRS轻量级框架【CQRSlite】学习使用小记

    前言 这几天在研究DDD和CQRS.快把我绕晕了.发现国外的好文质量还是挺高的.之所以先体验CQRSlite这个小框架,是因为看了一位大神写的文章:https://www.codeproject.co ...

  7. 在eclipse中配置Tomcat时,出现“Cannot create a server using the selected type”的错误。

    出现原因:Tomcat重新安装,并且安装目录改变了. 解决方案:在“Window->preferences->Server->Runtime Environment”,编辑Tomca ...

  8. 让NSArray数组中每个对象都调用的方法

    1. [array valueForKey:@"title"]; //Returns an array containing the results of invoking val ...

  9. bootstrap-table页码ALL显示为NAN

    在github上查阅找到的解决办法: https://github.com/wenzhixin/bootstrap-table/issues/435 页面部分: data-page-list=&quo ...

  10. windows用交互式命令执行python程序

    1.进入cmd命令 windows+r2.进入盘符,eg:E:3.使用dir命令查看当前文件夹下的所有目录4.使用绝对路径或者相对路径和cd命令直接进入想要到达的文件夹目录(或者使用cd命令一步一步达 ...