Longest Substring Without Repeating Characters
2015-11-19 13:01
369 查看
题意:
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.
代码:
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.
代码:
class Solution { public: int lengthOfLongestSubstring(string s) { const int SIZE=256; int record[SIZE]; memset(record,-1,sizeof(record));// 当顺序遍历到字符 s[i],s[i] 最后出现的位置 int final_ans=0; int new_start=0; // 这一轮不重复字符串的起点 for(int i=0;i<s.size();++i){ if(record[(int)s[i]]>=new_start) new_start=record[(int) s[i]]+1;//新的起点 if(i-new_start+1>final_ans) // 更新 final_ans final_ans=i-new_start+1; record[(int)s[i]]=i; // 更新 s[i] 最后一次出现的地方 } return final_ans; } };
相关文章推荐
- BAT批处理中的字符串处理详解(字符串截取)
- Lua函数与字符串处理简明总结
- asp.net 常用字符串处理方法
- Oracle 存储过程总结 二、字符串处理相关函数
- 学习php笔记 字符串处理
- PHP字符串处理的10个简单方法
- PHP5 字符串处理函数大全
- Perl 字符串处理备忘录
- 从Discuz里拿出来的PHP字符串加密函数
- 收藏几个支持中文的PHP字符串截取函数
- php字符串截取问题
- php字符串截取中文截取2,单字节截取模式
- PHP字符串 ==比较运算符的副作用
- PHP字符串处理的10个简单方法
- 第4章 数据处理-php字符串的处理-郑阿奇(续)
- 几个有用的php字符串过滤,转换函数代码
- php字符串分割函数explode的实例代码
- PHP字符串的编码问题的详细介绍
- 基于PHP字符串的比较函数strcmp()与strcasecmp()的使用详解
- 探讨各种PHP字符串函数的总结分析