小编Sus*_*tta的帖子

找到包含给定单词的最短子串的方法:需要优化

我有一个程序,要求我找到给定String的最短子段,包含一个单词列表.即使我的程序是正确的,我也无法在执行的时间范围内(5秒)交付.我认为问题是由于我使用的复杂(平凡)算法.它由嵌套循环组成,需要多次扫描list_of_words数组.这是我的搜索功能代码.a[]包含由单词存储的原始字符串,b[]包含要找到的用于形成子段的单词列表.String g存储由原始字符串中的单词形成的临时子分段,包括列表中的单词.

private static void search() // Function to find the subsegment with a required list of words
{
   int tail,x;//counters 
   String c[]=new String[b.length]; //initializing a temporary array to copy the list of words array.

   for(int i =0; i<a.length;i++)// looping throw original string array
    {
       System.arraycopy(b, 0, c, 0, b.length);//copying the list of words array to the temporary array

        for (int j=0;j<b.length;j++)//looping throw the temporary array
        { 
            x=0; //counter for temporary array

            if(a[i].equalsIgnoreCase(b[j]))//checking for …
Run Code Online (Sandbox Code Playgroud)

java algorithm

5
推荐指数
1
解决办法
7096
查看次数

标签 统计

algorithm ×1

java ×1