【LeetCode 34】Search for a Range

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

Your algorithm‘s runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].

题意:

  给定一个有序数组,其中可能包含一些重复数字,给定一个数字,求其第一次和最后一次出现的坐标,要求 O(log n)。

思路:

  有序数组+ O(log n),很显然二分搜索啦。

C++:

 1 class Solution {
 2 public:
 3
 4     int Bsearch(vector<int>& nums, int len, int start, int end, int tar, bool flag)
 5     {
 6         if(start > end)
 7             return -1;
 8
 9         int mid = (start + end) / 2;
10
11         if(nums[mid] == tar)
12         {
13             if(flag)
14             {
15                 if((mid > 0 && nums[mid - 1] != tar) || mid == 0)
16                 {
17                     return mid;
18                 }
19                 else
20                 {
21                     return Bsearch(nums, len, start, mid - 1, tar, flag);
22                 }
23             }
24             else
25             {
26                 if((mid < len - 1 && nums[mid + 1] != tar) || mid == len - 1)
27                 {
28                     return mid;
29                 }
30                 else
31                 {
32                     return Bsearch(nums, len, mid + 1, end, tar, flag);
33                 }
34             }
35
36         }
37         else if(nums[mid] < tar)
38         {
39             return Bsearch(nums, len, mid + 1, end, tar, flag);
40         }
41         else
42         {
43             return Bsearch(nums, len, start, mid - 1, tar, flag);
44         }
45     }
46
47     vector<int> searchRange(vector<int>& nums, int target) {
48
49         int len = nums.size();
50         vector<int> ret;
51         if(len == 0)
52             return ret;
53
54         ret.push_back(Bsearch(nums, len, 0, len - 1, target, true));
55         ret.push_back(Bsearch(nums, len, 0, len - 1, target, false));
56
57         return ret;
58     }
59 };
时间: 2024-11-02 13:06:59

【LeetCode 34】Search for a Range的相关文章

【LeetCode OJ 34】Search for a Range

题目链接:https://leetcode.com/problems/search-for-a-range/ 题目:Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not foun

【LeetCode OJ】Search Insert Position

题目:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,6]

【LeetCode OJ】Word Ladder I

Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in this problem: Hash Table. One hash set is the words dictionary where we can check if a word is in the dictionary in O(1) time. The other hash set is us

【LeetCode 229】Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. 思路: [LeetCode 169]Majority Element 的拓展,这回要求的是出现次数超过三分之一次的数字咯,动动我们的大脑思考下,这样的数最多会存在几个呢,当然是2个嘛.因此,接着上一题的方

【LeetCode OJ】Sum Root to Leaf Numbers

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 # Definition for a  binary tree node # class TreeNode: #     def __init__(self, x): #         self.val = x #         self.left = No

【LeetCode OJ】Longest Consecutive Sequence

Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classical problem where we can reduce the running time by the help of hash table. By given a list of numbers, we can find the longest consecutive sequence b

【leetcode 简单】 第七十七题 单词模式

给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循相同的模式. 这里的遵循指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非空单词之间存在着双向连接的对应模式. 示例1: 输入: pattern = "abba", str = "dog cat cat dog" 输出: true 示例 2: 输入:pattern = "abba", str = "dog cat cat fish&

【leetcode系列】String to Integer (atoi)

这个我就直接上代码了,最开始把"abc123"也算作合法的了,后来查了一下atoi的定义,把这种去掉了. public class Solution { public static int atoi(String inStr) { long result = 0L; /* * 网上查了一下,atoi函数的定义是如果第一个非空格字符存在,是数字或者正负号则开始做类型转换, * 之后检测到非数字(包括结束符\0)字符时停止转换,返回整型数.否则,返回零.可能的输入情况有: 1.空字符串 *

【leetcode系列】Valid Parentheses

很经典的问题,使用栈来解决,我这里自己实现了一个栈,当然也可以直接用java自带的Stack类. 自己实现的栈代码: import java.util.LinkedList; class StackOne { LinkedList<Object> data; int top; int maxSize; StackOne(int size) { // TODO Auto-generated constructor stub top = -1; maxSize = 100; data = new