LintCode 二分法查找, 搜索插入位置 和 二维矩阵
来源:互联网 发布:上海威纳海关数据 编辑:程序博客网 时间:2024/05/22 07:40
1. 二分查找
给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1。
样例
在数组 [1, 2, 3, 3, 4, 5, 10] 中二分查找3,返回2。
挑战
如果数组中的整数个数超过了2^32,你的算法是否会出错?
solution: 注意如果出现重复的数字时我们需要放回第一个出现的位置,因此在使用二分法查找时,我们需要添加一个while循环判断前面是否有相同的数字.
class Solution {public: /** * @param nums: The integer array. * @param target: Target number to find. * @return: The first position of target. Position starts from 0. */ int binarySearch(vector<int> &array, int target) { // write your code here int prev = 0, last = array.size()-1; while (prev <= last) { int mid = (prev + last) / 2; if (target > array[mid]) prev = mid + 1; else if(target < array[mid]) last = mid - 1; else { while (array[mid-1] == target) --mid; return mid; } } return -1; }};
2. 搜索插入位置
给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。
你可以假设在数组中无重复元素。
样例
[1,3,5,6],5 → 2
[1,3,5,6],2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6],0 → 0
solution:经典的二分查找算法
class Solution { /** * param A : an integer sorted array * param target : an integer to be inserted * return : an integer */public: int searchInsert(vector<int> &A, int target) { // write your code here int n = A.size(); int prev = 0, last = n - 1; while (prev <= last) { int mid = (prev + last) / 2; if(target > A[mid]) prev = mid + 1; else if (target < A[mid]) last = mid - 1; else return mid; } return prev; }};
3 搜索二维矩阵
写出一个高效的算法来搜索 m × n矩阵中的值。
这个矩阵具有以下特性:
每行中的整数从左到右是排序的。
每行的第一个数大于上一行的最后一个整数。
样例
考虑下列矩阵:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
给出 target = 3,返回 true
挑战
O(log(n) + log(m)) 时间复杂度
solution: 这题是前面两道题的综合,首先找到target所在的行,然后在所在的列进行查找
code
class Solution {public: /** * @param matrix, a list of lists of integers * @param target, an integer * @return a boolean, indicate whether matrix contains target */ bool searchMatrix(vector<vector<int> > &matrix, int target) { // write your code here if (matrix.size() == 0) return false; int m = matrix.size()-1; int row = 0, last = m, mid; //首先找到元素所在的行 while (row <= last) { mid = (row + last) / 2; if (matrix[mid][0] < target) row = mid + 1; else if (matrix[mid][0] > target) last = mid - 1; else return true; } row -= 1; //注意此处 last = matrix[row].size() - 1; int col = 0; while (col <= last) { mid = (col + last) / 2; if (matrix[row][mid] < target) col = mid + 1; else if (matrix[row][mid] > target) last = mid - 1; else return true; } return false; }};
0 0
- LintCode 二分法查找, 搜索插入位置 和 二维矩阵
- lintcode ---- 搜索二维矩阵
- lintcode ----搜索二维矩阵
- 【LintCode】搜索二维矩阵
- lintcode,搜索二维矩阵
- LintCode : 搜索二维矩阵
- Lintcode:搜索二维矩阵
- LintCode : 搜索插入位置
- lintcode ----搜索插入位置
- LintCode : 插入搜索位置
- Lintcode 搜索插入位置
- lintcode:搜索插入位置
- Lintcode搜索插入位置
- lintCode 搜索插入位置
- LintCode:搜索二维矩阵 II
- LintCode:搜索二维矩阵 II
- 【LintCode】搜索二维矩阵 II
- lintcode,搜索二维矩阵 II
- 第四周项目3-单链表的应用递增判断
- 第四周 循环双链表应用
- 第3周项目4—顺序表应用问题(2)
- android--窗口去掉标题栏和状态栏
- 2.Swift 类和接口详解
- LintCode 二分法查找, 搜索插入位置 和 二维矩阵
- 时间复杂度最快的排序算法 O(n) JAVA代码
- 第四周项目1-建立单链表
- 惯性导航之术语基础(一)
- centos6.5下asterisk 13.3.2安装(分别从自己制作的rpm包和源码进行安装),支持mysql数据库
- Flash、RAM、ROM的区别
- 如何写科研文章
- 带你玩转Visual Studio——带你多工程开发
- 第四周项目六——多项式求和