【面试题】—— 笔试题(4题)

2023-09-22 09:00:00

1.分割集合

题目:编写一个Java函数,实现批量获取数据的功能(BService.get(List ids))。具体要求如下:
1)提供一个函数BService.get(List ids),支持最多传入100个id;
2)在BService.get((List ids)函数内部,需要将传入的id列表分批(每批10个id)进行调用AService.get(List ids)函数获取数据;
3)BService.get((List ids)函数需要返回所有批次获取的数据的合并结果,即一个包含所有数据的List;

第一种方式:自己实现分割方法

import java.util.List;

public class AService {
    public List<Integer> get(List<Integer> ids){
        //业务处理
        return ids;
    }
}

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.List;

public class BService {

    public static List<Integer> get(List<Integer> ids){
        AService aService = new AService();
        if(ids == null || ids.size() == 0 || ids.size() > 100){
            throw new IllegalArgumentException("传入的ids大于100或者为空!");
        }
        List<Integer> result = new ArrayList<>();

        // 将ids分成大小为10的子列表,然后逐个调用AService.get方法
        for (List<Integer> sublist : partition(ids, 10)) {
            result.addAll(aService.get(sublist));
        }
        return result;

    }

    /**
     * 分割集合
     * @param list 集合
     * @param chunkSize 分割大小
     * @return
     */
    private static List<List<Integer>> partition(List<Integer> list ,Integer chunkSize){
        List<List<Integer>> partitions = new ArrayList<>();
        for (int i = 0; i < list.size(); i += chunkSize) {
            partitions.add(list.subList(i, Math.min(i + chunkSize, list.size())));
        }
        System.out.println(JSONObject.toJSONString(partitions));
        return partitions;
    }

    public static void main(String[] args) {
        ArrayList<Integer> testArray = new ArrayList<>();
        testArray.add(1);
        testArray.add(3);
        testArray.add(4);
        testArray.add(6);
        testArray.add(8);
        testArray.add(6);
        testArray.add(8);
        testArray.add(3);
        testArray.add(8);
        testArray.add(8);
        testArray.add(8);
        testArray.add(8);
        testArray.add(8);
        testArray.add(2);
        testArray.add(8);
        testArray.add(1);
        testArray.add(8);
        get(testArray);
    }
}

结果:

[[1,3,4,6,8,6,8,3,8,8],[8,8,8,2,8,1,8]]

第二种方式:使用第三方依赖

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>28.0-jre</version>
</dependency>
...
//list:集合 chunkSize分成的份数
Lists.partition(list,chunkSize);
...

2.链表计算

给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照顺序的方式存储的,并且每个节点只能存储一位数字。
请你将两个数相减,并以相同形式返回一个表示相减结果的链表。
你可以假设
1)除了数字 0 之外,这两个数都不会以 0 开头。
2)给定的第一个数字一定比第二个数字大。
举例:
输入:l1 = [9,8,7], l2 = [5,1,2]
输出:[4,7,5]
解释:987-512 = 475

public class LinkedCalculate {
    public static LinkedList<Integer> subtract(LinkedList<Integer> l1, LinkedList<Integer> l2) {
        LinkedList<Integer> result = new LinkedList<>();
        //借位
        int borrow = 0;
        while (!l1.isEmpty() || !l2.isEmpty()) {
            int x = !l1.isEmpty() ? l1.removeLast() : 0;
            int y = !l2.isEmpty() ? l2.removeLast() : 0;

            int diff = x - y - borrow;

            if (diff < 0) {
                diff += 10;
                borrow = 1;
            } else {
                borrow = 0;
            }

            result.addFirst(diff);
        }
        // 移除前导零
        while (!result.isEmpty() && result.getFirst() == 0) {
            result.removeFirst();
        }
        return result;
    }
    public static void main(String[] args) {

        LinkedList<Integer> l1 = new LinkedList<>();
        l1.add(9);
        l1.add(8);
        l1.add(1);

        LinkedList<Integer> l2 = new LinkedList<>();
        l2.add(5);
        l2.add(1);
        l2.add(2);
        LinkedList<Integer> result = subtract(l1, l2);
        for (Integer digit : result) {
            System.out.print(digit);
        }
    }
}

3. 字符串计算

提供一个方法,该方法接受两个字符串,这两个字符串是数字。该方法的功能是将这两个字符串数值相加并返回,不能使用类型转换。

public static String addStrings(String num1, String num2) {
        StringBuilder result = new StringBuilder();
        int carry = 0; // 进位初始化为0

        int i = num1.length() - 1;
        int j = num2.length() - 1;

        while (i >= 0 || j >= 0) {
            int x = (i >= 0) ? num1.charAt(i) - '0' : 0;
            int y = (j >= 0) ? num2.charAt(j) - '0' : 0;
            int sum = x + y + carry;
            // 计算进位
            carry = sum / 10; 
            // 添加当前位的数字到结果字符串
            result.append(sum % 10); 
            i--;
            j--;
        }

        if (carry > 0) {
        	// 如果最高位有进位,添加进位到结果字符串
            result.append(carry); 
        }
 		// 最后需要反转字符串得到正确的结果
        return result.reverse().toString();
    }

    public static void main(String[] args) {
        String num1 = "123";
        String num2 = "758";
        String sum = addStrings(num1, num2);
        System.out.println(sum); 

    }

结果:

881

4.找到相加为0的数字

提供一个方法,该方法接受连个数组,方法的功能是将数组中相加为0的数字组成一个新的数组并返回。比如:

int a = {1,2,3};
int b = {-1,-2,-3};
返回:[[-1,1],[-2,2],[-3,3]]
 public static List<List<Integer>> findZeroSumArray(List<Integer> arr1,List<Integer> arr2) {
        List<List<Integer>> result = new ArrayList<>();
        for (int num : arr2) {
            List<Integer> resultList = new ArrayList<>();
            if (arr1.contains(-num)) {
                resultList.add(num);
                resultList.add(-num);
                result.add(resultList);
            }
        }
        return result;
    }


    public static void main(String[] args) {
        List<Integer> arr1 = Arrays.asList(1, 2, 3, -2, -1,-1);
        List<Integer> arr2 = Arrays.asList(-3, 4, -2, 1, 5);
        List<List<Integer>> zeroSumArray = findZeroSumArray(arr1, arr2);
        System.out.println(JSONObject.toJSONString(zeroSumArray));
    }

结果:

[[-3,3],[-2,2],[1,-1]]
更多推荐

实例说明接口测试的关键是什么?(含文档+视频)

接口测试的关键在于验证应用程序接口(API)是否按照预期工作,并且在不同组件之间传输数据的正确性和可靠性。以下是接口测试的一些关键要点,后面会实例说明。1.请求和响应验证:接口测试需要验证发送到API的请求和API返回的响应是否符合预期。这包括检查请求的参数、HTTP状态码和响应的数据结构等方面。2.数据一致性:确保A

JUC下的异步编程工具使用详情以及源码分析(FutureTask、CompletableFuture)

异步编程一、FutureTask应用&源码分析1.1FutureTask介绍FutureTask是一个可以取消异步任务的类。FutureTask对Future做的一个基本实现。可以调用方法区开始和取消一个任务一般是配合Callable去使用异步任务启动之后,可以获取一个绑定当前异步任务的FutureTask可以基于Fu

RK3568开发笔记(八):开发板烧写buildroot固件(支持hdmi屏),搭建Qt交叉编译开发环境,编译一个Demo,目标板运行Demo测试

若该文为原创文章,转载请注明原文出处本文章博客地址:https://hpzwl.blog.csdn.net/article/details/132826197红胖子网络科技博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等)持续更新中…瑞芯微开

QT JSON数据格式讲解

文章目录前言一、JSON是什么二、JSON在线解析三、QT中的JSON类四、构建JSON字符串五、解析JSON数据六.核心类QJsonDocument类详解总结前言本篇文章开始带大家学习一下什么是JSON,并且学习QT当中的JSON使用。一、JSON是什么JSON(JavaScriptObjectNotation)是一

Spring-AOP+入门案例(注解)+AOP切入点语法+AOP通知类型

一、简介+工作流程。简介SpringAop实际上就是代理模式工作流程二、导入依赖1.spring-aop包该包是在spring-context依赖下的子包,所以有context就有aop<dependency><groupId>org.springframework</groupId><artifactId>sprin

Qt day2

作业:点击登录按钮后,判断账号(admin)和密码(123456)是否一致,如果匹配失败,则弹出错误对话框,文本内容“账号密码不匹配,是否重新登录”,给定两个按钮ok和cancel,点击ok后,会清除密码框中的内容,继续进行登录;如果点击cancel按钮,则关闭界面。如果账号和密码匹配,则弹出信息对话框,给出提示信息为

面试算法3:前n个数字二进制形式中1的个数

题目输入一个非负数n,请计算0到n之间每个数字的二进制形式中1的个数,并输出一个数组。例如,输入的n为4,由于0、1、2、3、4的二进制形式中1的个数分别为0、1、1、2、1,因此输出数组[0,1,1,2,1]。分析1很多人在面试的时候都能想到直观的解法,使用一个for循环来计算从0到n的每个整数i的二进制形式中1的个

模拟实现C语言--memcpy函数和memmove函数

模拟实现C语言–memcpy函数和memmove函数文章目录模拟实现C语言--memcpy函数和memmove函数一、memcpy函数和memmove函数1.1memcpy函数是什么1.1memmove函数是什么二、使用示例2.1从起始位置复制2.2从任意位置复制三、模拟实现3.1模拟实现1--memcpy函数3.2针

MySQL数据库——索引(2)-B+Tree、Hash结构,索引分类(聚集索引、二级索引)

目录索引结构(2)B+TreeHash思考索引分类索引分类聚集索引&二级索引查找过程思考索引结构(2)B+TreeB+Tree是B-Tree的变种,我们以一颗最大度数为4的b+树为例,来看一下其结构示意图:我们可以看到两部分:绿色虚线圈起来的部分,是所引部分,仅仅起到索引数据的作用,不存储数据。红色虚线圈起来的部分,是

什么是HTML?

互联网上的应用程序被称为Web应用程序,Web应用程序使用Web文档(网页)来表现用户界面,而Web文档都遵循标准HTML格式。HTML5是最新的HTML标准。之前的版本HTML4.01于1999年发布。20多年过去了,互联网已经发生了翻天覆地的变化,原有的标准已经不能满足各种Web应用程序的需求。本篇带大家一起了解H

CSP-J 2023 入门级 第一轮 阅读程序(3)

【题目】CSP-J2023入门级第一轮阅读程序(3)#include<iostream>#include<cmath>usingnamespacestd;intsolve1(intn){returnn*n;}intsolve2(intn){intsum=0;for(inti=1;i<=sqrt(n);i++){if(n

热文推荐