拼多多笔试: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(" ")));  }   }
	

#拼多多##笔试题目#
全部评论
楼主你测试下这种情况,ABC, CBA, CDC。实际是true,目测会判false。
点赞 回复 分享
发布于 2019-07-28 20:18
这个排版让我好难受啊
点赞 回复 分享
发布于 2019-07-28 20:10
取历史最高的
点赞 回复 分享
发布于 2019-07-28 20:11
第二题应该就是数据集问题了吧,我也是这个做法,ac20,这么做有明显错误,后来改成dfs
点赞 回复 分享
发布于 2019-07-29 13:17

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务