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. ASP.NET基础之HttpModule学习

    最近学习WCF知识时看到有关IIS版本的知识,发现对HttpContext,HttpModule,HttpHandler的内容都不是很了解,这三个也是ASP.NET相对基础的内容,晚上特地花点时间针对 ...

  2. vsftp FTP服务器外网访问设置

    引用: linux中VSFTP无法从外网访问问题! http://blog.csdn.net/zbulrush/article/details/841978 原文: FTP协议有两种工作方式:PORT ...

  3. 常用WebService一览表

    天气预报Web服务,数据来源于中国气象局 Endpoint :http://www.webxml.com.cn/WebServices/WeatherWebService.asmx Disco     ...

  4. [置顶] logistic回归(一)

    先介绍下基础的公式: 这个是Sigmoid函数,在这个回归过程中非常重要的函数,主要的算法思想和这个密切相关.这个函数的性质大家可以自己下去分析,这里就不细说了. 然后我们说明下流程,首先我们将每个特 ...

  5. 把python文件编译成exe文件

    我用的是py2exe. 下载地址http://sourceforge.net/projects/py2exe/files/py2exe/0.6.9/ 你可以根绝自己的Python版本选择适当的下载 我 ...

  6. Android KeyLogger Demo

    @author: dlive 代码见github: https://github.com/Dliv3/Android-KeyLogger-Demo 前言 之前开发过一个Android的木马,其中Key ...

  7. The Linux Mint 18.1:Eclipse Run The C++ And Python ConfigorationWhen You achieve above step,you can run the c++ and python! (Next OTL ,PYOTL is Project That Write By Ruimin Shen(ability man) )

    # Copyright (c) 2016, 付刘伟 (Liuwei Fu)# All rights reserved.# 转载请注明出处 1.Install The Eclipse,g++ Use T ...

  8. 毕向东_Java基础视频教程第19天_IO流(11~14)

    第19天-11-IO流(字节流File读写操作) import java.io.FileInputStream; import java.io.FileOutputStream; import jav ...

  9. readonly属性在各浏览器中的区别

    有个项目需求是正常显示时为只读,不可修改: 点击修改按钮后,可修改表单元素. 首先想到的是readonly属性,其用于规定输入字段为只读,不能修改.在javascript中消除readonly值,可将 ...

  10. 利用DataSet更改数据,将更改保存到数据库中

    RowState 是 DataRow 很重要的一个属性, 表示 DataRow 当前的状态. RowState 有 Added, Modified, Unchanged, Deleted, Detac ...