日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构与算法之RandomPool结构和岛问题

發布時間:2024/2/28 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构与算法之RandomPool结构和岛问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

數據結構與算法之RandomPool結構和島問題


目錄

  • RandomPool結構
  • 島問題

  • 1. RandomPool結構

    (一)題目概述

    (二)思路分析
  • 完成上述功能需要創建兩個hashmap結構
  • insert操作:直接往里面添加數據即可。
  • getRandom操作:因為知道hashmap里size個數,所以可以根據size得到隨機值,取出即可
  • delete操作:因為刪除一個元素后會產生一個空位,所以可以在取出后用最后一個元素添上空位,size減1。
  • (三)代碼實現
    import java.util.HashMap;public class Code_02_RandomPool {public static class Pool<K> {private HashMap<K, Integer> keyIndexMap;private HashMap<Integer, K> indexKeyMap;private int size;public Pool() {this.keyIndexMap = new HashMap<K, Integer>();this.indexKeyMap = new HashMap<Integer, K>();this.size = 0;}public void insert(K key) {if (!this.keyIndexMap.containsKey(key)) {this.keyIndexMap.put(key, this.size);this.indexKeyMap.put(this.size++, key);}}public void delete(K key) {if (this.keyIndexMap.containsKey(key)) {int deleteIndex = this.keyIndexMap.get(key);int lastIndex = --this.size;K lastKey = this.indexKeyMap.get(lastIndex);this.keyIndexMap.put(lastKey, deleteIndex);this.indexKeyMap.put(deleteIndex, lastKey);this.keyIndexMap.remove(key);this.indexKeyMap.remove(lastIndex);}}public K getRandom() {if (this.size == 0) {return null;}int randomIndex = (int) (Math.random() * this.size); // 0 ~ size -1return this.indexKeyMap.get(randomIndex);}}public static void main(String[] args) {Pool<String> pool = new Pool<String>();pool.insert("nu");pool.insert("li");pool.insert("a");System.out.println(pool.getRandom());System.out.println(pool.getRandom());System.out.println(pool.getRandom());System.out.println(pool.getRandom());System.out.println(pool.getRandom());System.out.println(pool.getRandom());}}

    2. 島問題

    (一)題目概述

    (二)思路
  • 判斷二維數組的合理性,不合理返回null。遍歷N,M獲取二維數組長寬,和創建一個變量記錄島的數量
  • 遍歷每一個元素,如果元素為1,res++,進入感染函數
  • 將元素1的位置賦值為2,然后遞歸它的上下左右。
  • (三)代碼實現
    public class Code_03_Islands {public static int countIslands(int[][] m) {if (m == null || m[0] == null) {return 0;}int N = m.length;int M = m[0].length;int res = 0;for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if (m[i][j] == 1) {res++;infect(m, i, j, N, M);}}}return res;}public static void infect(int[][] m, int i, int j, int N, int M) {if (i < 0 || i >= N || j < 0 || j >= M || m[i][j] != 1) {return;}m[i][j] = 2;infect(m, i + 1, j, N, M);infect(m, i - 1, j, N, M);infect(m, i, j + 1, N, M);infect(m, i, j - 1, N, M);}public static void main(String[] args) {int[][] m1 = { { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 1, 1, 0, 1, 1, 1, 0 }, { 0, 1, 1, 1, 0, 0, 0, 1, 0 },{ 0, 1, 1, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 1, 1, 0, 0 }, { 0, 0, 0, 0, 1, 1, 1, 0, 0 },{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }, };System.out.println(countIslands(m1));int[][] m2 = { { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 1, 1, 1, 1, 1, 1, 0 }, { 0, 1, 1, 1, 0, 0, 0, 1, 0 },{ 0, 1, 1, 0, 0, 0, 1, 1, 0 }, { 0, 0, 0, 0, 0, 1, 1, 0, 0 }, { 0, 0, 0, 0, 1, 1, 1, 0, 0 },{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }, };System.out.println(countIslands(m2));}}

    編譯結果:

    總結

    以上是生活随笔為你收集整理的数据结构与算法之RandomPool结构和岛问题的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。