实现MyHashMap类,修复LeetCode相关题目,调整TestInteger类代码结构。

This commit is contained in:
whaifree 2024-04-30 11:20:07 +08:00
parent 6bddb836c9
commit d5e3175b11
8 changed files with 544 additions and 1 deletions

View File

@ -0,0 +1,67 @@
package cn.whaifree.leetCode.Dynamic;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/4/26 11:44
* @注释
*/
public class LeetCode115 {
@Test
public void test()
{
Solution solution = new Solution();
System.out.println(solution.numDistinct("rabbbit", "rabbit"));
}
class Solution {
public int numDistinct(String s, String t) {
/**
* dp[i][j] 以i-1为 结尾的 s子序列 中出现以j-1为 结尾的 t的个数为dp[i][j]
*
* '' b a g
* '' 1 0 0 0
* b 1 1 0 0
* a 1 1 1 0
* e 1 1 1 0
* g 1 1 1 1
* g 1 1 1 2
*
* if s[i]==t[j]
* 1. 用s[i - 1]来匹配 dp[i - 1][j - 1] bagg和bag t匹配到s的第二个g时使用第一个g
* 2. 不用第s[i - 1]来匹配 dp[i - 1][j] bagg和bag t匹配到s的第二个g时不使用第一个g
* dp[i][j] = dp[i-1][j-1]+dp[i - 1][j];
* else
* 不用s[i - 1]来匹配模拟s中删除了这个元素
* dp[i][j] = dp[i - 1][j];
*
*/
char[] sChar = s.toCharArray();
char[] tChar = t.toCharArray();
int[][] dp = new int[sChar.length + 1][tChar.length + 1];
for (int i = 0; i < sChar.length; i++) {
dp[i][0] = 1;
}
for (int i = 1; i <= sChar.length; i++) {
for (int j = 1; j <= tChar.length; j++) {
if (sChar[i - 1] == tChar[j - 1]) {
// 如果相同
// 使用i-1进行匹配 dp[i - 1][j - 1] bagg和bag t匹配到s的第二个g时使用第一个g
// 不用第s[i - 1]来匹配 dp[i - 1][j] bagg和bag t匹配到s的第二个g时不使用第一个g
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
} else {
// 不用s[i - 1]来匹配
dp[i][j] = dp[i - 1][j];
}
}
}
return dp[sChar.length][tChar.length];
}
}
}

View File

@ -0,0 +1,57 @@
package cn.whaifree.redo.redo_24_4_27;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/4/27 11:37
* @注释
*/
public class LeetCode33 {
@Test
public void test() {
int[] nums = new int[]{4,5,6,7,0,1,2};
int target = 0;
int i = new Solution().search(nums, target);
System.out.println(i);
}
class Solution {
public int search(int[] nums, int target) {
// 二分查找
int left = 0;
int right = nums.length - 1;
int mid = 0;
while (left <= right) {
mid = (left + right) / 2;
if (nums[mid] == target) {
return mid;
}
// nums = nums1 + nums2
// 4,5,6,7,0,1,2
if (nums[mid] > nums[right]) { // nums1和nums2的交接处在mid右边
// nums1
if (nums[left] <= target && target < nums[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
} else {
// nums2
if (nums[mid] < target && target <= nums[right]) { // nums1和nums2的交接处在mid左边
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return -1;
}
}
}

View File

@ -0,0 +1,44 @@
package cn.whaifree.redo.redo_24_4_27;
import org.junit.Test;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/4/27 13:20
* @注释
*/
public class LeetCode69 {
@Test
public void test()
{
Solution solution = new Solution();
new CopyOnWriteArrayList<>().add(1);
solution.mySqrt(9);
for (int i = 0; i < 20; i++) {
System.out.println("i:" + i + " " + solution.mySqrt(i));
}
}
class Solution {
public int mySqrt(int x) {
int left = 0;
int right = x;
int ans = 0;
while (left <= right) {
int mid = left + (right - left) / 2;
if ((long) mid * mid > x) {
right = mid - 1;
} else {
ans = mid;
left = mid + 1;
}
}
return ans;
}
}
}

View File

@ -0,0 +1,79 @@
package cn.whaifree.redo.redo_24_4_27;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/4/29 11:48
* @注释
*/
public class LeetCode72 {
@Test
public void test()
{
Solution solution = new Solution();
int c = this.hashCode();
// 输出二进制
System.out.println(Integer.toBinaryString(c));
System.out.println(Integer.toBinaryString(c >>> 16));
System.out.println(Integer.toBinaryString(c & (c >>> 16)));
int i = solution.minDistance("ros", "horse");
System.out.println(i);
}
class Solution {
/**
* dp[i][j] 表示word1的0-i变为word2的0-j至少需要的操作数
* '' h o r s e
* '' 0 1 2 3 4 5
* r 1 1 2 2 3 4
* o 2 2 1 2 3 4
* s 3 3 2 2 3 3
*
*
* 相同
* [i-1][j-1]
*
* 不相同
* - 替换 [i-1][j-1] + 1
* - 增加 [i][j-1] + 1
* - 删除 [i-1][j] + 1
* @param word1
* @param word2
* @return
*/
public int minDistance(String word1, String word2) {
int len1 = word1.length();
int len2 = word2.length();
int[][] dp = new int[len1 + 1][len2 + 1];
for (int i = 0; i <= len1; i++) {
dp[i][0] = i;
}
for (int i = 0; i <= len2; i++) {
dp[0][i] = i;
}
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] =
Math.min(
dp[i - 1][j - 1] + 1, // 替换 [i-1][j-1]
Math.min(
dp[i - 1][j] + 1, // 增加 [i][j-1]
dp[i][j - 1] + 1) // 删除 [i-1][j]
);
}
}
}
return dp[len1][len2];
}
}
}

View File

@ -0,0 +1,38 @@
package cn.whaifree.redo.redo_24_4_27;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/4/29 12:07
* @注释
*/
public class LeetCode75 {
@Test
public void test() {
Solution solution = new Solution();
int[] nums = {2,0,2,1,1,0};
solution.sortColors(nums);
for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}
}
class Solution {
public void sortColors(int[] nums) {
int n0 = 0, n1 = 0; // 0的数量 0和1的数量
for(int i = 0; i < nums.length; i++){
int num = nums[i];
nums[i] = 2;
if(num < 2){
nums[n1++] = 1;
}
if(num < 1){
nums[n0++] = 0;
}
}
}
}
}

View File

@ -0,0 +1,78 @@
package cn.whaifree.redo.redo_24_4_27;
import org.junit.Test;
import java.util.Arrays;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/4/29 12:30
* @注释
*/
public class LeetCode912 {
@Test
public void test() {
// new ArrayList<>().iterator().next()
int[] nums = new int[]{5,1,1,2,0,0};
int[] res = new Solution1().sortArray(nums);
System.out.println(Arrays.toString(res));
}
class Solution {
public int[] sortArray(int[] nums) {
if (nums.length <= 1) {
return nums;
}
int mid = nums.length / 2;
int[] left = Arrays.copyOfRange(nums, 0, mid);
int[] right = Arrays.copyOfRange(nums, mid, nums.length);
left = sortArray(left);
right = sortArray(right);
return merge(left, right);
}
public int[] merge(int[] nums1, int[] nums2) {
int[] res = new int[nums1.length + nums2.length];
int index1 = 0;
int index2 = 0;
while (index1 < nums1.length && index2 < nums2.length) {
if (nums1[index1] < nums2[index2]) {
res[index1 + index2] = nums1[index1];
index1++;
} else {
res[index1 + index2] = nums2[index2];
index2++;
}
}
while (index1 < nums1.length) {
res[index1 + index2] = nums1[index1];
index1++;
}
while (index2 < nums2.length) {
res[index1 + index2] = nums2[index2];
index2++;
}
return res;
}
}
class Solution1 {
public int[] sortArray(int[] nums) {
sort(nums, 0, nums.length - 1);
return nums;
}
public void sort(int[] nums, int left, int right) {
}
public void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}

View File

@ -0,0 +1,131 @@
package cn.whaifree.selftDefinition;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/4/29 21:11
* @注释
*/
public class MyHashMap<K, V> {
public static void main(String[] args) {
MyHashMap<String, String> myHashMap = new MyHashMap<>();
myHashMap.put("12", "12");
for (int i = 0; i < 100; i++) {
myHashMap.put(String.valueOf(i), String.valueOf(i));
}
System.out.println(myHashMap.size());
for (int i = 0; i < 100; i++) {
System.out.println(myHashMap.get(String.valueOf(i)));
}
}
class Node<K,V>{
private K key;
private V value;
private Node<K,V> next;
public Node(K key,V value){
this.key = key;
this.value = value;
}
public Node(){
}
public Node(K key, V value, Node<K, V> next) {
this.key = key;
this.value = value;
this.next = next;
}
}
final float LOAD_FACTOR = 0.75f;
private int size;
Node<K, V>[] buckets;
final int DEFAULT_CAPACITY = 16;
public MyHashMap(int size) {
buckets = new Node[size];
size = 0;
}
public MyHashMap() {
buckets = new Node[DEFAULT_CAPACITY];
size = 0;
}
private int getIndex(K key,int length) {
return key.hashCode() % length;
}
public void put(K key, V value) {
put(key, value, buckets);
}
public void put(K key, V value,Node<K, V>[] myBuckets) {
if (size >= myBuckets.length * LOAD_FACTOR) {
resize();
}
// 根据hash获取桶的位置
int loc = getIndex(key, myBuckets.length);
Node<K, V> node = myBuckets[loc];
if (node == null) {
// 1.
myBuckets[loc] = new Node<>(key, value);
}else {
// 2. 不空尾插入法
while (node.next != null) {
node = node.next;
}
node.next = new Node<>(key, value);
}
size++;
}
public V get(K key) {
int loc = getIndex(key,buckets.length);
if (buckets[loc] == null) {
return null;
}else {
Node<K, V> node = buckets[loc];
while (node != null) {
if (node.key.equals(key)) {
return node.value;
}
node = node.next;
}
}
return null;
}
public void resize() {
// 扩容
size = 0;
Node<K, V>[] newBuckets = new Node[buckets.length * 2];
reHash(newBuckets);
buckets = newBuckets;
}
/**
* 把原来元素全部重新散列到新表
* @param newBuckets
*/
public void reHash(Node<K, V>[] newBuckets) {
for (Node<K, V> node : buckets) {
if (node == null) {
continue;
}
while (node != null) {
put(node.key, node.value, newBuckets);
node = node.next;
}
}
}
public int size() {
return size;
}
}

View File

@ -1,3 +1,6 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
/** /**
* @version 1.0 * @version 1.0
* @Author whai文海 * @Author whai文海
@ -6,7 +9,53 @@
*/ */
public class TestInteger { public class TestInteger {
public static void main(String[] args) { public static void main(String[] args) throws ExecutionException, InterruptedException {
// System.out.println(CompletableFuture.supplyAsync(new Supplier<Object>() {
// @Override
// public Object get() {
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
// System.out.println("ssdfsdfs");
// return "fdsfsdfsdf";
// }
// }).get());
//
// CompletableFuture.runAsync(new Runnable() {
// @Override
// public void run() {
// try {
// Thread.sleep(10000);
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
// }
// });
CompletableFuture.allOf(
CompletableFuture.supplyAsync(
() -> {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("1");
return 1;
}
),
CompletableFuture.runAsync(new Runnable() {
@Override
public void run() {
System.out.println("2");
}
})
);
/** /**