【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 ...
随机推荐
- [转]linux之cut命令的用法
转自:http://www.jb51.net/article/41872.htm cut是一个选取命令,就是将一段数据经过分析,取出我们想要的.一般来说,选取信息通常是针对“行”来进行分析的,并不是整 ...
- 关于学习C语言
c语言作为一种计算机的语言,我们学习它,有助于我们更好的了解计算机,与计算机进行交流,因此,c语言的学习对我们尤其重要. 在这个星期里,我们专业的学生在专业老师的带领下进行了c语言程序实践学习.在这之 ...
- C# asp.net repeater实现排序功能,自动排序,点击头部排序,点击列排序
在网上看到好多关于repeater排序的,自己动手用了,发现一些问题,贴源码后把发现的问题以及解决方法给出 repeater实现排序功能(单击升序排列,再单击降序排列).原理很简单,在<TD&g ...
- Android sensor 系统框架 (二)
连载上一篇http://www.cnblogs.com/hackfun/p/7327320.html (D) 如何加载访问.so库 在前一篇博客http://www.cnblogs.com/hackf ...
- Android集成二维码扫描功能
文章转载自 https://github.com/yipianfengye/android-zxingLibrary 在具体介绍该扫描库之前我们先看一下其具体的使用方式,看看是不是几行代码就可以集成 ...
- css3背景渐变色代码
从上到下 #grad { background: -webkit-linear-gradient(red, blue); background: -o-linear-gradient(red, b ...
- [Windows Server 2012] Filezilla安全加固方法
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:FileZ ...
- Sandbox 沙盒
In computer security, a sandbox is a security mechanism for separating running programs, usually in ...
- .net 内嵌 GeckoWebBrowser (firefox) 核心浏览器
引用nuget包: 注意:Geckofx45 nuget包必须是最后引用,否则初始化会出错 简单示例: using Gecko; using System; using System.Collecti ...
- Android 双屏异显
android双屏是克隆模式,如果要在第二屏幕显示不同内容,需要自定义一个Presentation类 1.先设置权限 (刚开始折腾很久没有效果,后来发现是没设置权限) <!-- 显示系统窗口权限 ...