diff options
author | terminaldweller <thabogre@gmail.com> | 2022-04-08 17:02:01 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-04-08 17:02:01 +0000 |
commit | 387a5c7b7de6fff63b3a094b02298bcd4b4271ed (patch) | |
tree | d4fbc67a3384848d3155ab16e20dc56af364e0b9 /3/main.cpp | |
parent | update (diff) | |
download | leetcode-387a5c7b7de6fff63b3a094b02298bcd4b4271ed.tar.gz leetcode-387a5c7b7de6fff63b3a094b02298bcd4b4271ed.zip |
update
Diffstat (limited to '')
-rw-r--r-- | 3/main.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/3/main.cpp b/3/main.cpp new file mode 100644 index 0000000..948a095 --- /dev/null +++ b/3/main.cpp @@ -0,0 +1,47 @@ + +#include "header.hpp" +#include <unordered_map> +#include <utility> + +class Solution { +public: + static int lengthOfLongestSubstring(std::string s) { + std::unordered_map<char, bool> dict; + int max = 0; + int cur = 0; + int begin = 0; + for (int i = 0; i < s.length(); ++i) { + if (dict.find(s[i]) == dict.end()) { + dict.emplace(std::make_pair(s[i], true)); + cur++; + } else { + auto culprit = s.rfind(s[i], i - 1); + if (culprit < begin) { + cur++; + continue; + } + if (cur >= max) { + max = cur; + } + cur = i - culprit; + begin = culprit + 1; + /* std::cout << s[culprit] << "\n"; */ + } + } + + /* std::cout << max << ":" << cur << "\n"; */ + return std::max(max, cur); + } +}; + +int main(int argc, char **argv) { + std::cout << Solution::lengthOfLongestSubstring("abcabcbb") << "\n"; + std::cout << Solution::lengthOfLongestSubstring("bbbbb") << "\n"; + std::cout << Solution::lengthOfLongestSubstring("pwwkew") << "\n"; + std::cout << Solution::lengthOfLongestSubstring(" ") << "\n"; + std::cout << Solution::lengthOfLongestSubstring("au") << "\n"; + std::cout << Solution::lengthOfLongestSubstring("aab") << "\n"; + std::cout << Solution::lengthOfLongestSubstring("dvdf") << "\n"; + std::cout << Solution::lengthOfLongestSubstring("tmmzuxt") << "\n"; + return 0; +} |