Conversation
|
この問題に出てきたFollow upがどういう意図なのかよくわからなかった。一文字ずつ見ていくのではなく、もっと効率的に処理できるようにコードを変更しろということなのだろうか?例えば、
参考になりそう→fhiyo/leetcode#55 |
| おそらくこれが一番シンプルなのではないだろうか。 | ||
| */ | ||
| func isSubsequenceStep3(s string, t string) bool { | ||
| i, j := 0, 0 |
There was a problem hiding this comment.
for文の外でも使うならi, jの命名をしてもいいと思いました。
s, tのどちらに対応しているくらいが分かると嬉しいです。
s_index, t_indexとかでしょうか。
| */ | ||
| func isSubsequenceStep3(s string, t string) bool { | ||
| i, j := 0, 0 | ||
| for i < len(s) && j < len(t) { |
There was a problem hiding this comment.
i < len(s) && j < len(t)と条件を並べられてしまうと、jを進めて同じ文字のときにiを進めるという意味が読み取りづらいかと思いました。
step2のコードが意図を把握しやすかったです。
| func isSubsequenceStep1(s string, t string) bool { | ||
| current := 0 | ||
| for i := 0; i < len(s); i++ { | ||
| for { |
There was a problem hiding this comment.
https://docs.python.org/3/library/stdtypes.html#str.find
Python の .find(sub, start) 相当のものがあるといいですが、なさそうですね。
https://pkg.go.dev/strings#Index
というように時々標準ライブラリーを調べておくといいです。
| } | ||
| current := 0 | ||
| for i := 0; i < len(t); i++ { | ||
| if s[current] == t[i] { |
There was a problem hiding this comment.
currentだとあんまり情報量がない気もするので、こっちもsi, ti(Goではこういう感じで省略するんでしたっけ?)でいいかもです
Is Subsequenceを解きました。レビューをお願い致します。
問題:https://leetcode.com/problems/is-subsequence/
言語:Go
すでに解いた方々:
hayashi-ay/leetcode#64
shining-ai/leetcode#57
goto-untrapped/Arai60#19
SuperHotDogCat/coding-interview#21
nittoco/leetcode#16
Mike0121/LeetCode#25
正規表現で解くということもできると言えばできる(計算理論をちゃんと理解できていないので勉強しなければ、、):goto-untrapped/Arai60#19 (comment)