From bdbfa1854ceb5e1403069fa9004267c0b70cd8fd Mon Sep 17 00:00:00 2001 From: whai Date: Wed, 3 Jan 2024 21:01:34 +0800 Subject: [PATCH] 1 454 --- pom.xml | 5 + .../whaifree/leetCode/Hash/LeetCode383.java | 94 ++++++++++++++++++ .../leetCode/LinkedList/LeetCode2487.java | 98 +++++++++++++++++++ .../cn/whaifree/leetCode/model/ListNode.java | 3 + .../cn/whaifree/leetCode/utils/MapUtils.java | 16 +++ 5 files changed, 216 insertions(+) create mode 100644 src/main/java/cn/whaifree/leetCode/Hash/LeetCode383.java create mode 100644 src/main/java/cn/whaifree/leetCode/LinkedList/LeetCode2487.java create mode 100644 src/main/java/cn/whaifree/leetCode/utils/MapUtils.java diff --git a/pom.xml b/pom.xml index ac263f8..78cce23 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,11 @@ + + cn.hutool + hutool-http + 5.8.18 + diff --git a/src/main/java/cn/whaifree/leetCode/Hash/LeetCode383.java b/src/main/java/cn/whaifree/leetCode/Hash/LeetCode383.java new file mode 100644 index 0000000..fffdc5e --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Hash/LeetCode383.java @@ -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 mapR = new HashMap<>(); + for (char c : ransomNote.toCharArray()) { + mapR.put(c, mapR.getOrDefault(c, 0) + 1); + } + + Map 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; + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/LinkedList/LeetCode2487.java b/src/main/java/cn/whaifree/leetCode/LinkedList/LeetCode2487.java new file mode 100644 index 0000000..bab2d86 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/LinkedList/LeetCode2487.java @@ -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; + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/model/ListNode.java b/src/main/java/cn/whaifree/leetCode/model/ListNode.java index 403bafd..1ade04c 100644 --- a/src/main/java/cn/whaifree/leetCode/model/ListNode.java +++ b/src/main/java/cn/whaifree/leetCode/model/ListNode.java @@ -45,7 +45,10 @@ public class ListNode { System.out.print(curr.val + " "); curr = curr.next; } + } + public void printList() { + printList(this); } } diff --git a/src/main/java/cn/whaifree/leetCode/utils/MapUtils.java b/src/main/java/cn/whaifree/leetCode/utils/MapUtils.java new file mode 100644 index 0000000..2bfef7c --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/utils/MapUtils.java @@ -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("=========="); + } +}