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, ...
随机推荐
- 老李推荐:第2章4节《MonkeyRunner源码剖析》了解你的测试对象: NotePad窗口Activity之菜单简介
老李推荐:第2章4节<MonkeyRunner源码剖析>了解你的测试对象: NotePad窗口Activity之菜单简介 NotePad窗口Activity之菜单简介 这里我们总共用到 ...
- Angularjs 实现移动端在线测评效果
注:此文所用的angular版本为 1.6 一.运行效果图 二.需求 1. 点击选项时,背景变为黄色(即选中状态),并且自动切换到下一题 2. 切换到下一题时,顶部进度随之改变 3. 选中时要把对应的 ...
- Java中一个方法只被一个线程调用一次
1.想在运行时抛出异常,终止方法的运行 private final Set<Long> THREADS = new HashSet<>(); public void someM ...
- fixed应用
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 【Android】XML文件的解析
1.首先我们可以在res包路径下创建一个raw包,然后在raw下创建一个email.xml 文件,并修改其内容如下: <?xml version="1.0" encoding ...
- Android Google AdMob 广告接入示例
Android Google AdMob 广告接入示例 [TOC] 首先请大家放心,虽然 Google搜索等服务被qiang了,但是 广告服务国内还是可以用的,真是普天同庆啊~~~噗! 其实这篇文章也 ...
- 六行python代码的爱心曲线
前些日子在做绩效体系的时候,遇到了一件囧事,居然忘记怎样在Excel上拟合正态分布了,尽管在第二天重新拾起了Excel中那几个常见的函数和图像的做法,还是十分的惭愧.实际上,当时有效偏颇了,忽略了问题 ...
- PixiJS - 基于 WebGL 的超快 HTML5 2D 渲染引擎
Pixi.js 是一个开源的HTML5 2D 渲染引擎,使用 WebGL 实现,不支持的浏览器会自动降低到 Canvas 实现.PixiJS 的目标是提供一个快速且轻量级的2D库,并能兼容所有设备.此 ...
- Oracle to_date函数
TO_DATE格式(以时间:2007-11-02 13:45:25为例)Year: yy two digits 两位年 显示值:07yyy three di ...
- MySQL操作符
简要介绍MySQL操作符 常用: 算术运算符.比较操作符.逻辑操作符.位运算符-- 一.算术运算符 +:加 -:减 *:乘 /:除,返回商 %,mod():除,返回余数 mysql> %,mod ...