![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode binary search
proudmore
这个作者很懒,什么都没留下…
展开
-
Leetcode Search for a range
Given a sorted array of integers, 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 ar原创 2015-03-12 04:54:55 · 153 阅读 · 0 评论 -
Leetcode Divide two integers
divisor增加的时候*2,减小/2. handle integer overflow常用trick: longpublic class Solution { public int divide(int a, int b) { if(a==Integer.MIN_VALUE && b==-1)return Integer.MAX_VALUE; long d原创 2015-06-22 08:24:51 · 179 阅读 · 0 评论 -
Leetcode Minimum size subarray
Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn’t one, return 0 instead.For example, given the array [2,3,1,2,4,原创 2015-06-02 12:35:39 · 109 阅读 · 0 评论 -
Leetcode sqrt(x)
考点在于binary search 结束条件和overflow. 用loop invariant来理解: 每次loop都保证 [low,high]左边是严格小的,右边是严格大的.public class Solution { public int mySqrt(int x) { int low=1, high=x; if(x==0 || x==1)retur原创 2015-06-22 07:34:34 · 262 阅读 · 0 评论 -
Leetcode Pow(x,n)
divide conquer. 注意正负,overflowpublic class Solution { public double myPow(double x, int n) { int sign= (n%2==0 || x>0) ? 1 :-1; x=Math.abs(x); if(x==0 ||x==1)return sign*x;原创 2015-06-22 07:52:37 · 161 阅读 · 0 评论 -
Leetcode 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原创 2015-04-10 02:18:46 · 155 阅读 · 0 评论 -
Leetcode Search in rotated sorted array I, II
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 in原创 2015-03-24 09:48:20 · 125 阅读 · 0 评论 -
Leetcode Search insert position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.Here原创 2015-03-12 05:12:19 · 122 阅读 · 0 评论 -
Leetcode 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原创 2015-03-12 05:24:22 · 152 阅读 · 0 评论 -
Leetcode Count complete tree node
Given a complete binary tree, count the number of nodes.Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely filled, and原创 2015-06-07 02:59:28 · 168 阅读 · 0 评论