c++

数组中重复的数字

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

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) {
        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) {
            while(numbers[i] != i) {
                if (numbers[numbers[i]] == numbers[i]) {
                    *duplication = numbers[i];
                    return true;
                }
                swap(numbers[i], numbers[numbers[i]]);
            }
        }
        return false;
    }
};
全部评论

相关推荐

2025-12-20 13:19
已编辑
曲阜师范大学 Java
点赞 评论 收藏
分享
2025-12-15 14:25
云南大学 Java
lei22:入职可能会看学信网,最好别伪装,这个简历找实习肯定是够的,肯定会有收 28 届实习生的公司的,多投就行
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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