ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

NC_95_MAX_SEQUENCE_SUBARR NC_96_LINKLIST_PAROLINE NC_97_TOPK_Strings

2022-02-11 10:32:06  阅读:175  来源: 互联网

标签:count String SEQUENCE int MAX NC str new public


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);
        }
    }
}

 

标签:count,String,SEQUENCE,int,MAX,NC,str,new,public
来源: https://www.cnblogs.com/juniorMa/p/15882139.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有