-
Notifications
You must be signed in to change notification settings - Fork 0
387. First Unique Character in a String #16
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
良いと思います
Pythonだとアルファベットに対するdictやlistを用意してカウントする方法が存在する | ||
https://discord.com/channels/1084280443945353267/1233603535862628432/1237973103670198292 | ||
vectorで実装してみる。 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
他に、Java だと LinkedHashMap というのがあります。順序のある HashMap です。これ Python で誰か実装していましたが C++ はまだないんじゃないでしょうか。
また、C++ だと、map が順序通りに並んでいるので、
map<char, int> first_index;
set duplicated;
map<int, char> first_index_to_char; // Remove if duplicated.
でできるはずです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
お疲れ様です。
下記のやり取りを参考に、liked hash mapでの実装を行いました。
d38adca
参考にしたコードやり取り
nittoco/leetcode@5fa1002?short_path=c637421#diff-c6374213bbbe17c2850ccd0bfb422c1f5bf40576b93320647cbfd9e9e3724c74
https://discord.com/channels/1084280443945353267/1201211204547383386/1211052303134629888
public: | ||
int firstUniqChar(string s) { | ||
map<char, int> letter_to_count; | ||
for (auto& letter : s) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
参照はどのように実装されているか分かりますか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@liquo-rice
遅くなりました。
&を使った呼び出し方とは異なりますが、参照をstep4.cppに実装してみました。
9915a08
for (auto letter : s) { | ||
frequency[letter - 'a']++; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C++わからないので質問みたいになってしまうのですが、これで英小文字以外が来た場合どのような挙動になりますか。(この辺 見ましたがどのページをよく見ればいいのかよくわからず。。。)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nittoco
https://cplusplus.com/reference/vector/vector/operator%5B%5D/
zより大きい文字がきたら範囲外アクセスでクラッシュするのではないでしょうか?
また、aより小さい文字が来たらunsined intに負数が代入されるので未定義動作が引き起こされるかもしれません(すいません自分もCpp詳しくないのではっきりとはいえません...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Yoshiki-Iwasa
おお、cplusplus.comというリファレンスもあるのですね。ありがとうございます
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nittoco @Yoshiki-Iwasa
コメントありがとうございます。返信できておらず申し訳ございません。
s consists of only lowercase English letters.
上記の制約から考えておりませんでした。
挙動分からなかったのでオンラインエディタ (Paiza)で試してみましたがエラーになりませんでした。
画像はPaizaのものですが、https://www.onlinegdb.com/online_c++_compiler
でも同じコードを実行しましたがエラーは出ませんでした。
なぜこのような挙動になるのか理由が分からなかったので調べてみました。公式ドキュメントなどは見つけられませんでしたが下記の理由はございました。
The reason for making operator[] be the one that doesn't perform the checks is because this is exactly how [] works for raw arrays and pointers. There is no sanity check in C/C++ for accessing raw arrays/pointers. The burden is on you to do that checking if it is needed.
https://stackoverflow.com/questions/12896773/why-i-dont-get-an-exception-when-using-operator-with-index-out-of-range-in-s
公式には動作未定義になると記載ございます。
Accessing a nonexistent element through this operator is undefined behavior.
https://en.cppreference.com/w/cpp/container/vector/operator_at
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます!資料まで詳しく感謝です🙇
これは知らなかったです。
問題へのリンク
https://leetcode.com/problems/first-unique-character-in-a-string/description/
問題文(プレミアムの場合)
備考
次に解く問題の予告
First Unique Character in a String
フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cppとvector.cppとmemo.mdとなります。
memo.md内に各ステップで感じたことを追記します。