Skip to content

392. Is Subsequence#7

Open
philip82148 wants to merge 1 commit into
mainfrom
solve/7.Is-Subsequence
Open

392. Is Subsequence#7
philip82148 wants to merge 1 commit into
mainfrom
solve/7.Is-Subsequence

Conversation

@philip82148
Copy link
Copy Markdown
Owner

注意

ファイルが複数ある場合、<番号>.cppの<番号>が最も大きいものが最新版です!
最新版のみレビューお願いします!(もちろん過去のものをレビューしていただいても構いませんmm)

問題

https://leetcode.com/problems/is-subsequence/description/?envType=problem-list-v2&envId=xo2bgr0r

@philip82148 philip82148 force-pushed the solve/7.Is-Subsequence branch from 713a29e to 9c12265 Compare November 22, 2024 09:06
// なお以下で出てくるコメントはプロダクト版にも書くつもりのコメント
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に与えられたものなので気にしてなかったですが、確かにちゃんとした方が良いですね。
リファレンスくださりありがとうございます!

}
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 確かにそうですね
ありがとうございます!

@Yoshiki-Iwasa
Copy link
Copy Markdown

Yoshiki-Iwasa commented Nov 24, 2024

この問題、他にも解き方の表現に幅があると思うで、Discordを漁ってみると良さそうです(例えば、subをqueueにつめて、一文字ずつ消して最終的に空になったらOKなど)

他の人の、自分だったら選ばなさそうな解法を読んで、なんでこう考えるのかを見るのも自分は勉強になったと思います

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.

3 participants