↓ 跳过正文
  1. Posts/

leetcode 33. Search in Rotated Sorted Array (无重复数的旋转数组找定值)

·372 字·1 分钟

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).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.

思路:找规律。。。二分。。。

0 1 2 3 4 5 6
1 2 3 4 5 6 0
2 3 4 5 6 0 1
3 4 5 6 0 1 2
4 5 6 0 1 2 3
5 6 0 1 2 3 4
6 0 1 2 3 4 5

观察发现。。。a[mid]<a[r]的时候,后半段有序;

a[mid]>a[r]的时候,前半段有序。。

然后根据有序区间的端点值,确定tar是在该有序区间,还是在另一半区间。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2017年04月13日 星期四 14时17分03秒
 4File Name :33.cpp
 5************************************************ */
 6class Solution {
 7
 8public:
 9
10    int bin(int n,int tar,vector<int>& nums)
11    {
12	int l = 0 ;
13	int r = n-1;
14	while (l<=r)
15	{
16	    int mid = (l+r)>>1;
17	    if (nums[mid]==tar) return mid;
18	    if (nums[mid]<nums[r])
19	    {
20		if (nums[mid]<tar && tar<=nums[r]) l = mid + 1;
21		else r = mid -1;
22	    }
23	    else
24	    {
25		if (nums[l]<=tar && tar < nums[mid]) r = mid - 1;
26		else l = mid + 1;
27	    }
28	}
29	return -1;
30    }
31
32    int search(vector<int>& nums, int target) {
33    int n = nums.size();
34    int res = bin(n,target,nums);
35    return res;
36
37
38    }
39
40};

相关文章

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

·333 字·1 分钟
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]. 思路:二分。。。 我好像根本不会二分啊??? 二分查找

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

·333 字·1 分钟
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 字·1 分钟
题目链接 题意:给一个二维数组。。。每一行每一列都分别递增。。问某个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 字·1 分钟
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个数,求所有的组合,和为定值,每个数可以重复用)