This commit is contained in:
fanxb 2024-04-01 19:39:24 +08:00
parent 121cc9b5ba
commit 610048f4ea
7 changed files with 313 additions and 0 deletions

View File

@ -0,0 +1,50 @@
package com.fanxb.common;
import java.util.*;
public class Q127 {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
Set<String> set = new HashSet<>(wordList);
Set<String> cache = new HashSet<>();
if (!set.contains(endWord)) return 0;
Queue<String> queue = new LinkedList<>();
queue.offer(beginWord);
cache.add(beginWord);
int res = Integer.MAX_VALUE;
int step = 1;
while (!queue.isEmpty()) {
int size = queue.size();
step++;
while (size-- > 0) {
String str = queue.poll();
boolean continueQueue = false;
for (int i = 0; i < str.length(); i++) {
for (int j = 0; j < 26; j++) {
char c = (char) ('a' + j);
if (c != str.charAt(i)) {
StringBuilder builder = new StringBuilder(str);
builder.setCharAt(i, c);
String newStr = builder.toString();
if (!set.contains(newStr) || cache.contains(newStr)) {
continue;
}
if (endWord.equals(newStr)) {
res = Math.min(res, step);
continueQueue = true;
break;
}
cache.add(newStr);
queue.add(newStr);
}
}
if (continueQueue) break;
}
}
}
return res == Integer.MAX_VALUE ? 0 : res;
}
public static void main(String[] args) {
new Q127().ladderLength("hot", "dog", Arrays.asList("hot", "dog"));
}
}

View File

@ -0,0 +1,54 @@
package com.fanxb.common;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Q208 {
private class Trie {
private boolean isEnd;
private Trie[] child;
public Trie() {
isEnd = false;
child = new Trie[26];
}
public void insert(String word) {
Trie root = this;
for (int i = 0; i < word.length(); i++) {
int index = word.charAt(i) - 'a';
if (root.child[index] == null) {
Trie trie = new Trie();
root.child[index] = trie;
}
root = root.child[index];
}
root.isEnd = true;
}
public boolean search(String word) {
Trie trie = searchRes(word);
return trie != null && trie.isEnd;
}
private Trie searchRes(String word) {
Trie root = this;
for (char c : word.toCharArray()) {
int index = c - 'a';
if (root.child[index] == null) return null;
root = root.child[index];
}
return root;
}
public boolean startsWith(String prefix) {
return searchRes(prefix) != null;
}
}
}

View File

@ -0,0 +1,34 @@
package com.fanxb.common;
import java.util.*;
public class Q210 {
public int[] findOrder(int numCourses, int[][] prerequisites) {
//课程入度统计
int[] courseCount = new int[numCourses];
//记录一门课上完以后接着可以上哪些课
Map<Integer, List<Integer>> nextCourseMap = new HashMap<>(numCourses);
for (int i = 0; i < prerequisites.length; i++) {
int[] temp = prerequisites[i];
courseCount[temp[0]]++;
nextCourseMap.computeIfAbsent(temp[1], k -> new ArrayList<>()).add(temp[0]);
}
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < courseCount.length; i++) {
if (courseCount[i] == 0) queue.offer(i);
}
int[] res = new int[numCourses];
int count = 0;
while (!queue.isEmpty()) {
Integer course = queue.poll();
res[count++] = course;
nextCourseMap.getOrDefault(course, new ArrayList<>(0)).forEach(item -> {
courseCount[item]--;
if (courseCount[item] == 0) {
queue.offer(item);
}
});
}
return count == numCourses ? res : new int[0];
}
}

View File

@ -0,0 +1,58 @@
package com.fanxb.common;
import java.util.*;
import java.util.stream.Stream;
public class Q211 {
public static class WordDictionary {
boolean isEnd;
WordDictionary[] child;
public WordDictionary() {
isEnd = false;
child = new WordDictionary[26];
}
public void addWord(String word) {
WordDictionary root = this;
for (char c : word.toCharArray()) {
int index = c - 'a';
if (root.child[index] == null) root.child[index] = new WordDictionary();
root = root.child[index];
}
root.isEnd = true;
}
public boolean search(String word) {
return dfs(this, word, word.length(), 0);
}
private boolean dfs(WordDictionary node, String word, int length, int index) {
if (index == length) return node.isEnd;
char c = word.charAt(index);
if (c == '.') {
for (WordDictionary item : node.child) {
boolean isOk = item != null && dfs(item, word, length, index + 1);
if (isOk) return isOk;
}
} else {
int i = c - 'a';
if (node.child[i] != null) return dfs(node.child[i], word, length, index + 1);
}
return false;
}
}
public static void main(String[] args) {
Q211.WordDictionary q = new Q211.WordDictionary();
q.addWord("bad");
q.addWord("dad");
q.addWord("mad");
q.search("pad");
q.search("bad");
q.search(".ad");
q.search("b..");
}
}

View File

@ -0,0 +1,41 @@
package com.fanxb.common;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Q212 {
public List<String> findWords(char[][] board, String[] words) {
Set<String> set = new HashSet<>(List.of(words));
List<String> res = new ArrayList<>(words.length);
int m = board.length, n = board[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int[][] cache = new int[board.length][board[0].length];
dfs(set, res, board, "", cache, 0, 0, board.length, board[0].length);
}
}
return res;
}
private void dfs(Set<String> set, List<String> res, char[][] board, String temp, int[][] cache, int i, int j, int m, int n) {
if (i < 0 || i >= m || j < 0 || j >= n || temp.length() >= 10) return;
if (cache[i][j] == 1) return;
char c = board[i][j];
cache[i][j] = 1;
String newStr = temp + c;
if (set.contains(newStr)) {
res.add(newStr);
set.remove(newStr);
}
dfs(set, res, board, newStr, cache, i, j - 1, m, n);
dfs(set, res, board, newStr, cache, i, j + 1, m, n);
dfs(set, res, board, newStr, cache, i - 1, j, m, n);
dfs(set, res, board, newStr, cache, i + 1, j, m, n);
}
public static void main(String[] args) {
new Q212().findWords(new char[][]{{'o', 'a', 'a', 'n'}, {'e', 't', 'a', 'e'}, {'i', 'h', 'k', 'r'}, {'i', 'f', 'l', 'v'}}, new String[]{"oath", "pea", "eat", "rain"});
}
}

View File

@ -0,0 +1,53 @@
package com.fanxb.common;
import java.util.*;
public class Q433 {
public int minMutation(String startGene, String endGene, String[] bank) {
Set<String> bankSet = new HashSet<>(bank.length);
bankSet.addAll(Arrays.asList(bank));
if (!bankSet.contains(endGene)) return -1;
if (startGene.equals(endGene)) return 0;
int step = 0;
char[] chars = new char[]{'A', 'C', 'G', 'T'};
Set<String> cache = new HashSet<>();
cache.add(startGene);
Queue<String> queue = new LinkedList<>();
queue.add(startGene);
while (!queue.isEmpty()) {
step++;
int size = queue.size();
while (size-- > 0) {
String str = queue.poll();
List<String> temp = checkDiff(str, chars, bankSet);
for (String one : temp) {
if (one.equals(endGene)) return step;
if (cache.contains(one)) continue;
queue.offer(one);
cache.add(one);
}
}
}
return -1;
}
/**
* 返回所有的替换情况
*
* @param str
* @param endChar
* @return
*/
private List<String> checkDiff(String str, char[] chars, Set<String> bankSet) {
List<String> res = new ArrayList<>();
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 4; j++) {
if (str.charAt(i) != chars[j]) {
String temp = str.substring(0, i) + chars[j] + str.substring(i + 1);
if (bankSet.contains(temp)) res.add(temp);
}
}
}
return res;
}
}

View File

@ -0,0 +1,23 @@
package com.fanxb.common;
import java.util.LinkedList;
import java.util.Queue;
public class Q909 {
//记录走过的格子
private int[][] cache;
private int res = Integer.MAX_VALUE;
public int snakesAndLadders(int[][] board) {
int n = board.length;
Queue<Integer[]> queue = new LinkedList<>();
queue.add(new Integer[]{n - 1, 0});
// int count
while (!queue.isEmpty()) {
Integer[] node = queue.poll();
}
return 0;
}
}