给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
/*
* 滑动窗口法
*/
func lengthOfLongestSubstring(_ s: String) -> Int {
let strlen = s.count
let chs: [Character] = Array(s)
var map = [Character : Int]()
var start = 0
var maxLen: Int = 0
for i in 0..<chs.count {
let char = chs[i]
if let preIdx = map[char] {
start = max(start, preIdx+1)
}
map[char] = i;
maxLen = max(maxLen, i-start+1)
print(start, maxLen)
}
return maxLen
}
/*
* 滑动窗口法
*/
- (NSInteger)lengthOfLongestSubstring:(NSString *)s {
if (s.length < 2) {
return s.length;
}
NSInteger strLen = s.length;
NSInteger maxLen = 0;
NSInteger start = 0;
NSMutableDictionary *map = [NSMutableDictionary dictionary];
for (NSInteger i=0; i