我能够走出阵列界限 [英] I Am Able To Go Outside Array Bounds

查看:70
本文介绍了我能够走出阵列界限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出两个字符串,编写一个方法来确定一个是否为另一个的字词/排列。这是我的方法:

我编写了此函数,以检查2个字符串是否是字谜(如狗和神)。

I wrote this function to check if 2 strings are anagrams (such as dog and god).

在ascii中,a到z为97-122。

In ascii, a to z is 97 - 122.

基本上,我有一系列最初都是假的布尔值。每当我在string1中遇到一个char时,它将其标记为true。

Basically I have an array of bools that are all initially false. Everytime I encounter a char in string1, it marks it as true.

要检查其字谜,我检查string2的任何字符是否为假(如果在string1中遇到,应该为真)。

To check if its an anagram, I check if any chars of string2 are false (should be true if encountered in string1).

我不确定如何运行,但这也可以:arr [num] = true; (不应该工作,因为我没有考虑到ascii从97开始,因此超出了范围。)。

I'm not sure how but this works too: arr[num] = true; (shouldnt work because I dont take into account that ascii starts at 97 and thus goes out of bounds).

(侧面说明:

(Side note: is there a better approach than mine?)

编辑:谢谢您的答复!将仔细阅读每一本。顺便说一句:不是任务。这是来自编码面试练习手册的一个问题

bool permutation(const string &str1, const string &str2)
{
    // Cannot be anagrams if sizes are different
    if (str1.size() != str2.size())
        return false;

    bool arr[25] = { false };

    for (int i = 0; i < str1.size(); i++) // string 1
    {
        char ch = (char)tolower(str1[i]); // convert each char to lower
        int num = ch; // get ascii
        arr[num-97] = true; 
    }

    for (int i = 0; i < str2.size(); i++) // string 2
    {
        char ch = (char)tolower(str2[i]); // convert char to lower
        int num = ch; // get ascii
        if (arr[num-97] == false) 
            return false;
    }

    return true;
}


推荐答案

C ++没有内在的本质数组,使您无法编写超出它们末尾的位置。但是,这样做会违反编译器的合约,因此它可以自由执行其希望的操作(未定义的行为)。

There is nothing inherent in C++ arrays that prevents you from writing beyond the end of them. But, in doing so, you violate the contract you have with the compiler and it is therefore free to do what it wishes (undefined behaviour).

如果需要的话,可以使用 vector 类对数组进行边界检查。

You can get bounds checking on "arrays" by using the vector class, if that's what you need.

作为一种更好的方法,如果您的数组足够大以覆盖每个个可能的字符(这样就不必担心边界检查),那可能会更好,并且不应该太大真值作为计数,以便处理字符串中的重复字符。如果这只是一个真值,那么这里将被视为字谜。

As for a better approach, it's probably better if your array is big enough to cover every possible character (so you don't have to worry about bounds checking) and it shouldn't so much be a truth value as a count, so as to handle duplicate characters within the strings. If it's just a truth value, then here and her would be considered anagrams.

即使您声明它不是 任务,但如果您自己实现它,您仍然会学到更多,所以它只是我的伪代码。基本想法是:

Even though you state it's not an assignment, you'll still learn more if you implement it yourself, so it's pseudo-code only from me. The basic idea would be:

def isAnagram (str1, str2):
    # Different lengths means no anagram.

    if len(str1) not equal to len(str2):
        return false

    # Initialise character counts to zero.

    create array[0..255] (assumes 8-bit char)
    for each index 0..255:
        set count[index] to zero

    # Add 1 for all characters in string 1.

    for each char in string1:
        increment array[char]

    # Subtract 1 for all characters in string 2.

    for each char in string2:
        decrement array[char]

    # Counts will be all zero for an anagram.

    for each index 0..255:
        if count[index] not equal to 0:
            return false
    return true

这篇关于我能够走出阵列界限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆