栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在单个数组中查找长度为5的副本。爪哇

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在单个数组中查找长度为5的副本。爪哇

您可以使用来实现

Hashtable
。我以您的代码为基础,编写了一个示例实现,但是在不知道您要执行的操作的情况下,我无法判断这是否是您想要的。

import java.util.Hashtable;public class findPat {    static final int COUNT = 100;    static Hashtable<String, Integer> compareSet = new Hashtable<String, Integer>();    static String groupInteger = "";    static int arr [] = new int [5];    static int st = 1;    static int end = 56;    static double t1;    static double t2;    public static void main(String[] args) {        t1=System.currentTimeMillis();         for(int n = 0; n < COUNT; n++){ for (int i = 0; i < arr.length; i++) {     arr[i] = (int) (Math.random()* (end - st + 1)) + st; } for (int i = 1; i <= 5; i++) {     groupInteger += arr[i-1];     System.out.print("t" + arr[i-1]);     if (i % 5 == 0) {         System.out.println();         if (compareSet.containsKey(groupInteger)) {  System.out.println("duplicate found");  int currentCount = compareSet.get(groupInteger);  compareSet.put(groupInteger, currentCount + 1);         } else {  compareSet.put(groupInteger, 1);}         groupInteger = "";     } }         }        t2=System.currentTimeMillis();        System.out.println();        System.out.println();        System.out.println("t" + "Total run time is " + ((t2 - t1)) + "ms");    }}

该代码通过添加唯一的随机数集来跟踪它们(创建一个键值,该键值对于具有相同顺序的相同值的每个集都是相同的,级联字符串负责此操作)。

您的代码在我的系统上运行了13秒,而我的则需要17秒。现在,如果运行时至关重要,您可能需要研究哈希技术。但是我不确定您是否能够节省很多,因为您将不得不添加一些额外的代码,这将花费更多的时间。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/418592.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号