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. Tuxedo低版本客户端(Tuxedo 9)连接到高版本Tuxedo服务端(Tuxedo 12.1.3)的问题

    经过我实测,是没问题的.但是客户端的Tuxedo DLL必须全部是Tuxedo 9的DLL,不能混用.不然即使用Dependency Walker 分析DLL依赖完全正确,但是实际运行时结果也会出现奇 ...

  2. SNF快速开发平台MVC-EasyUI3.9之-ueditor富文本编辑在 asp.net MVC下使用步骤

    mvc项目中用到了这个富文本编辑就试着把遇到的问题个使用步骤在这里记录一下,希望大家少走弯路. 1.首先我们先下载net版本的uediot 2.然后把整个文档拷贝到我们的项目中,记得是整个 把下载的文 ...

  3. Atitit xml框架类库选型 attilax总结

    Atitit xml框架类库选型 attilax总结 1. 1. XML类库可以分成2大类.标准的.这些类库通常接口和实现都是分开的1 2. Jdom 和dom4j1 2.1. 5.1. jdom1 ...

  4. JS中的PadLeft、PadRight,位数不足,自动补位,String扩展方法

    类似C#中的 PadLeft.PadRight方法 //方法一 function FillZero(p) { return new Array(3 - (p + '').length + 1).joi ...

  5. Java 分布式和集中式理解

    文章转载自:https://blog.csdn.net/youanyyou/article/details/79406507

  6. UML类建模(强烈推荐-思路很清晰)

    UML类建模(强烈推荐-思路很清晰) 2016年10月23日 15:17:47 mbshqqb 阅读数:2315 标签: uml面向对象设计模式 更多 个人分类: 面向对象程序设计   UML的构造快 ...

  7. C# 反射总结 获取 命名空间 类名 方法名

    一.获取 命名空间 类名 方法名 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...

  8. C# 窗口和程序的退出

    Application.Exit(); // 通知所有消息泵必须终止,并且在处理了消息以后关闭所有应用程序窗口. // 由 .NET Compact Framework 支持. Form.Close( ...

  9. PHP最全笔记(四)(值得收藏,不时翻看一下)

    // 序列化(串行化) # 数据传输均是字符串类型 # 除了资源类型,均可序列化 # 序列化在存放数据时,存放数据本身,也存放数据类型 1.在网络传输数据时:2.为了将数组或对象放在磁盘时 # 序列化 ...

  10. file_name[:-4]

    file_name: chair_0001.off file_name[:-4] : chair_0001