京东笔试 第二题子串个数超时咋整。。

第一题 体育馆火灾火灾逃离 100% (时间有限 写的很乱 抱歉)

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Main {  static class Node{  Set<Node> nodes;  int val;    public int hashCode() {  final int prime = 31;  int result = 1;  result = prime * result + ((nodes == null) ? 0 : nodes.hashCode());  result = prime * result + val;  return result;  }    public boolean equals(Object obj) {  if (this == obj)  return true;  if (obj == null)  return false;  if (getClass() != obj.getClass())  return false;  Node other = (Node) obj;    if (val != other.val)  return false;  return true;  }    }  public static void main(String[] args) {  Scanner scanner = new Scanner(System.in);  int n = scanner.nextInt();  int time = 0 ;  Node node = new Node();  node.nodes = new HashSet<Node>();  node.val = 1 ;  for(int i = 0;i<n-1;i++) {  int a1 = scanner.nextInt();  int a2 = scanner.nextInt();  if(a2 ==1) {  Node node2 = new Node();  node2.nodes = new HashSet<Node>();  Node node3 = new Node();  node3.val = a1;  node2.nodes.add(node3);  node2.val = a1;  node.nodes.add(node2);    }else {  Node tarNode =new Node();  tarNode.val=a2;  Node newNode = new Node();  newNode.val = a1;  for(Node n2:node.nodes) {   if(n2.nodes.contains(tarNode)) {  n2.nodes.add(newNode);  }  }  }  }  int max = Integer.MIN_VALUE;  for(Node node2 : node.nodes) {  if(max<node2.nodes.size()) {  max = node2.nodes.size();  }  }  //max++;  System.out.println(max);  }
}




第二题 子串个数 74% 超时
package util;



import java.util.Arrays;
import java.util.Scanner;

public class Main {  public static void main(String[] args) {  Scanner scanner = new Scanner(System.in);  int n = scanner.nextInt();  scanner.nextLine();  String[] strings = new String[n];
        for(int i = 0 ; i < n ; i ++) {  String s = scanner.nextLine();  strings[i] = s;
        }
        String tar = scanner.nextLine();
        StringBuilder sb = new StringBuilder(tar);
        Arrays.sort(strings, (s1,s2)->{  return s1.length()-s2.length();
        });
        int time = 0 ;
        for(String s : strings) {  int start = sb.indexOf(s);  for(;start!=-1;) {  sb.delete(start, start+s.length());  sb.insert(start, ',');  time++;  start = sb.indexOf(s);  }

        }
        System.out.println(time);  }
}
我是真不知道怎么 搞快点 了
我数组 哈希表 都试过了,还是超时 ,,,
#京东##TCL##笔试题目##春招##实习#
全部评论
第一题多叉,递归搜就行了。第二题dp
点赞 回复 分享
发布于 2019-04-14 01:06
我和你第二题差不多,不论我怎么改,它都是超时。第一题没看懂😭
点赞 回复 分享
发布于 2019-04-14 01:03
第一题就是看左右两个树哪边的总结点多吗。。。。妈嗨。。。
点赞 回复 分享
发布于 2019-04-13 21:41
请问一下楼主第一题是只用考虑二叉树吗
点赞 回复 分享
发布于 2019-04-13 21:34
这个是贪心嘛。。。
点赞 回复 分享
发布于 2019-04-13 21:21
为什么 我 第一个代码 格式乱了。。。
点赞 回复 分享
发布于 2019-04-13 21:11

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务