zoukankan      html  css  js  c++  java
  • 笔试01

    1.判断一个字符串中的最长回文串

    package exam;
    
    import java.util.Scanner;
    
    
    public class Main {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner sc = new Scanner(System.in);
    
            String str = sc.nextLine();
    
            System.out.println(getResultString(str));
        }
    
        private static String getResultString(String s) {
            int len = s.length();
            if (len == 0) {
                return "";
            }
            //用来记录最长回文串的长度
            int length=0;
            //用来记录最长回文串下标
            int left=0,right=0;
            //这里的二维数组下标别记错了,j是算横坐标,i算纵坐标,使用的是上三角矩阵
            boolean[][] dp =new boolean[s.length()][s.length()];
    
            for ( int i=0;i<s.length();i++){
                for (int j=0;j<=i;j++){
                    if (i==j){
                        dp[j][i]=true;
                    }else if (j+1==i){
                        dp[j][i]=(s.charAt(i)==s.charAt(j));
                    }else if (i-j>=2){
                        dp[j][i]=(s.charAt(i) == s.charAt(j)&&dp[j+1][i-1]);
                    }
                    if (dp[j][i]&&length<i-j+1){
                        length =i-j+1;
                        left = j;
                        right = i;
                    }
                }
            }
            //beginIndex -- 起始索引(包括), 索引从 0 开始,endIndex -- 结束索引(不包括)
            return s.substring(left,right+1);
        }
    
        
    }

    2.求一个序列中缺失哪个数字

    package kaoshi;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;
    
    public class Main {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            List<Integer> list = new ArrayList<Integer>();
            while (sc.hasNext()) {
                list.add(sc.nextInt());
            }
            System.out.println(lack(list));
    
            //
        }
    
        private static Integer lack(List<Integer> source) {
            int temp = 0;
            for (int i = 0; i < source.size(); i++) {
                temp = temp ^ (i + 1) ^ source.get(i);
            }
            return temp;
    
        }
    
    }

    3.统计一个字符串中字符出现的次数

    package exam;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;
    
    public class CountCharacter {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner in = new Scanner(System.in);
            String s = in.nextLine();
            Map<Character, Integer> map = new HashMap<Character, Integer>();
            char[] list = s.toCharArray();
    
            for (char ch : list) {
                if(map.containsKey(ch)) {
                    int num = map.get(ch);
                    num = num + 1;
                    map.put(ch, num);
                }else {
                    map.put(ch, 1);
                }
            }
            System.out.println(map);
        }
    
    }

    4.统计字符串中出现次数最多的k个字符

    package exam;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Scanner;
    
    public class CountCharacter {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner in = new Scanner(System.in);
            String s = in.nextLine();
            Map<Character, Integer> map = new HashMap<Character, Integer>();
            char[] list = s.toCharArray();
            int max = 0;
            for (char ch : list) {
                if (map.containsKey(ch)) {
                    int num = map.get(ch);
                    num = num + 1;
                    map.put(ch, num);
                } else {
                    map.put(ch, 1);
                }
            }
    
            List<Map.Entry<Character, Integer>> list2 = new ArrayList<Map.Entry<Character, Integer>>(map.entrySet());
            Collections.sort(list2, new Comparator<Map.Entry<Character, Integer>>() {
    
                @Override
                public int compare(Entry<Character, Integer> o1, Entry<Character, Integer> o2) {
                    // TODO Auto-generated method stub
                    return o2.getValue().compareTo(o1.getValue());
                }
            });
    
            for (Map.Entry<Character, Integer> m : list2) {
                System.out.println(m.getKey() + "...." + m.getValue());
            }
        }
    
    }

    5.连续字符串压缩

    package exam;
    
    import java.util.*;
    
    public class CountCharacter {
    
        public static void getNumFromString(String string) {
            StringBuilder sb = new StringBuilder();
            char c = string.charAt(0);
            int count = 1;
            for (int i = 1; i < string.length(); i++) {
                char s = string.charAt(i);
                if (s == c) {
                    count++;
                } else {
                    if (count > 1) {
                        sb.append(count);
                        sb.append(c);
                        count = 1;
                    } else {
                        sb.append(c);
                    }
                }
                c = s;
            }
    
            if (count > 1) {
                sb.append(count);
            }
            sb.append(c);
            System.out.println(sb.toString());
    
        }
    
        public static void main(String[] args) {
            getNumFromString("aabbcaaad");
        }
    }
  • 相关阅读:
    jmeter----Linux上安装与使用
    Jmeter----客户端内存配置修改(Windows)
    Jmeter----生成HTML性能测试报告(Windows)
    nginx
    夜神模拟器 该网站的安全证书有问题 转载:https://bbs.yeshen.com/forum.php?mod=viewthread&tid=8864
    redis慢日志
    Nginx 转载:https://blog.csdn.net/yujing1314/article/details/107000737
    top 转载:https://mp.weixin.qq.com/s/kEY1U-CGIqQ2mKbr1aG8mA
    高可用
    shell 脚本 转载:https://mp.weixin.qq.com/s/rTsLIb2p-AE3oDPQXMQmLw
  • 原文地址:https://www.cnblogs.com/xinmomoyan/p/11536521.html
Copyright © 2011-2022 走看看