Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/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
Arrays 比较两个数组并返回第一次出现的索引_Arrays_Time Complexity_Complexity Theory - Fatal编程技术网

Arrays 比较两个数组并返回第一次出现的索引

Arrays 比较两个数组并返回第一次出现的索引,arrays,time-complexity,complexity-theory,Arrays,Time Complexity,Complexity Theory,我有一个任务要做,我正在考虑,但我没有找到正确的答案 使用您选择的语言,编写一个函数,该函数获取一个名为str的字符串和一个名为set的字符串 函数将返回str中集合中第一次出现的任何字符的索引 例如: str=“hellohellohellohelloistom!” set=“t98765!” 函数将返回22(str中的“5”索引)。 确保时间复杂度不大于两个字符串的长度-O(m+n) 假设字符串仅包含ASCII字符 我在想,我想用分而治之的方法来做。我有一个基本情况,总是O(1),我把问题分

我有一个任务要做,我正在考虑,但我没有找到正确的答案

使用您选择的语言,编写一个函数,该函数获取一个名为str的字符串和一个名为set的字符串

函数将返回str中集合中第一次出现的任何字符的索引

例如: str=“hellohellohellohelloistom!” set=“t98765!”

函数将返回22(str中的“5”索引)。 确保时间复杂度不大于两个字符串的长度-O(m+n) 假设字符串仅包含ASCII字符

我在想,我想用分而治之的方法来做。我有一个基本情况,总是O(1),我把问题分成更小的问题,直到我得到答案。问题是,使用该解决方案,复杂性将是O(logn)


我认为另一种方法是制作一套。但我仍然不知道如何解决这个问题。有什么想法吗?

这个程序是用Swift编写的

let str = "hellohellohellohelloistom!"
let set = "t98765!"

func findFirstAppearance(str : String , set : String) -> Int? {
    var index : Int?

mainLoop: for setCharacter in set.characters{

    for (indexOfChar,strCharacter) in str.characters.enumerate(){

        if strCharacter == setCharacter{
        index = indexOfChar
            break mainLoop
        }
    }

}

return index
}

print(findFirstAppearance(str, set: set))
print(findFirstAppearance("helloWorld", set: "546Wo"))
或者另一种耗时更少的解决方案

let str = "hellohellohellohelloistom!"
let set = "t98765!"

func findFirstAppearance(str : String , set : String) -> Int? {
    var index : Int?

    mainLoop: for setCharacter in set.characters{

        if let range = str.rangeOfString(String(setCharacter)){

            index = str.startIndex.distanceTo(range.startIndex)

            break
        }

    }

    return index
}

print(findFirstAppearance(str, set: set))
print(findFirstAppearance("helloWorld", set: "546Wo")) 
注:

  • 如果没有找到任何字符,那么它将返回nil
  • 这是区分大小写的比较

  • 希望这能解决你的问题

    这个程序是用Swift编写的

    let str = "hellohellohellohelloistom!"
    let set = "t98765!"
    
    func findFirstAppearance(str : String , set : String) -> Int? {
        var index : Int?
    
    mainLoop: for setCharacter in set.characters{
    
        for (indexOfChar,strCharacter) in str.characters.enumerate(){
    
            if strCharacter == setCharacter{
            index = indexOfChar
                break mainLoop
            }
        }
    
    }
    
    return index
    }
    
    print(findFirstAppearance(str, set: set))
    print(findFirstAppearance("helloWorld", set: "546Wo"))
    
    或者另一种耗时更少的解决方案

    let str = "hellohellohellohelloistom!"
    let set = "t98765!"
    
    func findFirstAppearance(str : String , set : String) -> Int? {
        var index : Int?
    
        mainLoop: for setCharacter in set.characters{
    
            if let range = str.rangeOfString(String(setCharacter)){
    
                index = str.startIndex.distanceTo(range.startIndex)
    
                break
            }
    
        }
    
        return index
    }
    
    print(findFirstAppearance(str, set: set))
    print(findFirstAppearance("helloWorld", set: "546Wo")) 
    
    注:

  • 如果没有找到任何字符,那么它将返回nil
  • 这是区分大小写的比较

  • 希望这能解决你的问题

    由于所有涉及的字符串仅包含ASCII字符,因此使用
    常量内存
    可以在
    O(LengthOf(str)+LengthOf(set))中解决此问题。

    以下是“C”语言的代码:

    //ReturnValues:
    //-1 : if no occurrence of any character of set is found in str
    //value >=0 : index of character in str.
    int FindFirstOccurenceOfAnyCharacterInSet(const char *str, const char *set, int *index_of_set)
    {
        char hash[256];
        int i = 0;
        while(i < 256)
        {
            hash[i] = -1;
            ++i; 
        }
        i = 0;
        while(set[i] != '\0')    
        {
            hash[set[i]] = i;
            ++i;
        }
        i = 0;
        while(str[i] != '\0')
        {
            if(hash[str[i]] != -1)
            {
                *index_of_set = hash[str[i]];
                return i;
            }
            ++i;
        }
        *index_of_set = -1;
        return -1;
    }
    
    //返回值:
    //-1:如果在str中未找到集合的任何字符
    //值>=0:字符串中字符的索引。
    int findfirstoccureofanycharacterinset(const char*str,const char*set,int*index\u of\u set)
    {
    字符散列[256];
    int i=0;
    而(i<256)
    {
    散列[i]=-1;
    ++一,;
    }
    i=0;
    while(设置[i]!='\0')
    {
    hash[set[i]]=i;
    ++一,;
    }
    i=0;
    while(str[i]!='\0')
    {
    if(散列[str[i]!=-1)
    {
    *_集的索引_=hash[str[i];
    返回i;
    }
    ++一,;
    }
    *_集的索引_=-1;
    返回-1;
    }
    
    逻辑的工作原理是记录
    hash
    表中
    set
    的所有字符的位置/索引(大于等于0),然后解析
    str
    ,并检查
    hash
    表中是否存在
    str
    的当前字符


    index\u of_set
    还将报告
    set
    中的字符索引,该索引可在
    str
    中找到。如果
    index\u of_set=-1
    则找不到任何匹配项。

    由于所有涉及的字符串都只包含ASCII字符,因此使用
    常量内存可以在
    O(LengthOf(str)+LengthOf(set))中解决此问题。

    以下是“C”语言的代码:

    //ReturnValues:
    //-1 : if no occurrence of any character of set is found in str
    //value >=0 : index of character in str.
    int FindFirstOccurenceOfAnyCharacterInSet(const char *str, const char *set, int *index_of_set)
    {
        char hash[256];
        int i = 0;
        while(i < 256)
        {
            hash[i] = -1;
            ++i; 
        }
        i = 0;
        while(set[i] != '\0')    
        {
            hash[set[i]] = i;
            ++i;
        }
        i = 0;
        while(str[i] != '\0')
        {
            if(hash[str[i]] != -1)
            {
                *index_of_set = hash[str[i]];
                return i;
            }
            ++i;
        }
        *index_of_set = -1;
        return -1;
    }
    
    //返回值:
    //-1:如果在str中未找到集合的任何字符
    //值>=0:字符串中字符的索引。
    int findfirstoccureofanycharacterinset(const char*str,const char*set,int*index\u of\u set)
    {
    字符散列[256];
    int i=0;
    而(i<256)
    {
    散列[i]=-1;
    ++一,;
    }
    i=0;
    while(设置[i]!='\0')
    {
    hash[set[i]]=i;
    ++一,;
    }
    i=0;
    while(str[i]!='\0')
    {
    if(散列[str[i]!=-1)
    {
    *_集的索引_=hash[str[i];
    返回i;
    }
    ++一,;
    }
    *_集的索引_=-1;
    返回-1;
    }
    
    逻辑的工作原理是记录
    hash
    表中
    set
    的所有字符的位置/索引(大于等于0),然后解析
    str
    ,并检查
    hash
    表中是否存在
    str
    的当前字符

    index\u of_set
    还将报告
    set
    中的字符索引,该索引可在
    str
    中找到。如果
    index\u of\u set=-1
    则未找到任何事件。

    谢谢您的帮助

    下面是C#中的代码

    干杯

    public static int FindFirstOccurenceOfAnyCharacterInSet(string str, string set)
        {
            var hash = new int[256];
            int i = 0;
            while (i < 256)
            {
                hash[i] = -1;
                ++i;
            }
            i = 0;
            do
            {
                hash[set[i]] = i;
                ++i;
            } while (set[i] != '\0' && i < set.Length - 1);
            i = 0;
            while (str[i] != '\0')
            {
                if (hash[str[i]] != -1)
                {
                    return i;
                }
                ++i;
            }
            return -1;
        }
    
    public static int findfirstoccureofanycharacterinset(string str,string set)
    {
    var hash=newint[256];
    int i=0;
    而(i<256)
    {
    散列[i]=-1;
    ++一,;
    }
    i=0;
    做
    {
    hash[set[i]]=i;
    ++一,;
    }while(set[i]!='\0'&&i
    谢谢你的帮助

    下面是C#中的代码

    干杯

    public static int FindFirstOccurenceOfAnyCharacterInSet(string str, string set)
        {
            var hash = new int[256];
            int i = 0;
            while (i < 256)
            {
                hash[i] = -1;
                ++i;
            }
            i = 0;
            do
            {
                hash[set[i]] = i;
                ++i;
            } while (set[i] != '\0' && i < set.Length - 1);
            i = 0;
            while (str[i] != '\0')
            {
                if (hash[str[i]] != -1)
                {
                    return i;
                }
                ++i;
            }
            return -1;
        }
    
    public static int findfirstoccureofanycharacterinset(string str,string set)
    {
    var hash=newint[256];
    int i=0;
    而(i<256)
    {
    散列[i]=-1;
    ++一,;
    }
    i=0;
    做
    {
    hash[set[i]]=i;
    ++一,;
    }while(set[i]!='\0'&&i
    但在答案中,您标记为使用了两个for循环。这意味着解的复杂性是O(n2),或者说Im-worng?确切地说,这就是问题所在。我需要复杂性为O(m+n)。这就是问题所在。有什么想法吗?谢谢谢谢你的帮助,但我想我仍然处于同样的状态,更好但不完美:)哈哈。当我们使用For循环时,复杂性将是O(n),我需要它是O(m+n)。这就是为什么我想到了一个不同的策略。。明白了吗?你的世界