Skip to content

242. Valid Anagram#9

Merged
colorbox merged 2 commits intomainfrom
242
Nov 11, 2024
Merged

242. Valid Anagram#9
colorbox merged 2 commits intomainfrom
242

Conversation

@colorbox
Copy link
Copy Markdown
Owner

Copy link
Copy Markdown

@hayashi-ay hayashi-ay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

良いと思います。

class Solution {
public:
bool isAnagram(string s, string t) {
if (s.length() != t.length()) { return false; }
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

早期に枝刈りしたいのであれば、この文を入れても良いと思います。なくても良いと思います。

242/2.cpp Outdated
char_counter[c]--;
}

for (const pair<char, int> pair : char_counter) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (const auto& pair : char_counter) { としたほうがシンプルだと思います。

また、 pair<char, int> であれば大丈夫なのですが、サイズが sizeof(int64_t) を超えるオブジェクトで拡張 for 文を回す際は、 const 参照で回したほうが良いと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

型を明示できる方が良いと思っていましたが、autoを適宜使っていきます。

const参照勉強になります。
sizeof(int64_t)の閾値についても気にするようにします。

242/2.cpp Outdated
}

for (const pair<char, int> pair : char_counter) {
if (char_counter[pair.first]) { return false; }
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これ、pair.second では?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

char_counterを介する意味ないですね、冗長でした

@colorbox colorbox changed the title Add codes for 242. Valid Anagram 242. Valid Anagram May 13, 2024
@rihib rihib mentioned this pull request Aug 4, 2024
@colorbox colorbox merged commit bae6248 into main Nov 11, 2024
@colorbox colorbox deleted the 242 branch November 11, 2024 02:30
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

Successfully merging this pull request may close these issues.

4 participants