2019秋招字节跳动测试开发笔试编程题(原题)
题目一:
猜谜底题目,考察康拓编码
输入:
2
abc
bac
输出:
bac
cba
这个题的意思是:假如输入的为 A = abc,则它的字典全排列为:
[abc, acb , bac , bca ,cab , cba]
那么index(A) = 0,谜底B = index(0+3) = bac
假如输入是为 A = bac, index(A) = 2, 谜底 B = index(5) = cba
如果 index索引超过5,则去index(index - 5)
此题考查康拓编码问题,贴一下自己的代码,没有参加笔试,只是看到了题,在本地写了一下,仅参考~~
public class Main { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(bf.readLine()); for (int i = 0; i < n; i++) { System.out.println(getResult(bf.readLine())); } } /** * 康拓编码 */ private static String getResult(String s) { int n = s.length(); int[] jc = new int[n + 1]; jc[0] = 1; ArrayList<Character> list = new ArrayList<>(); for (int i = 1; i <= n; i++) { jc[i] = jc[i - 1] * i;//求阶乘 list.add(s.charAt(i - 1)); } int index_of_s = 0;//字符串s所在的字典的索引位置 for (int i = 0; i < n - 1; i++) { int count = 0;//比i处小 for (int j = i + 1; j < n; j++) { if (s.charAt(j) < s.charAt(i)) count++;//统计后面有几个数比 i处的小 } index_of_s += jc[n - i - 1] * count; } // B 的索引位置 int index = index_of_s + n - (index_of_s + n < jc[n] ? 0 : jc[n]); Collections.sort(list); int temp = 0; StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { temp = index / jc[n - 1 - i];//除数 index = index - temp * jc[n - i - 1];//余数 char ch = list.get(temp); sb.append(ch); list.remove(temp); } return sb.toString(); } }
题目二:
输入两个字符串s1和s2,判断s1是否为包含s2的任意排列。
包含输出 "True"
第二题写的好像有些问题
public class Main { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String s1 = bf.readLine(); String s2 = bf.readLine(); if (s1.length() < s2.length()) { System.out.println("False"); return; } if (s2.isEmpty()){ System.out.println("True"); return; } HashMap<Character, Integer> map2 = new HashMap<>(); for (int i = 0; i < s2.length(); i++) { map2.put(s2.charAt(i), map2.getOrDefault(s2.charAt(i), 0) + 1); } int len = s2.length(); for (int i = 0; i <= s1.length() - s2.length(); i++) { int count = 0; for (int j = i; j < i + s2.length(); j++) { char c = s1.charAt(j); if (map2.containsKey(c) && map2.get(c) > 0) { map2.put(c, map2.get(c) - 1); count++; if (count == s2.length()) { System.out.println("True"); return; } }else { break; } } } System.out.println("False"); } }#字节跳动#