diff --git a/5.leetcode/src/com/fanxb/common/Q127.java b/5.leetcode/src/com/fanxb/common/Q127.java new file mode 100644 index 0000000..239137e --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q127.java @@ -0,0 +1,50 @@ +package com.fanxb.common; + +import java.util.*; + +public class Q127 { + public int ladderLength(String beginWord, String endWord, List wordList) { + Set set = new HashSet<>(wordList); + Set cache = new HashSet<>(); + if (!set.contains(endWord)) return 0; + Queue 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")); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q208.java b/5.leetcode/src/com/fanxb/common/Q208.java new file mode 100644 index 0000000..63245a0 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q208.java @@ -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; + } + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q210.java b/5.leetcode/src/com/fanxb/common/Q210.java new file mode 100644 index 0000000..80d50df --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q210.java @@ -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> 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 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]; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q211.java b/5.leetcode/src/com/fanxb/common/Q211.java new file mode 100644 index 0000000..bd74d60 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q211.java @@ -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.."); + + } + +} diff --git a/5.leetcode/src/com/fanxb/common/Q212.java b/5.leetcode/src/com/fanxb/common/Q212.java new file mode 100644 index 0000000..a242ab2 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q212.java @@ -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 findWords(char[][] board, String[] words) { + Set set = new HashSet<>(List.of(words)); + List 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 set, List 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"}); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q433.java b/5.leetcode/src/com/fanxb/common/Q433.java new file mode 100644 index 0000000..de64402 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q433.java @@ -0,0 +1,53 @@ +package com.fanxb.common; + +import java.util.*; + +public class Q433 { + public int minMutation(String startGene, String endGene, String[] bank) { + Set 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 cache = new HashSet<>(); + cache.add(startGene); + Queue queue = new LinkedList<>(); + queue.add(startGene); + while (!queue.isEmpty()) { + step++; + int size = queue.size(); + while (size-- > 0) { + String str = queue.poll(); + List 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 checkDiff(String str, char[] chars, Set bankSet) { + List 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; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q909.java b/5.leetcode/src/com/fanxb/common/Q909.java new file mode 100644 index 0000000..16df77c --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q909.java @@ -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 queue = new LinkedList<>(); + queue.add(new Integer[]{n - 1, 0}); +// int count + while (!queue.isEmpty()) { + Integer[] node = queue.poll(); + } + return 0; + } + + +}