【问题标题】:How do I compare to a txt file line for line?如何与 txt 文件中的每一行进行比较?
【发布时间】:2025-12-04 23:05:02
【问题描述】:

这是文件:http://textuploader.com/5y6xh

我已经将 .txt 文件中的双精度数排列到一个数组列表中,以避免任何字符串干扰。

如何将每个双精度一次一行与整个数组中的单独双精度进行比较?

【问题讨论】:

  • 带循环?你知道whilefor 循环吗?
  • 是的,但是我如何有效地逐行...
  • 单个 while 或 for 循环通常为 O(n),而嵌套的 for 循环为 O(n^2)。

标签: java arrays text double line


【解决方案1】:

你可以暴力破解

take the first element
loop through the array
return false if the element equals any other element
move to the second element
loop through the array
return false if the element equals any other element
move to the third element
...
After the loop is finished, return true

这个算法基本上是蛮力搜索,它不是很有效,如果我没记错的话,O(n!),但它会做你需要的。如果您愿意创建 BST,则可以在寻找双打或整数时缩短时间。

【讨论】:

    最近更新 更多