剑指Offer1

数组中重复的数字

https://www.nowcoder.com/practice/623a5ac0ea5b4e5f95552655361ae0a8?tpId=13&tqId=11203&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github

题目

题解

注意这里必须要求判断遇到的第一个重复的数字;
使用哈希来解

class Solution {
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) {
        vector<bool> f(length, false);
        for(int i = 0; i < length; i++)
        {
            if(!f[numbers[i]])
            {
                f[numbers[i]] = true;
            }else{
                duplication[0] = numbers[i];
                return true;                     
            }            
        }
        return false;
    }
};
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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