码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 217. Contains Duplicate 287. Find the Duplicate Number

时间:2018-09-16 19:42:55      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:com   segment   www.   code   turn   div   star   als   start   

 217. Contains Duplicate 后面3个题都是限制在1~n的

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        int length = nums.size();
        if(length <= 0)
            return false;
        sort(nums.begin(),nums.end());
        for(int i = 1;i < length;i++){
            if(nums[i] == nums[i-1])
                return true;
        }
        return false;
    }
};

287. Find the Duplicate Number

class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        int length = nums.size();
        if(length <= 0)
            return -1;
        int start = 1;
        int end = length - 1;
        while(start < end){
            int mid = (start + end)/2;
            int count = 0;
            for(int i = 0;i < length;i++){
                if(nums[i] <= mid)
                    count++;
            }
            if(count > mid)
                end = mid;
            else
                start = mid + 1;
        }
        return start;
    }
};

http://www.cnblogs.com/grandyang/p/4843654.html

https://blog.csdn.net/xudli/article/details/48802345

https://segmentfault.com/a/1190000003817671

leetcode 217. Contains Duplicate 287. Find the Duplicate Number

标签:com   segment   www.   code   turn   div   star   als   start   

原文地址:https://www.cnblogs.com/ymjyqsx/p/9656954.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!