题目如下:


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

转载于:https://www.cnblogs.com/srain/p/6877746.html

LeetCode---------Longest Substring Without Repeating Characters解法相关推荐

  1. LeetCode——Longest Substring Without Repeating Characters

    原问题 Given a string, find the length of the longest substring without repeating characters. Example 1 ...

  2. LeetCode Longest Substring Without Repeating Characters

    题意 Given a string, find the length of the longest substring without repeating characters. For exampl ...

  3. [LeetCode] Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  4. LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)

    题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...

  5. LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)

    这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...

  6. [LeetCode]3.Longest Substring Without Repeating Characters

    [题目] Given a string, find the length of the longest substring without repeating characters. For exam ...

  7. LeetCode:3. Longest Substring Without Repeating Characters

    https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ 内容描述: Give ...

  8. LeetCode 第 3 题(Longest Substring Without Repeating Characters)

    LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...

  9. leetcode(三)—— Longest Substring Without Repeating Characters(最长不重复子串 Python/C++)

    Longest Substring Without Repeating Characters | LeetCode OJ 使用 hash 判重问题首先想到的就是 hash(或者使用 map): 思路: ...

  10. 【贪心】LeetCode 3. Longest Substring Without Repeating Characters

    LeetCode 3. Longest Substring Without Repeating Characters Solution1:我的答案 该方法中哈希表记录的是字符出现的次数.标准的贪心算法 ...

最新文章

  1. 学习《Linux设备模型浅析之驱动篇》笔记(一)
  2. C++学习手记五:C++流操作
  3. 深度学习卷积神经网络大事件一览
  4. 微软官方上线了Python教程,7个章节就把Python说通了!
  5. python ascii转utf8_ascii 转换为 utf-8
  6. 使用python+ffmpeg批量转换格式
  7. 2016年CCF第七次测试 俄罗斯方块
  8. 使用dva脚手架(dva-cli)快速构建React项目
  9. procreate 笔刷_插画学习必备:2000款Procreate大师级笔刷,超级强大,免费领取
  10. Android tinyplay与tinycap流程(十六)
  11. linux 下tomcat catalina.out日志操作
  12. 我常用的几款数据软件
  13. 设计师的十大金科玉律
  14. 用户认证授权系统方案思考
  15. 李彦宏偷换概念还是危言耸听?
  16. 复杂网络分析 08网络节点重要性
  17. 2019-CS224N-Assignment 1: Exploring Word Vectors
  18. C#中使用SHA1算法对密码进行加密
  19. 淘宝美工设计视频教程云盘分享
  20. 软件项目管理 第四周作业—项目技术指标(招标文件)

热门文章

  1. PreTranslateMessage作用和使用方法
  2. Java数据结构2:堆排序思考
  3. python如何安装scrapy库_python2安装scrapy库报错解决方案
  4. python3 urllib代理_Python3中使用urllib的方法详解(header,代理,超时,认证,异常处理)
  5. php 效率最高的递归,PHP 递归效率分析_PHP教程 - microtime
  6. android n进入分屏代码分析_智慧分屏为“何物”?华为MatePad Pro解锁便捷又高效的新姿势...
  7. C++编程进阶9(如何将构造函数和非成员函数虚化、无锁单例模式)
  8. python环境变量配置_python+ pycharm 环境安装 + pycharm使用
  9. Java Array 常见报错
  10. 阿里云风险识别决策引擎发布