-
Notifications
You must be signed in to change notification settings - Fork 0
392. Is Subsequence #10
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
Conversation
|
||
### 思考メモ | ||
* 他の方のPRを見て、ループ一つにつき一文字列を走査するという固定観念に囚われていたことに気づいた | ||
* あまり型にとらわれずに自由な発想で解き方を工夫することを学んだ |
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.
以前同様のフィードバックを受けたのですが、頭から抜けていたようです。癖付けないとですね。ありがとうございます。
問題文下のFollow upも考えて見るといいかもです
|
if (sLength > tLength) return false; | ||
|
||
for (int sIndex = 0; sIndex < sLength; sIndex++) { // traversal of s | ||
if (s.charAt(sIndex) == t.charAt(tIndex) && sIndex == sLength - 1) return true; |
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.
tIndex が配列外アクセスしそうですね。(そもそもこの行がいらない気がします。)
for (int sIndex = 0; sIndex < sLength; sIndex++) { // traversal of s | ||
if (s.charAt(sIndex) == t.charAt(tIndex) && sIndex == sLength - 1) return true; | ||
while (tIndex < tLength && s.charAt(sIndex) != t.charAt(tIndex)) { // traversal of t | ||
if (tIndex == tLength) return false; |
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.
ここ、上の while との関係で実行されません。while の後ろに。
0392_Is_Subsequence/solution.md
Outdated
if (sLength > 0 && tLength > 0 && s.charAt(sIndex) == t.charAt(tIndex)) sIndex++; | ||
if (tLength > 0) tIndex++; |
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.
ありがとうございます。これで十分でしたね。
while (true) {
if (sIndex == sLength) return true;
if (tIndex == tLength) return false;
if (s.charAt(sIndex) == t.charAt(tIndex)) sIndex++;
tIndex++;
}
0392_Is_Subsequence/solution.md
Outdated
int sLength = s.length(); | ||
int tLength = t.length(); |
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.
この変数は置かないほうがいいかもしれません。
sIndex < s.length()
のほうが意味がはっきりしていませんか。
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
t.length()はたしかに変数化する必要はないですね。
一方で、複数回呼び出す処理(ここでは s.length()
)は無思考的に変数に格納してしまっておりました。この程度のオーバーヘッドは無視していいと言われればそのとおりと思いつつ、このあたりのバランス感覚が乏しいので判断基準などあればアドバイスいただけないでしょうか。
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.
なるほど、理解しました。オーバーヘッドを過剰見積もりしていたと同時に、変数を読み手が追跡し続ける必要があるコストを考えると確かになと思いました。以後もう少し柔軟に適用していきたいと思います。
もしご覧になってなかったら、この辺を一通り見て、実際いろんな場合を書いてみるといいかもしれません |
@nittoco 承知しました。ありがとうございます。見てみます。 |
問題
https://leetcode.com/problems/is-subsequence/description/
言語
Java
次の問題
139. Word Break