面试题3_2:不修改数组找出重复的数字

面试题3(二):不修改数组找出重复的数字题目:在一个长度为n+1的数组里的所有数字都在1到n的范围内,所以数组中至少有一个数字是重复的。请找出数组中任意一个重复的数字,但不能修改输入的数组。例如,如果输入长度为8的数组{2, 3, 5, 4, 3, 2, 6, 7},那么对应的输出是重复的数字2或者3。

C++版本,测试用例很值得学习:

//==================================================================
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 作者:何海涛
//==================================================================

// 面试题3(二):不修改数组找出重复的数字
// 题目:在一个长度为n+1的数组里的所有数字都在1到n的范围内,所以数组中至
// 少有一个数字是重复的。请找出数组中任意一个重复的数字,但不能修改输入的
// 数组。例如,如果输入长度为8的数组{2, 3, 5, 4, 3, 2, 6, 7},那么对应的
// 输出是重复的数字2或者3。

#include<iostream>
using namespace std;

int countRange(const int* numbers, int length, int start, int end)
{
    if(numbers == nullptr)
        return 0;

    int count = 0;
    for(int i = 0; i < length; i++)
        if(numbers[i] >= start && numbers[i] <= end)
            ++count;
    return count;
}

int getDuplication(const int* numbers,int length){
    if(numbers == nullptr || length <= 0){
        return -1;
    }
    int start = 1;
    int end = length - 1;
    while(start <= end){
        int middle = (start + end) / 2;
        int count = countRange(numbers, length, start, middle);
        if(end == start){
            if(count > 1)
                return start;
            else
                break;
        }
        // 前半部分有重复
        if(count > (middle - start +1))
            end = middle;
        else
        // 后半部分有重复
            start = middle + 1;
    }
    return -1;
}

// ============== 测试代码 ==============
void test(const char* testName, int *numbers, int length, int *duplications, int dupLength){
    int result = getDuplication(numbers, length);
    for(int i = 0; i < dupLength; i++){
        if(result == duplications[i]){
            cout<<testName<<" passed."<<endl;
            return ;
        }
    }
    cout<<testName<<" failed."<<endl;
}

// 多个重复的数字
void test1(){
    int numbers[] = {1,2,2,3,3,4,5,6};
    int duplications[] = {2,3};
    test("test1", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}
// 一个重复的数字
void test2()
{
    int numbers[] = { 3, 2, 1, 4, 4, 5, 6, 7 };
    int duplications[] = { 4 };
    test("test2", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}

// 重复的数字是数组中最小的数字
void test3()
{
    int numbers[] = { 1, 2, 3, 4, 5, 6, 7, 1, 8 };
    int duplications[] = { 1 };
    test("test3", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}

// 重复的数字是数组中最大的数字
void test4()
{
    int numbers[] = { 1, 7, 3, 4, 5, 6, 8, 2, 8 };
    int duplications[] = { 8 };
    test("test4", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}

// 数组中只有两个数字
void test5()
{
    int numbers[] = { 1, 1 };
    int duplications[] = { 1 };
    test("test5", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}

// 重复的数字位于数组当中
void test6()
{
    int numbers[] = { 3, 2, 1, 3, 4, 5, 6, 7 };
    int duplications[] = { 3 };
    test("test6", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}

// 多个重复的数字
void test7()
{
    int numbers[] = { 1, 2, 2, 6, 4, 5, 6 };
    int duplications[] = { 2, 6 };
    test("test7", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}

// 一个数字重复三次
void test8()
{
    int numbers[] = { 1, 2, 2, 6, 4, 5, 2 };
    int duplications[] = { 2 };
    test("test8", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}

// 没有重复的数字
void test9()
{
    int numbers[] = { 1, 2, 6, 4, 5, 3 };
    int duplications[] = { -1 };
    test("test9", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}

// 无效的输入
void test10()
{
    int* numbers = nullptr;
    int duplications[] = { -1 };
    test("test10", numbers, 0, duplications, sizeof(duplications) / sizeof(int));
}

int main(){

    test1();
    test2();
    test3();
    test4();
    test5();
    test6();
    test7();
    test8();
    test9();
    test10();
    return 0;

}



Java版本:

package com.zr.test;

/**
 * 
 * @Description 
 * @author ZR
 * @date 2020年6月3日 下午3:52:18 
 * @version V0.1
 */
public class Num3_2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		test1();
	    test2();
	    test3();
	    test4();
	    test5();
	    test6();
	    test7();
	    test8();
	    test9();
	    test10();
	    
	}

	public static int countRange(int[] numbers, int length, int start, int end) {
		if (numbers == null)
			return 0;

		int count = 0;
		for (int i = 0; i < length; i++)
			if (numbers[i] >= start && numbers[i] <= end)
				++count;
		return count;
	}

	public static int getDuplication(int[] numbers, int length) {
		if (numbers == null || length <= 0) {
			return -1;
		}
		int start = 1;
		int end = length - 1;
		while (start <= end) {
			int middle = (start + end) / 2;
			int count = countRange(numbers, length, start, middle);
			if (end == start) {
				if (count > 1)
					return start;
				else
					break;
			}
			// 前半部分有重复
			if (count > (middle - start + 1))
				end = middle;
			else
				// 后半部分有重复
				start = middle + 1;
		}
		return -1;
	}

	// ============== 测试代码 ==============
	public static void test(String testName, int[] numbers, int length, int[] duplications, int dupLength) {
		int result = getDuplication(numbers, length);
		for (int i = 0; i < dupLength; i++) {
			if (result == duplications[i]) {
				System.out.println(testName + " passed.");
				return;
			}
		}
		System.out.println(testName + " failed.");
	}

	// 多个重复的数字
	public static void test1() {
		int numbers[] = { 1, 2, 2, 3, 3, 4, 5, 6 };
		int duplications[] = { 2, 3 };
		test("test1", numbers, numbers.length, duplications, duplications.length);
	}

	// 一个重复的数字
	public static void test2()
	{
	    int numbers[] = { 3, 2, 1, 4, 4, 5, 6, 7 };
	    int duplications[] = { 4 };
	    test("test2", numbers, numbers.length, duplications, duplications.length);
	}

	// 重复的数字是数组中最小的数字
	public static void test3()
	{
	    int numbers[] = { 1, 2, 3, 4, 5, 6, 7, 1, 8 };
	    int duplications[] = { 1 };
	    test("test3", numbers, numbers.length, duplications, duplications.length);
	}

	// 重复的数字是数组中最大的数字
	public static void test4()
	{
	    int numbers[] = { 1, 7, 3, 4, 5, 6, 8, 2, 8 };
	    int duplications[] = { 8 };
	    test("test4", numbers, numbers.length, duplications, duplications.length);
	}

	// 数组中只有两个数字
	public static void test5()
	{
	    int numbers[] = { 1, 1 };
	    int duplications[] = { 1 };
	    test("test5", numbers, numbers.length, duplications, duplications.length);
	}

	// 重复的数字位于数组当中
	public static void test6()
	{
	    int numbers[] = { 3, 2, 1, 3, 4, 5, 6, 7 };
	    int duplications[] = { 3 };
	    test("test6", numbers, numbers.length, duplications, duplications.length);
	}

	// 多个重复的数字
	public static void test7()
	{
	    int numbers[] = { 1, 2, 2, 6, 4, 5, 6 };
	    int duplications[] = { 2, 6 };
	    test("test7", numbers, numbers.length, duplications, duplications.length);
	}

	// 一个数字重复三次
	public static void test8()
	{
	    int numbers[] = { 1, 2, 2, 6, 4, 5, 2 };
	    int duplications[] = { 2 };
	    test("test8", numbers, numbers.length, duplications, duplications.length);
	}

	// 没有重复的数字
	public static void test9()
	{
	    int numbers[] = { 1, 2, 6, 4, 5, 3 };
	    int duplications[] = { -1 };
	    test("test9", numbers, numbers.length, duplications, duplications.length);
	}

	// 无效的输入
	public static void test10()
	{
	    int []numbers = null;
	    int duplications[] = { -1 };
	    test("test10", numbers, 0, duplications, duplications.length);
	}

}

原文地址:https://www.cnblogs.com/flyingrun/p/13037957.html