↓ Skip to main content
  1. Posts/

leetcode 34. Search for a Range (二分,找到一段值为tar的区间)

·333 words·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

Given an array of integers sorted in ascending order, 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].

思路:二分。。。

我好像根本不会二分啊???

二分查找

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2017年04月13日 星期四 10时49分02秒
 4File Name :34.cpp
 5************************************************ */
 6class Solution {
 7
 8public:
 9
10    int n;
11    vector<int>res;
12    int bin(int l,int r,vector<int>& nums,int tar) //
13    {
14	while (l<=r)
15	{
16	    int mid = (l+r)>>1;
17	    if (nums[mid]>=tar) r = mid - 1;
18	    else l = mid + 1;
19	}
20	return nums[r+1]==tar?r+1:-1;//找到第一个等于tar的下标
21    }
22    int bin2(int l,int r,vector<int>& nums,int tar)
23    {
24	while (l<=r)
25	{
26	    int mid = (l+r)>>1;
27	    if (nums[mid]>tar) r = mid-1;
28	    else l = mid + 1;
29	}
30	return nums[l-1]==tar?l-1:-1; //找到最后一个等于tar的下标
31    }
32    vector<int> searchRange(vector<int>& nums, int target) {
33        n = nums.size();
34	if (n==0) return vector<int>(2,-1);
35	int L = bin(0,n-1,nums,target);
36	int R = bin2(0,n-1,nums,target);
37	if (L>=n) L = -1; //对于只有一个数的情况,需要维护一下。
38	if (R>=n) R = -1;
39	res.push_back(L);
40	res.push_back(R);
41	return res;
42    }
43
44};

Related

leetcode 81. Search in Rotated Sorted Array II (有重复元素的旋转数组找给定值)

·333 words·1 min
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Write a function to determine if a given target is in the array. The array may contain duplicates. 好像阿里一面的时候问过。。。 思路:肯定是二分。。。不过由于有重复元素。。。所以很恶心。。。

leetcode 74. Search a 2D Matrix

·183 words·1 min
题目链接 题意:给一个二维数组。。。每一行每一列都分别递增。。问某个value是否出现过。。。 思路:单调。。显然二分。。。唯一的技巧是从右上角开始搜。 1/* *********************************************** 2Author :111qqz 3Created Time :2017年03月09日 星期四 19时03分07秒 4File Name :74.cpp 5************************************************ */ 6class Solution { 7 8public: 9 10 bool searchMatrix(vector<vector<int>>& matrix, int target) { 11 12 int n = matrix.size(); 13 if (n==0) return false; 14 int m = matrix[0].size(); 15 if (m==0) return false; 16 int row = 0 ; 17 int col = m-1; 18 while (col>=0&&row<n) 19 { 20 if (matrix[row][col]==target) return true; 21 else 22 if (matrix[row][col]>target) col--; 23 else row++; 24 } 25 return false; 26 27 } 28 29};

leetcode 39. Combination Sum (dfs,求所有的组合,和为定值,每个数可以重复用)

·278 words·1 min
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: * All numbers (including target) will be positive integers. * The solution set must not contain duplicate combinations. 题意:给n个数,求所有的组合,和为定值,每个数可以重复用)

leetcode 40. Combination Sum II (枚举子集,和为定值)

·106 words·1 min
* Total Accepted: **106670** * Total Submissions: **329718** * Difficulty: **Medium** * Contributor: **LeetCode** Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: * All numbers (including target) will be positive integers. * The solution set must not contain duplicate combinations. 题意:若干正数,求所有和为target的组合。