首页 > 其他 > 详细

leetcode 217. Contains Duplicate 287. Find the Duplicate Number

时间:2018-09-16 18:57:25      阅读:181      评论:0      收藏:0      [点我收藏+]

 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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!