Skip to content

387.-First-Unique-Character-in-a-String#14

Open
maeken4 wants to merge 1 commit into
mainfrom
387.-First-Unique-Character-in-a-String
Open

387.-First-Unique-Character-in-a-String#14
maeken4 wants to merge 1 commit into
mainfrom
387.-First-Unique-Character-in-a-String

Conversation

@maeken4
Copy link
Copy Markdown
Owner

@maeken4 maeken4 commented Jul 28, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit ですが、md 配下に code block 書くと syntax highlight ちゃんと機能していない箇所がありますね。VS Code ではちゃんと機能しているのでGitHub の仕様っぽいですが。
image

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.

md中に空行があると変になってる気がしますね。
自分は見にくいときはrich diffから見ています。
image

// Return the index of the first character which appears exactly once in the string,
// or -1 if no such character exists.
int firstUniqChar(const std::string& s) {
std::map<char, int> char_to_first_appear_index;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

char_to_first_index くらいでよいように思います。

int firstUniqChar(const std::string& s) {
std::set<char> repeated_chars;
std::map<char, int> candidate_char_to_index;
for (int i = 0; i < s.size(); ++i) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

C++ のお作法よくわからないのですが、ここについては代入後の式の値を使うわけではないので ++i でも i++ でも挙動に違いがないんですね。

https://qiita.com/suuungwoo/items/e054fdcb5a4805bb226b
https://qiita.com/baby-degu/items/901f22c7758d3f619a33

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.

この場面では前置でも後置でもあまり違いはないと思います。後置インクリメントit++については、インクリメント前の値が必要でコピーが生成されるため、必要がない限り使わない使わないほうがよいようです。そのため紛らわしいので基本前置インクリメントを使うようにしています。
irohafternoon/LeetCode#22 (comment)

auto c = s[i];
if (repeated_chars.contains(c)) {
continue;
} else if (candidate_char_to_index.contains(c)) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

前で cotinue しているので if で良いと思います。

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.

ifで十分なことを見落としていました。ありがとうございます。

public:
int firstUniqChar(const std::string& s) {
std::set<char> repeated_chars;
std::map<char, int> candidate_char_to_index;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

unique_char_to_index の方が実態を表しているような気がします。

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.

なるほど。後で消しうるというのでcandidateとしたのですが、それも確かにそうですね…

#include <string>

class Solution {
public:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

空白を 3 個入れ停電とするのはあまり見かけないように思います。 public: であればインデント無しか空白 2 個が多いように思います。通常の行のインデントが 2 個の場合は、 1 個にする場合もあるようです。

参考までにスタイルガイドへのリンクを貼ります。

https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace

// Spaces around the colon in inheritance and initializer lists.
class Foo : public Bar {
 public:

上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。また、所属するチームにより何が良いとされているかは変わります。自分の中で良い書き方の基準を持ちつつ、チームの平均的な書き方で書くことをお勧めいたします。

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.

ありがとうございます。違和感がありつつもフォーマッタの動作に任せてしまっていたのですがやはり一般的ではないのですね。よく見られる書き方に合わせようと思います。

std::map<char, int> char_to_first_appear_index;
for (int i = 0; i < s.size(); ++i) {
if (char_to_first_appear_index.contains(s[i])) {
char_to_first_appear_index[s[i]] = -1;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

-1 のような特別な値を使うと、バグを購入しやすくなる印象があります。定数においてあげれば、やや緩和できるように感じます。

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.

非負整数でないことを表現したかったのですが確かにマジックナンバーになってしまってますね。このような数字を使わざるを得ないときはせめて定数化するようにします。

https://github.com/quinn-sasha/leetcode/pull/15/files?short_path=5ec7c3c#diff-5ec7c3c87171edf4d61e9eb79fd926cafa27caf068da7474222897c8e9e7ab96
```cpp:step2-3.cpp
#include <algorithm>
#include <map>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

mapではなくunordered_mapを使うことを検討しても良いかもしれません。
(C++素人なので的外れだったらご放念ください。)
参考:https://note.com/modern_ferret431/n/n48b154df7362

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