
Binary Search
文章平均质量分 71
zshouyi
这个作者很懒,什么都没留下…
展开
-
349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].Note:Each element in the result must be unique.The res原创 2017-01-04 04:07:42 · 320 阅读 · 0 评论 -
209. Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.For example, given the原创 2017-03-02 04:55:48 · 249 阅读 · 0 评论 -
35. 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.原创 2017-02-14 16:07:29 · 197 阅读 · 0 评论 -
167. Two Sum II - Input array is sorted
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two number原创 2017-03-05 12:17:13 · 327 阅读 · 0 评论 -
454. 4Sum II
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l]is zero.To make problem a bit easier, all A, B, C, D have same le原创 2017-02-27 07:22:51 · 283 阅读 · 0 评论 -
34. Search for a Range
Given an array of integers sorted in ascending order, 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原创 2017-02-27 07:50:13 · 373 阅读 · 0 评论 -
29. Divide Two Integers
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.1、让divisor一直乘22、如果divisor>dividend,则用dividend - 上一个divisor3、余下的数继续重复1,2步这里有两种情原创 2017-03-21 10:04:10 · 307 阅读 · 0 评论 -
367. Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else False.Note: Do not use any built-in library function such as sqrt.Example 1:Input: 16Return原创 2017-03-30 01:50:33 · 333 阅读 · 0 评论 -
222. Count Complete Tree Nodes
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,原创 2017-05-06 15:40:00 · 313 阅读 · 0 评论 -
230. Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ? k ? BST's total elements.Follow up:What if the原创 2017-05-07 04:04:33 · 247 阅读 · 0 评论 -
300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence.For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], ther原创 2017-05-07 07:11:33 · 267 阅读 · 0 评论 -
378. Kth Smallest Element in a Sorted Matrix
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.Note that it is the kth smallest element in the sorted order, not原创 2017-05-07 08:57:53 · 259 阅读 · 0 评论 -
275. H-Index II
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?如果citations是升序排列,则可以使用二分查找。这道题是让找有几篇论文的引用超过了几次。判断条件为:citations[mid] public原创 2017-05-07 10:01:12 · 314 阅读 · 0 评论 -
392. Is Subsequence
Given a string s and a string t, check if s is subsequence of t.You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) strin原创 2017-05-07 13:58:39 · 251 阅读 · 0 评论 -
436. Find Right Interval
Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on原创 2017-05-07 15:03:52 · 286 阅读 · 0 评论 -
4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).Example 1:nums1 =原创 2017-01-21 14:46:49 · 282 阅读 · 0 评论 -
240. Search a 2D Matrix II
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 in ascending from left to right.Integers in原创 2017-03-01 03:15:31 · 261 阅读 · 0 评论 -
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原创 2017-03-01 02:45:10 · 211 阅读 · 0 评论 -
350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].Note:Each element in the result should appear as ma原创 2017-01-05 07:42:40 · 229 阅读 · 0 评论 -
441. Arranging Coins
You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.Given n, find the total number of full staircase rows that can be formed.原创 2017-01-16 09:38:03 · 293 阅读 · 0 评论 -
374. Guess Number Higher or Lower
We are playing the Guess Game. The game is as follows:I pick a number from 1 to n. You have to guess which number I picked.Every time you guess wrong, I'll tell you whether the number is h原创 2017-01-17 08:24:15 · 395 阅读 · 0 评论 -
278. First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the原创 2017-01-17 08:50:20 · 206 阅读 · 0 评论 -
475. Heaters
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses.Now, you are given positions of houses and heaters on a horizontal原创 2017-01-17 14:27:36 · 314 阅读 · 0 评论 -
33. Search in Rotated Sorted Array
Suppose an array sorted in ascending order 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 foun原创 2017-02-17 08:30:22 · 250 阅读 · 0 评论 -
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?Suppose an array sorted in ascending order is rotated a原创 2017-02-17 08:48:20 · 306 阅读 · 0 评论 -
154. Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Suppose an array sorted in ascending order is rotat原创 2017-02-17 10:16:52 · 229 阅读 · 0 评论 -
270. Closest Binary Search Tree Value
Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.Note:Given target value is a floating point.You are guaranteed to have only o原创 2017-02-09 09:22:06 · 243 阅读 · 0 评论 -
153. Find Minimum in Rotated Sorted Array
Suppose an array sorted in ascending order 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原创 2017-01-18 04:46:26 · 190 阅读 · 0 评论 -
50. Pow(x, n)
Implement pow(x, n).实现pow(x, n). 用递归来做,首先返回条件是n==0时返回1,接下来可以把n全换到正数去,不要要注意Integer.MIN_VALUE 到Integer.MAX_VALUE 转换时会出现溢出,需要处理一下。之后就是判断n是不是odd,是的话返回x*pow(x*x, n/2),不是的话返回pow(x*x, n/2)。代码如下:public原创 2017-01-18 07:52:59 · 295 阅读 · 0 评论 -
69. Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.可以用binary search,适合用upperbound的模板,模板:Binary Search - 二分搜索。因为num除了0、1外,sqrt的结果都会x/mid。注意这里不要用mid*mid>x,因为mid*mid会溢出。代码如下:public原创 2017-01-18 12:54:21 · 301 阅读 · 0 评论 -
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, i原创 2017-01-19 09:40:50 · 256 阅读 · 0 评论 -
287. Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number,原创 2017-01-19 16:43:05 · 249 阅读 · 0 评论 -
540. Single Element in a Sorted Array
Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1:Input:原创 2017-08-09 13:06:54 · 289 阅读 · 0 评论