NC_111_MAXVALUE NC_113_CHECK_IP NC_119_SMALLEST_K
package org.example.interview.practice; import java.util.Arrays; /** * @author xianzhe.ma * @date 2021/8/14 */ public class NC_111_MAXVALUE { public String solve (int[] nums) { // write code here if(nums == null || nums.length == 0) return ""; int n = nums.length; String[] strs = new String[n]; for(int i = 0; i < n; i++){ strs[i] = nums[i]+""; } Arrays.sort(strs,(s1, s2)->{ return (s2+s1).compareTo(s1+s2); /*降序排序*/ }); if(strs[0].equals("0")) return "0"; StringBuffer sb = new StringBuffer(); for(String num:strs) sb.append(num); return sb.toString(); } }
package org.example.interview.practice; /** * @author xianzhe.ma * @date 2021/11/6 */ public class NC_113_CHECK_IP { public static String solve (String IP) { // write code here if (IP.contains(".")) { String[] array = IP.split("\\."); if (array == null || array.length != 4) { return "Neither"; } String mode = "[0-9]*"; for (String str : array) { if (str.startsWith("0") || str.startsWith("00") || str.equals("000")) { return "Neither"; } if (!str.matches(mode)) { return "Neither"; } if (Integer.valueOf(str).compareTo(Integer.valueOf(255)) > 0) { return "Neither"; } } return "IPv4"; } String mode = "[0-9]*[A-F]*[a-f]*[0-9]*[A-F]*[a-f]*"; if (IP.contains(":")) { String[] array = IP.split(":"); if (array == null || array.length != 8) { return "Neither"; } for (String str : array) { if (str.length() > 4) { return "Neither"; } if (!str.matches(mode)) { return "Neither"; } } return "IPv6"; } return "Neither"; } public static void main (String[] args) { String input = "2001:0db8:85a3:0:0:8A2E:0370:7334"; solve(input); } }
package org.example.interview.practice; import java.util.ArrayList; import java.util.Comparator; import java.util.PriorityQueue; /** * @author xianzhe.ma * @date 2021/7/7 */ public class NC_119_SMALLEST_K { public static ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) { ArrayList<Integer> list = new ArrayList<>(); if (input == null || input.length == 0 || k > input.length || k == 0) return list; int[] arr = new int[k];//数组下标0的位置作为哨兵,不存储数据 //初始化数组 for (int i = 0; i < k; i++) arr[i] = input[i]; buildMaxHeap(arr, k);//构造大根堆 for (int i = k; i < input.length; i++) { if (input[i] < arr[0]) { arr[0] = input[i]; adjustDown(arr, 0, k);//将改变了根节点的二叉树继续调整为大根堆 } } for (int i = 0; i < arr.length; i++) { list.add(arr[i]); } return list; } /** * @Author: ZwZ * @Description: 构造大根堆 * @Param: [arr, length] length:数组长度 作为是否跳出循环的条件 * @return: void * @Date: 2020/1/30-22:06 */ public static void buildMaxHeap(int[] arr, int length) { if (arr == null || arr.length == 0 || arr.length == 1) return; for (int i = (length - 1) / 2; i >= 0; i--) { adjustDown(arr, i, arr.length); } } /** * @Author: ZwZ * @Description: 堆排序中对一个子二叉树进行堆排序 * @Param: [arr, k, length] * @return: * @Date: 2020/1/30-21:55 */ public static void adjustDown(int[] arr, int k, int length) { int value = arr[k];//哨兵 for (int i = 2 * k; i < length; i *= 2) { if (i < length - 1 && arr[i] < arr[i + 1]) i++;//取k较大的子结点的下标 if (i > length - 1 || value >= arr[i]) break; else { arr[k] = arr[i]; k = i; //向下筛选 } } arr[k] = value; } public static void main (String[] args) { int[] arr = {14,5,1,6,12,7,3,8}; ArrayList<Integer> result = GetLeastNumbers_Solution2(arr, 4); System.out.println(result.toString()); } public static ArrayList<Integer> GetLeastNumbers_Solution2(int [] input, int k) { ArrayList<Integer> list = new ArrayList<>(); if (input == null || input.length == 0 || k > input.length || k == 0) return list; PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(k, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2-o1; } }); //初始化数组 for (int value : input) { priorityQueue.add(value); } for (int i=k;i<input.length;i++) { priorityQueue.poll(); } for (Integer value : priorityQueue) { list.add(value); } return list; } }