前言
最近,字节跳动的青训营再次扬帆起航,作为第二次参与其中的小北,深感荣幸能借此机会为那些尚未了解青训营的友友们带来一些详细介绍。青训营不仅是一个技术学习与成长的摇篮,更是一个连接未来与梦想的桥梁~
1、报名方式
2、考核内容
在指定的题库中自主选择不少于 15 道算法题并完成解题,其中题目难度分配如下:
- 简单题不少于 10 道
- 中等题不少于 4 道
- 困难题不少于 1 道
eg:解答代码(中等4题)
1、简单四则运算解析器 (中等)
初始代码:
def solution(binary1, binary2):
# Please write your code here
return ""
if __name__ == "__main__":
# You can add more test cases here
print(solution("101", "110") == "11")
print(solution("111111", "10100") == "83")
print(solution("111010101001001011", "100010101001") == "242420")
print(solution("111010101001011", "10010101001") == "31220")
手搓代码:
import java.util.Stack;
public class Main {
public static int solution(String expression) {
Stack<Integer> numStack = new Stack<>();
Stack<Character> opStack = new Stack<>();
// 遍历表达式
for (int i = 0; i < expression.length(); i++) {
char c = expression.charAt(i);
// 如果是数字,将其转换为整数并压入数字栈
if (Character.isDigit(c)) {
int num = 0;
while (i < expression.length() && Character.isDigit(expression.charAt(i))) {
num = num * 10 + (expression.charAt(i) - '0');
i++;
}
i--;
numStack.push(num);
}
// 如果是左括号,直接压入运算符栈
else if (c == '(') {
opStack.push(c);
}
// 如果是右括号,进行计算直到遇到左括号
else if (c == ')') {
while (opStack.peek() != '(') {
char op = opStack.pop();
int num2 = numStack.pop();
int num1 = numStack.pop();
if (op == '+') {
numStack.push(num1 + num2);
} else if (op == '-') {
numStack.push(num1 - num2);
} else if (op == '*') {
numStack.push(num1 * num2);
} else if (op == '/') {
numStack.push(num1 / num2);
}
}
opStack.pop();
}
// 如果是运算符,根据优先级进行处理
else if (isOperator(c)) {
while (!opStack.isEmpty() && precedence(c) <= precedence(opStack.peek())) {
char op = opStack.pop();
int num2 = numStack.pop();
int num1 = numStack.pop();
if (op == '+') {
numStack.push(num1 + num2);
} else if (op == '-') {
numStack.push(num1 - num2);
} else if (op == '*') {
numStack.push(num1 * num2);
} else if (op == '/') {
numStack.push(num1 / num2);
}
}
opStack.push(c);
}
}
// 处理剩余的运算符
while (!opStack.isEmpty()) {
char op = opStack.pop();
int num2 = numStack.pop();
int num1 = numStack.pop();
if (op == '+') {
numStack.push(num1 + num2);
} else if (op == '-') {
numStack.push(num1 - num2);
} else if (op == '*') {
numStack.push(num1 * num2);
} else if (op == '/') {
numStack.push(num1 / num2);
}
}
// 返回数字栈的栈顶元素即为结果
return numStack.pop();
}
// 判断是否为运算符
public static boolean isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
// 定义运算符的优先级
public static int precedence(char op) {
if (op == '+' || op == '-') {
return 1;
} else if (op == '*' || op == '/') {
return 2;
}
return -1;
}
public static void main(String[] args) {
// You can add more test cases here
System.out.println(solution("1+1") == 2);
System.out.println(solution("3+4*5/(3+2)") == 7);
System.out.println(solution("4+2*5-2/1") == 12);
System.out.println(solution("(1+(4+5+2)-3)+(6+8)") == 23);
}
}
AI调试:
def solution(expression):
num_stack = []
op_stack = []
i = 0
while i < len(expression):
c = expression[i]
if c.isdigit():
num = 0
while i < len(expression) and expression[i].isdigit():
num = num * 10 + int(expression[i])
i += 1
i -= 1
num_stack.append(num)
elif c == '(':
op_stack.append(c)
elif c == ')':
while op_stack[-1] != '(':
op = op_stack.pop()
num2 = num_stack.pop()
num1 = num_stack.pop()
if op == '+':
num_stack.append(num1 + num2)
elif op == '-':
num_stack.append(num1 - num2)
elif op == '*':
num_stack.append(num1 * num2)
elif op == '/':
num_stack.append(num1 // num2) # 使用整数除法
op_stack.pop()
elif is_operator(c):
while op_stack and precedence(c) <= precedence(op_stack[-1]):
op = op_stack.pop()
num2 = num_stack.pop()
num1 = num_stack.pop()
if op == '+':
num_stack.append(num1 + num2)
elif op == '-':
num_stack.append(num1 - num2)
elif op == '*':
num_stack.append(num1 * num2)
elif op == '/':
num_stack.append(num1 // num2) # 使用整数除法
op_stack.append(c)
i += 1
while op_stack:
op = op_stack.pop()
num2 = num_stack.pop()
num1 = num_stack.pop()
if op == '+':
num_stack.append(num1 + num2)
elif op == '-':
num_stack.append(num1 - num2)
elif op == '*':
num_stack.append(num1 * num2)
elif op == '/':
num_stack.append(num1 // num2) # 使用整数除法
return num_stack.pop()
def is_operator(c):
return c in '+-*/'
def precedence(op):
if op in '+-':
return 1
elif op in '*/':
return 2
return -1
# 测试用例
print(solution("1+1") == 2)
print(solution("3+4*5/(3+2)") == 7)
print(solution("4+2*5-2/1") == 12)
print(solution("(1+(4+5+2)-3)+(6+8)") == 23)
运行结果:
2、机器人能量冒险(中等)
初始代码:
public class Main {
public static String solution(int n, int[] array) {
// Edit your code here
return "TRUE";
}
public static void main(String[] args) {
// Add your test cases here
System.out.println(solution(5, new int[]{2, 3, 1, 1, 4}).equals("TRUE"));
System.out.println(solution(5, new int[]{3, 2, 1, 0, 4}).equals("FALSE"));
}
}
手搓代码:
public class Main {
public static String solution(int n, int[] array) {
// 初始化当前位置为 0
int currentPosition = 0;
// 循环直到到达最后一个位置或者无法前进
while (currentPosition < n - 1) {
// 如果当前位置的能量值为 0,直接返回 FALSE
if (array[currentPosition] == 0) {
return "FALSE";
}
// 计算可以前进的最大步数
int maxSteps = array[currentPosition];
// 尝试从当前位置前进
for (int step = 1; step <= maxSteps && currentPosition + step < n; step++) {
// 如果能直接到达最后一个位置,返回 TRUE
if (currentPosition + step == n - 1) {
return "TRUE";
}
}
// 选择能前进最远的步数
int bestStep = 0;
for (int step = 1; step <= maxSteps && currentPosition + step < n; step++) {
if (array[currentPosition + step] > array[currentPosition + bestStep]) {
bestStep = step;
}
}
// 前进所选的步数
currentPosition += bestStep;
}
// 如果循环结束还没到达最后一个位置,返回 FALSE
if (currentPosition == n - 1) {
return "TRUE";
} else {
return "FALSE";
}
}
public static void main(String[] args) {
// Add your test cases here
System.out.println(solution(5, new int[] { 2, 3, 1, 1, 4 }).equals("TRUE"));
System.out.println(solution(5, new int[] { 3, 2, 1, 0, 4 }).equals("FALSE"));
}
}
AI调试:
public class Main {
public static String solution(int n, int[] array) {
// 初始化当前能到达的最远位置
int maxReach = 0;
// 遍历数组
for (int i = 0; i < n; i++) {
// 如果当前位置超过了能到达的最远位置,说明无法继续前进
if (i > maxReach) {
return "FALSE";
}
// 更新能到达的最远位置
maxReach = Math.max(maxReach, i + array[i]);
// 如果最远位置已经超过或等于最后一个位置,直接返回 TRUE
if (maxReach >= n - 1) {
return "TRUE";
}
}
// 如果遍历结束还没到达最后一个位置,返回 FALSE
return "FALSE";
}
public static void main(String[] args) {
// Add your test cases here
System.out.println(solution(5, new int[] { 2, 3, 1, 1, 4 }).equals("TRUE"));
System.out.println(solution(5, new int[] { 3, 2, 1, 0, 4 }).equals("FALSE"));
}
}
运行结果:
结语
在这篇技术博客中,小北与友友们分享了字节跳动青训营的精彩内容,从报名方式到考核内容,再到具体的算法题目示例和解答代码,让我们对青训营有了更深入的了解。通过这些实际的算法题目和解决方案,我们不仅能够学习到编程技巧,还能够感受到解决实际问题的乐趣。
希望这篇博客能够激励更多的技术爱好者参与到青训营中,提升自己的技术水平,同时小北也期待在未来能带给友友们更多有价值的技术分享~
最后,祝愿所有参与青训营的朋友们都能学有所成,技术精进,未来在技术的道路上越走越远。同时,也期待字节跳动青训营能够培养出更多的技术人才,为技术社区注入更多的活力和创新!
感谢友友们的阅读,我们下次再见~