java笔试题

来源:互联网 发布:mac向iphone传照片 编辑:程序博客网 时间:2024/06/03 14:41
给定一个字符串S,求所有长度小于等于3的子串出现次数,输出结果按出现从大到小排序,如果次数相同,按字典排序。比如,给定字符串"abcdab",输出结果为
a 2
ab 2
b 2
abc 1
bc 1
bcd 1
c 1
cd 1
cda 1
d 1
da 1

dab 1

package cn.bjut.test;import java.util.*;/** * Created by N3verL4nd on 2017/6/6. */public class Test {    private static void printSubString(String s) {        if (s != null) {            final Map<String, Integer> map = new LinkedHashMap<>();            List<String> list = new ArrayList<>();            for (int start = 0; start < s.length(); start++)            {                for (int end = start + 1; end <= start + 3 && end <= s.length(); end++)                {                    String subString = s.substring(start, end);                    if (map.containsKey(subString)) {                        map.put(subString, map.get(subString) + 1);                    } else {                        map.put(subString, 1);                        list.add(subString);                    }                }            }            list.sort((o1, o2) -> {                int i = map.getOrDefault(o1, 0);                int j = map.getOrDefault(o2, 0);                return i == j ? o1.compareTo(o2) : (j - i);            });            for (String str : list) {                System.out.println(str + " " + map.get(str));            }        }    }    public static void main(String[] args) {        String s = "abcdab";        printSubString(s);    }}


以下的程序实现了链表的数据结构,其核心功能的函数需要补全,请仔细阅读全部代码后完成代码片段。

package cn.bjut.test;/** * Created by N3verL4nd on 2017/6/7. */public class Link {    private static final int NODE_COUNT = 10;    private static class Node {        private int data;        private Node next;        public Node(int data, Node next) {            this.data = data;            this.next = next;        }    }    private static Node Create(int count) {        Node list = null;        for (int i = count; i > 0; i--) {            list = new Node(i, list);        }        return list;    }    private static void print(Node list) {        while (list != null) {            System.out.print(list.data + " ");            list = list.next;        }        System.out.println();    }    private static Node reverse(Node list) {        Node reverseList = null;        while (list != null) {            Node p = list.next;            list.next = null;            reverseList = new Node(list.data, reverseList);            list = p;        }        return reverseList;    }    public static void main(String[] args) {        Node list = Create(NODE_COUNT);        print(list);        Node reverseList = reverse(list);        print(reverseList);    }}



原创粉丝点击