Question

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.

Solution

Use extra map to record whether the character has appeared. Remember to consider the situation of last character. Time complexity O(n), space cost O(n).

 public class Solution {
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() < 1)
return 0;
Map<Character, Integer> map = new HashMap<Character, Integer>();
char[] copy = s.toCharArray();
int length = s.length();
int maxCount = 1;
char start = s.charAt(0);
for (int i = 0; i <= length; i++) {
char tmp;
if (i == length) {
tmp = s.charAt(length - 1);
} else {
tmp = s.charAt(i);
}
if (!map.containsKey(tmp)) {
map.put(tmp, i);
} else {
int startIndex = map.get(start);
int dupIndex = map.get(tmp);
if (startIndex <= dupIndex) {
if (dupIndex < length - 1)
start = s.charAt(dupIndex + 1);
maxCount = Math.max(maxCount, i - startIndex);
}
map.put(tmp, i);
}
}
return maxCount;
}
}

Longest Substring Without Repeating Characters 解答的更多相关文章

  1. 3.Longest Substring Without Repeating Characters(string; HashTable)

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

  2. LeetCode[3] Longest Substring Without Repeating Characters

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

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

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

  4. Longest Substring Without Repeating Characters

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

  5. 3. Longest Substring Without Repeating Characters(c++) 15ms

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

  6. 【leetcode】Longest Substring Without Repeating Characters

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

  7. Longest Substring Without Repeating Characters(C语言实现)

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

  8. leetcode: longest substring without repeating characters

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

  9. [LeetCode_3] Longest Substring Without Repeating Characters

    LeetCode: 3. Longest Substring Without Repeating Characters class Solution { public: int lengthOfLon ...

随机推荐

  1. 编程内功修炼之数据结构—BTree(一)

    BTree,和二叉查找树和红黑树中一样,与关键字相联系的数据作为关键字存放在同一节点上. 一颗BTree树具有如下的特性:(根为root[T]) 1)每个节点x有以下域: (a)n[x],当前存储在节 ...

  2. ios 运行模式

    1, IOS下的 NSTimer与Run loop Modes http://blog.csdn.net/yuquan0821/article/details/16843195

  3. Linux usb子系统(一):子系统架构

    一.USB协议基础知识   前序:USB概念概述 USB1.0版本速度1.5Mbps(低速USB) USB1.1版本速度12Mbps(全速USB)  USB2.0版本速度480Mbps(高速USB). ...

  4. [转]Binarized Neural Networks_ Training Neural Networks with Weights and Activations Constrained to +1 or −1

    原文: 二值神经网络(Binary Neural Network,BNN) 在我刚刚过去的研究生毕设中,我在ImageNet数据集上验证了图像特征二值化后仍然具有很强的表达能力,可以在检索中达到较好的 ...

  5. extjs tree check 级联选择

    extjs4 tree check 级联选择 实现效果: 关键代码: function changeAllNode(node, isCheck) { allChild(node, isCheck); ...

  6. 关于scrollTop

    如下图

  7. 13年山东省赛 The number of steps(概率dp水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud The number of steps Time Limit: 1 Sec  Me ...

  8. (原+转)ubuntu中删除文件夹

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5638030.html 参考网址: http://zhidao.baidu.com/link?url=A ...

  9. android 常用颜色

    reference: http://blog.csdn.net/leewenjin/article/details/17386265

  10. haproxy之配置文件解析

    功能--> 提供高可用/负载均衡/基于tcp和http应用的代理;支持虚拟主机,特别适用于负载特大的web站点. 配置文件解析--> #配置文件-->开启/proc/net/ipv4 ...