8.15 携程第二道编程java实现

package allcoding;
import java.util.*;
import java.util.stream.Collectors;

class WorkflowNode {
String nodeId;
int timeoutMillis;
List<WorkflowNode> nextNodes;
boolean initialised;

public static WorkflowNode load(String value) {//传入字符串
// Create head node;
Map<String, WorkflowNode> map = new HashMap<>();// 存储id和节点
WorkflowNode head = new WorkflowNode("HEAD", 0, null);//id,time,nexttime
map.put(head.nodeId, head);//

for (String nodeValue : value.split("\\|")) {//分割节点
String[] properties = nodeValue.split("\\`");//分割毫秒
WorkflowNode node = map.get(properties[0]);//获取head

node.timeoutMillis = Integer.parseInt(properties[1]);//获取时间
node.initialised = true;
// Check next nodes
if (properties[2].equals("END")) {//为end的情况
continue;
}
node.nextNodes = Arrays.stream(properties[2].split(","))
.map(p -> new WorkflowNode(p, 0, null))
.collect(Collectors.toList());
node.nextNodes.forEach(p -> map.put(p.nodeId, p));

map.put(node.nodeId, node);
}

return head;
}

public WorkflowNode(String nodeId, int timeoutMillis, List<WorkflowNode> nextNodes) {
this.nodeId = nodeId;
this.timeoutMillis = timeoutMillis;
this.nextNodes = nextNodes;
}
}

public class Demo
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
while (cin.hasNext())
{
WorkflowNode node = WorkflowNode.load(cin.next());
System.out.println(text(node));
}

}
public static int text(WorkflowNode root){
if(root==null) return 0;
List<WorkflowNode> nextNodes = root.nextNodes;
if(nextNodes!=null) {
int [] aa = new int[nextNodes.size()];
WorkflowNode temp;
for(int i= 0;i<nextNodes.size();i++){
temp= (WorkflowNode)nextNodes.get(i);
aa[i] = text(temp)+root.timeoutMillis;
}
Arrays.sort(aa);
return aa[aa.length-1];
}else {
return root.timeoutMillis;
}
}
}
//HEAD`0`A,B,C|A`20`END|B`100`END|C`50`D,E|D`80`F|E`150`END|F`30`END
#携程##笔试题目#
全部评论
全ac了吗
点赞
送花
回复 分享
发布于 2020-08-15 20:44
我也是递归,只过了0.44😥
点赞
送花
回复 分享
发布于 2020-08-15 20:55
元戎启行
校招火热招聘中
官网直投

相关推荐

点赞 1 评论
分享
牛客网
牛客企业服务