【數據結構】數組及常見的面試題

前言

  首先理解數據結構的概念,數據結構是計算機存儲、組織數據的方式。相互之間存在一種或多種特定關係的數據元素的集合。選擇一種合適的數據結構可以實現更高的運行或存儲效率。
   數據結構往往同高效的檢索算法和索引技術有關。數據結構主要研究數據的邏輯結構,劃分爲:線性結構和非線性結構。常用的數據結構:數組Array、棧Stack、隊列Queue、鏈表Linked List、樹Tree、圖Graph、堆Heap和散列表Hash等。
   下面先介紹數組的基本內容和常見的面試題。

數組概念

  1.有序的元素序列,儲存多個相同類型數據的集合。
  2.組成數組的各個變量成爲數組的分量,即數組的元素或下標變量。
  3.把相同類型的若干元素按無序的形式組織起來。

數組面試題

一、在未排序的整數數組中找到最大值與最小值

   使用一層循環,假設第一個數最大或最小
 public static void main(String[] args) {
        int[] setArray={5,2,1,7,10,56};
        int max=setArray[0];
        int min=setArray[0];
        for (int i = 0; i <setArray.length ; i++) {
            if (max<setArray[i]) {
                max=setArray[i];
            }
            if (min>setArray[i]) {
                min=setArray[i];
            }
        }
        System.out.println(max+"最大,"+min+"最小");
    }

二、請在給出的整數數組中找到重複的數字

  1.第一種方法:兩層循環
/**
     * 方法一: 兩層循環
     */
    public static void test1() {
        int[] setArray = {5, 6, 8, 8, 5, 10, 6, 10, 9};
        System.out.println("重複的數字如下:");
        for (int i = 0; i < setArray.length; i++) {
            for (int j = i + 1; j < setArray.length; j++) {
                if (setArray[i] == setArray[j]) {
                    System.out.print(setArray[j] + " ");
                }
            }
        }
    }

  2 第二種方法 .使用HashMap

 /**
     * 方法二: HashMap key 存放數字,value 存放出現的次數
     */
    public static void test2() {
        int[] setArray = {5, 6, 8, 8, 5, 10, 6, 10, 9, 5, 5};
        HashMap<Integer, Integer> map = new HashMap<>();
        System.out.println("重複的數字如下:");
        for (Integer integer : setArray) {
            if (map.containsKey(integer)) {
                map.put(integer, map.get(integer) + 1);
            } else {
                map.put(integer, 1);
            }
        }
        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            if ((int) (entry.getValue()) > 1) {
                System.out.print(entry.getKey() + " ");
            }
        }
        System.out.println(map.toString());

    }

三、找到1到100中丟失的數字

  1.第一個方法:兩個數組實現,利用桶原理
/**
     * 1 生成一個指定大小的數組,數字重複
     *
     * @param size     數組實際大小
     * @param capacity 數組容量
     */
    private static void test1(int size, int capacity) {

        int[] arrayMiss = new int[size];
        int[] arrayAll = new int[capacity];

        if (size > capacity) {
            System.out.println("size 不能大於數組最大容量capacity");
            return;
        }

        /**
         * 生成兩個數組
         */
        System.out.println("生成隨機數組");
        int randNum;
        Random random=new Random();
        for (int i = 0; i <= capacity; i++) {
            randNum = random.nextInt(capacity);
            if (i < size) {
                arrayMiss[i] = randNum;
                System.out.print(randNum + " ");
            }
        }

        /**
         * 將缺失數組的對應數字放到對應的既定數組中
         */
        for (int i = 0; i < arrayMiss.length; i++) {
            arrayAll[arrayMiss[i]] = arrayMiss[i];
        }
        System.out.println();
        System.out.println("缺失的數字:");
        for (int i = 1; i < arrayAll.length; i++) {
            if (arrayAll[i] == 0) {
                System.out.print(i+" ");
            }
        }


    }

  運行結果
在這裏插入圖片描述

  2.第二個方法:使用兩個Set集合,利用Set無序不重複的特點

public static void test2() {

        Set<Integer> setMiss = new HashSet<>();
        Set<Integer> setAll = new HashSet<>();
        Boolean isContain = false;
        int randNum = 0;
        Random rand = new Random();
        System.out.println("隨機數組");
        for (int i = 0; i < 100; i++) {
            randNum = rand.nextInt(100);
            if (randNum != 0) {
                setMiss.add(randNum);
            }
            setAll.add(i + 1);
            System.out.print(randNum + " ");
        }
        System.out.println();
        System.out.println("丟失的數字");
        for (int num : setAll) {
            isContain = setMiss.contains(num);
            if (!isContain) {
                System.out.print(num + " ");
            }
        }
    }

小結

  解決這幾個問題的過程中,假設最大最小值的思想,藉助第二個數組的思想,都在數組試題中有所體現。
感謝您的訪問!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章