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. C语言栈与调用惯例

    C语言栈与调用惯例 1.前言 最近在再看<程序员的自我修养>这本书,对程序的链接.装载与库有了更深入的认识.关于这本书的评价可以去豆瓣看看http://book.douban.com/su ...

  2. HTML5 拖放及排序的简单实现

    HTML5 拖放及排序的简单实现 之前写过个类似的例子,看这里. 但想再深入一步,希望能通过拖放,来交换二个元素的位置.最好有应用到手机平台上. 作了个简单的例子,在手机上测试的时候不成功..查了好多 ...

  3. iOS核心应用对象

    IOS应用之设计模式:模型-视图-控制器 iOS应用与其它应用的区别就在于它所管理的数据(和相应的业务逻辑)以及将数据展现给用户的方式.大多数UIKit对象并不定义应用而是帮助完善其行为.例如,你的应 ...

  4. Machine Learning/Random Projection

    这次突然打算写点dimension reduction的东西, 虽然可以从PCA, manifold learning之类的东西开始, 但很难用那些东西说出好玩的东西. 这次选择的是一个不太出名但很有 ...

  5. LNMP安装与配置

    Nginx与apache.lighttp性能综合对比,如下图:                           注意:关闭rpm默认安装的apache和mysql 1.准备php函数的rpm包 y ...

  6. 腾讯地图api接收坐标提交坐标

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 寻找两个已序数组中的第k大元素

    寻找两个已序数组中的第k大元素 1.问题描述 给定两个数组与,其大小分别为.,假定它们都是已按照增序排序的数组,我们用尽可能快的方法去求两个数组合并后第大的元素,其中,.例如,对于数组,.我们记第大的 ...

  8. hdu 1671 Phone List(字典树)

    知道bug的时候我眼泪掉下来... 我的第一道字典树,看了字典树的注意事项和实现方式,我写这道题的时候格外认真,就是奔着1A去的.结果这是几A来着? 第一遍写的时候提交MLA,我看了一下,是因为我释放 ...

  9. 如何在ASP.NET Core应用中实现与第三方IoC/DI框架的整合?

    我们知道整个ASP.NET Core建立在以ServiceCollection/ServiceProvider为核心的DI框架上,它甚至提供了扩展点使我们可以与第三方DI框架进行整合.对此比较了解的读 ...

  10. express框架+jade+bootstrap+mysql开发用户注册登录项目

    完整的项目代码(github):https://github.com/suqinhui/express-demo express是基于Node.js平台的web应用开发框架,用express框架开发w ...