Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions 01-15/7.Is Subsequence/1.cpp
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) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

(コードのやっていることは異なるものですが、シグネイチャーの参考にです。)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!LeetCodeに与えられたものなので気にしてなかったですが、確かにちゃんとした方が良いですね。
リファレンスくださりありがとうございます!

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;
}
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

外側を s で回して、内側を t で回したほうがコードが自然になる気はしますね。

Copy link
Copy Markdown
Owner Author

@philip82148 philip82148 Nov 22, 2024

Choose a reason for hiding this comment

The 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;
  }
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

そうですね。他の人の解法を探すとこれも出てきたと思います。
引数の型は const string& にするかもしれません。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

初見時、s_i と sub_i を空目したので、string sub, string targetとかにして、s_i, t_i などにしたほうが読みやすいと思いました

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

@Yoshiki-Iwasa 確かにそうですね
ありがとうございます!