题目

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
这道题看似简单。可是用遍历方法的话非常麻烦。以下这样的用hashmap处理方法。是在discuss里看到的,非常巧妙。

代码

public class Solution {
public int lengthOfLongestSubstring(String s) {
if(s.length()==0) return 0;
HashMap<Character,Integer> map=new HashMap<Character,Integer>();
int max=0;
for(int i=0,j=0;i<s.length();i++){
if(map.containsKey(s.charAt(i))){
j = Math.max(j,map.get(s.charAt(i))+1);
}
map.put(s.charAt(i),i);
max = Math.max(max,i-j+1);
}
return max; } }

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/

【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters的更多相关文章

  1. LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List

    题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: Given a string, find the length of the longest substring without ...

  2. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  3. leetcode: longest substring without repeating characters

    July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...

  4. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  5. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  6. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  7. [LeetCode][Python]Longest Substring Without Repeating Characters

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

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

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

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

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

随机推荐

  1. ThinkPHP - 配置项目结构

    配置项目结构: 项目如果分为前后台使用. 那么最关键的就是,使用公共部分文件的划分,其中最为核心的就是公共配置文件的使用. 下面介绍的就是怎么将前后台项目的公共部分提起出来. 首先是其他公共的文件夹: ...

  2. UIButton上同时显示图片和文字的方法

    copy from CPLASF_lixj  http://blog.csdn.net/qijianli/article/details/8152726 项目中经常会遇到Button上同时显示图片和文 ...

  3. Chapter 9 原型模式

    原型模式:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 简单的说就是clone一个对象实例.使得clone出来的copy和原有的对象一模一样. 插一个简单使用clone的例子,如果 ...

  4. 让Solr返回JSON数据

    http://localhost:1985/solr/select/?q=*%3A*&version=2.2&start=0&rows=10&indent=on& ...

  5. 上海投行需要一大群JAVA,C++,C#,UNIX.走过路过不要错过!过完年想换工作看过来初级资深都有 - V2EX

    上海投行需要一大群JAVA,C++,C#,UNIX.走过路过不要错过!过完年想换工作看过来初级资深都有 - V2EX 上海投行需要一大群JAVA,C++,C#,UNIX.走过路过不要错过!过完年想换工 ...

  6. Java CopyOnWriteArrayList分析

    CopyOnWriteArrayList是一种线程安全的ArrayList,顾名思义,它会利用写时拷贝技术,它对共享对象做仅仅读操作的时候,大家都用一个共享对象,假设有可变的操作时,就会复制一份出来, ...

  7. BT基础知识简介

    1. 蓝牙概述   无线局域网的通信 适用范围:10米到100米(根据发射功率的class不同有所差别,典型的class2为10m,而class1为100m,class3为1m) 应用:   局域网络 ...

  8. 查看linux下各数据类型的大小

    代码如下: #include<stdio.h> int main() { printf("int:%d bytes\n",sizeof(int)); printf(&q ...

  9. ARM标准汇编与GNU汇编

    ARM标准汇编与GNU汇编 http://www.cnblogs.com/hnrainll/archive/2011/05/17/2048315.html

  10. Mac删除废纸篓中的单一文件和文件夹

    http://www.macappbox.com/tips/159/ 通过Automator创建教程: 1.打开Automator并选择新建 2.选择服务类型 3.搜索Run Shell Script ...