每日一题(day3)
2016-07-09 16:44
267 查看
Leetcode003
Longest Substring Without Repeating Characters问题int lengthOfLongestSubstring(string s) { int table[256]; int curlen = 0, maxlen = 0, curhead = 0, maxhead = 0; memset(table, -1, sizeof(table)); for (int i = 0; i < s.length(); i++) { if (table[s[i]]<0) { table[s[i]] = i; if (++curlen > maxlen) { maxlen = curlen; maxhead = curhead; } } else { curlen -= table[s[i]] - curhead; for (int j = curhead; j < table[s[i]]; j++) table[s[j]] = -1; curhead = table[s[i]] + 1; table[s[i]] = i; } } return maxlen; }
采用技巧:利用数组记录重复字符出现的最终位置,-1表示未出现
注意问题:长度更新与数组内容替换
相关文章推荐
- javascript Split方法,indexOf方法、lastIndexOf 方法和substring 方法
- 常常会用到的截取字符串substr()、substring()、slice()方法详解
- JS中的substring和substr函数的区别说明
- js substr、substring和slice使用说明小记
- js中substring和substr的详细介绍与用法
- javascript indexOf方法、lastIndexOf 方法和substring 方法
- javascript中String类的subString()方法和slice()方法
- sqlserver substring函数使用方法小结
- JavaScript中使用Substring删除字符串最后一个字符
- JavaScript截取字符串的Slice、Substring、Substr函数详解和比较
- 深入浅析JavaScript字符串操作方法 slice、substr、substring及其IE兼容性
- 3种js实现string的substring方法
- 浅谈Javascript中substr和substring的区别
- mysql截取的字符串函数substring_index的用法
- js中substr,substring,indexOf,lastIndexOf,split,replace的用法详解
- js substring()字符串截取函数
- Js中的substring,substr与C#中的Substring比较
- js中substring和substr的定义和用法
- JavaScript字符串对象substring方法入门实例(用于截取字符串)
- js中substr,substring,indexOf,lastIndexOf的用法小结