【数组】数组中重复的数字【剑指offer】

数组中重复的数字

http://www.nowcoder.com/questionTerminal/623a5ac0ea5b4e5f95552655361ae0a8

/*
思路1:先排序,依次遍历找出重复的数。O(nlogn) O(1)
思路2:哈希表,从头到尾扫描数组,如果哈希表中没有该数字,把它加入哈希表;如果存在,找到一个。O(n) O(n) 
思路3:交换法,从头到尾扫描数组,当扫描到下标为i的数字时,首先比较这个数字是不是等于i,
        如果是接着扫描下一个数字;如果不是,就拿它和第m个数字进行比较,如果它和第m个数字相等,就找到了一个重复的数字(该数字在下标为i和下表为m的位置都出现过)。
        O(n) O(1)
*/

#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <map>
using namespace std;


class Solution1{
public:
    // Parameters:
    //        numbers:     an array of integers
    //        duplication: (Output) the duplicated number in the array number
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    bool duplicate(int numbers[], int length, int* duplication) 
    {
        //合法性检验
        if(numbers == NULL || length <=0)
            return false;
        for(int i=0;i<length;i++)
        {
            if(numbers[i]<0 || numbers[i]>length-1)
                return false;
        }
        //借助vector排序
        vector<int> v;
        for(int i=0;i<length;i++)
            v.push_back(numbers[i]);
        sort(v.begin(), v.end());
        //copy(v.begin(), v.end(), ostream_iterator<int>(cout," "));  //打印排序后的结果
        // cout<<endl;

        int flag = v[0];
        for(int i=1;i<length;i++)
        {   
            if(flag == v[i])
            {
                // duplication = new int(flag);
                // cout<<*duplication<<endl;

                *duplication = flag;
                return true;
            }
            else
                flag = v[i];
        }
        return false;
    }

};


class Solution2{
public:
    // Parameters:
    //        numbers:     an array of integers
    //        duplication: (Output) the duplicated number in the array number
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    bool duplicate(int numbers[], int length, int* duplication) 
    {
        //合法性检验
        if(numbers == NULL || length <=0)
            return false;
        for(int i=0;i<length;i++)
        {
            if(numbers[i]<0 || numbers[i]>length-1)
                return false;
        }

        map<int, int> hash; 
        for(int i=0;i<length;i++)
        {
            hash[numbers[i]]++;
            //当m数字存在在哈希表中,则重复
            if(hash[numbers[i]] >1 )
            {
                *duplication = numbers[i];
                return true;
            }
        }
        return false;
    }
};


class Solution3{
public:
    // Parameters:
    //        numbers:     an array of integers
    //        length:      the length of array numbers
    //        duplication: (Output) the duplicated number in the array number
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    bool duplicate(int numbers[], int length, int* duplication) 
    {
        //合法性检验
        if(numbers == NULL || length <=0)
            return false;
        for(int i=0;i<length;i++)
        {
            if(numbers[i]<0 || numbers[i]>length-1)
                return false;
        }

        //遍历数组
        for(int i=0;i<length;i++)
        {
            //2, 3, 1, 0, 2, 5, 3
            //1, 2, 3, 4, 5, 6, 7
            //如果i对应位置的数字与i不相等
            while(numbers[i] != i)
            {
                int m = numbers[i];
                if(numbers[i] == numbers[m])
                {
                    *duplication = m;
                    return true;
                }

                //交换两个数字
                int temp = numbers[i];
                numbers[i] = numbers[temp];
                numbers[temp] = temp;
            }
        }
        return false;
    }
};


int main()
{

    int num[] = {0,2,5,1,3,2,1};
    int a[] = {-1};

    //排序法
    // Solution1 solu1;
    // bool x = solu1.duplicate(num, 7, a);
    // cout<<"isDupLication: "<<x<<endl;
    // if(x == true)
    //     cout<<"first num: "<<*a<<endl;


    //哈希表
    // Solution1 solu2;
    // bool x = solu2.duplicate(num, 7, a);
    // cout<<"isDupLication: "<<x<<endl;
    // if(x == true)
    //     cout<<"first num: "<<*a<<endl;


    //索引
    Solution3 solu3;
    bool x = solu3.duplicate(num, 7, a);
    cout<<"isDupLication: "<<x<<endl;
    if(x == true)
        cout<<"first num: "<<*a<<endl;



    return 0;
}
全部评论

相关推荐

点赞 评论 收藏
分享
09-16 14:43
已编辑
江娱互动_研发_客户端开发
背景&nbsp;双一流本硕&nbsp;双非大圆满&nbsp;只找游戏开发相关的岗位。&nbsp;8&nbsp;月初开始秋招到现在&nbsp;投了四五十家吧,&nbsp;目前两&nbsp;offer,&nbsp;不打算继续投了,把剩下的流程走完就开始沉淀了。目前两&nbsp;offer&nbsp;一个是网易互娱测开&nbsp;base&nbsp;广州,一个是江娱互动客户端开发&nbsp;base&nbsp;北京。应该确定网易这个了,说实话北京这个我挺想去的,这家的产品和工作氛围我了解了也不错,是那种踏实做事的,可惜我是广东人。网易的测开是调剂的二志愿,看了下有内部转岗机会,所以打算后面找个时间提前实习,沉淀下再做一个&nbsp;demo&nbsp;作品,写一些&nbsp;shader,增强下图形学渲染的能力,再学点编辑器开发。看到时候内部转岗或者春招继续投客户端开发这样。后面还能再动摇的话应该就灵犀或者腾子了吧(假如这两家确认的是客户端开发岗的话)。-----------------------补下timeline网易互娱&nbsp;测开&nbsp;8.2笔试&nbsp;&nbsp;8.21&nbsp;技术面&nbsp;&nbsp;8.29&nbsp;leader&amp;HRBP面(终面)&nbsp;9.8&nbsp;录用审核(之前一直显示面试中)9.14&nbsp;oc江娱互动&nbsp;客户端开发&nbsp;8.29主程面&nbsp;9.3&nbsp;制作人面&nbsp;9.5&nbsp;BOSS面&nbsp;9.11&nbsp;口头OC&nbsp;9.15&nbsp;正式offer后面考虑了一下&nbsp;&nbsp;感觉还是能走开发就开发吧,测开不太感兴趣,要内部活水转岗还要满1年才能申请。。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务