Algorithm 384. 打亂數組
Review Java 8 HashMap Implementation and Performance
Tip/Tech B+樹
Share 無人機找小男孩
Algorithm
384. 打亂數組
https://leetcode-cn.com/problems/shuffle-an-array/
這道題,的思路就是有點像這種:
你在帽子中隨機抽出一個數字,然後在數組中一次放置就行。
比如我們要選擇放在數組第0
個位置的數字是什麼,那麼我們就要選擇在數組的所有的位置隨便選一個。
然後就繼續看第1
個位置。
上面這種思想就是洗牌算法,具體的名字叫做Fisher-Yates 洗牌算法
Fisher-Yates 洗牌算法跟暴力算法很像。在每次迭代中,生成一個範圍在當前下標到數組末尾元素下標之間的隨機整數。接下來,將當前元素和隨機選出的下標所指的元素互相交換 - 這一步模擬了每次從 “帽子” 裏面摸一個元素的過程,其中選取下標範圍的依據在於每個被摸出的元素都不可能再被摸出來了。此外還有一個需要注意的細節,當前元素是可以和它本身互相交換的 - 否則生成最後的排列組合的概率就不對了。
作者:LeetCode
鏈接:https://leetcode-cn.com/problems/shuffle-an-array/solution/da-luan-shu-zu-by-leetcode/
來源:力扣(LeetCode)
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。
接下來,我們來看看代碼:
class Solution {
private int[] originArray;
private int[] changeArray;
private Random random;
public Solution(int[] nums) {
originArray = nums.clone();
changeArray = nums.clone();
random = new Random();
}
/** Resets the array to its original configuration and return it. */
public int[] reset() {
return originArray;
}
/** Returns a random shuffling of the array. */
public int[] shuffle() {
int len = this.changeArray.length;
for (int i = 0; i < len - 1; i++) {
int randomIndex = getRandomInRange(i, len) + i;
swap(this.changeArray, i, randomIndex);
}
return this.changeArray;
}
private int getRandomInRange(int start, int end) {
return this.random.nextInt(end -start);
}
private void swap(int[] arr, int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}
Review
Java 8 HashMap Implementation and Performance
https://dzone.com/articles/java8-hashmap-implementation-and-performance
這個主要是對HashMap的一個比較簡單的分析。
HashMap的影響性能,主要是兩個,一個initial capacity 和load factor.
初始的大小主要是會影響擴容,如果會不斷的擴容,擴容主要是需要數據搬移,所以會很耗費時間。
以及在1.8 之後,一旦數量超過一定程度(8)了,就會轉化爲紅黑樹了。
注意要點:
- 如果有預感存的數據會很多,那麼毫無疑問,你需要先設置一個比較合適的map的數組大小
- 如果你是自己的構造的類,那麼你要注意你的hashcode(),如果設計不好,就會影響HashMap的哈希函數的作用,比如某些位置的數字出現的比較多,或者值分佈不均衡的情況。
Tip/Tech
B+ 樹
主要是B+樹的索引節點和葉子及節點。
/**
* 這是B+樹非葉子節點的定義。
*
* 假設keywords=[3, 5, 8, 10]
* 4個鍵值將數據分爲5個區間:(-INF,3), [3,5), [5,8), [8,10), [10,INF)
* 5個區間分別對應:children[0]...children[4]
*
* m值是事先計算得到的,計算的依據是讓所有信息的大小正好等於頁的大小:
* PAGE_SIZE = (m-1)*4[keywordss大小]+m*8[children大小]
*/
public class BPlusTreeNode {
public static int m = 5; // 5叉樹
public int[] keywords = new int[m-1]; // 鍵值,用來劃分數據區間
public BPlusTreeNode[] children = new BPlusTreeNode[m];//保存子節點指針
}
/**
* 這是B+樹中葉子節點的定義。
*
* B+樹中的葉子節點跟內部結點是不一樣的,
* 葉子節點存儲的是值,而非區間。
* 這個定義裏,每個葉子節點存儲3個數據行的鍵值及地址信息。
*
* k值是事先計算得到的,計算的依據是讓所有信息的大小正好等於頁的大小:
* PAGE_SIZE = k*4[keyw..大小]+k*8[dataAd..大小]+8[prev大小]+8[next大小]
*/
public class BPlusTreeLeafNode {
public static int k = 3;
public int[] keywords = new int[k]; // 數據的鍵值
public long[] dataAddress = new long[k]; // 數據地址
public BPlusTreeLeafNode prev; // 這個結點在鏈表中的前驅結點
public BPlusTreeLeafNode next; // 這個結點在鏈表中的後繼結點
}
Share
Photographer uses drone with thermal camera to find missing 6-year-old boy
https://www.dpreview.com/news/1907862357/photographer-uses-drone-with-thermal-camera-to-find-missing-6-year-old-boy
這個應該科技向善例子了,用無人機來找失蹤的兒童。
這個攝影師在無人機上面安裝了熱源傳感器,用熱源傳感器後來找到了這個失蹤小男孩。
包括中國現在的天眼系統,這些系統我相信會以後會讓這個世界更好的。
這些天眼系統可以幫助的人們反恐,捉罪犯等等。
科技就應該多多應用於讓人類更好。