LeetCode:34. Search for a Range(Medium)
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)的更多相关文章
- LeetCode:24. Swap Nodes in Pairs(Medium)
1. 原题链接 https://leetcode.com/problems/swap-nodes-in-pairs/description/ 2. 题目要求 给定一个链表,交换相邻的两个结点.已经交换 ...
- [Leetcode][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
- 【一天一道LeetCode】#34. Search for a Range
一天一道LeetCode系列 (一)题目 Given a sorted array of integers, find the starting and ending position of a gi ...
- 【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 ...
- LeetCode 34. Search for a Range (找到一个范围)
Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...
- 【leetcode】Search for a Range(middle)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- 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 ...
- 【leetcode】Bitwise AND of Numbers Range(middle)
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- LeetCode:21. Merge Two Sorted Lists(Easy)
1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...
随机推荐
- PyCharm的Debug工具栏中的Watches
In the Watches pane you can evaluate any number of variables or expressions in the context of the cu ...
- 关于mvvm:UI、数据、绑定、状态、中间变量、数据适配、数据处理
绑定: UI控件 --> VM VM -> UI控件 关于mvvm:UI.数据.绑定.状态.中间变量.数据适配.数据处理: https://github.com/zzf073/Log ...
- jquery Jbox 插件实现弹出窗口在修改的数据之后,关闭弹出窗口刷新父页面的问题
http://blog.csdn.net/nsdnresponsibility/article/details/51282797 问题如题: 这里我们在父页面定义一个全局的变量来标识是否需要刷新父页面 ...
- BZOJ2654:tree(最小生成树,二分)
Description 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. Input 第一行V,E,need分别表示点数,边数和需要的白色 ...
- 搭建一个redis高可用系统
一.单个实例 当系统中只有一台redis运行时,一旦该redis挂了,会导致整个系统无法运行. 单个实例 二.备份 由于单台redis出现单点故障,就会导致整个系统不可用,所以想到的办法自然就是备份( ...
- mac端抓包工具——Charles使用
一.简介 Charles(http://www.charlesproxy.com/)是在Mac 下常用的截取网络封包的工具.Charles 通过将自己设置成系统的网络访问代理服务器,使得所有的网络访问 ...
- UglifyJS 压缩选项
UglifyJS 压缩选项 1.使用逗号运算符连接简单语句 2.使用点符号代替中括号属性 foo [“bar”]→foo.bar 3.删除逻辑上走不到的代码 4.删除调试代码 debug ...
- Learning by doing——获黄色领骑衫之感
获奖感言 能拿到这件黄色的领骑衫,心里真的非常高兴.仔细看了一下,扣子.领子.各种图案各种细节十分精致.可以说这件领骑衫既有纪念意义,又有实用意义,真的很棒. 背后的故事 其实开始接触博客的时候,我是 ...
- Layered Architecture 分层架构
分层的价值在于每一层都只代表程序中的某一特定方面.这种限制使每个方面的设计都更具有内聚性,更容易解释. 大多数成功的架构使用的都是包括下面这四个概念层的某个版本
- 集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096)
集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096) 题目描述 有一个专门为了集合运算而设计的"集合栈"计算机.该 ...