AI刷题题解

155 阅读5分钟
  1. 找单独的数

问题描述

在一个班级中,每位同学都拿到了一张卡片,上面有一个整数。有趣的是,除了一个数字之外,所有的数字都恰好出现了两次。现在需要你帮助班长小C快速找到那个拿了独特数字卡片的同学手上的数字是什么。

要求:

  1. 设计一个算法,使其时间复杂度为 O(n),其中 n 是班级的人数。
  2. 尽量减少额外空间的使用,以体现你的算法优化能力。

测试样例

样例1:

输入:cards = [1, 1, 2, 2, 3, 3, 4, 5, 5]
输出:4
解释:拿到数字 4 的同学是唯一一个没有配对的。

样例2:

输入:cards = [0, 1, 0, 1, 2]
输出:2
解释:数字 2 只出现一次,是独特的卡片。

样例3:

输入:cards = [7, 3, 3, 7, 10]
输出:10
解释:10 是班级中唯一一个不重复的数字卡片。

约束条件

  • 1 ≤ cards.length ≤ 1001
  • 0 ≤ cards[i] ≤ 1000
  • 班级人数为奇数
  • 除了一个数字卡片只出现一次外,其余每个数字卡片都恰好出现两次
public class Main {
    public static int solution(int[] cards) {
        int result = 0; // 初始化结果为0
        for(int i = 0; i < cards.length; i++) {
            // 使用异或运算来找到唯一的数字
            result ^= cards[i];
        }
        return result; // 返回结果
    }

    public static void main(String[] args) {
        // Add your test cases here
        
        System.out.println(solution(new int[]{1, 1, 2, 2, 3, 3, 4, 5, 5}) == 4);
        System.out.println(solution(new int[]{0, 1, 0, 1, 2}) == 2);
    }
}

2.## 徒步旅行中的补给问题

问题描述

小R正在计划一次从地点A到地点B的徒步旅行,总路程需要 N 天。为了在旅途中保持充足的能量,小R每天必须消耗1份食物。幸运的是,小R在路途中每天都会经过一个补给站,可以购买食物进行补充。然而,每个补给站的食物每份的价格可能不同,并且小R最多只能同时携带 K 份食物。

现在,小R希望在保证每天都有食物的前提下,以最小的花费完成这次徒步旅行。你能帮助小R计算出最低的花费是多少吗?


测试样例

样例1:

输入:n = 5 ,k = 2 ,data = [1, 2, 3, 3, 2]
输出:9

样例2:

输入:n = 6 ,k = 3 ,data = [4, 1, 5, 2, 1, 3]
输出:9

样例3:

输入:n = 4 ,k = 1 ,data = [3, 2, 4, 1]
输出:10


public class Main {
    public static int solution(int n, int k, int[] data) {
        // Edit your code here
   // dp数组初始化
   int[] dp = new int[n + 1];
   for (int i = 1; i <= n; i++) {
       dp[i] = Integer.MAX_VALUE; // 初始化为最大值
   }
      int cnt=data[0];
   // 动态规划计算
   dp[0]=1000000000;
   for (int i = 2; i <= n; i++) {
       for (int j = Math.max(0, i - k); j <i; j++) {
            dp[0] = Math.min(dp[0], data[j]);
       }
       cnt+=dp[0];
       dp[0]=Integer.MAX_VALUE;
   }
   return cnt;
}
    
    public static void main(String[] args) {
        // Add your test cases here
        System.out.println(solution(5, 2, new int[]{1,2,3,3,2}) == 9);
    }
}

3.## 数字字符串格式化

问题描述

小M在工作时遇到了一个问题,他需要将用户输入的不带千分位逗号的数字字符串转换为带千分位逗号的格式,并且保留小数部分。小M还发现,有时候输入的数字字符串前面会有无用的 0,这些也需要精简掉。请你帮助小M编写程序,完成这个任务。


测试样例

样例1:

输入:s = "1294512.12412"
输出:'1,294,512.12412'

样例2:

输入:s = "0000123456789.99"
输出:'123,456,789.99'

样例3:

输入:s = "987654321"
输出:'987,654,321'

public class Main {
    public static String solution(String s) {
        // 去除前导零
        s = s.replaceFirst("^0+(?!$)", "");
        
        // 分割整数和小数部分
        String[] parts = s.split("\\.");
        String integerPart = parts[0];
        String decimalPart = parts.length > 1 ? "." + parts[1] : "";
        
        // 格式化整数部分
        StringBuilder formattedInteger = new StringBuilder();
        int count = 0;
        for (int i = integerPart.length() - 1; i >= 0; i--) {
            formattedInteger.append(integerPart.charAt(i));
            count++;
            if (count % 3 == 0 && i != 0) {
                formattedInteger.append(",");
            }
        }
        formattedInteger.reverse();
        
        // 合并结果
        return formattedInteger.toString() + decimalPart;
    }

    public static void main(String[] args) {
        System.out.println(solution("1294512.12412").equals("1,294,512.12412"));
        System.out.println(solution("0000123456789.99").equals("123,456,789.99"));
        System.out.println(solution("987654321").equals("987,654,321"));
    }
}

4.## 数字分组求偶数和

问题描述

小M面对一组从 1 到 9 的数字,这些数字被分成多个小组,并从每个小组中选择一个数字组成一个新的数。目标是使得这个新数的各位数字之和为偶数。任务是计算出有多少种不同的分组和选择方法可以达到这一目标。

  • numbers: 一个由多个整数字符串组成的列表,每个字符串可以视为一个数字组。小M需要从每个数字组中选择一个数字。

例如对于[123, 456, 789],14个符合条件的数为:147 149 158 167 169 248 257 259 268 347 349 358 367 369


测试样例

样例1:

输入:numbers = [123, 456, 789]
输出:14

样例2:

输入:numbers = [123456789]
输出:4

样例3:

输入:numbers = [14329, 7568]
输出:10

public class Main {
    public static int solution(int[] numbers) {
        // 将整数数组转换为字符串数组
        String[] numStrings = new String[numbers.length];
        for (int i = 0; i < numbers.length; i++) {
            numStrings[i] = String.valueOf(numbers[i]);
        }
        
        // 调用递归函数计算符合条件的组合数
        return countEvenSumCombinations(numStrings, 0, 0);
    }

    private static int countEvenSumCombinations(String[] numStrings, int index, int currentSum) {
        // 如果已经处理完所有数字组
        if (index == numStrings.length) {
            // 检查当前和是否为偶数
            return (currentSum % 2 == 0) ? 1 : 0;
        }

        int count = 0;
        // 遍历当前数字组中的每个数字
        for (char digit : numStrings[index].toCharArray()) {
            // 将字符转换为整数
            int num = digit - '0';
            // 递归处理下一个数字组
            count += countEvenSumCombinations(numStrings, index + 1, currentSum + num);
        }

        return count;
    }

    public static void main(String[] args) {
        // 测试用例
        System.out.println(solution(new int[]{123, 456, 789}) == 14);
        System.out.println(solution(new int[]{123456789}) == 4);
        System.out.println(solution(new int[]{14329, 7568}) == 10);
    }
}