
Binary Search
文章平均质量分 72
dyllanzhou
这个作者很懒,什么都没留下…
展开
-
[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原创 2015-10-01 22:04:38 · 260 阅读 · 0 评论 -
[Leetcode]Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 andn (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, f原创 2015-12-01 15:37:38 · 265 阅读 · 0 评论 -
[Leetcode]Divide Two Integers
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.class Solution {public: /*algorithm binary search */ int bSearch(lon原创 2015-11-19 15:11:26 · 262 阅读 · 0 评论 -
[Leetcode]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.You may assume no duplicate exists in the arra原创 2015-09-23 16:36:50 · 301 阅读 · 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.原创 2015-09-22 14:18:00 · 249 阅读 · 0 评论 -
[Leetcode]Pow(x, n)
Implement pow(x, n).Have you met this question in a real interview?class Solution {public: /*algorithm: divde and conqure */ double myPow(double x,原创 2015-10-26 20:31:41 · 292 阅读 · 0 评论 -
[Leetcode]Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find thekth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Follow up:What if the BST原创 2015-08-20 22:20:59 · 288 阅读 · 0 评论 -
[Leetcode]Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.class Solution {public: /*algorithm: binary search use x/m < m instead of x < m*m to prevent overflow */ int原创 2015-11-05 22:01:28 · 244 阅读 · 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原创 2015-10-05 17:25:23 · 284 阅读 · 0 评论 -
[Leetcode]H-Index II
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?Hint:Expected runtime complexity is inO(log n) and the input is sorted.原创 2015-11-04 15:14:54 · 246 阅读 · 0 评论 -
[Leetcode]Count of Smaller Numbers After Self
You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i].Example:G原创 2016-01-20 18:08:32 · 1060 阅读 · 0 评论