题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/description/

题目要求:求出最长的不重复子串

思路:

1、采用map,遍历字符串,将每个字符put进map
2、如果长度为1,直接输出1
3、end-begin+1(注意+1)
end为当前遍历到的位置,
begin为重复位置+1
4、在遍历字符串的时候,判断当前字符是否存在resultMap.containsKey(key),
如果不存在,将其put进map,并维护end-begin+1
如果存在,重置begin位置,计算长度
(begin的位置判断:如果当前begin位置index比重复位置大,需要begin==当前begin位置index,否则等于重复位置)
begin = begin > (resultMap.get(key)+1) ? begin : (resultMap.get(key)+1)
长度维护:
result = result > (end-begin+1) ? result : (end-begin+1);

public class Solution {
public int lengthOfLongestSubstring(String s) {
char str[] = s.toCharArray();
int len = str.length;
if(len == 1 ){
return 1;
}else {
// int sum = 0;
int result = 0;
int begin = 0;
int end = 0;
Map<String,Integer> resultMap = new HashMap<String, Integer>();
for(int i = 0; i<len; i++){
String key = str[i]+"";
end = i;
//未重复
if(!resultMap.containsKey(key)){
resultMap.put(key,i);
result = result > (end-begin+1) ? result : (end-begin+1);
}else { //遇到重复
// resultMap.clear();
begin = begin > (resultMap.get(key)+1) ? begin : (resultMap.get(key)+1);
result = result > (end-begin+1) ? result : (end-begin+1);
resultMap.put(key,i);
}
}
// System.out.println(result);
return result;
}
}
}

  

leetcode - 3、Longest Substring Without Repeating Characters的更多相关文章

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

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

  2. 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters

    一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...

  3. 【LeetCode OJ】Longest Substring Without Repeating Characters

    题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...

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

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

  5. 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

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

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

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

  7. leetcode题解 3. Longest Substring Without Repeating Characters

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

  8. 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)

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

  9. 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

随机推荐

  1. elasticsearch 6.0.0及之后移除了一个索引允许映射多个类型的操作(Removal of mapping types)

    分给线一下内容为理解错误内容,实际允许建立父子分档,只是类型改成来 join 官方demo: join datatypeedit The join datatype is a special fiel ...

  2. EF6:编写你自己的code first 数据迁移操作(睡前来一篇,翻译的)

    原英文版由EF团队成员 Rowan Miller 在2013年发表,此处只作翻译备忘. 数据迁移提供了一套强类型API,用于执行通用的操作,比如CreateIndex("dbo.Blogs& ...

  3. Renesas APIs ***

    一个线程,强行结束另外一个线程,并将其挂起: static void SuspendTask(TX_THREAD *thread) { UINT status = ; UINT state; stat ...

  4. Java堆栈解析

    1.RAM和ROM区别RAM-RamdomAccessMemory随机存取存储器(断电后数据会丢失),高速存取,读写时间相等,且与地址无关,如计算机内存等. ROM-Read Only Memory只 ...

  5. *.app 无法打开或已损坏解决办法

    1.系统偏好设置... -> 安全性与隐私-->修改为任何来源 2.如果没有任何来源  ,打开终端执行:sudo spctl --master-disable

  6. date.js

    /** * 此JS文件是格式化JS中日期时间的工具类,其中包含了传入日期对象Date,格式化成想要的格式,<br> * 或者传入字符串格式的时间个,次字符串日期对应的格式可以转换为相应的日 ...

  7. node 中的定时器, nextTick()和setImmediate()的使用

    1.node中使用定时器的问题在于,它并非精确的.譬如setTimeout()设定一个任务在10ms后执行,但是在9ms后,有一个任务占用了5ms,再次轮到定时器时,已经耽误了4ms. 好了node中 ...

  8. 安装scikit-image问题

    参考地址: Image Processing Using Python https://code.tutsplus.com/tutorials/image-processing-using-pytho ...

  9. CentOS7.6安装Maven

    官网下载地址:http://maven.apache.org/download.cgi 第一步:软件下载安装 进行安装目录:cd /opt/software (如果目录不存在,请先创建目录) 下载二进 ...

  10. 安装完Apache后,配置httpd.conf来使apache来加载php模块

    以apache模块的方式来安装php,在httpd.conf文件中首先使用LoadModule php5_module '.../php5apache2.dll'来动态装载Php模块,然后再用语句Ad ...