![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法-二分搜索
sparksnail
这个作者很懒,什么都没留下…
展开
-
nowcoder 元素最左出现
题目对于一个有序数组arr,再给定一个整数num,请在arr中找到num这个数出现的最左边的位置。 给定一个数组arr及它的大小n,同时给定num。请返回所求位置。若该元素在数组中未出现,请返回-1。 测试样例: [1,2,3,3,4],5,3 返回:2思路二分,依次比较num和中间值的大小关系,更新res值。代码class LeftMostAppearance:原创 2018-01-27 18:26:24 · 163 阅读 · 0 评论 -
LeetCode 34. Search for a Range
题目思路二分查找代码class Solution: def searchRange(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ left ...原创 2018-05-06 00:01:19 · 125 阅读 · 0 评论 -
LintCode 61. Search for a Range
题目Given a sorted array of n integers, find the starting and ending position of a given target value. If the target is not found in the array, return [-1, -1]. Have you met this question in a real原创 2018-02-07 20:12:09 · 229 阅读 · 0 评论 -
LintCode 28. 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 eac原创 2018-02-07 17:53:03 · 167 阅读 · 0 评论 -
LintCode 159. Find Minimum 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). Find the minimum element. Notice You may assume no duplicate exis原创 2018-02-07 17:41:28 · 177 阅读 · 0 评论 -
LintCode 74. First Bad Version
题目The code base version is an integer start from 1 to n. One day, someone committed a bad version in the code case, so it caused this version and the following versions are all failed in the unit te原创 2018-02-07 16:07:47 · 217 阅读 · 0 评论 -
LintCode 14. First Position of Target
题目For a given sorted array (ascending order) and a target number, find the first index of this number in O(log n) time complexity. If the target number does not exist in the array, return -1. Have原创 2018-02-07 15:55:42 · 239 阅读 · 0 评论 -
LintCode 457. Classical Binary Search
题目Find any position of a target number in a sorted array. Return -1 if target does not exist. Have you met this question in a real interview? Yes Example Given [1, 2, 2, 4, 5, 5]. For target原创 2018-02-07 15:50:36 · 298 阅读 · 0 评论 -
nowcoder 快速N次方
题目如果更快的求一个整数k的n次方。如果两个整数相乘并得到结果的时间复杂度为O(1),得到整数k的N次方的过程请实现时间复杂度为O(logN)的方法。 给定k和n,请返回k的n次方,为了防止溢出,请返回结果Mod 1000000007的值。 测试样例: 2,3 返回:8思路快速幂算法。把幂分解为二进制,不断循环遍历每一位的值,连乘。代码class QuickPowe原创 2018-01-29 17:09:34 · 209 阅读 · 0 评论 -
nowcoder 完全二叉树计数
题目给定一棵完全二叉树的根节点root,返回这棵树的节点个数。如果完全二叉树的节点数为N,请实现时间复杂度低于O(N)的解法。 给定树的根结点root,请返回树的大小。思路二分查找。统计根节点最左节点的深度。当左边深度和右边深度相同时,说明左边是完全二叉树,递归遍历右边,否则右边是完全二叉树,递归遍历左边。代码# class TreeNode:# def __i原创 2018-01-29 16:54:10 · 216 阅读 · 0 评论 -
nowcoder 最左原位
题目有一个有序数组arr,其中不含有重复元素,请找到满足arr[i]==i条件的最左的位置。如果所有位置上的数都不满足条件,返回-1。 给定有序数组arr及它的大小n,请返回所求值。 测试样例: [-1,0,2,3],4 返回:2思路二分查找。当arr[mid]代码class Find: def findPos(self, arr, n):原创 2018-01-29 16:20:23 · 199 阅读 · 0 评论 -
LintCode 62. 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 ...原创 2018-02-09 16:57:21 · 166 阅读 · 0 评论 -
LintCode 75. Find Peak Element
题目There is an integer array which has the following features: The numbers in adjacent positions are different. A[0] < A[1] && A[A.length - 2] > A[A.length - 1]. We define a position...原创 2018-02-09 16:13:26 · 232 阅读 · 0 评论 -
LeetCode 35. Search Insert Position
题目思路二分查找代码class Solution: def searchInsert(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ if target ...原创 2018-05-06 00:11:13 · 113 阅读 · 0 评论