Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InterviewGuide 勘误 #16

Open
Cloud-cat opened this issue Sep 26, 2021 · 1 comment
Open

InterviewGuide 勘误 #16

Cloud-cat opened this issue Sep 26, 2021 · 1 comment
Labels

Comments

@Cloud-cat
Copy link

错误题目 : 精选力扣300+题目之字符串 Easy 859. 亲密字符串
具体内容 : 第二版参考答案

bool buddyStrings(string A, string B) {
    if (A.size() != B.size() || A.size() < 2) return false;
    int len = A.size();
    vector<int> res;
    res.reserve(len);
    for (int i = 0; i < len; ++i)
    {
        if (A[i] != B[i])
        {
            res.push_back(i);
            if (res.size() > 2) return false;
        }
    }
    

    if (res.size() == 0)
    {
        unordered_set<char> misMatch(A.begin(), A.end());
        return misMatch.size() < len;
    }
    return A[res[0]] == B[res[1]] && A[res[1]] == B[res[0]];
}

测试用例"abcb""abcd"res.size()1时,缺少判断return false导致res[1]报错,故应添加一行代码如下:
if (res.size() == 1) return false;

@forthespada
Copy link
Owner

赞!很细心,已经补充,感谢~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants