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

85 阅读7分钟

前言

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

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:解答代码(简单10题)

9、 环状 DNA 序列的最小表示法(简单)

AI调试代码:

import java.util.ArrayList;
import java.util.List;
 
public class Main {
    public static String solution(String dna_sequence) {
        // Create a list to hold the different representations of the DNA sequence
        List<String> representations = new ArrayList<>();
        int length = dna_sequence.length();
 
        // Generate all rotations of the DNA sequence
        for (int i = 0; i < length; i++) {
            String representation = dna_sequence.substring(i) + dna_sequence.substring(0, i);
            representations.add(representation);
        }
 
        // Find the minimum representation
        String minRepresentation = representations.get(0);
        for (String representation : representations) {
            if (representation.compareTo(minRepresentation) < 0) {
                minRepresentation = representation;
            }
        }
 
        return minRepresentation;
    }
 
    public static void main(String[] args) {
        // Add your test cases here
        System.out.println(solution("ATCA").equals("AATC")); // Should print true
        System.out.println(solution("CGAGTC").equals("AGTCCG")); // Should print true
        System.out.println(solution("TCATGGAGTGCTCCTGGAGGCTGAGTCCATCTCCAGTAG")
                .equals("AGGCTGAGTCCATCTCCAGTAGTCATGGAGTGCTCCTGG")); // Should print true
    }
}

转存失败,建议直接上传图片文件

运行结果: 转存失败,建议直接上传图片文件

10、 Base32 编码和解码问题(简单)

AI调试代码:

import java.util.ArrayList;
import java.util.List;
 
public class Main {
    static char[] base32 = {
        '9', '8', '7', '6', '5', '4', '3', '2', '1', '0', 
        'm', 'n', 'b', 'v', 'c', 'x', 'z', 'a', 's', 'd', 
        'f', 'g', 'h', 'j', 'k', 'l', 'p', 'o', 'i', 'u', 
        'y', 't'
    };
 
    public static String decimalToBinary8Bits(int num) {
        StringBuilder binary = new StringBuilder();
        for (int i = 7; i >= 0; i--) {
            binary.append((num & (1 << i)) != 0 ? '1' : '0');
        }
        return binary.toString();
    }
 
    public static List<Integer> binaryToDecimal(String binaryStr) {
        List<Integer> decimalNums = new ArrayList<>();
        for (int i = 0; i < binaryStr.length(); i += 5) {
            String group = binaryStr.substring(i, Math.min(i + 5, binaryStr.length()));
            int decimal = 0;
            for (char c : group.toCharArray()) {
                decimal = decimal * 2 + (c - '0');
            }
            decimalNums.add(decimal);
        }
        return decimalNums;
    }
 
    public static String decimalArrayToBinary(List<Integer> arr) {
        StringBuilder result = new StringBuilder();
        for (int num : arr) {
            StringBuilder binary = new StringBuilder();
            for (int i = 4; i >= 0; i--) {
                binary.append((num & (1 << i)) != 0 ? '1' : '0');
            }
            result.append(binary);
        }
        return result.toString();
    }
 
    public static String binaryToChar(String binaryStr) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < binaryStr.length(); i += 8) {
            String group = binaryStr.substring(i, Math.min(i + 8, binaryStr.length()));
            int decimal = 0;
            for (char c : group.toCharArray()) {
                decimal = decimal * 2 + (c - '0');
            }
            result.append((char) decimal);
        }
        return result.toString();
    }
 
    public static String jiema(String encodedStr) {
        int i;
        for (i = encodedStr.length() - 1; i > 0 && encodedStr.charAt(i) == '+'; i--);
        String newstr = encodedStr.substring(0, i + 1);
        List<Integer> st2 = new ArrayList<>();
        for (char c : newstr.toCharArray()) {
            for (int j = 0; j < base32.length; j++) {
                if (c == base32[j]) {
                    st2.add(j);
                }
            }
        }
        String newnewstr = decimalArrayToBinary(st2);
        String res2 = "";
 
        if (newnewstr.length() % 40 == 10) {
            String newstrs = newnewstr.substring(0, newnewstr.length() - 2);
            res2 = binaryToChar(newstrs);
        } else if (newnewstr.length() % 40 == 20) {
            String newstrs = newnewstr.substring(0, newnewstr.length() - 4);
            res2 = binaryToChar(newstrs);
        } else if (newnewstr.length() % 40 == 25) {
            String newstrs = newnewstr.substring(0, newnewstr.length() - 1);
            res2 = binaryToChar(newstrs);
        } else if (newnewstr.length() % 40 == 35) {
            String newstrs = newnewstr.substring(0, newnewstr.length() - 3);
            res2 = binaryToChar(newstrs);
        }
 
        return res2;
    }
 
    public static String solution(String rawStr, String encodedStr) {
        List<Integer> st = new ArrayList<>();
        StringBuilder str1 = new StringBuilder();
 
        for (char c : rawStr.toCharArray()) {
            str1.append(decimalToBinary8Bits(c));
        }
 
        int r = str1.length() % 40;
        if (r == 8) {
            str1.append("00");
        } else if (r == 16) {
            str1.append("0000");
        } else if (r == 24) {
            str1.append("0");
        } else if (r == 32) {
            str1.append("000");
        }
 
        st = binaryToDecimal(str1.toString());
        StringBuilder res1 = new StringBuilder();
 
        for (int value : st) {
            res1.append(base32[value]);
        }
 
        while (res1.length() % 8 != 0) {
            res1.append("+");
        }
 
        StringBuilder res2 = new StringBuilder();
        for (int i = 0; i < encodedStr.length(); i += 8) {
            res2.append(jiema(encodedStr.substring(i, Math.min(i + 8, encodedStr.length()))));
        }
 
        return res1.toString() + ":" + res2.toString();
    }
 
    public static void main(String[] args) {
        // You can add more test cases here
        System.out.print(solution("foo", "b0zj5+++"));
        System.out.println(solution("foo", "b0zj5+++").equals("bljhy+++:bar")); // Should print true
        System.out.println(solution("The encoding process represents 40-bit groups of input bits as output strings of 8 encoded characters.  Proceeding from left to right, a 40-bit input group is formed by concatenating 5 8bit input groups. These 40 bits are then treated as 8 concatenated 5-bit groups, each of which is translated into a single character in the base 32 alphabet.  When a bit stream is encoded via the base 32 encoding, the bit stream must be presumed to be ordered with the most-significant-bit first. That is, the first bit in the stream will be the high-order bit in the first 8bit byte, the eighth bit will be the low-order bit in the first 8bit byte, and so on.", "bljhy+++b0zj5+++").equals("maf3m164vlahyl60vlds9i6svuahmiod58l3mi6sbglhmodfcbz61b8vb0fj1162c0jjmi6d58jhb160vlk2mu89b0fj1il9b4ls9oogcak2mu89cvp25pncbuls9oo359i79lncbvjh1ln558ahzknsb4aj1lnscbj7917zc0jh3ln4bafhill9bll3yo09vashbu89cajs9id0buf21n89b5z61b8vb0fj1160vlk2mu89bul3yunz58fj3163vul3pln558a2s166vuj33knfbgj37u60vlds9v0928a3su89v4j29unf58dj5oogc8lsi17fv8sj3l093zk79kd0cals9knsbfz21p64vkz21id4b4p3ml89b4ls9c89bvjhiko8cashiknfbgs79v0vb0fj1162c0jjmi6d4zz3mkn6v9z3yla9cuf3sko158fj316fc0zhiiobb4p3ml89v4j21ol9b5z23pncbuh3m166v8zj5kn6casj5160vkz21p6458a37io459ld5168vak3zkn7bgp7i189muf3moa9b5z35pnf58lj1id4b4hs9pnd58shikoxbash116hv4zs9u61bfz35kndbfz63ba9bgj33oo5v4j3cn89caf3m167v4p79iofc0sh7o09vgpj3u89b0ss9i6sbgljmon4bzz21ol9b0ss9oosbasj5ln558ohsu6158p3zl09vgjj3u8vcvfhcod0blfh3kncczhs9kd0czz3bpnscvp7i17fv8zj1160cbh79u61bfz3bpnscvp79kd0czz3soa9caf3m16dcal3mknv58ohso6b58a3m16fv8ss9p60buf7p16xc0s3mia9b0fj1160vkz21p6458d3siddczz6zkd0czz35ynfbfh79u61bfz3mpn2v8p3z167v4p79uo0vah79kd458p3zl09vajjcn09vul31lns58a3su89v4j79u61bfz3bpnscvp79c67v4p79kdlcassk168vls79iox58jhinz+:foobar")); // Should print true
    }
}

转存失败,建议直接上传图片文件

运行结果: 转存失败,建议直接上传图片文件

 结语

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

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

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

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