NC_121_Permutation NC_122_MATCH_REGEX NC_123_TREE_SERIALIZE NC_125_MAX_LEN_SUB_ARRAY_EQUALSK
package org.example.interview.practice; /** * @author xianzhe.ma * @date 2021/11/3 */ import java.util.*; public class NC_121_Permutation { public ArrayList<String> Permutation(String str) { int length = str.length(); ArrayList<String> inputList = new ArrayList<>(); for (int i = 0;i<length;i++) { char c = str.charAt(i); String temp = String.valueOf(c); inputList.add(temp); } String[] array = new String[length]; inputList.toArray(array); ArrayList<String> result = new ArrayList<>(); doPerm(array, 0, length, result); Set<String> set = new HashSet<>(); set.addAll(result); ArrayList<String> result2 = new ArrayList<>(); result2.addAll(set); return result2; } public void doPerm(String[] array, int start, int end, ArrayList<String> result) { if (start == end) { int size = array.length; StringBuilder stringBuilder = new StringBuilder(); for (int i=0;i<size;i++) { stringBuilder.append(array[i]); } result.add(stringBuilder.toString()); return; } for (int i= start;i<end;i++) { swap(array, start, i); doPerm(array,start+1,end, result); swap(array, start, i); } } public void swap(String[] array, int i, int j) { String temp = array[i]; array[i] = array[j]; array[j] = temp; } }
package org.example.interview.practice; /** * @author xianzhe.ma * @date 2021/8/22 */ public class NC_122_MATCH_REGEX { public static boolean match (String str, String pattern) { // write code here int n = str.length(); int m = pattern.length(); boolean[][] f = new boolean[n + 1][m + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { //分成空正则和非空正则两种 if (j == 0) { f[i][j] = i == 0; } else { //非空正则分为两种情况 * 和 非* if (pattern.charAt(j - 1) != '*') { if (i > 0 && (str.charAt(i - 1) == pattern.charAt(j - 1) || pattern.charAt(j - 1) == '.')) { f[i][j] = f[i - 1][j - 1]; } } else { //碰到 * 了,分为看和不看两种情况 //不看 if (j >= 2) { f[i][j] |= f[i][j - 2]; } //看 if (i >= 1 && j >= 2 && (str.charAt(i - 1) == pattern.charAt(j - 2) || pattern.charAt(j - 2) == '.')) { f[i][j] |= f[i - 1][j]; } } } } } return f[n][m]; } public static void main (String[] args) { String str = "aaa"; String pattern = "aa*"; match(str, pattern); } }
package org.example.interview.practice; import java.util.HashMap; import java.util.Map; /** * @author xianzhe.ma * @date 2021/12/31 * 和为K的连续子数组 */ public class NC_125_MAX_LEN_SUB_ARRAY_EQUALSK { public static int maxlenEqualK(int[] arr, int k) { // write code here int len = arr.length; if (arr == null || len == 0) { return 0; } Map<Integer, Integer> map = new HashMap<Integer, Integer>(); map.put(0, -1); int length = 0; int sum = 0; for (int i = 0; i < arr.length; i++) { sum += arr[i]; if (map.containsKey(sum - k)) { length = Math.max(i - map.get(sum - k), length); } if (!map.containsKey(sum)) { map.put(sum, i); } } return length; } public static void main(String[] args) { int[] arr = {0,1,2,3}; maxlenEqualK(arr, 3); } }
import java.util.*; /* public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class NC_123_TREE_SERIALIZE{ int INF = 0x3f3f3f3f; TreeNode emptyNode = new TreeNode(INF); String Serialize(TreeNode root) { if (root == null) return ""; StringBuilder sb = new StringBuilder(); // 使用队列进行层序遍历,起始先将 root 放入队列 Deque<TreeNode> d = new ArrayDeque<>(); d.addLast(root); while (!d.isEmpty()) { // 每次从队列中取出元素进行「拼接」,包括「正常节点」和「叶子节点对应的首位空节点」 TreeNode poll = d.pollFirst(); sb.append(poll.val + "_"); // 如果取出的节点不为「占位节点」,则继续往下拓展,同时防止「占位节点」不继续往下拓展 if (!poll.equals(emptyNode)) { d.addLast(poll.left != null ? poll.left : emptyNode); d.addLast(poll.right != null ? poll.right : emptyNode); } } return sb.toString(); } TreeNode Deserialize(String data) { if (data.equals("")) return null; // 根据分隔符进行分割 String[] ss = data.split("_"); int n = ss.length; // 怎么序列化就怎么反序列化 // 使用队列进行层序遍历,起始先将 root 构建出来,并放入队列 TreeNode root = new TreeNode(Integer.parseInt(ss[0])); Deque<TreeNode> d = new ArrayDeque<>(); d.addLast(root); for (int i = 1; i < n - 1; i += 2) { TreeNode poll = d.pollFirst(); // 每次从中取出左右节点对应 val int a = Integer.parseInt(ss[i]), b = Integer.parseInt(ss[i + 1]); // 如果左节点对应的值不是 INF,则构建「真实节点」 if (a != INF) { poll.left = new TreeNode(a); d.addLast(poll.left); } // 如果右节点对应的值不是 INF,则构建「真实节点」 if (b != INF) { poll.right = new TreeNode(b); d.addLast(poll.right); } } return root; } }