【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 ...
随机推荐
- Netty--数据通信和心跳检测
数据通信 概述: netty的ReadTimeOut实现方案3 服务端: public class Server { public static void main(String[] args) th ...
- tp 3.2 组合查询, 字符串模式查询
$User = M("User"); // 实例化User对象 $map['id'] = array('neq',1); $map['name'] = 'ok'; $map['_s ...
- fastjson读取json配置文件
fastjson读取json配置文件: ClassLoader loader=FileUtil.class.getClassLoader(); InputStream stream=loader.ge ...
- Hadoop基础(一)
Hadoop 基础知识 大数据已经火了很长很长时间了,从最开始是个公司都说自己公司的数据量很大,我们在搞大数据.到现在大数据真的已经非常成熟并且已经在逐渐的影响我们的生产生活.你可能听过支付宝的金融大 ...
- SublimeText学习(一)-安装
1.下载安装包:http://www.sublimetext.com/2 2.开始安装,一直下一步 3.开始汉化 汉化包下载:http://files.cnblogs.com/files/2star/ ...
- PHP开发之旅-验证码功能实现
验证码这样的功能可以说是无处不在了,接下来使用php来实现验证码这样的功能,这里我是将验证码实现抽取到一个类中独立开来,那么后面如果再使用到验证码功能,直接引入该类文件并创建该类的实例,就可以使用验证 ...
- Angular——流程控制指令
基本介绍 (1)ng-repeat,类似于for循环,对数组进行遍历 (2)ng-switch on,ng-switch-when,类似于switch,case 基本使用 ng-repeat < ...
- Canvas——基本入门
基本概念 1.canvas 是 HTML5 提供的一个用于展示绘图效果的标签. canvas 原意画布, 帆布. 在 HTML 页面中用于展示绘图效果. 最早 canvas 是苹果提出的一个方案, 今 ...
- python 分割文件、组合文件
import glob big_file = open('index.sql', 'rb') bak_file = 'index_bak' i = 1 while True: chunk = big_ ...
- HyperLink的使用
<asp:HyperLink ID="Hyperlink2" NavigateUrl='<%# string.Format("AddOrganizition. ...