小北的青训营 X MarsCode 技术训练营——AI 加码,字节跳动青训营入营考核解答3(持续更新中~~~)

63 阅读6分钟

前言

       最近,字节跳动的青训营再次扬帆起航,作为第二次参与其中的小北,深感荣幸能借此机会为那些尚未了解青训营的友友们带来一些详细介绍。青训营不仅是一个技术学习与成长的摇篮,更是一个连接未来与梦想的桥梁~

1、报名方式

  1. 点击以下链接进行报名:字节跳动青训营报名入口https://juejin.cn/activityregister/7381401591805247534/info?utm_source=campus-2303&utm_medium=qingxunying&utm_campaign=registrationhttps://juejin.cn/activityregister/7381401591805247534/info?utm_source=campus-2303&utm_medium=qingxunying&utm_campaign=registration

2、考核内容

在指定的题库中自主选择不少于 15 道算法题并完成解题,其中题目难度分配如下:

  • 简单题不少于 10 道
  • 中等题不少于 4 道
  • 困难题不少于 1 道

 eg:解答代码(中等4题)

 3、最大异或和计算(中等)

AI调试代码:

public class Main {
    public static int solution(int n, int[] a, int[] b) {
        // 计算数组 a 和 b 的总和
        int sumA = 0;
        int sumB = 0;
 
        for (int num : a) {
            sumA += num;
        }
 
        for (int num : b) {
            sumB += num;
        }
 
        int maxXor = 0;
 
        // 尝试删除数组 a 中的每一个元素
        for (int i = 0; i < n; i++) {
            int newSumA = sumA - a[i];
            int newSumB = sumB;
            maxXor = Math.max(maxXor, newSumA ^ newSumB);
        }
 
        // 尝试删除数组 b 中的每一个元素
        for (int i = 0; i < n; i++) {
            int newSumA = sumA;
            int newSumB = sumB - b[i];
            maxXor = Math.max(maxXor, newSumA ^ newSumB);
        }
 
        return maxXor;
    }
 
    public static void main(String[] args) {
        System.out.println(solution(3, new int[]{1, 2, 3}, new int[]{3, 2, 1}) == 5);
        System.out.println(solution(4, new int[]{4, 5, 6, 7}, new int[]{7, 8, 9, 10}) == 51);
        System.out.println(solution(5, new int[]{10, 20, 30, 40, 50}, new int[]{50, 40, 30, 20, 10}) == 248);
    }
}

运行结果:

4、数字翻译成字符串的可能性(中等)

AI调试代码:

public class Main {
    public static int solution(int num) {
        String str = String.valueOf(num); // 将数字转成字符串
        int n = str.length();
 
        if (n == 0) return 0;
        if (n == 1) return 1; // 单字符的情况
 
        // 动态规划数组
        int[] dp = new int[n + 1];
        dp[0] = 1; // 空字符串一种方式
        dp[1] = 1; // 一个字符也有一种方式
 
        for (int i = 2; i <= n; i++) {
            // 只翻译当前字符
            dp[i] = dp[i - 1];
 
            // 结合前一个字符一起翻译
            int twoDigit = Integer.parseInt(str.substring(i - 2, i)); // 当前字符及前一个字符形成的数
            if (twoDigit >= 10 && twoDigit <= 25) {
                dp[i] += dp[i - 2]; // 如果能够组合翻译
            }
        }
 
        return dp[n]; // 返回总的翻译方式
    }
 
    public static void main(String[] args) {
        // You can add more test cases here
        System.out.println(solution(12258) == 5);
        System.out.println(solution(1400112) == 6);
        System.out.println(solution(2110101) == 10);
    }
}

运行结果:

 5、五子棋获胜策略(中等)
# 问题描述
- 假设存在一个五子棋棋盘,大小未知。上面只摆放了一些白色的棋子,现在你的手中还有一个白色棋子,要求找出在棋盘的哪些位置摆放这个棋子,能够使棋盘上出现五颗棋子连成一线。
- 备注:棋盘上当前不存在连成一条线的五个棋子,但至少存在一个点能够凑出五子一线(不限于横、竖、斜线)
 
## 输入格式
- 第一行输入一个正整数 `n`,表示棋盘的宽度,棋盘总共可以容纳 `n^2` 个棋子。
- 第 2 到 `n+1` 行输入 `n` 个数字。每次输入 `n` 个数,其中 `1` 代表有棋子,`0` 代表没有棋子。
 
## 输出格式
- 如果有 `n` 个可放置点,输出 `n` 行。
- 每行输出两个数字,以空格分隔,分别代表放置点在棋盘上的行数和列数。
- 输出顺序需要按照行数从小到大、列数从小到大的顺序。
 
## 输入样例
```
6  
0 0 0 0 0 0  
0 1 0 0 0 0  
0 0 1 0 0 0  
0 0 0 1 0 0  
0 0 0 0 1 0  
0 0 0 0 0 0  
```
 
 
## 输出样例
 
1 1
6 6
 
 
## 数据范围
- 第一行中,棋盘宽度为 `[1, 10)` 中的整数。
- 第 2 到 `n+1` 行中,只会出现 `0``1`

问题分析:

       这个题目跟小北之前做过的一个题目很像,那个是动态的,双方互相落子判断谁胜利,这个是给你一个棋局,没有黑白方,判断落子胜利有几种方式,原理就是落子时,判断上下左右左斜右斜,且在规定范围内,是否可以达到五子连珠。

AI调试代码:

import java.util.ArrayList;
import java.util.List;
public class Main {
    // 检查某个位置是否可以形成五子连线
    public static boolean canFormLine(int x, int y, int[][] board, int n) {
        // 定义四个方向:右,下,右下,左下
        int[][] directions = {{1, 0}, {0, 1}, {1, 1}, {1, -1}};
 
        for (int[] dir : directions) {
            int count = 1; // 当前位置记为1
            int dx = dir[0], dy = dir[1];
 
            // 检查正向
            for (int step = 1; step < 5; ++step) {
                int nx = x + dx * step;
                int ny = y + dy * step;
                if (nx >= 0 && nx < n && ny >= 0 && ny < n && board[nx][ny] == 1) {
                    count++;
                } else {
                    break;
                }
            }
 
            // 检查反向
            for (int step = 1; step < 5; ++step) {
                int nx = x - dx * step;
                int ny = y - dy * step;
                if (nx >= 0 && nx < n && ny >= 0 && ny < n && board[nx][ny] == 1) {
                    count++;
                } else {
                    break;
                }
            }
 
            // 如果形成五子连接,则返回 true
            if (count >= 5) return true;
        }
 
        return false; // 没有符合条件的连线
    }
 
    // 主解决方案函数
    public static int[][] solution(int n, int[][] board) {
        List<int[]> results = new ArrayList<>();
 
        // 检查每个位置是否能放置新棋子形成五子连线
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (board[i][j] == 0) { // 只检查空位
                    if (canFormLine(i, j, board, n)) {
                        results.add(new int[]{i + 1, j + 1}); // 记录行列,+1因要求从1开始
                    }
                }
            }
        }
 
        // 将结果转换为二维数组
        int[][] resultArray = new int[results.size()][2];
        for (int i = 0; i < results.size(); i++) {
            resultArray[i] = results.get(i);
        }
 
        return resultArray.length > 0 ? resultArray : new int[][]{}; // 如果没有结果,返回 {-1, -1}
    }
 
    public static void main(String[] args) {
        // 测试用例
        int[][] array = {
            {0, 0, 0, 0, 0, 0},
            {0, 1, 0, 0, 0, 0},
            {0, 0, 1, 0, 0, 0},
            {0, 0, 0, 1, 0, 0},
            {0, 0, 0, 0, 1, 0},
            {0, 0, 0, 0, 0, 0}
        };
 
        int[][] expectedOutput = {{1, 1}, {6, 6}};
        System.out.println(java.util.Arrays.deepEquals(solution(6, array), expectedOutput)); // 验证测试结果
    }
}

运行结果:

 结语

        在这篇技术博客中,小北与友友们分享了字节跳动青训营的精彩内容,从报名方式到考核内容,再到具体的算法题目示例和解答代码,让我们对青训营有了更深入的了解。通过这些实际的算法题目和解决方案,我们不仅能够学习到编程技巧,还能够感受到解决实际问题的乐趣。

       希望这篇博客能够激励更多的技术爱好者参与到青训营中,提升自己的技术水平,同时小北也期待在未来能带给友友们更多有价值的技术分享~

      最后,祝愿所有参与青训营的朋友们都能学有所成,技术精进,未来在技术的道路上越走越远。同时,也期待字节跳动青训营能够培养出更多的技术人才,为技术社区注入更多的活力和创新!

感谢友友们的阅读,我们下次再见~