本来看Death Note呢,想起来有笔试,结果做着有些浪费时间了。。。
第一题代码尬过去了,我都觉得无语。
public static String getRes(String sentence, String[] word2) { StringBuilder sent = new StringBuilder(); Map<Integer, String> map = new HashMap<>(); String[] word1 = sentence.split("\\s+"); for (String word : word1) sent.append(word); for (String word : word2) { int idx = sent.indexOf(word, 0); while (idx != -1) { map.put(idx, word); idx = sent.indexOf(word, idx + 1); } } StringBuilder res = new StringBuilder(); int cur = 0, start = 0; for (int i = 0; i < sentence.length(); i++) { char ch = sentence.charAt(i); if (map.containsKey(start)) { res.append(" " + map.get(start) + " "); start += map.get(start).length(); } else { res.append(ch); if (ch != ' ') { cur++; start++; } } boolean flag = false; while (cur < start) { flag = true; if (sentence.charAt(i) != ' ') cur++; i++; } if (flag) i--; } return res.toString().trim(); }
枉我第一题还写了半天,看其他朋友说直接输出就可以AC?
第二题不给行数?
Scanner scan = new Scanner(System.in); int n = Integer.parseInt(scan.nextLine()); System.out.println(scan.nextLine());
我直接尬过去33%
全部评论
(5) 回帖