From 413ec11ba241e89c3128e1f51b40e95527fe8ccc Mon Sep 17 00:00:00 2001 From: BGMer7 Date: Sat, 30 Apr 2022 14:39:20 +0800 Subject: [PATCH 1/3] =?UTF-8?q?908.=20=E6=9C=80=E5=B0=8F=E5=B7=AE=E5=80=BC?= =?UTF-8?q?I?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/gatsby/_908SmallestRangeI.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/com/gatsby/_908SmallestRangeI.java diff --git a/src/com/gatsby/_908SmallestRangeI.java b/src/com/gatsby/_908SmallestRangeI.java new file mode 100644 index 0000000..2d098c2 --- /dev/null +++ b/src/com/gatsby/_908SmallestRangeI.java @@ -0,0 +1,20 @@ +package com.gatsby; + +/** + * @author -- gatsby + * @date -- 2022/4/30 + * @description -- leetcode 908 最小差值 + */ + + +public class _908SmallestRangeI { + public int smallestRangeI(int[] nums, int k) { + int max = Integer.MIN_VALUE; + int min = Integer.MAX_VALUE; + for (int num : nums) { + if (num > max) max = num; + if (num < min) min = num; + } + return max - min > 2 * k ? max - min - 2 * k : 0; + } +} \ No newline at end of file From a6c7c4458c80554834117039e6eb3479938d6490 Mon Sep 17 00:00:00 2001 From: BGMer7 Date: Tue, 12 Jul 2022 17:14:00 +0800 Subject: [PATCH 2/3] 208. Implement Trie (Prefix Tree) --- src/com/gatsby/_208ImplementTrie.java | 55 +++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/com/gatsby/_208ImplementTrie.java diff --git a/src/com/gatsby/_208ImplementTrie.java b/src/com/gatsby/_208ImplementTrie.java new file mode 100644 index 0000000..1f021a8 --- /dev/null +++ b/src/com/gatsby/_208ImplementTrie.java @@ -0,0 +1,55 @@ +package com.gatsby; + +/** + * @author -- gatsby + * @date -- 2022/7/12 + * @description -- + */ + + +public class _208ImplementTrie { + class TrieNode { + boolean isEnd = false; + // 直接创建一个26字母的数组,这样已经可以考虑到所有的下一位字母的情况 + TrieNode[] children = new TrieNode[26]; + } + + private TrieNode root; + + public _208ImplementTrie() { + root = new TrieNode(); + } + + public void insert(String word) { + TrieNode p = root; + for (int i = 0; i < word.length(); ++i) { + int letter = word.charAt(i) - 'a'; + if (p.children[letter] == null) { + p.children[letter] = new TrieNode(); + } + p = p.children[letter]; + } + // 此时在最后一个字母,将isEnd置为true + p.isEnd = true; + } + + public boolean search(String word) { + TrieNode p = root; + for (int i = 0; i < word.length(); ++i) { + int letter = word.charAt(i) - 'a'; + if (p.children[letter] == null) return false; + else p = p.children[letter]; + } + return p.isEnd; + } + + public boolean startsWith(String prefix) { + TrieNode p = root; + for (int i = 0; i < prefix.length(); ++i) { + int letter = prefix.charAt(i) - 'a'; + if (p.children[letter] == null) return false; + else p = p.children[letter]; + } + return true; + } +} \ No newline at end of file From edbbbbb8d5ca1d1247ee1708cc79061196054ba1 Mon Sep 17 00:00:00 2001 From: BGMer7 Date: Wed, 13 Jul 2022 15:29:40 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=89=91=E6=8C=87OfferII=20004.=20?= =?UTF-8?q?=E5=8F=AA=E5=87=BA=E7=8E=B0=E4=B8=80=E6=AC=A1=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/gatsby/offerII/_4OnlyOnce.java | 40 ++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/com/gatsby/offerII/_4OnlyOnce.java diff --git a/src/com/gatsby/offerII/_4OnlyOnce.java b/src/com/gatsby/offerII/_4OnlyOnce.java new file mode 100644 index 0000000..8697a72 --- /dev/null +++ b/src/com/gatsby/offerII/_4OnlyOnce.java @@ -0,0 +1,40 @@ +package com.gatsby.offerII; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +/** + * @author -- gatsby + * @date -- 2022/7/13 + * @description -- 剑指 Offer II 004. 只出现一次的数字 + */ + + +public class _4OnlyOnce { + public int singleNumber(int[] nums) { + Map count = new HashMap<>(); + for (int num : nums) { + count.put(num, count.getOrDefault(num, 0) + 1); + } + + for (int num : count.keySet()) { + if (count.get(num) == 1) return num; + } + + return -1; + } + + public int single(int start, int[] nums) { + return nums[start] ^ nums[start + 1] ^ nums[start + 2]; + } + + public int singleNumberSort(int[] nums) { + Arrays.sort(nums); + + for (int i = 0; i + 3 < nums.length; i += 3) { + if (nums[i] != nums[i+2]) return single(i, nums); + } + return -1; + } +} \ No newline at end of file