Skip to content

Conversation

@Yoshiki-Iwasa
Copy link
Owner

問題: https://leetcode.com/problems/longest-substring-without-repeating-characters/description/

次に解く問題: https://leetcode.com/problems/minimum-size-subarray-sum/description/

ファイルの構成
problems/src/<解いた問題名>/以下にstep1~step3のファイルを保存しています

@Yoshiki-Iwasa Yoshiki-Iwasa changed the title Solve 3 3. Longest Substring Without Repeating Characters Aug 8, 2024
Copy link

@fhiyo fhiyo left a comment

Choose a reason for hiding this comment

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

良いと思います!

Comment on lines +23 to +24
let mut head = 0;
let mut tail = 0;
Copy link

Choose a reason for hiding this comment

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

rihib/leetcode#7 (comment) にも書いたんですが、head, tailがindexなの個人的には少し違和感あります。
(が、コメント見たうえでそうでもないよとかあれば教えて下さい)

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます
いわれてみれば確かに... left, rightのほうが自然ですね

なんでhead, tailになれてるかというと、sliding windowってもともとしゃくとり法って名前で知ったんですよね
そこでしゃくとり虫の頭と尻尾を移動する感覚で覚えちゃって、手癖になっていたようです

コメントありがとうござます

// HashMapを使った解法
pub fn length_of_longest_substring_1(s: String) -> i32 {
let mut head = 0;
let mut tail = 0;
Copy link

Choose a reason for hiding this comment

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

こちら参考にしてもらえると🙏
僕もhead, tailを使ってたんですが、Sliding Windowアルゴリズム(またはTwo Pointers)として捉えると、left, rightを使う方が一般的っぽいですね、、

rihib/leetcode#7 (comment)

Copy link

Choose a reason for hiding this comment

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

すいません、fhiyoさんと同時に同じ箇所を指摘してしまいましたw

Copy link
Owner Author

Choose a reason for hiding this comment

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

いえ、ありがとうございます

#42 (comment)

left, rigthのほうが自然だと思います

@rihib
Copy link

rihib commented Aug 8, 2024

コメントした箇所以外は良いと思いました!

Copy link

@thonda28 thonda28 left a comment

Choose a reason for hiding this comment

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

ans という変数名が汎用的すぎて少し引っかかりましたが、それ以外はよさそうに思いました。

半開区間に設定するのも1つの手だと思いましたが、以下のコメントを見るに Rust には不向きみたいですね。
fhiyo/leetcode#49 (comment)

@Yoshiki-Iwasa
Copy link
Owner Author

はい、Rustではindex accessはusizeという符号無し整数(サイズはCPUのbitサイズ次第)で行われるので負の数が表現できないんです
一応、isizeとかi32とかでやる方法もあるんですが、キャスト地獄になって可読性が激下がります

head += 1
}
unique_chars.insert(chars[tail]);
ans = ans.max(tail - head + 1);
Copy link

Choose a reason for hiding this comment

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

https://doc.rust-lang.org/core/primitive.i32.html#method.max
ほー、こんなのがあるんですね。

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.

6 participants