NC_56_NUMBER_PALINDROME NC_59_MIN_MATRIX_PATH NC_60_IS_TREE_SEARCH_AND_COMPLETE
package org.example.interview.practice; import java.util.ArrayList; import java.util.Arrays; /** * @author xianzhe.ma * @date 2021/9/7 */ public class NC_56_NUMBER_PALINDROME { public boolean isPalindrome (int x) { // write code here if (x < 0) { return false; } String str = String.valueOf(x); int left = 0; int right = str.length() - 1; while (left < right) { char leftChar = str.charAt(left); char rightChar = str.charAt(right); if (leftChar != rightChar) { return false; } left++; right--; } return true; } }
package org.example.interview.practice; /** * @author xianzhe.ma * @date 2021/7/24 */ public class NC_59_MIN_MATRIX_PATH { public static int minPathSum (int[][] matrix) { // write code here int[][] dp = new int[matrix.length][matrix[0].length]; dp[0][0] = matrix[0][0]; dp[0][1] = matrix[0][1] + matrix[0][0]; dp[1][0] = matrix[1][0] + matrix[0][0]; int colCount = matrix[0].length; for (int j=1;j<colCount;j++) { dp[0][j] = matrix[0][j] + dp[0][j-1]; } for (int j=1;j<matrix.length;j++) { dp[j][0] = matrix[j][0] + dp[j-1][0]; } for (int i=1;i<matrix.length;i++) { for (int j=1;j<colCount;j++) { dp[i][j] = Math.min(dp[i][j-1] + matrix[i][j], dp[i-1][j] + matrix[i][j]); } } return dp[matrix.length-1][colCount-1]; } public static void main (String[] args) { int[][] matrix = {{1,3,5,9},{8,1,3,4},{5,0,6,1},{8,8,4,0}}; System.out.println(minPathSum(matrix)); } }
package org.example.interview.practice; import java.util.Deque; import java.util.LinkedList; /** * @author xianzhe.ma * @date 2021/8/28 */ public class NC_60_IS_TREE_SEARCH_AND_COMPLETE { public boolean[] judgeIt (TreeNode root) { // write code here boolean[] res = new boolean[2]; res[0] = isSerachTreeBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE); res[1] = isAllTreeBST(root); return res; } //判断搜索树 public boolean isSerachTreeBST(TreeNode root, Integer left, Integer right){ if(root == null) return true; if(root.val <= left || root.val >= right) return false; return isSerachTreeBST(root.left, left, root.val) && isSerachTreeBST(root.right, root.val, right); } //判断完全树 public static boolean isAllTreeBST(TreeNode root){ if(root == null) return true; Deque<TreeNode> queue = new LinkedList<>(); queue.offer(root); TreeNode left = null; TreeNode right = null; boolean flag = false; //标记是否遇到节点不双全的节点 while(!queue.isEmpty()){ root = queue.poll(); left = root.left; right = root.right; //遇到左右孩子不双全的节点并且该节点不是叶子节点的时候就不是完全二叉树 //左孩子为空并且右孩子不为空的时候不是完全二叉树 if((flag && !(left == null && right == null)) || (left == null && right != null)){ return false; } if(left != null) queue.offer(left); if(right != null) queue.offer(right); if(left == null || right == null) flag = true; } return true; } public static class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } public static void main (String[] args) { TreeNode root = new TreeNode(1); TreeNode node2 = new TreeNode(2); TreeNode node3 = new TreeNode(3); TreeNode node4 = new TreeNode(4); // TreeNode node5 = new TreeNode(5); // TreeNode node6 = new TreeNode(6); root.left = node2; root.right = node3; node2.left = node4; // node2.right = node5; node3.left = node6; Boolean ret = isAllTreeBST(root); } public static boolean isAllTreeBST2(TreeNode root){ if(root == null) return true; Deque<TreeNode> queue = new LinkedList<>(); queue.push(root); TreeNode left = null; TreeNode right = null; boolean flag = false; //标记是否遇到节点不双全的节点 while (!queue.isEmpty()) { root = queue.poll(); left = root.left; right = root.right; if ((flag && !(left == null && right == null)) || (left == null && right != null)) { return false; } if (left != null) queue.offer(left); if (right != null) queue.offer(right); if (left == null || right == null) flag = true; } return true; } }