-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main'
- Loading branch information
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer, Integer> 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; | ||
} | ||
} |