leetcode第三题:

题目:

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

源码(使用java语言):

class Solution {
public int lengthOfLongestSubstring(String s) {
int result = 0;
int front = 0 ;
int after = 0;
char[] temp = s.toCharArray();//save the String to a chararray
Map<Character,Integer> map = new HashMap<>();
while(after<temp.length&&front<temp.length){
if(map.containsKey(temp[after])){
if(front<map.get(temp[after])+1){
front = map.get(temp[after])+1;
}
}
map.put(temp[after],after);
after++;
if(after - front >result){
result = after -front;
}
}
return result;
}
}

用时64ms

算法亮点,利用map键值对以及游标来巧妙获取最长子串长度,且仅需遍历一遍

因为map的containskey在查询到一个相同的值是就返回true,所以二层嵌套的if语句要加上front<map.get(temp[after])以保证front游标一直向前浮动

以上

leetcode第三题的更多相关文章

  1. leetcode第三题--Longest Substring Without Repeating Characters

    Problem:Given a string, find the length of the longest substring without repeating characters. For e ...

  2. leetcode第三题Longest Substring Without Repeating Characters java

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

  3. LeetCode第三题—— Longest Substring Without Repeating Characters(最长无重复子字符串)

    题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...

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

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

  5. 刷题之路第三题--Longest Substring Without Repeating Characters

    问题简介:求给定字符串中最长的字符不重复的字符串的长度 问题详解: 给定一个字符串,寻找给定字符串中包含的最长的字符不重复的字符串的长度 注:答案必须是子字符串,不是子序列 是连续的字符不重复的字符串 ...

  6. 【LeetCode】3 、Longest Substring Without Repeating Characters

    题目等级:Medium 题目描述:   Given a string, find the length of the longest substring without repeating chara ...

  7. Leetcode经典试题:Longest Substring Without Repeating Characters解析

    题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...

  8. LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters

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

  9. LeetCode(3)Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  10. LeetCode Hash Table 3. Longest Substring Without Repeating Characters

    HashMap的应用可以提高查找的速度,键key,值value的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...

随机推荐

  1. Python3 笔记

    Ubuntu18.04 Python3环境 默认python3已经安装了, 可能是安装其他应用的时候因为依赖关系安装的. 安装pip3, 先sudo apt update 一下, apt-cache ...

  2. swift常用第三方库

    网络 Alamofire:http网络请求事件处理的框架. Moya:这是一个基于Alamofire的更高层网络请求封装抽象层. Reachability.swift:用来检查应用当前的网络连接状况. ...

  3. SQL使用技巧

    SQLServer 数据库变成单个用户后无法访问问题的解决方法 USE master; GO DECLARE @SQL VARCHAR(MAX); SET @SQL='' SELECT @SQL=@S ...

  4. php中cal_days_in_month不可用时的替代方法(计算一个月的天数)

    在计算某个月中的天数时,由于PHP编译时没有加上--enable-calendar选项,会导致cal_days_in_month方法不可用. 这时,如果不能更改服务器的编译设置,可以通过以下方法实现该 ...

  5. 入门:移动APP中的各种导航

    即使是移动应用界面的原型设计,导航的形式也可以多种多样.尽管尺寸小,又必须紧凑排列大量数据,它们似乎受到了紧密的约束,但依然有着形形色色的选择. 人们曾经一度只会考虑一种形式——流行且广泛使用的垂直导 ...

  6. [docker]macvlan实现双vlan互通

    关于vlan的冷知识 vlan范围:0~4095 0,4095 保留 仅限系统使用 用户不能查看和使用这些VLAN 1 正常 Cisco默认VLAN 用户能够使用该VLAN,但不能删除它 2-1001 ...

  7. conflicting types for xx错误

    编译libvmi 0.8版本时,出现以下错误: libtool: compile:  gcc -DHAVE_CONFIG_H -I. -I.. -I.. -fvisibility=hidden -I/ ...

  8. python打开文件的N种姿势

    # python打开文件的N种姿势 print('[1]使用open()函数+简单for循环') f1 = open('python.txt') for line in f1: print(line. ...

  9. phpcmsv9 管理加密解密

    例子:  密码:123123  encrypt:Jiu5He 第一步:  md5("123456")="4297f44b13955235245b2497399d7a93& ...

  10. cp显示进度条

    cp显示进度条 alias cp='rsync -av --progress'