leetcode lengthOfLongestSubstring
2016-03-07 16:37
281 查看
public static int lengthOfLongestSubstring(String s) { boolean exit[]=new boolean[2024]; for(int i=0;i<1024;i++) { exit[i]=false; } if(s.length()==0) { return 0; } if(s.length()==1) { return 1; } int tail=0,head=1; exit[s.charAt(tail)]=true; int result=0; int current=1; while(head<s.length()) { char c=s.charAt(head); if(exit[c]) { if(result<current) { result=current; } while(tail<s.length()&&s.charAt(tail)!=c) { exit[s.charAt(tail)]=false; current--; } tail++; } else { exit[c]=true; current++; } head++; } if(result<current) { result=current; current=1; } return result; }
牛人写的代码
思想是一样的
class Solution { public: int lengthOfLongestSubstring(string s) { vector<int> table(256, 0); int maxstr=0, track=0; for(int i=0;i<s.length();i++) { while(table[s[i]])table[s[track++]]=0; table[s[i]]=1; maxstr=max(maxstr, i-track+1); } return maxstr; }
相关文章推荐
- webservice开发(cxf)
- NaN
- AFNetworking 全部类的详解 转
- 关于ehcache缓存中eternal及timeToLiveSeconds和timeToIdleSeconds的说明
- Android串口通信
- 第一次关于计算机大数据的感悟
- Android 从app 携带参数 跳转到其他app的指定界面
- centos 6.6 Nginx 安装配置(已纠正)
- SQL语句大全
- python编写网页爬虫脚本并实现APScheduler调度
- 如何安全退出已调用多个Activity的Application?
- 软件项目管理 数独游戏项目
- 故障案例,mongo副本集主节频繁切换
- 虚拟机下centos 6网卡的添加
- 排序算法-基数排序
- debian修改配置软件源
- MyBatis XML 映射配置文件
- SharePoint 2010 - 无法发布InfoPath表单到SharePoint Library或Site Content Type的解决方案
- 字符串比较函数
- Eclipse下的Android JNI 开发