454
This commit is contained in:
whai 2024-01-03 21:01:34 +08:00
parent 8490fdfcf7
commit bdbfa1854c
5 changed files with 216 additions and 0 deletions

View File

@ -17,6 +17,11 @@
<dependencies> <dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId>
<version>5.8.18</version>
</dependency>
<dependency> <dependency>

View File

@ -0,0 +1,94 @@
package cn.whaifree.leetCode.Hash;
import cn.hutool.core.map.MapUtil;
import cn.whaifree.leetCode.utils.MapUtils;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
/**
* 383. 赎金信
* 给你两个字符串ransomNote magazine 判断 ransomNote 能不能由 magazine 里面的字符构成
*
* 如果可以返回 true 否则返回 false
*
* magazine 中的每个字符只能在 ransomNote 中使用一次
*
* 示例 1
*
* 输入ransomNote = "a", magazine = "b"
* 输出false
* 示例 2
*
* 输入ransomNote = "aa", magazine = "ab"
* 输出false
* 示例 3
*
* 输入ransomNote = "aa", magazine = "aab"
* 输出true
*
*
* 提示
*
* 1 <= ransomNote.length, magazine.length <= 105
* ransomNote magazine 由小写英文字母组成
*/
public class LeetCode383 {
@Test
public void test() {
boolean b = new Solution1().canConstruct("aabb", "aabbc");
System.out.println(b);
}
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
Map<Character, Integer> mapR = new HashMap<>();
for (char c : ransomNote.toCharArray()) {
mapR.put(c, mapR.getOrDefault(c, 0) + 1);
}
Map<Character, Integer> mapM = new HashMap<>();
for (char c : magazine.toCharArray()) {
mapM.put(c, mapM.getOrDefault(c, 0) + 1);
}
MapUtils.printMap(mapR);
MapUtils.printMap(mapM);
for (Character c : mapR.keySet()) {
if (mapM.getOrDefault(c, 0) < mapR.getOrDefault(c, 0)) {
return false;
}
}
return true;
}
}
class Solution1 {
// ransomNote magazine 由小写英文字母组成
public boolean canConstruct(String ransomNote, String magazine) {
int[] map = new int[26];
// 大的先存入Map
// 相比转换为Char更快
for (int i = 0; i < magazine.length(); i++) map[magazine.charAt(i) - 'a'] += 1;
// 减去小的如果小于0如果不够减就返回false
for (int i = 0; i < ransomNote.length(); i++) {
int index = ransomNote.charAt(i) - 97;
map[index] -= 1;
if (map[index] < 0) {
return false;
}
}
return true;
}
}
}

View File

@ -0,0 +1,98 @@
package cn.whaifree.leetCode.LinkedList;
import cn.whaifree.leetCode.model.ListNode;
import org.junit.Test;
public class LeetCode2487 {
@Test
public void test() {
new Solution1().removeNodes(ListNode.listNodeFromArray(new int[]{5, 2, 13, 3, 8})).printList();
}
/**
* 找到最大的节点找到下一个最大的节点此区间全部删掉
* 每次删除后MaxNode为null
*
* 即最后变成了递减的链表
*
*/
class Solution {
/**
* 递归
* @param head
* @return
*/
// public ListNode removeNodes(ListNode head) {
// ListNode dummy = new ListNode(0, head);
// ListNode pre = dummy;
// ListNode index = head;
// ListNode maxNode = dummy;
// while (index != null) {
// if (index.val > maxNode.val) {
// maxNode = index;
// // 删除pre到maxNode的前一个节点
// pre.next = maxNode;
// maxNode.next = removeNodes(maxNode.next);
// }
// index = index.next;
// }
//
// return dummy.next;
// }
public ListNode removeNodes(ListNode head) {
return remove(new ListNode(0, head));
}
public ListNode remove(ListNode head) {
if (head == null) {
return null;
}
head.next = remove(head.next);
if (head.next != null && head.val < head.next.val) {
return head.next;
} else {
return head;
}
}
}
class Solution1 {
public ListNode removeNodes(ListNode head) {
ListNode newHead = reverse(head);
// 变成从右往左边移除比newHead小的
ListNode index = newHead;
ListNode tmpMaxNode = newHead;
while (index.next != null) {
if (index.next.val < tmpMaxNode.val) {
index.next = index.next.next;
} else {
index = index.next;
tmpMaxNode = index;
}
}
// 最后那个数是永远不会删除的
return reverse(newHead);
}
public ListNode reverse(ListNode head) {
// 反转链表
ListNode index = head;
ListNode pre = null;
while (index != null) {
ListNode tmp = index.next;
index.next = pre;
pre = index;
index = tmp;
}
// {5, 2, 13, 3, 8}
return pre;
}
}
}

View File

@ -45,7 +45,10 @@ public class ListNode {
System.out.print(curr.val + " "); System.out.print(curr.val + " ");
curr = curr.next; curr = curr.next;
} }
}
public void printList() {
printList(this);
} }
} }

View File

@ -0,0 +1,16 @@
package cn.whaifree.leetCode.utils;
import java.util.Map;
public class MapUtils {
public static void printMap(Map map) {
if (map == null) {
return;
}
for (Object key : map.keySet()) {
System.out.println(key + " : " + map.get(key));
}
System.out.println("==========");
}
}