二分搜索
i-Blue
抱平常心走平常路
展开
-
349. Intersection of Two Arrays
Tags- binary search, hash table, two pointers, sort方法一:sort+binary search先对nums1、nums2从小到大排序,然后,对于nums2中不同的元素,我们在nums1中二分搜索,看看是否存在。这里注意,nums2中的元素是排过序的,所以没有必要每个元素都在(0, nums1.size()-1)中二分搜索。比如,num原创 2016-08-19 15:33:29 · 284 阅读 · 0 评论 -
Russian Doll Envelopes-最长递增子序列
方法:DP+ 二分搜索具体一点说,二分搜索是用在LIS(最长递增子序列)中(LIS见:点击打开链接)。主要思想就是LIS的思想。有O(N*N)和O(NlgN)两种解法。不论是哪种解法,首先都得按照w(宽)值对envelopes进行递增排序。简单直观的O(N*N)的方法。class Solution {public: int maxEnvelopes(vector>& env原创 2016-08-17 01:12:34 · 326 阅读 · 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 fil原创 2016-09-06 16:29:20 · 230 阅读 · 0 评论 -
34. 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 n原创 2016-09-07 20:02:16 · 327 阅读 · 0 评论 -
378. Kth Smallest Element in a Sorted Matrix-binary search/heap
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-02-18 11:56:36 · 315 阅读 · 0 评论 -
最长递增子序列
问题给定一个长度为N的数组,找出一个最长的单调自增子序列(不一定连续,但是顺序不能乱)。例如:给定一个长度为6的数组A{5, 6, 7, 1, 2, 8},则其最长的单调递增子序列为{5,6,7,8},长度为4.解法1:最长公共子序列法这个问题可以转换为最长公共子序列问题。如例子中的数组A{5,6, 7, 1, 2, 8},则我们排序该数组得到数组A‘{1, 2, 5, 6转载 2017-03-31 23:28:00 · 397 阅读 · 0 评论 -
287. Find the Duplicate Number--binary search/快慢指针
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, fi原创 2017-02-18 20:55:27 · 539 阅读 · 0 评论