-
Notifications
You must be signed in to change notification settings - Fork 0
392. Is Subsequence #7
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #if __has_include("../debug.hpp") | ||
| #include "../debug.hpp" | ||
| #endif | ||
| // ここまでローカルでのデバッグ用なので気にしないでください -------------------- | ||
|
|
||
| #include <bits/stdc++.h> | ||
|
|
||
| using namespace std; | ||
|
|
||
| // <時間> | ||
| // 1分 | ||
| // <感想> | ||
| // <疑問・不安点(・個人的な感想、違う意見があれば教えてほしいもの)> | ||
| // - 20行目の後に改行を入れるかどうか | ||
| // - indexの命名をこだわるか迷った。 | ||
| // 他の候補としてはsiとか(sのイテレータみたいな)。 | ||
| // リーダブルコードあったが、これは複数イテレータがあったときに | ||
| // 区別しやすいという話なので今回はしなかった | ||
|
|
||
| // なお以下で出てくるコメントはプロダクト版にも書くつもりのコメント | ||
| class Solution { | ||
| public: | ||
| bool isSubsequence(string s, string t) { | ||
| if (s.size() == 0) return true; | ||
| int index = 0; | ||
| for (auto c : t) { | ||
| if (s[index] == c) { | ||
| ++index; | ||
| if (index == s.size()) return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 外側を s で回して、内側を t で回したほうがコードが自然になる気はしますね。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こんな感じになりました、、 // <時間>
// 10分
// <感想>
// <疑問・不安点(・個人的な感想、違う意見があれば教えてほしいもの)>
// なお以下で出てくるコメントはプロダクト版にも書くつもりのコメント
class Solution {
public:
bool isSubsequence(string sub, string s) {
int s_i = 0;
for (int sub_i = 0; sub_i < sub.size(); ++sub_i) {
while (s_i < s.size() && s[s_i] != sub[sub_i]) ++s_i;
if (s_i == s.size()) return false;
++s_i;
}
return true;
}
};There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. 初見時、s_i と sub_i を空目したので、
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yoshiki-Iwasa 確かにそうですね |
||
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.
s, t はどっちがどっちか分からないので避けたいですね。以下、Chrome での例。
https://source.chromium.org/chromium/infra/infra_superproject/+/main:build/recipes/recipe_modules/chromium_tests/tests/api/set_swarming_test_execution_info.py?q=is.%3Fsubsequence
https://source.chromium.org/chromium/infra/infra_superproject/+/main:build/recipes/recipe_modules/chromium_orchestrator/tests/api/trybot_steps.py?q=is.%3Fsubsequence
https://source.chromium.org/chromium/chromium/src/+/main:third_party/devtools-frontend/src/extensions/cxx_debugging/third_party/llvm/src/clang/unittests/AST/RandstructTest.cpp?q=is.%3Fsubsequence
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.
ありがとうございます!LeetCodeに与えられたものなので気にしてなかったですが、確かにちゃんとした方が良いですね。
リファレンスくださりありがとうございます!