当前位置: 首页 > 面试经验 >

虾皮笔试4.1

优质
小牛编辑
76浏览
2024-04-01

虾皮笔试4.1

第一题66%(不知道为啥),其他两道都过了

public int[] solution(int[] costs, int coins) {
        int[][] matrix = new int[costs.length][2];
        for (int i = 0; i < matrix.length; i++) {
            matrix[i][0] = costs[i];
            matrix[i][1] = i;
        }

        // 根据价值排序
        Arrays.sort(matrix, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if (o1[0] != o2[0]) {
                    return o1[0] - o2[0];
                }
                return o1[1] - o2[1];
            }
        });

        int index = 0;
        ArrayList<Integer> indexs = new ArrayList<>();

        while (index < matrix.length && coins >= 0) {
            if (coins >= matrix[index][0]) {
                indexs.add(matrix[index][1]);
                coins -= matrix[index++][0];
            } else {
                break;
            }
        }

        // 排序下标
        Collections.sort(indexs);
        int[] res = new int[indexs.size()];

        // 将下标转换为值
        for (int i = 0; i < res.length; i++) {
            res[i] = costs[indexs.get(i)];
        }

        return res;
    }

第二道:

public static ListNode Rotate(ListNode head, int k) {
        if (head == null || head.next == null) {
            return head;
        }

        ListNode dummy = new ListNode(-1);
        dummy.next = head;

        // 遍历链表
        ListNode cur = head;
        int length = 0;
        while (cur != null) {
            length++;
            cur = cur.next;
        }

        k = k % length;
        if (k == 0) {
            return head;
        }

        cur = head;
        ListNode pre = null;
        // 找到断开的前后节点
        for (int i = 0; i < length - k; i++) {
            pre = cur;
            cur = cur.next;
        }

        pre.next = null;
        dummy.next = cur;
        // 让cur指向旋转链表的最后一个节点
        while (cur != null && cur.next != null) {
            cur = cur.next;
        }
        cur.next = head;

        return dummy.next;
    }

第三道:

public static String reverses(String original_str) {
        // 每个元素单独处理之后拼接到一起
        String[] splits = original_str.split(" ");
        StringBuilder res = new StringBuilder();

        // 每次循环处理一个
        for (int i = 0; i < splits.length; i++) {
            // 存储初始字符串,然后变为修改后的字符串
            StringBuilder sb = new StringBuilder(splits[i]);
            // 存储小写字母然后反转之后修改sb
            StringBuilder reverse = new StringBuilder();

            // 获取sb中的小写字母并修改成『.』
            for (int j = 0; j < sb.length(); j++) {
                char c = sb.charAt(j);
                if (Character.isLowerCase(c)) {
                    reverse.append(c);
                    sb.replace(j, j + 1, ".");
                }
            }

            reverse.reverse();
            int index = 0; //
            for (int j = 0; j < sb.length(); j++) {
                char c = sb.charAt(j);
                if (c == '.') {
                    sb.replace(j, j + 1, reverse.substring(index, index + 1));
                    index++;
                }
            }

            res.append(sb);
            res.append(" ");
        }
        // 去除最后一个空格
        res.deleteCharAt(res.length() - 1);
        return res.toString();
    }

 类似资料: