Java编写各种数据结构时常用操作与复杂度分析以及热门算法的基本实现

数据结构分类

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0sNA1BsL-1648709929606)(C:\Users\lcy\AppData\Roaming\Typora\typora-user-images\image-20220331145752756.png)]

数据结构

对数据结构的操作一般包含:

  • 创建和初始化(可选)
  • 添加元素
  • 按索引访问元素
  • 在指定索引插入元素
  • 按索引更新/修改元素的值
  • 按索引移除/删除元素
  • 获取长度
  • 遍历
  • 查找指定索引的元素
  • 查找指定值的索引
  • 排序

不同数据结构(在不同编程语言)进行以上操作时有不同的时间复杂度,我们需要根据自己项目对以上操作不同的需求选取适合的数据结构。

在这里插入图片描述

数组

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

class ArrayTest {
	public static void main(String[] args) {
        // Four solutions to create an array in Java
        // Take [1,2,3] as example
        //Solution 1
        int[] a = {1,2,3};
        System.out.println("a: "+Arrays.toString(a));
        // Solution 2
        int[] b = nre int[]{1,2,3};
        // Solution 3
        int[] c = new int[3];
        for(int i = 0;i < 3; i++) {
            arr.add(i+1);
        }
        // Solution 4
        ArrayList<Integer> arr = new ArrayList<>();
        for(int i = 0; i < 3; i++) {
            arr.add(i+1);
        }
        System.out.println("arr: "+arr.toString());
        
        // Add element
        // Trim Complexity: O(1)
        arr.add(99);
        arr.add(3, 88);
        
        // Access element
        // Time Complexity: O(1)
        int c1 = c[1];
        int arr1 = arr.get(1);
        
        // Remove element
        // Time complexity: O(N)
        arr.remove(3);
        
        // The length of an array
        // Time Complexity: O(1)
        int cSize = c.lenghth;
        int arrSize = arr.size();
        
        // Iterate an array
        // Iterate c
        for (int i = 0; i < c.length; i++) {
            int current = c[i];
        }
        // Iterate arr
        for (int i = 0; i < arr.size(); i++) {
            int current = arr.get(i);
        }
        
        // Find an element
        // Find an element in c
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 99) {
                System.out.println("We found 99 in c.");
            }
        }
        //Fine an element in arr
        boolean is99 = arr.contains(99);
        if (is99) {
            System.out.println("We found 99 in arr.");
        }
        
        // Sort an array by built-in lib
        c = new int[]{2,3,1};
        arr = new ArrayList<>();
        arr.add(2,3,1);
        // From small to big
        // Time complexity: O(NlogN)
        Arrays.sort(c);
        Collections.sort(arr);
        // From big to small
        // Time comlexity: O(NlogN)
        // For c, you can read an array in reverse
        Arrays.sort(c, Collections.reverseOrder());
        // For arr
        Collections.sort(arr, Collections.reverseOrder());
    }
}

链表

import java.util.LinkedList;

class LinkedListTest {
    public static void main(String[] args) {
        // Create a LinkedList
        LinkedList<Integer> list = new LinkedList<>();
        
        // Add a element O(1)
        list.add(1);
        
        // Insert add(ind, value) O(N)
        list.add(2,99);
        
        // Access element O(N)
        int element = list.get(2);
        
        // Search element O(N)
        int index = list.indexOf(99);
        
        // Update element O(N)
        list.set(2, 88);
        
        // Remove element O(N)
        list.remove(2);
        
        // Length O(1)
        int length = list.size();
    }
}
    

队列

import java.util.LinkedList;
import java.util.Queue;

class QueueTest {
    public static void main(String[] args) {
        // Create a queue
        Queue<Integer> queue = new LinkedList<>();
        
        // Add element O(1)
        queue.add(1);
        queue.add(2);
        queue.add(3);
        
        // Get the head of queue O(1)
        int temp = queue.peek();
        
        // Remove the head of queue O(1)
        int temp = queue.poll();
        
        // Queue is empty? O(1)
        queue.isEmpty();
        
        // The length of Queue O(1)
        queue.size();
        
        // Interate
        while (!queue.isEmpty()) {
            int temp = queue.poll();
        }       
    }
}

import java.util.Stack;

class StackTest {
    public static void main(String[] args) {
        // Create a stack
        Stack<Integer> stack = new Stack<>();
        
        // Add element O(1)
        stack.push(1);
        stack.push(2);
        stack.push(3);
        
        // Get the top of stack O(1)
        stack.peek();
        
        // Remote the top of stack O(1)
        int temp = stack.pop();
        
        // Stack length O(1) 
        stack.size();
        
        // Stack is empty?
        stack.isEmpty();
        
        // Iterate a stack O(N)
		while (!stack.isEmpty()) {
            int num = stack.pop();
        }
        
    }
}

哈希表

import java.t.util.HashMap;

public static void main(String[] args) {
    // Create HashTable by Array
    String[] hashTable = new String[4];
    // Create HashTable by HashMap lib
    HashMap<Integer, String> map = new HashMap<>();
    
    // add element O(1)
    hashTable[1] = "hanmeimei";
    hashTable[2] = "lihua";
    hashTable[3] = "siyangyuan";
    map.put(1, "hameimei");
    map.put(2, "lihua");
    map.put(3, "siyangyuan");
    
    // update element O(1)
    hashTable[1] = "bish";
    map.put(1, "bishi");
    
    // remote element O(1)
    hashTable[1] = "";
    map.remove(1);
   
    // get value O(1)
    String temp = hashTablep[3];
    map.get(3);
    
    // check key
    // hashTable check the length
    map.size();
    
    // empty?
    // hashTable size variable
    map.isEmpty();
}

import java.util.Collections;
import java.util.PriortyQueue;

public class HeapTest{
    public static void main(String[] args) {
        PriorityQueue<Integer> minheap = new PriorityQueue<>();
        PriorityQueue<Integer> maxheap = new PriorityQueue<>();
        
        minheap.add(10);
        maxheap.add(10);
        
        minheap.peek();
        maxheap.peek();
        
        minheap.poll();
        maxheap.poll();
        
        minheap.size();
        maxheap.size();
        
        while (!minheap.isEmpty()) {
            System.out.println(minheap.poll());
        }
    }
}

集合

import java.util.HashSet;

public class HashSetTest {
    public static void main(String[] args) {
        HashSet<Integer> set = new HashSet<>();
        
        // O(1)
        set.add(10);
        
        set.contains(2);
        
        set.remove(2);
        
        set.size();
    }
}

算法

posted @ 2022-04-07 20:30  wowLovia  阅读(112)  评论(0)    收藏  举报