本文主要是介绍字节跳动2018校招后端方向(第二批),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
字节跳动2018校招后端方向
- 第一题
- 第二题
- 第三题
第一题
import java.io.*;
import java.util.*;public class Main {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));int n = Integer.parseInt(br.readLine().trim()); //读取用户个数String[] str = br.readLine().trim().split(" ");//读取每个用户的喜好度Map<Integer,List<Integer>> userLike = new HashMap<>();for (int i = 0; i < n; ++i) {int key = Integer.parseInt(str[i]);if (!userLike.containsKey(key)) {List<Integer> list = new ArrayList<>();list.add(i);userLike.put(key,list);}else {userLike.get(key).add(i);}}int q = Integer.parseInt(br.readLine().trim());//读取查询组数for (int j = 0; j < q; ++j){String[] s = br.readLine().trim().split(" ");int l = Integer.parseInt(s[0]);int r = Integer.parseInt(s[1]);int k = Integer.parseInt(s[2]);int count = 0;List<Integer> list = userLike.get(k);if (list != null) {for (Integer i : list){if (i >= l-1 && i <= r-1) {++count;}}}System.out.println(count);}}
}
第二题
import java.io.*;
import java.util.*;public class Main {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));String[] str = br.readLine().trim().split(" ");int n = Integer.parseInt(str[0]); //珠子的个数int m = Integer.parseInt(str[1]);//连续m个珠子int c = Integer.parseInt(str[2]);//颜色种类Map<Integer,List<Integer>> map = new HashMap<>();// key:颜色, value:包含这种颜色的珠子for (int i = 0; i < n; ++i) {String[] s = br.readLine().trim().split(" ");int num_i = Integer.parseInt(s[0]);for (int j = 1; j <= num_i; ++j) {int key = Integer.parseInt(s[j]); if (!map.containsKey(key)) {List<Integer> list = new ArrayList<>();list.add(i);map.put(key,list);}else {map.get(key).add(i);}}}int count = 0;for(List<Integer> list : map.values()){for(int j=0; j<list.size();j++) {if (j+1<list.size() && list.get(j+1)- list.get(j)<m){count++;break;}else if(j+1 == list.size() && list.get(0)+n-list.get(j)<m){count++;break;}}}System.out.println(count);}
}
第三题
import java.io.*;
import java.util.*;public class Main {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));String[] str = br.readLine().trim().split(" ");String s = str[0];int m = Integer.parseInt(str[1]);int ans = 1;for(char c = 'a' ; c<='z' ; c++){int[] pos = new int[s.length()];//存储字符串中当前字符出现的位置int cnt = 0;for(int i = 0 ; i<s.length();i++){if(s.charAt(i) == c){pos[cnt] = i;cnt++;}}//只有一个字符,最多为1次if(cnt<2)continue;int[][] dp = new int[cnt][cnt];int tem = 1;for(int len = 2 ;len<=cnt;len++){for(int i = 0 ;i+len-1<cnt;i++){dp[i][i+len-1] = dp[i+1][i+len-2] + pos[i+len-1] - pos[i] + 1 - len;if(dp[i][i+len-1]<=m)tem = len;}}ans = Math.max(ans,tem);}System.out.println(ans);}
}
这篇关于字节跳动2018校招后端方向(第二批)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!