常见算法复习

快速排序

时间复杂度:最好情况O(nlong(n)),最坏情况O(n^2)
思路:选择一个基准,将比他小的挪到他的前边,比他大的挪到他的后边

public class Main {
    private static void quickSort(int[] arr, int l, int r) {
        if (l >= r) {
            return;
        }
        int i = l - 1;
        int j = r + 1;
        int x = arr[l+r>>1];
        while(i<j){
            do i++;while(arr[i]<x);
            do j--;while(arr[j]>x);
            if(i<j){
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
        quickSort(arr,l,j);
        quickSort(arr,j+1,r);
    }

    public static void main(String[] args) {
        int[] arr = {3, 7, 2, 9, 1, 5, 8, 6};
        quickSort(arr,0,arr.length-1);
        for(int num : arr){
            System.out.println(num);
        }
    }
}

归并排序

时间复杂度:最佳情况(O(n log n)),平均情况(O(n log n)),最差情况(O(n log n))
采用分治的思想,先局部有序再整体有序

public class Main {
    private static void mergeSort(int[] arr, int l, int r) {
        if (l >= r) {
            return;
        }
        int[] res = new int[arr.length];
        int mid = l + r >> 1;
        mergeSort(arr, l, mid);
        mergeSort(arr, mid + 1, r);
        int num = 0;
        int i = l;
        int j = mid + 1;
        while (i <= mid && j <= r) {
            if (arr[i] < arr[j]) res[num++] = arr[i++];
            else res[num++] = arr[j++];
        }
        while (i <= mid) res[num++] = arr[i++];
        while (j <= r) res[num++] = arr[j++];
        for (i = l, j = 0; i <= r; i++, j++) {
            arr[i] = res[j];
        }
    }

    public static void main(String[] args) {
        int[] arr = {3, 1, 4, 2, 5, 6, 0};
        mergeSort(arr, 0, arr.length - 1);
        for (int num : arr) {
            System.out.println(num);
        }
    }
}

单例模式

public class Main {
    public static void main(String[] args) {
        // 创建多个线程来获取单例实例
        Runnable task = () -> {
            Singleton singleton = Singleton.getInstance();
            singleton.showMessage();
        };
        // 创建10个线程并启动它们
        for (int i = 0; i < 10; i++) {
            new Thread(task).start();
        }
    }
}
class Singleton {
    // volatile 关键字确保 instance 的可见性和禁止指令重排序
    private static volatile Singleton instance = null;

    // 私有构造函数,防止实例化
    private Singleton() {
        // 可以在这里初始化一些资源
    }
    // 获取单例实例的方法,使用双重检查锁定
    public static Singleton getInstance() {
        if (instance == null) { // 第一次检查:如果实例已被创建,就直接返回实例,避免了不必要的加锁操作。
            synchronized (Singleton.class) { // 加锁只有在第一次检查确定 instance 为 null 时,才进入同步块。
                if (instance == null) { // 第二次检查在同步块内再次检查 instance 是否为 null,确保只有一个线程能够创建实例。
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
    // 一个简单的方法,用于测试单例类的功能
    public void showMessage() {
        System.out.println("Hello, I am a singleton!");
    }
}
public enum Singleton {
    // 定义枚举实例,这个就是单例对象
    INSTANCE;

    // 单例类的成员方法
    public void showMessage() {
        System.out.println("Hello, I am a singleton using Enum!");
    }
}

public class Main {
    public static void main(String[] args) {
        // 获取单例实例并调用方法
        Singleton singleton = Singleton.INSTANCE;
        singleton.showMessage();
        // 验证多个线程访问是否是同一个实例
        Runnable task = () -> {
            Singleton s = Singleton.INSTANCE;
            System.out.println("Instance: " + s.hashCode());
        };
        // 启动多个线程进行验证
        for (int i = 0; i < 10; i++) {
            new Thread(task).start();
        }
    }
}

多线程交替打印奇偶数

public class Main {
    private static final int max = 10;
    private static final Object lock = new Object();
    private static boolean isOddTurn = true; // 标识当前是否应该打印奇数

    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            for (int i = 1; i <= max; i += 2) {
                synchronized (lock) {
                    while (!isOddTurn) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        }
                    }
                    System.out.println("奇数:" + i);
                    isOddTurn = false;
                    lock.notify(); // 唤醒偶数线程
                }
            }
        });

        Thread thread2 = new Thread(() -> {
            for (int i = 2; i <= max; i += 2) {
                synchronized (lock) {
                    while (isOddTurn) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        }
                    }
                    System.out.println("偶数:" + i);
                    isOddTurn = true;
                    lock.notify(); // 唤醒偶数线程
                }
            }
        });

        thread1.start();
        thread2.start();
    }
}

信号量机制的实现方法

public class Main {
    private static final int MAX = 10; // 打印的最大数字
    private static final Object lock = new Object();
    private static boolean isOddTurn = true; // 标识当前是否应该打印奇数
    public static void main(String[] args) {
        // 奇数线程
        Thread oddThread = new Thread(() -> {
            for (int i = 1; i <= MAX; i += 2) {
                synchronized (lock) {
                    while (!isOddTurn) { // 如果不是奇数线程的执行时机,则等待
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        }
                    }
                    System.out.println("奇数: " + i);
                    isOddTurn = false; // 该偶数线程执行了
                    lock.notify(); // 唤醒偶数线程
                }
            }
        });
        // 偶数线程
        Thread evenThread = new Thread(() -> {
            for (int i = 2; i <= MAX; i += 2) {
                synchronized (lock) {
                    while (isOddTurn) { // 如果不是偶数线程的执行时机,则等待
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        }
                    }
                    System.out.println("偶数: " + i);
                    isOddTurn = true; // 该奇数线程执行了
                    lock.notify(); // 唤醒奇数线程
                }
            }
        });
        // 启动两个线程
        oddThread.start();
        evenThread.start();
    }
}

三个线程交替打印

public class ThreeThreadPrinter {
    private static final int MAX = 9; // 每个线程打印的最大数字
    private static final Object lock = new Object();
    private static int current = 1; // 当前应该由哪个线程打印,1表示线程1,2表示线程2,3表示线程3

    public static void main(String[] args) {
        // 线程1,打印1, 4, 7
        Thread t1 = new Thread(() -> {
            for (int i = 1; i <= MAX; i += 3) {
                synchronized (lock) { // 获取锁
                    while (current != 1) { // 如果不是线程1的执行时机,则等待
                        try {
                            lock.wait(); // 进入等待状态
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        }
                    }
                    System.out.println("Thread 1: " + i); // 打印数字
                    current = 2; // 设置为线程2的执行时机
                    lock.notifyAll(); // 唤醒其他线程
                }
            }
        });

        // 线程2,打印2, 5, 8
        Thread t2 = new Thread(() -> {
            for (int i = 2; i <= MAX; i += 3) {
                synchronized (lock) { // 获取锁
                    while (current != 2) { // 如果不是线程2的执行时机,则等待
                        try {
                            lock.wait(); // 进入等待状态
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        }
                    }
                    System.out.println("Thread 2: " + i); // 打印数字
                    current = 3; // 设置为线程3的执行时机
                    lock.notifyAll(); // 唤醒其他线程
                }
            }
        });

        // 线程3,打印3, 6, 9
        Thread t3 = new Thread(() -> {
            for (int i = 3; i <= MAX; i += 3) {
                synchronized (lock) { // 获取锁
                    while (current != 3) { // 如果不是线程3的执行时机,则等待
                        try {
                            lock.wait(); // 进入等待状态
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        }
                    }
                    System.out.println("Thread 3: " + i); // 打印数字
                    current = 1; // 设置为线程1的执行时机
                    lock.notifyAll(); // 唤醒其他线程
                }
            }
        });

        // 启动三个线程
        t1.start();
        t2.start();
        t3.start();
    }
}

0-1背包

public class Main {

    // 0-1背包问题的解决方法
    public static int bagValue(int[] weights, int[] values, int capacity) {
        int n = weights.length;
        // 创建一个二维数组dp,其中dp[i][j]表示前i件物品在总容量为j的条件下的最大价值
        int[][] dp = new int[n + 1][capacity + 1];
        // 遍历每一个物品
        for (int i = 1; i <= n; i++) {
            // 遍历每一个可能的容量值
            for (int j = 1; j <= capacity; j++) {
                // 如果当前物品的重量大于当前的背包容量,那么当前物品不能放入背包
                if (weights[i - 1] > j) {
                    dp[i][j] = dp[i - 1][j];
                } else {
                    // 当前物品可以放入背包,我们选择放入或不放入的最大值
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weights[i - 1]] + values[i - 1]);
                }
            }
        }
        // 返回最大值,即dp[n][capacity]
        return dp[n][capacity];
    }

    public static void main(String[] args) {
        int[] weights = {2, 3, 4, 5}; // 每个物品的重量
        int[] values = {3, 4, 5, 6};  // 每个物品的价值
        int capacity = 5;  // 背包的容量
        int maxValue = bagValue(weights, values, capacity);
        System.out.println("背包可以获得的最大价值是: " + maxValue);
    }
}

LRU缓存

public class LRUCache {
    //函数 get 和 put 必须以 O(1) 的平均时间复杂度运行所以必须用双向链表
    private class Node {
        int key, val;
        Node pre, next;

        Node(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }

    //初始化容量
    private final int capacity;
    Node dummy = new Node(0, 0);
    Map<Integer, Node> keyNode = new HashMap<>();

    public LRUCache(int capacity) {
        this.capacity = capacity;
        dummy.pre = dummy;
        dummy.next = dummy;
    }

    public int get(int key) {
        Node node = getNode(key);
        return node == null ? -1 : node.val;
    }

    public void put(int key, int value) {
        //有就更新,没有就插入
        Node node = getNode(key);
        if (node != null) {
            node.val = value;
            return;
        }
        node = new Node(key, value);//新增
        keyNode.put(key, node);
        pushFront(node);//放在最前面
        if (keyNode.size() > capacity) {
            //容量过大就删除,此处为双向循环链表,dummy.pre可以找到尾节点的数
            Node pre = dummy.pre;
            keyNode.remove(pre.key);
            remove(pre);
        }

    }

    private Node getNode(int key) {
        if (!keyNode.containsKey(key)) {
            return null;
        }
        Node node = keyNode.get(key);
        remove(node);//先删除
        pushFront(node);//再更新
        return node;
    }

    private void pushFront(Node node) {
        node.pre = dummy;
        node.next = dummy.next;
        node.pre.next = node;
        node.next.pre = node;
    }

    private void remove(Node node) {
        node.pre.next = node.next;
        node.next.pre = node.pre;
    }
}

leetcode 49 字母异位词组合

public static List<List<String>> groupAnagrams(String[] strs) {
    HashMap<String, List<String>> map = new HashMap<>();
    for(String str : strs){
      char[] charArray = str.toCharArray();
      Arrays.sort(charArray);
      String key = charArray.toString();
      List<String> list = map.getOrDefault(key, new ArrayList<>());
      list.add(str);
      map.put(key,list);
    }
    return new ArrayList<>(map.values());
  }

leetcode 128 最长连续序列

通过hash表可以O(1)的复杂度来判断元素是否存在

public int longestConsecutive(int[] nums) {
    if (nums == null || nums.length == 0){
      return 0;
    }
    Set<Integer> set = new HashSet<>();
    for (int x : nums){
      set.add(x);
    }
    int res = 0;
    for(int x : set){
      if (set.contains(x-1)){
        continue;//存在更长的虚列
      }
      // 此时x是一个序列的起点
      int y = x + 1;
      while(set.contains(y)){
        y++;// 一直查询下一个数是否存在哈希表中
      }
      res = Math.max(res,y-x); // 从 x 到 y-1 一共 y-x 个数
    }
    return res;
  }

leetcode 283 移动零

public void moveZeroes(int[] nums) {
    int index = 0;
    for (int i = 0; i < nums.length; i++) {
      if (nums[i] != 0) {
        nums[index] = nums[i];
        index++;
      }
    }
    for (int i = index; i < nums.length; i++) {
      nums[i] = 0;
    }
  }

leetcode 11 盛最多水的容器

public int maxArea(int[] height) {
   int i = 0,j = height.length - 1;
   int res = 0 ;
   while(i < j){
       res = Math.max((j-i) * Math.min(height[j],height[i]),res);
       if(height[i] <= height[j]){
           i++;
       }else{
           j--;
       }
   }
   return res;
}

leetcode 15 三数之和

public List<List<Integer>> threeSum(int[] nums) {
    List<List<Integer>> res = new ArrayList<>();
    if (nums.length < 3){
      return res;
    }
    Arrays.sort(nums);
    for (int i = 0; i < nums.length; i++) {
      if (nums[i] > 0){
        break;
      }
      if (i > 0 && nums[i] == nums[i-1]){
        continue; // 去除重复三元组
      }
      int l = i+1;
      int r = nums.length-1;
      while(l<r){
        int sum = nums[i]+nums[l]+nums[r];
        if (sum == 0){
          res.add(Arrays.asList(nums[i],nums[l],nums[r]));
          while(l<r && nums[l] == nums[l+1]){
            l++; // 重复情况
          }
          while(l<r && nums[r] == nums[r-1]){
            r--;// 重复情况
          }
          l++;
          r--;
        } else if (sum < 0) {
          l++;
        }else{
          r--;
        }
      }
    }
    return res;
  }

leetcode 3 无重复字符串的最长字串

public int lengthOfLongestSubstring(String s) {
    if (s.length() == 0 || s == null){
      return 0;
    }
    int res = 0;
    int i = 0,j = 0;
    Set<Character> set = new HashSet<>();
    while(j<s.length()){
      while (set.contains(s.charAt(j))){
        set.remove(s.charAt(i));
        i++;
      }
      set.add(s.charAt(j));
      j++;
      res = Math.max(res,j-i);
    }
    return res;
  }

leetcode 438 找到字符中所有字母的异位词

给定两个字符串 s 和 p,找到 s 中所有 p 的 异位词 的子串,返回这些子串的起始索引。不考虑答案输出的顺序。

public List<Integer> findAnagrams(String s, String p) {
    List<Integer> list = new ArrayList<>();
    int[] arr1 = new int[26];
    int[] arr2 = new int[26];

    for (int i = 0; i < p.length(); i++) {
      arr2[p.charAt(i)-'a']++;
    }
    for (int r = 0; r < s.length(); r++) {
     arr1[s.charAt(r)-'a']++;// 字符串右边的字母进入窗口
     int l = r - p.length() + 1;
     if (l < 0){
       continue;
     }
     if (Arrays.equals(arr1,arr2)){
       list.add(l);
     }
     arr1[s.charAt(l)-'a']--;// 左边的字母离开窗口
    }
    return list;
  }

leetcode 560 和为k的子数组个数

前缀和优化

public int subarraySum(int[] nums, int k) {
    int len = nums.length;
    // 计算前缀和
    int[] preSum = new int[len + 1];
    preSum[0] = 0;
    for (int i = 0; i < len; i++) {
      preSum[i+1] = preSum[i] + nums[i];
    }
    int count = 0;
    for (int left = 0; left < len; left++) {
      for (int right = 0; right < len; right++) {
        if (preSum[right+1] - preSum[left] == k){
          count++;
        }
      }
    }
    return count;
  }
 

leetcode 239 滑动窗口最大值

public int[] maxSlidingWindow(int[] nums, int k) {
    int n = nums.length;
    int[] ans = new int[n-k+1];
    Deque<Integer> deque = new LinkedList<>();
    for (int i = 0; i < n; i++) {
      // 1入队列
      while(!deque.isEmpty() && nums[deque.getLast()] <= nums[i]){
        deque.removeLast();
      }
      deque.addLast(i);
      // 2出队列
      if (i - deque.getFirst() >= k){
        deque.removeFirst();
      }
      // 3算答案
      if (i >= k-1){
        // 单调递减,所以窗口最大值为对首
        ans[i-k+1] = nums[deque.getFirst()];
      }
    }
    return ans;
  }

leetcoe 189 轮转数组

public void rotate(int[] nums, int k) {
    int len = nums.length;
    reverse(nums,0,len - 1);
    reverse(nums,0,k-1);
    reverse(nums,k,len-1);
  }
  private void reverse(int[] nums,int left,int right){
    while (left < right){
      int t = nums[left];
      nums[left++] = nums[right];
      nums[right--] = t;
    }
  }

比较版本号

class Solution {
    public int compareVersion(String v1, String v2) {
        String[] ss1 = v1.split("\\."), ss2 = v2.split("\\.");
        int n = ss1.length, m = ss2.length;
        int i = 0, j = 0;
        while (i < n || j < m) {
            int a = 0, b = 0;
            if (i < n) a = Integer.parseInt(ss1[i++]);
            if (j < m) b = Integer.parseInt(ss2[j++]);
            if (a != b) return a > b ? 1 : -1;
        }
        return 0;
    }
}

有效括号匹配

class Solution {
    public boolean isValid(String s) {
        if(s==null||s.length()==0){
            return true;
        }
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            //获取每个元素
            char c = s.charAt(i);
            //左括号就入栈
            if (c == '(' || c == '[' || c == '{') {
                stack.push(c);
            } else {
                if(stack.isEmpty()){
                    return false;
                }
                char top = stack.pop();
                if ((c == ')' && top == '(') || (c == ']' && top == '[') || (c == '}' && top == '{')) {
                    continue;
                } else {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
}

螺旋矩阵

private static final int[][] DIRS = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; // 右下左上
    public static List<Integer> spiralOrder(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        List<Integer> ans = new ArrayList<>(m * n); // 预分配空间
        int i = 0;
        int j = 0;
        int di = 0;
        for (int k = 0; k < m * n; k++) { // 一共走 mn 步
            ans.add(matrix[i][j]);
            matrix[i][j] = Integer.MAX_VALUE; // 标记,表示已经访问过(已经加入答案)
            int x = i + DIRS[di][0];
            int y = j + DIRS[di][1]; // 下一步的位置
            // 如果 (x, y) 出界或者已经访问过
            if (x < 0 || x >= m || y < 0 || y >= n || matrix[x][y] == Integer.MAX_VALUE) {
                di = (di + 1) % 4; // 右转 90°
            }
            i += DIRS[di][0];
            j += DIRS[di][1]; // 走一步
        }
        return ans;
    }

接雨水

 public int trap(int[] height) {
        int ans = 0;
        int left = 0;
        int right = height.length - 1;
        int preMax = 0; // 前缀最大值,随着左指针 left 的移动而更新
        int sufMax = 0; // 后缀最大值,随着右指针 right 的移动而更新
        while (left < right) {
            preMax = Math.max(preMax, height[left]);
            sufMax = Math.max(sufMax, height[right]);
            ans += preMax < sufMax ? preMax - height[left++] : sufMax - height[right--];
        }
        return ans;
    }

死锁程序

public class SimpleDeadlockExample {

    // Two resources for demonstration
    private static final Object resource1 = new Object();
    private static final Object resource2 = new Object();

    public static void main(String[] args) {
        // Thread 1 tries to lock resource1 then resource2
        Thread thread1 = new Thread(() -> {
            synchronized (resource1) {
                System.out.println("Thread 1: locked resource 1");

                try {
                    // Simulate some work with resource1
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }

                System.out.println("Thread 1: waiting for resource 2");
                synchronized (resource2) {
                    System.out.println("Thread 1: locked resource 2");
                }
            }
        });

        // Thread 2 tries to lock resource2 then resource1
        Thread thread2 = new Thread(() -> {
            synchronized (resource2) {
                System.out.println("Thread 2: locked resource 2");

                try {
                    // Simulate some work with resource2
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }

                System.out.println("Thread 2: waiting for resource 1");
                synchronized (resource1) {
                    System.out.println("Thread 2: locked resource 1");
                }
            }
        });

        thread1.start();
        thread2.start();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值