【LeetCode】Longest Substring Without Repeating Characters 解题报告
【题意】
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.
【思路】
一開始我是用 HashMap 做的,然后就在考虑 字母做key还是下标做key,总之比較麻烦。
后来看到网上普遍用数组来实现 HashMap 的功能。感觉还是挺新的。
至于为什么数组的大小设为256,始终想不明确。我查了下ASCII码表,上面也仅仅有127个字符。A~Z是65~90,a~z是97~112。
以下的代码我认为是已经相当简洁了。參考出处:Java,C++。大家共同学习。
public class Solution {
public int lengthOfLongestSubstring(String s) {
int len = s.length();
if (s == null || len == 0) return 0; int[] table = new int[256];
Arrays.fill(table, -1); int maxlen = 1;
int begin = 0, end = 1;
table[s.charAt(0)] = 0; //important!
while (end < len) {
char endch = s.charAt(end);
if (table[endch] >= begin) {
begin = table[endch] + 1;
}
table[endch] = end;
maxlen = Math.max(maxlen, end-begin+1);
end++;
} return maxlen;
}
}
上面代码有下面优点:它推断有没有反复是通过当前字母在table中的值是不是比子串的開始位置大,这样就不用每次出现反复字母后都须要把之前的字符在table中的值又一次设为-1。
【LeetCode】Longest Substring Without Repeating Characters 解题报告的更多相关文章
- Leetcode:Longest Substring Without Repeating Characters 解题报告
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- [LeetCode] 3. Longest Substring Without Repeating Characters 解题思路
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- leetcode: longest substring without repeating characters
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- C++ leetcode Longest Substring Without Repeating Characters
要开学了,不开森.键盘声音有点大,担心会吵到舍友.今年要当个可爱的技术宅呀~ 题目:Given a string, find the length of the longest substring w ...
- [LeetCode]Longest Substring Without Repeating Characters题解
Longest Substring Without Repeating Characters: Given a string, find the length of the longest subst ...
随机推荐
- memcache缓存系统
一.缓存系统 静态web页面: 1.在静态Web程序中,客户端使用Web浏览器(IE.FireFox等)经过网络(Network)连接到服务器上,使用HTTP协议发起一个请求(Request),告诉服 ...
- Linux搭建tomcat文件服务器
Linux搭建tomcat文件服务器 Linux下配置Tomcat服务器和Windows下其实差不多,可以去官网下载安装包释放或者在线下载,只是当时下载的windows.zip文件,现在下载.tar. ...
- android 虚拟机,文件导入sdcard下报错,Read-only file system
解决方案-------------------- eclipse -> windows->Android AVD Manager 里选择你的AVD,edit里SD Card 选择File, ...
- jQuery使用手册,【新手必备】
jQuery是一款同prototype一样优秀js开发库类,特别是对css和XPath的支持,使我们写js变得更加方便!如果你不是个js高手又想写出优 秀的js效果,jQuery可以帮你达到目的! ...
- Java系列学习(十三)-字符串
1.字符串基础 概念:字符串本质是打包字符数组的对象,是java.lang.String类的实例 2.字符串的构造方法 public String() public String(byte[] byt ...
- Android studio如何显示代码行数
1.首先打开as:File-->Settings... 2.Editor-->General-->Appearence 3.右边show line numbers 4.依次点击app ...
- Android基础TOP2:单机按钮改变字体颜色
---恢复内容开始--- Activity: <TextView android:id="@+id/t1" android:textSize="30dp" ...
- (转)Arcgis for JS之Cluster聚类分析的实现
http://blog.csdn.net/gisshixisheng/article/details/40711075 在做项目的时候,碰见了这样一个问题:给地图上标注点对象,数据是从数据库来的,包含 ...
- struts.xml详解
参考自:http://blog.csdn.net/zz_mm/article/details/5460397 1. 深入Struts2的配置文件 本部分主要介绍struts.xml的常用配置. ...
- HDU_1087_Super Jumping! Jumping! Jumping!_dp
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...