NC_78_REVERSE_LIST NC_81_BINARY_TREE_KTH_SMALL NC_82_MAX_IN_WINDOW NC_83_SEQUENCEMAXTIMES
package org.example.interview.practice; /** * @author xianzhe.ma * @date 2021/11/9 */ public class NC_78_REVERSE_LIST { public ListNode ReverseList(ListNode head) { if (head == null) return head; ListNode next = head.next; head.next = null; while (next != null) { ListNode temp = next.next; next.next = head; head = next; next = temp; } return head; } public static class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } } }
package org.example.interview.practice; import com.sun.jmx.remote.internal.ArrayQueue; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Queue; /** * @author xianzhe.ma * @date 2021/8/21 */ public class NC_81_BINARY_TREE_KTH_SMALL { public static TreeNode KthNode(TreeNode pRoot, int k) { if ( k == 0) return null; LinkedList<TreeNode> list = new LinkedList<>(); putArray(pRoot, k, list); if (list.size() < k) return null; return list.getLast(); } private static void putArray(TreeNode pRoot, int k, List<TreeNode> list) { if (list.size() == k) { return; } if (pRoot == null) { return; } putArray(pRoot.left,k,list); if (list.size() == k) { return; } list.add(pRoot); if (list.size() == k) { return; } putArray(pRoot.right,k,list); } 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 = makeTree(); KthNode(root, 3); } public static TreeNode makeTree() { TreeNode node5 = new TreeNode(5); TreeNode node3 = new TreeNode(3); TreeNode node7 = new TreeNode(7); TreeNode node2 = new TreeNode(2); TreeNode node4 = new TreeNode(4); TreeNode node6 = new TreeNode(6); TreeNode node8 = new TreeNode(8); node5.left = node3; node5.right = node7; node3.left = node2; node3.right = node4; node7.left = node6; node7.right = node8; return node5; } }
package org.example.interview.practice; import java.util.ArrayList; /** * @author xianzhe.ma * @date 2021/8/14 */ public class NC_82_MAX_IN_WINDOW { /** * 输入: * [2,3,4,2,6,2,5,1],3 * 复制 * 返回值: * [4,4,6,6,6,5] * @param num * @param size * @return */ public ArrayList<Integer> maxInWindows(int [] num, int size) { int length = num.length; ArrayList<Integer> list = new ArrayList<>(); if (size == 0 || size > length) { return new ArrayList(); } if (length <= size) { list.add(max(num,0,length)); return list; } int stop = length - size; for (int i=0; i<=stop; i++) { list.add(max(num,i,i+size)); } return list; } private int max(int[] num,int left, int right) { int max = Integer.MIN_VALUE; for (int i=left;i<right;i++) { if (num[i] > max) { max = num[i]; } } return max; } }
package org.example.interview.practice; /** * @author xianzhe.ma * @date 2021/11/7 * 连续子数组乘积最大 */ public class NC_83_SEQUENCEMAXTIMES { public static double maxProduct(double[] arr) { int length = arr.length; //特殊值(空数组)处理 if(length == 0){ return 0; } //数组长度为1 if(length == 1){ return arr[0]; } double[] max_dp = new double[length];//最大值数组,max_dp[i]表示前i个数中的最大乘积 double[] min_dp = new double[length];//最小值数组 max_dp[0] = arr[0]; min_dp[0] = arr[0]; double max = arr[0]; for(int i=1;i<length;i++){ //动态规划,最优子结构,状态转换方程。 //使用两个dp数组是因为元素中的数可以为负数,有负号影响(负数*最小值也可能是最大值)。 //最大值可以有三个来源: //一是数组当前的值(arr[i]); //二是数组当前的值(arr[i])*max_dp[i-1],即是正数*最大值; //三是数组当前的值(arr[i])*min_dp[i-1],即是负数*最小值; //最大值来源不能是max_dp[i-1]的原因是题目要求的是子数组(连续)的最大乘积。 max_dp[i] = Math.max(Math.max(min_dp[i-1]*arr[i],arr[i]),max_dp[i-1]*arr[i]); min_dp[i] = Math.min(Math.min(max_dp[i-1]*arr[i],arr[i]),min_dp[i-1]*arr[i]); if(max_dp[i] > max){ max = max_dp[i]; } } return max; } public static void main(String[] args) { double[] array = {-2.5,4,0,3,0.5,8,-1}; maxProduct(array); } }