Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 时间复杂度算法综述 我想知道这在最坏的情况下是否有效,如Θ(n+m)_Algorithm_Complexity Theory_Time Complexity - Fatal编程技术网

Algorithm 时间复杂度算法综述 我想知道这在最坏的情况下是否有效,如Θ(n+m)

Algorithm 时间复杂度算法综述 我想知道这在最坏的情况下是否有效,如Θ(n+m),algorithm,complexity-theory,time-complexity,Algorithm,Complexity Theory,Time Complexity,n是搜索的i_字符串的大小,m是查找的i_子字符串的大小 2.是否有任何程序建议检查给定代码的时间复杂度,我更喜欢一个公认的免费工具 多谢各位 public static boolean isSubstring(String i_StringForSearch, String i_SubStringToFind) { int strForSearchIndex = 0; int subStrToFindIndex = 0; boolean

n
是搜索的
i_字符串的大小,
m
是查找的
i_子字符串的大小

2.是否有任何程序建议检查给定代码的时间复杂度,我更喜欢一个公认的免费工具

多谢各位

public static boolean isSubstring(String i_StringForSearch, String i_SubStringToFind)
    {
        int strForSearchIndex = 0;
        int subStrToFindIndex = 0;
        boolean endOfStringToSearch = false;
        boolean foundSubString = false;
        boolean isThereASequenceOfMatching = false;


        while(!endOfStringToSearch && !foundSubString)
        {
            if(strForSearchIndex == i_StringForSearch.length())
            {
                endOfStringToSearch = true;
            }

            else if(i_StringForSearch.charAt(strForSearchIndex) == i_SubStringToFind.charAt(subStrToFindIndex))
            {
                isThereASequenceOfMatching = true;
                if(subStrToFindIndex == i_SubStringToFind.length() -1 )
                {
                    foundSubString = true;
                }
                subStrToFindIndex++;
                strForSearchIndex++;
            }

            else if(i_StringForSearch.charAt(strForSearchIndex) != i_SubStringToFind.charAt(subStrToFindIndex))
            {
                if(isThereASequenceOfMatching)
                {
                    subStrToFindIndex = 0;
                    isThereASequenceOfMatching = false;
                }
                strForSearchIndex++;
            }
        }

       return foundSubString;
    }
  • 回答您的问题:是的,算法是
    O(n+m)
    。每个 迭代增加了strForSearchIndex,因此最多有
    n
    迭代。[这是假设
    i\u StringForSearch.length()
    已完成 在
    O(1)
    中,这通常是正确的]

  • 该算法与
    isSubstring(“aaab”、“aab”)==false
    的反例错误

  • 你可能想看看和/或和/或


  • 我认为算法是错误的
    isSubstring(“aaab”,“aab”)==false
    好吧,一个计算时间复杂度的免费工具怎么样,你知道吗?@JavaSa:这样的算法不存在。它与算法密切相关(没有通用算法可以确定另一个算法是否会停止)。我相信有一些启发法,但恐怕我不熟悉。