这道题是LeetCode里的第3道题。

题目描述:

给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

示例 1:

输入: "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。

示例 2:

输入: "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。

示例 3:

输入: "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。 请注意,你的答案必须是 子串 的长度,"pwke"是一个子序列,不是子串。

这题可以使用双指针解题,但是不推荐,最好理解的方法就是我这种建立哈希表。但是呢,哈希表可以和双指针一起使用,节约一半的时间。哈希表的用途在于记录该字符是否出现过。

解题代码:

class Solution {
public int lengthOfLongestSubstring(String s) {
if(s.equals(""))return 0; int maxLength = 0,len = 0;
int[] hashMap = new int[128];
int i=0,j=1;
hashMap[s.charAt(i)] = 1;
len++;
while(i < s.length() && j < s.length()) {
while(j < s.length()) {
if(hashMap[s.charAt(j)] == 1) {
hashMap[s.charAt(i)] = 0;
i++;
len = j - i + 1;
if(len > maxLength)maxLength = len;
hashMap[s.charAt(i)] = 1;
if(j<=i)j = i+1;
break;
}
hashMap[s.charAt(j)] = 1;
//len++;
j++;
}
}
len = j - i;
if(len > maxLength)maxLength = len; return maxLength;
}
}

提交结果:

个人总结:

双指针:

class Solution {
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0) return 0;
int start = 0, max = 1;
char[] str = s.toCharArray();
for(int j = 1; j < s.length(); j++) {
for(int i = start; i < j; i++){
if(str[i] == str[j]) {
start = i + 1;
break;
}
}
max = Math.max(j - start + 1, max);
}
return max;
}
}

双指针的基本思想就是确定开始遍历的字符位置。

【LeetCode】Longest Substring Without Repeating Characters(无重复字符的最长子串)的更多相关文章

  1. 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...

  2. leetcode 3. Longest Substring Without Repeating Characters 无重复字符的最长子串

    一.题目大意 https://leetcode.cn/problems/longest-substring-without-repeating-characters/ 给定一个字符串 s ,请你找出其 ...

  3. 3. Longest Substring Without Repeating Characters 无重复字符的最长子串

    1. 原始题目 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 &quo ...

  4. [LeetCode]3. Longest Substring Without Repeating Characters无重复字符的最长子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  5. 3. Longest Substring Without Repeating Characters无重复字符的最长子串

    网址:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 显然采用sliding window滑 ...

  6. Leetcode3.Longest Substring Without Repeating Characters无重复字符的最长字串

    给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 无重复字符的最长子串是 "abc",其长度为 ...

  7. [leetcode]3. Longest Substring Without Repeating Characters无重复字母的最长子串

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  8. leetcode刷题第三天<无重复字符的最长子串>

    给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 : 输入: "abcabcbb" 输出: 解释: 因为无重复字符的最长子串是 . 示例 : 输入: &quo ...

  9. leetcode 刷题(3)--- 无重复字符的最长子串

    给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 无重复字符的最长子串是 "abc",其长度为 ...

随机推荐

  1. C#之MVC3继续整理问题

    1.注释验证[EmailAddress(ErrorMessage = "×")],用的MVC3框架,此处报错,找不到类“EmailAddress”,看到原文有using Syste ...

  2. leetcode——2

    1. 题目 Add Two Numbers You are given two linked lists representing two non-negative numbers. The digi ...

  3. docker 快速搭建 mysql

    准备工作 系统 centos7 切换阿里源 #备份资源文件 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo ...

  4. ES6中set和map的区别

    Set ES6提供了新的数据结构Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set函数可以接受一个数组(或类似数组的对象)作为参数,用来初始化. // 例一 var set = ne ...

  5. Go - 环境安装

    目录 你好,Go语言 环境安装 目录结构 命令 开发工具 学习网址 小结 你好,Go语言 Go 是一个开源的编程语言,它能让构造简单.可靠且高效的软件变得容易. 因工作需要,准备入坑,先从环境安装开始 ...

  6. Object.prototype.toString的应用

    使用Object.prototype上的原生toString()方法判断数据类型,使用方法如下: Object.prototype.toString.call(value)1.判断基本类型: Obje ...

  7. 增强的格式化字符串format函数

    http://blog.csdn.net/handsomekang/article/details/9183303 自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓 ...

  8. 多线程:InterlockedIncrement

    1.InterlockedIncrement保护多线程中操作的整数. #include <stdio.h> #include <windows.h> volatile long ...

  9. 两种简单的servlet实现反向代理

    以下两种方法都需要引入jar包: <dependency> <groupId>org.mitre.dsmiley.httpproxy</groupId> <a ...

  10. indexOf和contains查找的字符串是空字符,返回值是什么呢?

    一直以为indexOf方法查找的字符串如果不匹配返回值就是-1.今天发现空字符返回值是0.看源码原来如此,阴沟里翻船啊!