拼多多笔试:70%,100%,0,0(附唯一一道AC的代码)
有个问题,第一题AC了70,然后又改低了这个咋算。。。第二题代码:
思路:准备一个list存头尾,每读到一个字符串,判断列表里是否有尾 = 其头,有则在list中remove,加新头新尾进list,无则加进list,,最后就判断list是否为空啦~
import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class Tow { public static List<Node> list = new LinkedList<>(); public static class Node { char head; char tail; public Node (char head, char tail) { this.head = head; this.tail = tail; } } public static boolean solution(String[] strs) { for (int i = 0; i < strs.length; i++) { String cur = strs[i].trim(); char head = cur.charAt(0); char tail = cur.charAt(cur.length()-1); if (!doHead(head, tail)) { list.add(new Node(head, tail)); } } if (list.isEmpty()) { return true; } return false; } public static boolean doHead(char head, char tail) { for (Node node : list) { if (node.tail == head) { if (node.head == tail) { list.remove(node); return true; } char preHead = node.head; list.remove(node); list.add(new Node(preHead, tail)); return true; } } return false; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String line = scanner.nextLine(); System.out.println(solution(line.split(" "))); } }
#拼多多##笔试题目#