From efb59f18ec0f8476004f3d894f90f12de2c82c53 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Wed, 20 Jun 2018 22:57:09 +0800 Subject: [PATCH] Create 3.cpp --- LeetCode-CN/3.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 LeetCode-CN/3.cpp diff --git a/LeetCode-CN/3.cpp b/LeetCode-CN/3.cpp new file mode 100644 index 0000000..f34d73b --- /dev/null +++ b/LeetCode-CN/3.cpp @@ -0,0 +1,38 @@ +#include +#include +class Solution { +public: + int lengthOfLongestSubstring(string s) { + int sz = s.size(); + int bin[256]; + int maxlen = 0; + int clen = 0; + int L = 0; + while (L < sz) + { + memset(bin, 0, sizeof(int) * 256); + int i; + for (i = L; i < sz; i++) + { + if (bin[s[i]]) + { + maxlen = clen > maxlen ? clen : maxlen; + clen = 0; + L++; + break; + } + else + { + bin[s[i]] = 1; + clen++; + } + } + if (i == sz) + { + maxlen = clen > maxlen ? clen : maxlen; + break; + } + } + return maxlen; + } +};