题目链接: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. mongodb 的一些启动命令

    启动命令 nohup /home/sh/local/mongodb-linux-x86_64-rhel62-3.4.0/bin/mongod -dbpath /home/sh/local/mongod ...

  2. 算法提高 P1001【大数乘法】

    当两个比较大的整数相乘时,可能会出现数据溢出的情形.为避免溢出,可以采用字符串的方法来实现两个大数之间的乘法.具体来说,首先以字符串的形式输入两个整数,每个整数的长度不会超过8位,然后把它们相乘的结果 ...

  3. java web----刷新页面的程序 (重复包括)

    <%@ page language="java" import="java.util.*" pageEncoding="gb2312" ...

  4. java集合遍历删除指定元素异常分析总结

    在使用集合的过程中,我们经常会有遍历集合元素,删除指定的元素的需求,而对于这种需求我们往往使用会犯些小错误,导致程序抛异常或者与预期结果不对,本人很早之前就遇到过这个坑,当时没注意总结,结果前段时间又 ...

  5. OCR (Optical Character Recognition,光学字符识别)

    OCR (Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗.亮的模式确定其形状,然后用字符识别方法将形状翻译 ...

  6. PCI、PCI-x,PCI-E兼容以及他们之间的区别详细图解

    一.PCI PCI接口分为32bit和64bit两种,32bit就是一般台式机使用的普通的pci接口(图一.图三),64bit接口比32bit接口长一些一般只出现在服务器上(图四.图五).32bit和 ...

  7. Ubuntu 下 安装 hadoop(转+修改)

    出于需要在电脑上安装hadoop,版本:hadoop 1.2.1 (stable) 按照网上教程安装成功,把一点需要修改的地方说说. 参考博客: ubuntu12.04 hadoop单机模式和伪分布模 ...

  8. update project maven项目的时候出错

    preference node "org.eclipse.wst.validation"has been remove 上面的错误是因为修改包名无法互相引入导致的,仅仅需要将Ecl ...

  9. C# 鼠标穿透窗体与恢复

    转自原文 C# 鼠标穿透窗体与恢复 引入user32.dll [DllImport("user32.dll")] public static extern uint SetWind ...

  10. DataSnap 连接池 DSServer1Disconnect

    DataSnap Server DSServer1Disconnect 这个函数什么时候执行? void __fastcall TServerContainer1::DSServer1Disconne ...