分治法
shiyang6017
这个作者很懒,什么都没留下…
展开
-
Find the Duplicate Number
int findDuplicate(int* nums, int numsSize) { int low=1; int high=numsSize-1; int mid; // answer_value 在[low,high]上 while(true){ mid=(low+high)/2; int smaller=0;原创 2015-11-16 19:01:23 · 217 阅读 · 0 评论 -
Median of Two Sorted Arrays
主要思想: 在两个数组中查找第k个元素时(k从1开始),若nums1[k/2-1]>nums2[k/2-1],这说明在数组nums2中,前k/2个元素(数组从nums2[0]开始)中肯定不包含第k个元素,将这前k/2个元素剔除,在num1和剩余的nums2元素中查找第k-k/2个元素.double find_the_Kth_element(int* nums1, i原创 2015-11-25 10:15:12 · 313 阅读 · 0 评论 -
[leetcode] 368. Largest Divisible Subset
class Solution {public: bool isPerfectSquare(int num) { long long low = 0, high = num; while ( low long long mid= (low + high) / 2; if原创 2016-07-30 13:27:33 · 223 阅读 · 0 评论 -
leetcode 69 sqrt(x)
class Solution {public: int mySqrt(int x){ long long low = 0, high = x;// 时刻警醒溢出!! while(low <= high){ long long mid =(low + high)/2; long long cmp = pow(m原创 2016-08-02 20:15:58 · 176 阅读 · 0 评论 -
leetcode 209. Minimum Size Subarray Sum
方法一:class Solution { // O(nlgn), binary searchpublic: int minSubArrayLen(int s, vector& nums) { vector sum(nums); for (int i = 1; i < nums.size(); ++i)原创 2016-09-09 16:09:14 · 213 阅读 · 0 评论 -
leetcode H-Index (I、II)
class Solution {// I1:空间换时间public: int hIndex(vector& citations) { vector count(citations.size() + 1, 0); for (int i = 0; i < citations.size(); ++i){ if ( citatio原创 2016-08-17 15:56:26 · 182 阅读 · 0 评论 -
leetcode Partition List
class Solution {public: ListNode* partition(ListNode* head, int x) { ListNode* smallHead = new ListNode(0);// 增加头结点,简化代码 ListNode* notSmallHead = new ListNode(0); ListNode原创 2016-08-05 23:21:08 · 166 阅读 · 0 评论 -
leetcode 354. Russian Doll Envelopes
思路:1. 按宽度对envelops进行升序排列,对相同宽度的envelops按长度降序排列2. 对重新排好序的envelops,求最长严格升序的envelops长度序列代码:/* * */class Solution {public: int maxEnvelopes(vector>& envelopes) { if (envelopes原创 2016-08-01 20:42:42 · 222 阅读 · 0 评论 -
leetcode 410 Split Array Largest Sum
二分查找:个人总结的二分查找有两种: 1、二分对象为索引。例如,有序表查找某个元素的存在性2、二分对象为值(所求值)。例如,本题。先确定ret的范围[low,high],然后,不断验证ret,从而缩小范围class Solution {public: int splitArray(vector& nums, int m) { long high = 0, lo原创 2016-10-06 22:47:10 · 1034 阅读 · 0 评论