LeetCode---------Longest Substring Without Repeating Characters解法
题目如下:
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
大致翻译:
给出一个字符串,求出没有重复字符的最长子串的长度。
例如:
给出"abcabcbb",答案是"abc"的长度为3.
给出"bbbbb",答案是"b"的长度为1.
给出"pwwkew",答案是"wke"的长度为3. 注意答案必须是一个子串,"pwke"是一个子序列但并不是子串.
本题重点在于不重复的子串,想到HashSet是不允许存储重复的数据的,所以解法就利用HashSet来实现。
【Java代码】
public class Solution {
public int lengthOfLongestSubstring(String s) {
//不重复子串的长度
int length = 0;
//构造不重复的set表
Set<Character> set = new HashSet<Character>();
int i = 0, j = 0;
for(i = 0; i < s.length(); i++){
set.clear();//清空set表
set.add(s.charAt(i));//加入开始字符
for(j = i + 1; j < s.length(); j++){
if(set.add(s.charAt(j)));//如果成功加入,证明没有重复,程序继续
else break;//如果没成功加入,则跳出
}
if(j - i >= length) length = j - i;//计算长度并保留最长长度
}
return length;
}
}
如果有任何问题,欢迎跟我联系:xiaomenxiaomen@qq.com
我的github地址:github.com/WXRain
LeetCode---------Longest Substring Without Repeating Characters解法的更多相关文章
- [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 最长无重复字符的子串
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 ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- 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. For exam ...
- 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 (C++)
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- [LeetCode] Longest Substring Without Repeating Characters (LinkedHashSet的妙用)
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- 【C++】浅谈三大特性之一继承(三)
四,派生类的六个默认成员函数 在继承关系里,如果我们没有显示的定义这六个成员函数,则编译系统会在适合场合为我们自动合成. 继承关系中构造函数和析构函数的调用顺序: class B { public: ...
- SVN使用小记
SVN(Subversion)是优秀的版本控制工具,之前在eclipse里面项目管理的时候,File-->Import-->SVN-->从SVN检出项目-->创建新的资源库位置 ...
- GWT开端
这篇文章是转载的,原地址:GWT开端 以前的基于GWT的项目中广泛使用的还是gwt-windows-1.5.3这个版本的. 1.下载地址:http://code.google.com/webtoolk ...
- C语言 动态数组实现
一.概述 C语言是不能直接定义动态数组的,数组必须在初始化时确定长度. 如果要在程序运行时才确定数组的长度,就需要在运行的时候,自己去向系统申请一块内存用动态内存分配实现动态数组. 二.动态内存分配函 ...
- 整合初步______SH
什么是框架 在的J2EE开发中,经常会提到"框架"这个词汇,例如Spring,Struts,Webx等等都称之为J2EE开发框架.那么,什么是框架呢? 框架的英文为Framewor ...
- dotnet Core 调用HTTP接口,系统大量CLOSE_WAIT连接问题的分析,尚未解决。
环境: dotnet core 1.0.1 CentOS 7.2 今天在服务器巡检的时候,发现一个服务大量抛出异常 异常信息为: LockStatusPushError&&Messag ...
- 【Direct2D开发】 通过操作像素实现纹理混合
转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 我们都知道Direct2D可以加载并显示图片,但是不知道你有没有想过,这个2D的图形引擎可以进行纹理混合吗?如果 ...
- mysql自动备份删除5天前的备份
1.查看磁盘空间情况: # df -h 2.创建备份目录: 上面我们使用命令看出/home下空间比较充足,所以可以考虑在/home保存备份文件: cd /home mkdir backup cd ba ...
- oralce set
1 SET TIMING ON 说明:显示SQL语句的运行时间.默认值为OFF. 在SQLPLUS中使用,时间精确到0.01秒.也就是10毫秒. 在PL/SQL DEVELOPER 中 ...
- Ubuntu12.04嵌入式交叉编译环境arm-linu-gcc搭建过程,图解
转载:王文松的博客Ubuntu12.04嵌入式交叉编译环境arm-linu-gcc搭建过程,图解 安装环境 Linux版本:Ubuntu 12.04 内核版本:Linux 3.5.0 ...