-
Notifications
You must be signed in to change notification settings - Fork 0
3. Longest Substring Without Repeating Characters #52
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
| まず思いついた方法は総当たりで解く方法。 | ||
| substringを作るための開始点と終了点の2つのインデックス用意する。 | ||
| setに一文字ずつ記録し重複があるのかないのか管理。 | ||
| TLEにはならないが、2つのループで全ての位置を試しているため時間計算量がO(n^2)、空間計算量はO(n) |
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.
特に問題ないです。計算量は文字の種類が m とすると、O(mn) でも抑えられてしまいそうですね。
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.
@oda
レビューありがとうございます。総当たりだからすぐ「だめ」とみなす姿勢は改めないとですね。
| class Solution { | ||
| public: | ||
| int lengthOfLongestSubstring(string s) { | ||
| int longest_length = 0; |
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.
どちらでもいいんですがmax_len(gth)という選択肢もあります(短いので)
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.
@philip82148 レビューありがとうございます。そうですね。少しlongest_lengthは長いと感じてました。。。
| public: | ||
| int lengthOfLongestSubstring(string s) { | ||
| int longest_length = 0; | ||
| map<char, int> letters_to_index; |
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.
mapなら単数to単数/複数がいいと思います!letter_to_index等
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.
また、今回の場合ascii128文字なので、配列にするのもありです。
そうすると-1で初期化出来て、letter_to_index[s[i]] = i;と言葉通りに出来ます。
(max()の中で1を足す)
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.
| int longest_length = 0; | ||
| map<char, int> letters_to_index; | ||
| int left = 0; | ||
| for (int right = 0; right < s.size(); right++) { |
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.
僕はleftやright、begin/endが半開区間([begin, end))のイメージがあり、(もしかしたらそれを気にしないで書くことも多いのかもしれませんが)、ちょっと余計なことを考えてしまいそうなので、僕の回答では[begin, i]で書いていましたね。(どちらでもいいと思います)
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.
僕はleftやright、begin/endが半開区間([begin, end))のイメージがあり
これはbinary searchの問題を解く中でleft right が必ずしも半開区間という考え方は無くなりました。
僕の回答では[begin, i]で書いていましたね。(どちらでもいいと思います)
この辺りは好みでしょうか。たくさんレビューいただきありがとうございました!
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.
ご参考までに共有ですが、リーダブルコードには、begin/endが [begin, end)半開区間、start/lastが [start, last]閉区間のイメージということが記載されていました。
問題へのリンク
https://github.com/Ryotaro25/leetcode_first60/compare/problem48?expand=1
問題文(プレミアムの場合)
備考
次に解く問題の予告
https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cppとbrute_force.cppとmemo.mdとなります。
memo.md内に各ステップで感じたことを追記します。