From a51cfa673c1d47682c80007ec77b27e28ba70b58 Mon Sep 17 00:00:00 2001 From: philip82148 <92205270+philip82148@users.noreply.github.com> Date: Mon, 18 Nov 2024 00:59:22 +0900 Subject: [PATCH 1/6] solve 3.longest substring ... --- ...Substring-Without-Repeating-Characters.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 01-15/3.Longest-Substring-Without-Repeating-Characters.cpp diff --git a/01-15/3.Longest-Substring-Without-Repeating-Characters.cpp b/01-15/3.Longest-Substring-Without-Repeating-Characters.cpp new file mode 100644 index 0000000..aa27f27 --- /dev/null +++ b/01-15/3.Longest-Substring-Without-Repeating-Characters.cpp @@ -0,0 +1,27 @@ +#if __has_include("../debug.hpp") +#include "../debug.hpp" +#endif +// ここまでローカルでのデバッグ用なので気にしないでください -------------------- + +#include + +using namespace std; + +class Solution { + public: + int lengthOfLongestSubstring(string s) { + vector ascii_to_last_index(256, -1); + int max_len = 0, unique_string_begin = 0; + + for (int i = 0; i < s.size(); ++i) { + if (ascii_to_last_index[s[i]] >= unique_string_begin) { + unique_string_begin = ascii_to_last_index[s[i]] + 1; + } + + // [unique_string_begin, i]が重複なしの範囲 + max_len = max(max_len, i - unique_string_begin + 1); + ascii_to_last_index[s[i]] = i; + } + return max_len; + } +}; From 83a9d22d53ab8527f311ec23190476f527497e2d Mon Sep 17 00:00:00 2001 From: philip82148 <92205270+philip82148@users.noreply.github.com> Date: Wed, 20 Nov 2024 23:28:19 +0900 Subject: [PATCH 2/6] add parent folder --- .../1.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 01-15/{3.Longest-Substring-Without-Repeating-Characters.cpp => 3.Longest-Substring-Without-Repeating-Characters/1.cpp} (100%) diff --git a/01-15/3.Longest-Substring-Without-Repeating-Characters.cpp b/01-15/3.Longest-Substring-Without-Repeating-Characters/1.cpp similarity index 100% rename from 01-15/3.Longest-Substring-Without-Repeating-Characters.cpp rename to 01-15/3.Longest-Substring-Without-Repeating-Characters/1.cpp From ca070792b34f5a3c2bbe3a4e36a728289878bd5f Mon Sep 17 00:00:00 2001 From: philip82148 <92205270+philip82148@users.noreply.github.com> Date: Wed, 20 Nov 2024 23:35:23 +0900 Subject: [PATCH 3/6] improve code --- .../1.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/01-15/3.Longest-Substring-Without-Repeating-Characters/1.cpp b/01-15/3.Longest-Substring-Without-Repeating-Characters/1.cpp index aa27f27..d0a4dc3 100644 --- a/01-15/3.Longest-Substring-Without-Repeating-Characters/1.cpp +++ b/01-15/3.Longest-Substring-Without-Repeating-Characters/1.cpp @@ -10,17 +10,19 @@ using namespace std; class Solution { public: int lengthOfLongestSubstring(string s) { - vector ascii_to_last_index(256, -1); - int max_len = 0, unique_string_begin = 0; + vector char_code_to_last_index(256, -1); + int max_len = 0, unique_str_begin = 0; for (int i = 0; i < s.size(); ++i) { - if (ascii_to_last_index[s[i]] >= unique_string_begin) { - unique_string_begin = ascii_to_last_index[s[i]] + 1; + unsigned char char_code = s[i]; + + if (char_code_to_last_index[char_code] >= unique_str_begin) { + unique_str_begin = char_code_to_last_index[char_code] + 1; } - // [unique_string_begin, i]が重複なしの範囲 - max_len = max(max_len, i - unique_string_begin + 1); - ascii_to_last_index[s[i]] = i; + // [unique_str_begin, i]が重複なしの範囲 + max_len = max(max_len, i - unique_str_begin + 1); + char_code_to_last_index[char_code] = i; } return max_len; } From 274b2768aa0d0b5c0721c2b86a4b4bbb82d2f9e4 Mon Sep 17 00:00:00 2001 From: philip82148 <92205270+philip82148@users.noreply.github.com> Date: Thu, 21 Nov 2024 00:11:20 +0900 Subject: [PATCH 4/6] add comment --- .../1.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/01-15/3.Longest-Substring-Without-Repeating-Characters/1.cpp b/01-15/3.Longest-Substring-Without-Repeating-Characters/1.cpp index d0a4dc3..c449abd 100644 --- a/01-15/3.Longest-Substring-Without-Repeating-Characters/1.cpp +++ b/01-15/3.Longest-Substring-Without-Repeating-Characters/1.cpp @@ -7,6 +7,22 @@ using namespace std; +// <時間> +// 不明(次から測る) +// <選定事項と理由> +// mapではなくvector:最悪ケース(256文字すべて使う)のメモリはどちらも同じだから。 +// <疑問・不安点(・個人的な感想、違う意見があれば教えてほしいもの)> +// - unique_str_beginの代わりにbegin。流石にbeginじゃ分からない? +// 他の人ので単にleftとしているものがあったが、これでもよいだろうか? +// Goで書かれるとなぜか短くても理解できてしまうところがある。 +// - iの代わりにend。これだとなんのendか分からない。ならunique_str_endは? +// これもunique_str_end単体で使う場面があって気持ち悪い。 +// - begin&end、またはfirst&lastは半開区間[begin,end)を表すイメージがある。 +// 閉区間を表す単語はないものか。left&rightにそのイメージはありますか? +// - char_codeをasciiにしたら文字数が減ること。 +// でもasciiとは限らないと思った(なおその場合char_code_to...は0x80要素用意すれば十分か) + +// なお以下で出てくるコメントはプロダクト版にも書くつもりのコメント class Solution { public: int lengthOfLongestSubstring(string s) { @@ -20,7 +36,7 @@ class Solution { unique_str_begin = char_code_to_last_index[char_code] + 1; } - // [unique_str_begin, i]が重複なしの範囲 + // [unique_str_begin, i]は文字が重複しない範囲 max_len = max(max_len, i - unique_str_begin + 1); char_code_to_last_index[char_code] = i; } From c8141f1cb7bdc9d40da1983b951553d60777364b Mon Sep 17 00:00:00 2001 From: philip82148 <92205270+philip82148@users.noreply.github.com> Date: Mon, 25 Nov 2024 23:54:11 +0900 Subject: [PATCH 5/6] add 2.cpp --- .../2.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 01-15/3.Longest-Substring-Without-Repeating-Characters/2.cpp diff --git a/01-15/3.Longest-Substring-Without-Repeating-Characters/2.cpp b/01-15/3.Longest-Substring-Without-Repeating-Characters/2.cpp new file mode 100644 index 0000000..ca561cf --- /dev/null +++ b/01-15/3.Longest-Substring-Without-Repeating-Characters/2.cpp @@ -0,0 +1,34 @@ +#if __has_include("../../debug.hpp") +#include "../../debug.hpp" +#endif +// ここまでローカルでのデバッグ用なので気にしないでください -------------------- + +#include +#include + +using namespace std; + +// <時間> +// 6分 +// <疑問・不安点(・個人的な感想、違う意見があれば教えてほしいもの)> + +// なお以下で出てくるコメントはプロダクト版にも書くつもりのコメント +class Solution { + public: + int lengthOfLongestSubstring(string s) { + int ascii_to_last_index[128]; + for (int i = 0; i < 128; ++i) ascii_to_last_index[i] = -1; + + int max_len = 0, begin = 0; + for (int i = 0; i < s.size(); ++i) { + if (ascii_to_last_index[s[i]] >= begin) { + begin = ascii_to_last_index[s[i]] + 1; + } + ascii_to_last_index[s[i]] = i; + + // [begin, i]が文字が重複しない範囲 + max_len = max(max_len, i - begin + 1); + } + return max_len; + } +}; From eedd2a4186c591b85f6d6c1a637b824a4d2ffe9e Mon Sep 17 00:00:00 2001 From: philip82148 <92205270+philip82148@users.noreply.github.com> Date: Tue, 15 Apr 2025 12:02:50 +0900 Subject: [PATCH 6/6] add readme --- .../3.Longest-Substring-Without-Repeating-Characters/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 01-15/3.Longest-Substring-Without-Repeating-Characters/README.md diff --git a/01-15/3.Longest-Substring-Without-Repeating-Characters/README.md b/01-15/3.Longest-Substring-Without-Repeating-Characters/README.md new file mode 100644 index 0000000..cf93d4c --- /dev/null +++ b/01-15/3.Longest-Substring-Without-Repeating-Characters/README.md @@ -0,0 +1,3 @@ +# 3. Longest Substring Without Repeating Characters + +