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

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]]

猜你喜欢

转载自blog.csdn.net/qq_42785250/article/details/133150068