NC_72_Tree_Mirror NC_73_MoreThanHalfNum NC_74_GET_NUMBER_OF_K
package org.example.interview.practice; /** * @author xianzhe.ma * @date 2021/8/15 */ public class NC_72_Tree_Mirror { public TreeNode Mirror (TreeNode pRoot) { // write code here if (pRoot == null) { return pRoot; } TreeNode left = pRoot.left; TreeNode right = pRoot.right; pRoot.right = reserver(left); pRoot.left = reserver(right); return pRoot; } private TreeNode reserver(TreeNode node) { if (node == null) { return node; } TreeNode temp = reserver(node.left); node.left = reserver(node.right); node.right = temp; return node; } public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } }
package org.example.interview.practice; import java.util.Arrays; /** * @author xianzhe.ma * @date 2021/11/6 */ public class NC_73_MoreThanHalfNum { public int MoreThanHalfNum_Solution(int [] array) { Arrays.sort(array); // 返回数组中间的数字 return array[array.length / 2]; } }
package org.example.interview.practice; /** * @author xianzhe.ma * @date 2021/8/14 */ public class NC_74_GET_NUMBER_OF_K { public static int GetNumberOfK(int [] array , int k) { int index = binarySearch(array, 0, array.length-1, k); if (index == -1) { return 0; } while (index >= 0 && array[index] == k ) { index --; } index ++; int count = 0; while (index < array.length && array[index] == k) { count++; index++; } return count; } public static int binarySearch(int[] array, int left, int right, int k) { if (left > right) { return -1; } int mid = (left + right) / 2; int value = array[mid]; if (value == k) { return mid; } if (k < value) { return binarySearch(array, left, mid - 1, k); } else { return binarySearch(array, mid + 1, right, k); } } public static void main (String[] args) { int[] arrays = {1,2,3,3,3,3}; int count = GetNumberOfK(arrays, 3); System.out.println(count); } }