diff --git a/Week_04/id_38/LeetCode_169_038.java b/Week_04/id_38/LeetCode_169_038.java new file mode 100644 index 00000000..c556f19b --- /dev/null +++ b/Week_04/id_38/LeetCode_169_038.java @@ -0,0 +1,32 @@ +/* + * @lc app=leetcode.cn id=169 lang=java + * + * [169] 求众数 + */ + +class LeetCode_169_038 { + + public int majorityElement(int[] nums) { + Arrays.sort(nums); + return nums[nums.length/2]; + } + + public int majorityElement2(int[] nums) { + int majorityCount = nums.length / 2; + + Map countMap = new HashMap(); + for (int num : nums) { + if (countMap.get(num) != null) { + countMap.put(num, countMap.get(num) + 1); + } else { + countMap.put(num, 1); + } + + if (countMap.get(num) > majorityCount) { + return num; + } + } + + return -1; + } +} diff --git a/Week_04/id_38/LeetCode_455_038.java b/Week_04/id_38/LeetCode_455_038.java new file mode 100644 index 00000000..57684879 --- /dev/null +++ b/Week_04/id_38/LeetCode_455_038.java @@ -0,0 +1,37 @@ +/* + * @lc app=leetcode.cn id=455 lang=java + * + * [455] 分发饼干 + */ +class LeetCode_455_038 { + public int findContentChildren(int[] g, int[] s) { + Arrays.sort(g); + Arrays.sort(s); + int count = 0; + int i = 0, j = 0; + while (i < g.length && j < s.length) { + if (g[i] <= s[j]) { + i++; + j++; + count++; + } else { + j++; + } + } + return count; + } + + public int findContentChildren2(int[] g, int[] s) { + Arrays.sort(g); + Arrays.sort(s); + int i = 0, j = 0; + int l1 = g.length, l2 = s.length; + while (i < l1 && j < l2) { + if (g[i] <= s[j]) { + i++; + } + j++; + } + return i; + } +} diff --git a/Week_04/id_38/LeetCode_51_038.java b/Week_04/id_38/LeetCode_51_038.java new file mode 100644 index 00000000..256aa8cc --- /dev/null +++ b/Week_04/id_38/LeetCode_51_038.java @@ -0,0 +1,76 @@ +/* + * @lc app=leetcode.cn id=51 lang=java + * + * [51] N皇后 + */ + +class LeetCode_51_038 { + + int rows[]; + // "hill" diagonals + int hills[]; + // "dale" diagonals + int dales[]; + int n; + // output + List> output = new ArrayList(); + // queens positions + int queens[]; + + public boolean isNotUnderAttack(int row, int col) { + int res = rows[col] + hills[row - col + 2 * n] + dales[row + col]; + return (res == 0) ? true : false; + } + + public void placeQueen(int row, int col) { + queens[row] = col; + rows[col] = 1; + hills[row - col + 2 * n] = 1; // "hill" diagonals + dales[row + col] = 1; //"dale" diagonals + } + + public void removeQueen(int row, int col) { + queens[row] = 0; + rows[col] = 0; + hills[row - col + 2 * n] = 0; + dales[row + col] = 0; + } + + public void addSolution() { + List solution = new ArrayList(); + for (int i = 0; i < n; ++i) { + int col = queens[i]; + StringBuilder sb = new StringBuilder(); + for(int j = 0; j < col; ++j) sb.append("."); + sb.append("Q"); + for(int j = 0; j < n - col - 1; ++j) sb.append("."); + solution.add(sb.toString()); + } + output.add(solution); + } + + public void backtrack(int row) { + for (int col = 0; col < n; col++) { + if (isNotUnderAttack(row, col)) { + placeQueen(row, col); + // if n queens are already placed + if (row + 1 == n) addSolution(); + // if not proceed to place the rest + else backtrack(row + 1); + // backtrack + removeQueen(row, col); + } + } + } + + public List> solveNQueens(int n) { + this.n = n; + rows = new int[n]; + hills = new int[4 * n - 1]; + dales = new int[2 * n - 1]; + queens = new int[n]; + + backtrack(0); + return output; + } +} diff --git a/Week_04/id_38/LeetCode_70_038.java b/Week_04/id_38/LeetCode_70_038.java new file mode 100644 index 00000000..4a6997c9 --- /dev/null +++ b/Week_04/id_38/LeetCode_70_038.java @@ -0,0 +1,35 @@ +/* + * @lc app=leetcode.cn id=70 lang=java + * + * [70] 爬楼梯 + */ + +class LeetCode_70_038 { + + public int climbStairs(int n) { + if (n == 1){ + return 1; + } + int[] dp = new int[n + 1]; + dp[1] = 1; + dp[2] = 2; + for (int i = 3; i <= n; i++){ + dp[i] = dp[i -1] + dp[i - 2]; + } + return dp[n]; + } + + public int climbStairs2(int n) { + return climb_Stairs(0, n); + } + + public int climb_Stairs(int i, int n) { + if (i > n) { + return 0; + } + if (i == n) { + return 1; + } + return climb_Stairs(i + 1, n) + climb_Stairs(i + 2, n); + } +} diff --git a/Week_04/id_38/LeetCode_720_038.java b/Week_04/id_38/LeetCode_720_038.java new file mode 100644 index 00000000..6bb3af5e --- /dev/null +++ b/Week_04/id_38/LeetCode_720_038.java @@ -0,0 +1,91 @@ +/* + * @lc app=leetcode.cn id=720 lang=java + * + * [720] 词典中最长的单词 + */ + +class LeetCode_720_038 { + + public String longestWord(String[] words) { + Set s = new HashSet<>(); + for(String word : words) + s.add(word); + int ml = 0; + String mw = ""; + for(String w : words){ + if(w.length() < ml) continue; + if(w.length() == ml && w.compareTo(mw) >= 0) continue; + int n = w.length(); + boolean flag = true; + for(int i = 1; i < n; i++){ + if(!s.contains(w.substring(0, i))){ + flag = false; + break; + } + } + if(flag){ + ml = w.length(); + mw = w; + } + } + + return mw; + } + + + //#endregion + private static final int R = 26; + private TrieNode root; + + private static class TrieNode { + private final TrieNode[] children; + private String word; + + private TrieNode() { + children = new TrieNode[R]; + word = null; + } + } + + private void insertWord(String word) { + int len = word.length(); + TrieNode curr = root; + char c; + int idx; + for (int i = 0; i < len; ++i) { + c = word.charAt(i); + idx = c - 'a'; + + if (curr.children[idx] == null) { + curr.children[idx] = new TrieNode(); + } + + curr = curr.children[idx]; + } + + curr.word = word; + } + + private String findLongestWord(TrieNode root) { + String rst = root.word == null ? "" : root.word; + for (TrieNode child : root.children) { + if (child == null || child.word == null) continue; + String s = findLongestWord(child); + if (s.length() > rst.length()) rst = s; + } + return rst; + } + + public String longestWord2(String[] words) { + // initialize a new trie + root = new TrieNode(); + + // construct the trie based on input dictionary words + for (String word : words) { + insertWord(word); + } + + // find the longest dictionary word in this trie according to given rules + return findLongestWord(root); + } +} diff --git a/Week_04/id_38/LeetCode_784_038.java b/Week_04/id_38/LeetCode_784_038.java new file mode 100644 index 00000000..0c81d770 --- /dev/null +++ b/Week_04/id_38/LeetCode_784_038.java @@ -0,0 +1,32 @@ +/* + * @lc app=leetcode.cn id=784 lang=java + * + * [784] 字母大小写全排列 + */ + +class LeetCode_784_038 { + public List letterCasePermutation(String S) { + List ans = new ArrayList<>(); + StringBuilder root = new StringBuilder(S); + dfs(ans, root, 0); + return ans; + } + + private void dfs(List ans, StringBuilder root, int index) { + if (index == root.length()) { + ans.add(root.toString()); + return; + } + char c = root.charAt(index); + if ('a' <= c && c <= 'z') { + root.setCharAt(index, (char) (c - 32)); + dfs(ans, root, index + 1); + root.setCharAt(index, c); + } else if ('A' <= c && c <= 'Z') { + root.setCharAt(index, (char) (c + 32)); + dfs(ans, root, index + 1); + root.setCharAt(index, c); + } + dfs(ans, root, index + 1); + } +}