387. First Unique Character in a String#4
Conversation
| // なお以下で出てくるコメントはプロダクト版にも書くつもりのコメント | ||
| class Solution { | ||
| public: | ||
| int firstUniqChar(string s) { |
There was a problem hiding this comment.
string sがストリームで与えられるならどうしますか?
int firstUniqChar(char c);
| public: | ||
| SolutionStream() : is_repeated(26), last_indexes(26, -1), index(0) {} | ||
|
|
||
| int firstUniqChar(char c) { |
There was a problem hiding this comment.
charではなくてintだったらどうでしょうか?ストリームなので、インデックスを返すのではなく、一度だけ出現した数の内、最初に出現したものを返すとしましょう。
int firstUniqueNum(int c);
| class Solution { | ||
| public: | ||
| int firstUniqChar(string s) { | ||
| vector<int> indexes(26, -1); |
There was a problem hiding this comment.
#4 にもコメントで書いたのですが、固定長でサイズが小さいのであれば、 int indexes[26]; でよいと思います。宣言してから -1 で埋める必要はあります。
| public: | ||
| int firstUniqChar(string s) { | ||
| vector<int> indexes(26, -1); | ||
| vector<bool> is_repeated(26); |
There was a problem hiding this comment.
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> がモデルを満たすようになった。
There was a problem hiding this comment.
はい、存じております
いつも|=や&=をなぜオーバロードしなかったのだろうと疑問に思います
(小田さんが言っていた調べる力的には多分どっかに議論があると思うので調べた方がいいなと思っています…(時間のある時に))
There was a problem hiding this comment.
https://stackoverflow.com/questions/28882959/operator-on-stdvectorbool
Stack Overflow にあるレベルならばこれです。委員会レベルの話には見当たりませんでした。
| class Solution { | ||
| public: | ||
| int firstUniqChar(string s) { | ||
| vector<int> indexes(26, -1); |
|
すみませんが、フォルダ構成を変更したので今までのレビューがFiles Changedから見れなくなっていますmm |
注意
ファイルが複数ある場合、<番号>.cppの<番号>が最も大きいものが最新版です!
最新版のみレビューお願いします!(もちろん過去のものをレビューしていただいても構いませんmm)
なお、Ex*.cppとなったものがある場合は問題設定を少し変更して解いたものになっています。(PRコメントにやり取りがあると思います。)
問題
https://leetcode.com/problems/first-unique-character-in-a-string/description/?envType=problem-list-v2&envId=xo2bgr0r