可以这样学习算法:
Mac有神器CodeRunner,可是只能运行不能调试。
所以我用XCode的新建OSX Application下的Command Line Tool项目:
然后 打断点!!
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1.
提示 Hash Table
Two pointers
找到 最长的 没有重复子母的 字符串的 长度!
LeetCode上给了参考答案:
我把它写到了XCode中:
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int lengthOfLongestSubstring(string s) {
int n = (int)s.length();
int i = 0, j = 0;
int maxLen = 0;
bool exist[256] = { false };
while (j < n) {
if (exist[s[j]]) {
maxLen = max(maxLen, j-i);
while (s[i] != s[j]) {
exist[s[i]] = false;
i++;
}
i++;
j++;
} else {
exist[s[j]] = true;
j++;
}
}
maxLen = max(maxLen, n-i);
return maxLen;
}
int main(int argc, const char * argv[]) {
string s = "abcabcbb";
int length = lengthOfLongestSubstring(s);
printf("length: %d\n", length);
return 0;
}
LeetCode -- Longest Substring Without Repeating Characters
原文:http://blog.csdn.net/mennoa/article/details/46239069