这道题是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#中split的方法汇总

    字符串的处理往往离不开split方法,下面介绍几种split的用法: 1.对单个字符进行分割(注意这里是字符,不是字符串,故只能用单引号‘’) string s=abcdeabcdeabcde; st ...

  2. 虚拟机中Ubuntu安装vmtools

    1.解压vmtools文件为VMWARETO.TGZ VMtools文件一般在系统桌面,如果没有可以点击左上方的"虚拟机-安装VMware Tools"即可出现在桌面,也可以通过U ...

  3. C#环形缓冲区(队列)完全实现

    公司项目中经常设计到串口通信,TCP通信,而且大多都是实时的大数据的传输,然后大家都知道协议通讯肯定涉及到什么,封包.拆包.粘包.校验--什么鬼的概念一大堆,说简单点儿就是要一个高效率可复用的缓存区. ...

  4. pta编程题5 Pop Sequence

    第一次提交结果都是YES,后来检查发现Push,Pop函数写的有问题,即Stack sta改为引用Stack &sta,否则不能改变实参的值. #include <iostream> ...

  5. IOS UITabBarController(控制器)的子控制器

    UITabBarController的简单使用 ● UITabBarController的使用步骤 ➢ 初始化UITabBarController ➢ 设置UIWindow的rootViewContr ...

  6. autoreleasing on a thread

    So basically, if you are running on OS X 10.9+ or iOS 7+, autoreleasing on a thread without a pool s ...

  7. PAT (Advanced Level) Practise - 1092. To Buy or Not to Buy (20)

    http://www.patest.cn/contests/pat-a-practise/1092 Eva would like to make a string of beads with her ...

  8. 查询Linux下已安装软件的版本

    #rpm -qa | grep mysql

  9. Java continue break 制作简单聊天室程序,屏蔽不文明语言,显示每句话聊天时间 for(;;) SimpleDateFormat("yyyy-MM-dd hh:mm:ss") equalsIgnoreCase

    package com.swift; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanne ...

  10. Vue源码学习二 ———— Vue原型对象包装

    Vue原型对象的包装 在Vue官网直接通过 script 标签导入的 Vue包是 umd模块的形式.在使用前都通过 new Vue({}).记录一下 Vue构造函数的包装. 在 src/core/in ...