必修 -- 常用笔试题

冒泡排序

java 复制代码
public class BubbleSort {

    public static void main(String[] args) {
        int[] numbers = {64, 34, 25, 12, 22, 11, 90};
        bubbleSort(numbers);
        for (int number : numbers) {
            System.out.print(number + " ");
        }
    }


    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }

    }
}

二分算法

java 复制代码
 public static int binarySearch(int[] arr, int target) {
        int left = 0, right = arr.length - 1;

        while (left <= right) {
            int mid = left + (right - left) / 2; //拿到二分下表

            if (arr[mid] == target) {
                return mid;
            }
            if (arr[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }

        }
        return -1;
    }

大文件拆分合并

java 复制代码
/**
 * Author: 徐寿春
 * Date:    2024/11/15 10:33
 * <p>
 * 名称
 */
public class FileSplitter {


    public static void splitFile(File inputFile, int chunkSize) throws Exception {
        @Cleanup FileInputStream fis = new FileInputStream(inputFile);
        @Cleanup BufferedInputStream bis = new BufferedInputStream(fis);
        byte[] buffer = new byte[chunkSize];
        String fileName = inputFile.getName();
        int tmp;
        while ((tmp = bis.read()) != -1) {
            String filePartName = String.format("%s.%03d", fileName, tmp);

            //创建小文件
            File newFile = new File(inputFile.getParent(), filePartName);

            @Cleanup FileOutputStream out = new FileOutputStream(newFile);
            out.write(buffer, 0, tmp); // tmp是实际读取的字节数

        }

    }

    public static void mergeFiles(File[] inputFiles, File outputFile) throws Exception {
        @Cleanup FileOutputStream fos = new FileOutputStream(outputFile);
        @Cleanup BufferedOutputStream mergingStream = new BufferedOutputStream(fos);

        for (File file : inputFiles) {
            @Cleanup FileInputStream fis = new FileInputStream(file);
            @Cleanup BufferedInputStream mingStream = new BufferedInputStream(fis);

            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = mingStream.read(buffer)) != -1) {
                mergingStream.write(buffer, 0, bytesRead);
            }
        }
    }



    public static void main(String[] args) throws Exception {
        File inputFile = new File("path/to/large/file.txt");
        int chunkSize = 1024 * 1024 * 10; // 10MB
        splitFile(inputFile, chunkSize);


        File outputFile = new File("path/to/output/file.txt");
        File[] inputFiles = new File[]{
                new File("path/to/large/file.txt.001"),
                new File("path/to/large/file.txt.002"),
                // ...
        };
        mergeFiles(inputFiles, outputFile);
    }
}

统计并返回出现次数最多的单词及其出现次数

给定一个包含多行文本的字符串(每行以换行符\n分隔),要求编写一个Java方法来实现以下功能:统计并返回字符串中所有单词的总数(假设单词由空格分隔,且单词之间可以有多个空格,单词只有英文字符)。统计并返回出现次数最多的单词及其出现次数(如果有多个单词出现次数相同且最多,则返回其中任意一个)。统计并返回字符串中所有不同单词的数量。

例子:String text = "Hello world. This is a test.\n Another test, with Hello again.";

java 复制代码
/**
 * Author: 徐寿春
 * Date:    2024/11/15 11:09
 * <p>
 * 名称
 */
public class WordStatistics {

    public static void wordStatistics(String str) {
        Map<String, Long> collect = Arrays.stream(str.split("")).filter(i ->
                !Objects.equals(i, ",")
                        && !Objects.equals(i, ".")
                        && Strings.isNotBlank(i)
        ).collect(Collectors.groupingBy(String::toLowerCase, Collectors.counting()));

        collect.entrySet().stream().max(Map.Entry.comparingByValue()).ifPresent(i -> {
            System.out.println("出现最多的单词 = " + i.getKey());
            System.out.println("出现最多的数量 = " + i.getValue());
        });

        System.out.println("共出现的数量 = " + collect.entrySet().size());
    }


    public static void main(String[] args) {
        String text = "Hello world. This is a test.\n Another test, with Hello again.";
        wordStatistics(text);

    }
}
相关推荐
苹果醋313 分钟前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx
Hello.Reader33 分钟前
深入解析 Apache APISIX
java·apache
菠萝蚊鸭1 小时前
Dhatim FastExcel 读写 Excel 文件
java·excel·fastexcel
旭东怪1 小时前
EasyPoi 使用$fe:模板语法生成Word动态行
java·前端·word
007php0071 小时前
Go语言zero项目部署后启动失败问题分析与解决
java·服务器·网络·python·golang·php·ai编程
∝请叫*我简单先生1 小时前
java如何使用poi-tl在word模板里渲染多张图片
java·后端·poi-tl
ssr——ssss1 小时前
SSM-期末项目 - 基于SSM的宠物信息管理系统
java·ssm
一棵星2 小时前
Java模拟Mqtt客户端连接Mqtt Broker
java·开发语言
鲤籽鲲2 小时前
C# Random 随机数 全面解析
android·java·c#
zquwei2 小时前
SpringCloudGateway+Nacos注册与转发Netty+WebSocket
java·网络·分布式·后端·websocket·网络协议·spring