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

76题提错 #72

Closed
jaydu666 opened this issue Sep 26, 2020 · 4 comments
Closed

76题提错 #72

jaydu666 opened this issue Sep 26, 2020 · 4 comments

Comments

@jaydu666
Copy link

if sFreq[s[left]-'a'] == tFreq[s[left]-'a'] {

这里判断count--条件感觉有误,应该是
if sFreq[s[left]-'a'] <= tFreq[s[left]-'a'] {

@halfrost
Copy link
Owner

@jaydu666 您好,你标记的这里判断条件是 == 是没有问题的。理由如下:

当条件进入到 else 里面的时候,说明右窗口已经无法往右移动了,这个时候 count == len(t),窗口中已经完全包含了 t 字符串。这时窗口中左边界的字母有 2 种情况:

    1. 左边界的字母是 t 中的字母。
    1. 左边界的字母不是 t 中的字母。

如果是情况 1,那么直接移动左边界,缩小窗口即可。如果是情况 2,那么左边界的 freq 一定和 t 中这个字母的 freq 相等,因为此时 count == len(t),那么这个时候要移动左边界,要把 count--。所以这里判断 freq 是否相等是正确的。

你要是把代码改成 <= 提交,代码也是可以通过的,因为 <= 是 == 的 必要不充分条件。所以这里直接写成 == 更准确一些。

@jaydu666
Copy link
Author

jaydu666 commented Sep 29, 2020 via email

@halfrost
Copy link
Owner

你写 <= 这题也能过,不过不准确。

没事,有问题随时提出来一起交流哈~💪🏻

@jaydu666
Copy link
Author

jaydu666 commented Sep 29, 2020 via email

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

No branches or pull requests

2 participants