优化Project Euler#22

Jac*_*k K 2 java optimization file-io

提前致谢.

我刚刚解决了Project Euler#22,这个问题涉及从文件中读取大约5,000行文本,并根据字符串字符的总和以及字母顺序确定特定名称的值.

但是,代码运行大约需要5-10秒,这有点烦人.优化此代码的最佳方法是什么?我目前正在使用扫描仪将文件读入字符串.还有另一种更有效的方法吗?(我尝试使用BufferedReader,但这甚至更慢)

public static int P22(){


    String s = null;

    try{
        //create a new Scanner to read file
        Scanner in = new Scanner(new File("names.txt"));
        while(in.hasNext()){
            //add the next line to the string
            s+=in.next();
        }

    }catch(Exception e){

    }
    //this just filters out the quotation marks surrounding all the names
    String r = "";
    for(int i = 0;i<s.length();i++){
        if(s.charAt(i) != '"'){
            r += s.charAt(i);
        }
    }
    //splits the string into an array, using the commas separating each name
    String text[] = r.split(",");
    Arrays.sort(text);



    int solution = 0;
    //go through each string in the array, summing its characters
    for(int i = 0;i<text.length;i++){
        int sum = 0;
        String name = text[i];
        for(int j = 0;j<name.length();j++){
            sum += (int)name.charAt(j)-64;
        }
        solution += sum*(i+1);
    }
    return solution;


}
Run Code Online (Sandbox Code Playgroud)

Ama*_*dan 5

如果您要使用Scanner,为什么不将它用于它应该做的事情(标记化)?

  Scanner in = new Scanner(new File("names.txt")).useDelimiter("[\",]+");
  ArrayList<String> text = new ArrayList<String>();
  while (in.hasNext()) {
    text.add(in.next());
  }
  Collections.sort(text);
Run Code Online (Sandbox Code Playgroud)

您不需要删除引号,也不需要用逗号分隔 - Scanner为您完成所有操作.

此片段(包括java启动时间)在我的计算机上以0.625秒(用户时间)执行.我怀疑它应该比你正在做的快一点.

编辑 OP询问传递给字符串的useDelimiter是什么.这是一个正则表达式.当你去除Java所要求的转义以将引号字符包含在字符串中时,它是[",]+- 而且意思是:

[...]   character class: match any of these characters, so
[",]    match a quote or a comma
...+    one or more occurence modifier, so
[",]+   match one or more of quotes or commas
Run Code Online (Sandbox Code Playgroud)

与此模式匹配的序列包括:

"
,
,,,,
""",,,",","
Run Code Online (Sandbox Code Playgroud)

事实上",",我们在这里追求的是什么.

  • 我实现了这个解决方案,现在运行大约0.3秒!谢谢! (2认同)