1. 普通的二分法查找查找等于target的数字

2. 还可以查找小于target的数字中最小的数字和大于target的数字中最大的数字

由于新的查找结果总是比旧的查找结果更接近于target,因此只需不停更新result

3. 查找最接近于target的数字

这种情况下,新的查找结果不一定比旧的查找结果更接近target,所以要比较他们与target的差值。

题目:

74. Search a 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

  • Integers in each row are sorted from left to right.
  • The first integer of each row is greater than the last integer of the previous row.

For example,

Consider the following matrix:

[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]

Given target = 3, return true.

查找第一列中大于target的index1,查找最后一列中小于target的index2

数字在index1< n < index2列中

在每一列中再使用二分法

33. Search in Rotated Sorted Array

Suppose a sorted array 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.

public class Solution {
public int search(int[] nums, int target) {
int length = nums.length;
int start = 0;
int end = length - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
int midNum = nums[mid];
if (midNum == target) {
return mid;
}
int startNum = nums[start];
if (startNum <= midNum) {
if (midNum < target) {
start = mid + 1;
} else {
if (startNum == target) {
return start;
} else if (startNum < target) {
end = mid - 1;
} else {
start = mid + 1;
}
}
} else {
if (midNum > target) {
end = mid - 1;
} else {
int endNum = nums[end];
if (endNum == target) {
return end;
} else if (endNum > target) {
start = mid + 1;
} else {
end = mid - 1;
}
}
}
}
return -1;
}
}

81. Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

Write a function to determine if a given target is in the array.

为了防止最坏情况的出现,从头搜到尾。。。

具体解释见九章算法

Search in Rotated Sorted Array II

162. Find Peak Element

A peak element is an element that is greater than its neighbors.

Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.

The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.

You may imagine that num[-1] = num[n] = -∞.

For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.

click to show spoilers.

Note:

Your solution should be in logarithmic complexity.

public class Solution {
public int findPeakElement(int[] nums) {
int length = nums.length;
if (length == 0) {
return 0;
}
if (length == 1) {
return 0;
}
if (nums[0] > nums[1]) {
return 0;
}
if (nums[length - 1] > nums[length - 2]) {
return length - 1;
}
int start = 1;
int end = length - 2;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (nums[mid - 1] < nums[mid]) {
start = mid;
} else if (nums[mid] > nums[mid + 1]) {
end = mid - 1;
} else {
end = mid - 1;
}
}
if (nums[start] >= nums[end]) {
return start;
} else {
return end;
}
}
}

[leetcode] 题型整理之查找的更多相关文章

  1. [leetcode] 题型整理之二叉树

    94. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' va ...

  2. [leetcode] 题型整理之动态规划

    动态规划属于技巧性比较强的题目,如果看到过原题的话,对解题很有帮助 55. Jump Game Given an array of non-negative integers, you are ini ...

  3. [leetcode] 题型整理之排列组合

    一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...

  4. [leetcode] 题型整理之数字加减乘除乘方开根号组合数计算取余

    需要注意overflow,特别是Integer.MIN_VALUE这个数字. 需要掌握二分法. 不用除法的除法,分而治之的乘方 2. Add Two Numbers You are given two ...

  5. [leetcode] 题型整理之cycle

    找到环的起点. 一快一慢相遇初,从头再走再相逢.

  6. [leetcode]题型整理之用bit统计个数

    137. Single Number II Given an array of integers, every element appears three times except for one. ...

  7. [leetcode] 题型整理之图论

    图论的常见题目有两类,一类是求两点间最短距离,另一类是拓扑排序,两种写起来都很烦. 求最短路径: 127. Word Ladder Given two words (beginWord and end ...

  8. [leetcode] 题型整理之排序

    75. Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects ...

  9. [leetcode] 题型整理之字符串处理

    71. Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = &q ...

随机推荐

  1. 洛谷P2434 [SDOI2005]区间

    题目描述 现给定n个闭区间[ai, bi],1<=i<=n.这些区间的并可以表示为一些不相交的闭区间的并.你的任务就是在这些表示方式中找出包含最少区间的方案.你的输出应该按照区间的升序排列 ...

  2. UVa 11987 Almost Union-Find(支持删除操作的并查集)

    传送门 Description I hope you know the beautiful Union-Find structure. In this problem, you’re to imple ...

  3. php如何防止图片盗用/盗链的两种方法

    如今的互联网,采集网站非常多,很多网站都喜欢盗链/盗用别人网站的图片,这样不仅侵犯网权,还导致被盗链的网站消耗大量的流量,给服务器造成比较大的压力,本文章向大家介绍php如何防止图片盗用/盗链的两种方 ...

  4. Excel 函数VLOOKUP初学者使用指南

    1.基础说明 =VLOOKUP(lookup_value,tabble_array,col_index_num,(range_lookup)) lookup_value:用什么查找 tabble_ar ...

  5. SQL SERVER 中 GO 的用法

    用信号通知 Microsoft® SQL Server™ 实用工具一批 Transact-SQL 语句的结束.GO 不是 Transact-SQL 语句:而是可为 osql 和 isql 实用工具及 ...

  6. 常用的Firefox浏览器插件、Chrome浏览器插件收藏

    [血的教训] 不要去下载“Firefox中国版(谋智网络)”,默认情况下会给你安装好多的莫名其妙的插件,推荐去Firefox官方下载原版. Firefox 原版官方网址: https://www.mo ...

  7. PHP如何判断一个gif图片是否为动画?

    首先想到的是用getimagesize()看看type,发现都是gif. 然后想gif动画是gif89格式的,发现文件开头是gif89,但是很多透明图片也是用的gif89格式. 看来必须分析文件的祯了 ...

  8. CentOS6.3 编译安装LAMP(2):编译安装 Apache2.2.25

    所需源码包: /usr/local/src/Apache-2.2.25/httpd-2.2.25.tar.gz 编译安装 Apache2.2.25 #切换到源码目录 cd /usr/local/src ...

  9. .htaccess 基础教程(二)

    今天要介绍的:.htaccess的第一个应用是自定义错误页面,这将使你可以拥有自己的.个性化的错误页面(例如找不到文件时),而不是你的服务商提供的错误页或没有任何页面.这会让你的网站在出错的时候看上去 ...

  10. css-单位%号-background-size-background-position-遁地龙卷风

    (-1)写在前面 我用的是chrome49,这篇是为后续做准备.重要性的调整以及毕业资料的整体导致最近没看JQuery和H5特效,以后只能晚上看了. (0)准备 div长宽都为300px,我们一张大小 ...