记录笔试0829
第一部分:单项选择
第二部分:多项选择
第三部分:编程三道题
第一题:SQL
有一个视频发布记录表,有视频主键,发布者id,发布视频时长,请在这个表中,请用MYSQL查询,在视频数据表中,筛选发布视频平均时长大雨300秒的up主,按照up主全部视频的平均时长倒序,视频ID升序,返回第三行到第六行的视频id
SELECT video_id FROM video_table WHERE uploader_id IN ( SELECT uploader_id FROM video_table GROUP BY uploader_id HAVING AVG(video_duration) > 300 ) ORDER BY ( SELECT AVG(video_duration) FROM video_table AS t WHERE t.uploader_id = video_table.uploader_id ) DESC, video_id ASC LIMIT 4 OFFSET 2;
第二题:动态规划
给定两个字符串,返回使两个字符串相等所需要删除字符的ASCII值的最小和
public class Main { public static int minimumDeleteSum(String s1, String s2) { int[][] dp = new int[s1.length() + 1][s2.length() + 1]; for (int i = s1.length() - 1; i >= 0; i--) { dp[i][s2.length()] = dp[i + 1][s2.length()] + s1.codePointAt(i); } for (int j = s2.length() - 1; j >= 0; j--) { dp[s1.length()][j] = dp[s1.length()][j + 1] + s2.codePointAt(j); } for (int i = s1.length() - 1; i >= 0; i--) { for (int j = s2.length() - 1; j >= 0; j--) { if (s1.charAt(i) == s2.charAt(j)) { dp[i][j] = dp[i + 1][j + 1]; } else { dp[i][j] = Math.min(dp[i + 1][j] + s1.codePointAt(i), dp[i][j + 1] + s2.codePointAt(j)); } } } return dp[0][0]; } public static void main(String[] args) { String s1 = "delete"; String s2 = "leet"; System.out.println(minimumDeleteSum(s1, s2)); } }
第三题:二叉树
给定一个二叉树的root,返回最长路径的长度,这个路径中每个节点具有相同的值,这条路径可以经过也可以不经过根节点,两个节点之间的长度由他们之间的边数表示
public int longestUnivaluePath(TreeNode root) { int[] maxLen = new int[1]; maxLen[0] = 0; longestPath(root, maxLen); return maxLen[0]; } private int longestPath(TreeNode node, int[] maxLen) { if (node == null) { return 0; } int left = longestPath(node.left, maxLen); int right = longestPath(node.right, maxLen); int leftLen = 0, rightLen = 0; if (node.left != null && node.left.val == node.val) { leftLen = left + 1; } if (node.right != null && node.right.val == node.val) { rightLen = right + 1; } maxLen[0] = Math.max(maxLen[0], leftLen + rightLen); return Math.max(leftLen, rightLen); }
B站