Skip to content

387. First Unique Character in a String#4

Open
philip82148 wants to merge 10 commits into
mainfrom
solve/4.first-unique-character-in-a-string
Open

387. First Unique Character in a String#4
philip82148 wants to merge 10 commits into
mainfrom
solve/4.first-unique-character-in-a-string

Conversation

@philip82148
Copy link
Copy Markdown
Owner

@philip82148 philip82148 commented Nov 22, 2024

注意

ファイルが複数ある場合、<番号>.cppの<番号>が最も大きいものが最新版です!
最新版のみレビューお願いします!(もちろん過去のものをレビューしていただいても構いませんmm)

なお、Ex*.cppとなったものがある場合は問題設定を少し変更して解いたものになっています。(PRコメントにやり取りがあると思います。)

問題

https://leetcode.com/problems/first-unique-character-in-a-string/description/?envType=problem-list-v2&envId=xo2bgr0r

// なお以下で出てくるコメントはプロダクト版にも書くつもりのコメント
class Solution {
public:
int firstUniqChar(string s) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

string sがストリームで与えられるならどうしますか?

int firstUniqChar(char c);

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.

Ex1.cppに解かせていただきました!

public:
SolutionStream() : is_repeated(26), last_indexes(26, -1), index(0) {}

int firstUniqChar(char 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.

charではなくてintだったらどうでしょうか?ストリームなので、インデックスを返すのではなく、一度だけ出現した数の内、最初に出現したものを返すとしましょう。

int firstUniqueNum(int c);

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.

作ってみます!
少々お待ちください…

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.

Ex2.cppに解かせていただきました!

class Solution {
public:
int firstUniqChar(string s) {
vector<int> indexes(26, -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.

#4 にもコメントで書いたのですが、固定長でサイズが小さいのであれば、 int indexes[26]; でよいと思います。宣言してから -1 で埋める必要はあります。

public:
int firstUniqChar(string s) {
vector<int> indexes(26, -1);
vector<bool> is_repeated(26);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

vector の特殊化についてはご存じでしょうか?

https://cpprefjp.github.io/reference/vector/vector.html

vector は bool 型に対して特殊化されている。

この特殊化はメモリ領域を最小化するために提供されていて、各要素は1bitの領域のみを必要とする。

vector::reference は bool への参照ではなく、領域内の1bitを指す型であり、以下のようなインタフェースである ( noexcept はC++11から、 constexpr はC++20から付加される)。

C++23には vector::iterator が出力イテレータとなるために、 vector::reference が const 修飾を持つ bool からの代入演算子が追加され、 indirectly_writable<vector::iterator, bool> がモデルを満たすようになった。

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.

はい、存じております
いつも|=や&=をなぜオーバロードしなかったのだろうと疑問に思います
(小田さんが言っていた調べる力的には多分どっかに議論があると思うので調べた方がいいなと思っています…(時間のある時に))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

https://stackoverflow.com/questions/28882959/operator-on-stdvectorbool
Stack Overflow にあるレベルならばこれです。委員会レベルの話には見当たりませんでした。

class Solution {
public:
int firstUniqChar(string s) {
vector<int> indexes(26, -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.

step2 のようにカウントする方法のほうがシンプルでよいと思います。

@philip82148
Copy link
Copy Markdown
Owner Author

すみませんが、フォルダ構成を変更したので今までのレビューがFiles Changedから見れなくなっていますmm
今までのレビューはこちらですmm

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