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

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

Solution:

Use two-pointer and HashTable to solve.

Keep p1 at the beginning of the substring and move p2, add characters to hash table and keep the index as value,

if map contains s[p2] then need to  move p1 to the right of the duplicated char index, then update the index of map[s[p2]] to p2;

also p1 and p2 should only move forward;

ex) "abba" when p1=0, p2 = 2, we met the second 'b' then p1 should move to 2, and p2 stay at 2.

then when p1=2, p2=3 we met the second a, p1 should never move back to index 1 to start over;

Before come up with this solution, my solution was to clear the map and move p1 to the right of the first duplicated char, and same to p2 then go through again and add to map,

This method worst case runtime is O(n2), so exceeded the time Limited

 public class Solution {
public int LengthOfLongestSubstring(string s) {
if(string.IsNullOrEmpty(s))
{
return ;
}
int l = s.Length;
int max = ;
int p1=;
int p2=;
Dictionary<char,int> map = new Dictionary<char,int>();
while(p2<l)
{
if(!map.ContainsKey(s[p2]))
{
map.Add(s[p2], p2);
}
else
{
if(p1<=map[s[p2]])
{
p1=map[s[p2]]+; }
map[s[p2]]= p2;
}
max= Math.Max(max, p2-p1+);
p2++; }
return max;
}
}

LeetCode #3. Longest Substring Without Repeating Characters C#的更多相关文章

  1. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

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

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

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

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

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

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

  5. LeetCode之Longest Substring Without Repeating Characters

    [题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...

  6. Leetcode 3. Longest Substring Without Repeating Characters (Medium)

    Description Given a string, find the length of the longest substring without repeating characters. E ...

  7. [Leetcode Week1]Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...

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

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  9. LeetCode[3] Longest Substring Without Repeating Characters

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

  10. 【leetcode】Longest Substring Without Repeating Characters

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

随机推荐

  1. Lucene全文检索(一)

    全文检索的概念 1.从大量的信息中快速.准确的查找要的信息2.收索的内容是文本信息3.不是根据语句的意思进行处理的(不处理语义)4.全面.快速.准确是衡量全文检索系统的关键指标.5.搜索时英文不区分大 ...

  2. .NET:国际化和本地化

    .NET:国际化和本地化 背景 国际化(i18n)和本地化(l10n)是高端程序的必备技术,可惜从业五年从没有尝试过,下一步准备做一个多用户的博客系统,想支持多语言,今天就学习了一下,写出来,希望大家 ...

  3. 深入浅出Mybatis-sql自动生成

    本文提供了一种自动生成sql语句的方法,它针对的对象是有主键或唯一索引的单表,提供的操作有增.删.改.查4种.理解本文和本文的提供的代码需要有java注解的知识,因为本文是基于注解生成sql的.本文适 ...

  4. Linux-gate.so技术细节

    1. linux-gate.so是什么参考这里:http://www.trilithium.com/johan/2005/08/linux-gate/简而言之,linux-gate.so是为了实现用户 ...

  5. Linux文件系统十问,你知道吗?

    关于文件系统,相信大家都不陌生.身为攻城狮的我们几乎天天都会与之打交道,但是细深剖一下,其中又有多少是我们理解深度不够的呢.那么让我们一起来看一下下面这一组Linux文件系统相关的问题吧: 1.机械磁 ...

  6. Eclipse 中,web项目在Tomcat运行时填写不了Server name

    最近开发项目,从MyEclipse中导入项目到Eclipse中,那些WEB属性都在,可就是不能在Tomcat上运行.纠结一番,最后发现是这个问题: WEB的版本问题. 具体问题看下图: 解决方案: W ...

  7. java后台接受app上传的图片

    package com.sujinabo.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOEx ...

  8. Node.js调用百度地图Web服务API的Geocoding接口进行点位反地理信息编码

    (从我的新浪博客上搬来的,做了一些修改.) 最近迷上了node.js以及JavaScript.现在接到一个活,要解析一个出租车点位数据的地理信息.于是就想到使用Node.js调用百度地图API进行解析 ...

  9. SUSElinux的pam模块中$ISA变量的作用

    目的:限制非wheel用户切换至root 方法:vi /etc/pam.d/su文件,增加如下两行 auth sufficient /lib/security/pam_rootok.soauth re ...

  10. Django命令行相关命令 以及创建一个空白网页的步骤

    django相关命令行命令: django.admin.py是Django的一个用于管理任务的命令行工具,manage.py是对django-admin.py的简单包装,每个Django Projec ...