• NC_95_MAX_SEQUENCE_SUBARR NC_96_LINKLIST_PAROLINE NC_97_TOPK_Strings


    package org.example.interview.practice;
    
    import java.util.HashSet;
    import java.util.Set;
    
    /**
     * @author xianzhe.ma
     * @date 2021/7/24
     */
    
    public class NC_95_MAX_SEQUENCE_SUBARR {
    
        public int MLS (int[] arr) {
            // write code here
            //先把数组放到集合set中
            Set<Integer> set = new HashSet<>();
            for (int num : arr)
                set.add(num);
            int longest = 0;//记录最长的有序序列
            for (int num : arr) {
                //这里要找有序序列最小的元素(不一定是最长
                //有序序列的)。如果还有更小的,说明当前元素
                //不是最小的,直接跳过
                if (set.contains(num - 1))
                    continue;
                //说明当前元素num是当前序列中最小的元素(这里
                //的当前序列不一定是最长的有序序列)
                int currentNum = num;
                //统计当前序列的长度
                int count = 1;
                while (set.contains(currentNum + 1)) {
                    currentNum++;
                    count++;
                }
                //保存最长的值
                longest = Math.max(longest, count);
            }
            return longest;
        }
    }
    package org.example.interview.practice;
    
    import java.util.ArrayDeque;
    import java.util.Deque;
    
    /**
     * @author xianzhe.ma
     * @date 2021/7/23
     */
    
    public class NC_96_LINKLIST_PAROLINE {
    
        public boolean isPail (ListNode head) {
            // write code here
    
            Deque<ListNode> stack = new ArrayDeque<>() ;
            ListNode newHead = head;
            while (newHead != null) {
                stack.push(newHead);
                newHead = newHead.next;
            }
            while (head != null) {
                ListNode node = stack.pop();
                if (node.val != head.val) {
                    return false;
                }
                head = head.next;
            }
            return true;
        }
    
        public static class ListNode {
            int val;
            ListNode next = null;
    
            public ListNode(int val) {
                this.val = val;
            }
        }
    
    
    }
    package org.example.interview.practice;
    
    import java.util.*;
    
    /**
     * @author xianzhe.ma
     * @date 2021/7/22
     */
    
    public class NC_97_TOPK_Strings {
    
        public static String[][] topKstrings (String[] strings, int k) {
            // write code here
            Map<String, Integer> map = new HashMap<>();
            for (String str: strings) {
                if (!map.containsKey(str)) {
                    map.put(str, 1);
                } else {
                    map.put(str, map.get(str) + 1);
                }
    
            }
    
            PriorityQueue<Node> queue = new PriorityQueue( new Comparator<Node>() {
                public int compare(Node o1, Node o2) {
                    return o1.compareTo(o2);
                }
            });
    
            Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
            for (Map.Entry<String, Integer> entry : entrySet) {
                String str = entry.getKey();
                Integer count = entry.getValue();
                Node node = new Node(str, count);
                queue.add(node);
            }
            String[][] result = new String[k][2];
            for (int i=0;i<k;i++) {
                Node node = queue.poll();
                String str = node.str;
                String count = node.count.toString();
                String[] arr = new String[2];
                arr[0] = str;
                arr[1] = count;
                result[i] = arr;
            }
            return result;
    
        }
    
        public static void main (String[] args) {
            String[] input = {"1","1","2","3"};
            Integer k = 2;
            topKstrings(input, 2);
        }
        private static class Node implements Comparable<Node>{
            public String str;
            public Integer count;
    
            public Node (String str, Integer count) {
                this.str = str;
                this.count = count;
            }
    
    
            @Override
            public int compareTo(Node o) {
                if (this.count > o.count) {
                    return -1;
                } else if (this.count < o.count) {
                    return 1;
                }
                return this.str.compareTo(o.str);
            }
        }
    }
  • 相关阅读:
    用sed删除文件中指定行
    传输文件到docker容器
    RAID技术全解图解-RAID0、RAID1、RAID5、RAID100
    Best PDF Document Viewers for Linux Systems
    nvidia docker install
    cuda apt install
    Ubuntu16_18建立返回桌面、显示桌面的快捷图标的特殊方法
    Linux Shell sort排序常用命令
    linux cut用法
    DispatcherServlet的作用
  • 原文地址:https://www.cnblogs.com/juniorMa/p/15882139.html
Copyright © 2020-2023  润新知