劍指offer第一天之數組中重複的數字

劍指offer第一天
題目描述:在一個長度爲n的數組裏的所有數字都在0到n-1的範圍內。 數組中某些數字是重複的,但不知道有幾個數字是重複的。也不知道每個數字重複幾次。請找出數組中任意一個重複的數字。 例如,如果輸入長度爲7的數組{2,3,1,0,2,5,3},那麼對應的輸出是第一個重複的數字2。

public class Solution {
    // Parameters:
    //    numbers:     an array of integers
    //    length:      the length of array numbers
    //    duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
    //                  Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
    //    這裏要特別注意~返回任意重複的一個,賦值duplication[0]
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    /*
data:2020515
author:魏振東
fun:在一個長度爲n的數組裏的所有數字都在0到n-1的範圍內。 數組中某些數字是重複的,
 * 但不知道有幾個數字是重複的。也不知道每個數字重複幾次。請找出數組中任意一個重複的數字。
 * 例如,如果輸入長度爲7的數組{2,3,1,0,2,5,3},那麼對應的輸出是第一個重複的數字2。
* */
    public boolean 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)
        {
//            如果當第i個數字不等於numbers[i]
            while(numbers[i]!=i)
            {
//                第i個位置的值爲numbers[i] = x ,第x的位置的值是否等於x,如果等於說明重複,不等則交換
                if(numbers[i]==numbers[numbers[i]])
                {
                    duplication[0] = numbers[i];
                    return true;
                }
                int temp = numbers[i];
                numbers[i]=numbers[temp];
                numbers[temp]=temp;
            }
        }
        return false;
    }
}
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[i]==numbers[numbers[i]])
                {
                    *duplication = numbers[i];
                    return true;
                }
                int temp =  numbers[i];
                numbers[i] = numbers[temp];
                numbers[temp] =temp;
            }
        }
        return false;
    }
};
# -*- coding:utf-8 -*-
class Solution:
    # 這裏要特別注意~找到任意重複的一個值並賦值到duplication[0]
    # 函數返回True/False
    def duplicate(self, numbers, duplication):
        # write code here
        if numbers==[]:
            return False
        for num in numbers:
            if num <0 or num>len(numbers)-1:
                return False
        for i in range(len(numbers)):
            while numbers[i]!=i:
                if numbers[i]==numbers[numbers[i]]:
                    duplication[0] = numbers[i]
                    return True
                temp = numbers[i]
                numbers[i] = numbers[temp]
                numbers[temp] = temp
        return False
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章