LeetCode #3. Longest Substring Without Repeating Characters C#
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#的更多相关文章
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- [LeetCode][Python]Longest Substring Without Repeating Characters
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- LeetCode之Longest Substring Without Repeating Characters
[题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...
- Leetcode 3. Longest Substring Without Repeating Characters (Medium)
Description Given a string, find the length of the longest substring without repeating characters. E ...
- [Leetcode Week1]Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
随机推荐
- mvc使用JsonResult返回Json数据
mvc使用JsonResult返回Json数据 controller 中定义以下方法: public JsonResult UpdateSingle(int id, string actionNa ...
- HTML5 Canvas中实现绘制一个像素宽的细线
正统的HTML5 Canvas中如下代码 ctx.lineWidth = 1; ctx.beginPath(); ctx.moveTo(10, 100); ctx.lineTo(300,100); c ...
- [解决]Windows Server 2012 不能安装IE版的Flash
1.问题描述 在server 2012下安装IE版的Flash提示包含最新版本 2.解决方法 2.1.开启“桌面体验”功能 2.2.关闭“IE增强的安全配置”功能 3.重启计算机后安装Flash
- 关于code reivew
关于code reivew 先谈谈三个code review的关键因素: 一.创建review要简单 code reivew是一个程序员日常工作中经常做的一件事,理论上来讲,任何一个将要submit到 ...
- 机器学习的Spark与Scala开发简介
一.机器学习常用开发软件:Spark.Scala 1. Spark简介: MLlib包含的库文件有: 分类 降维 回归 聚类 推荐系统 自然语言处理 在线学习 统计学习方法:偏向理论性,数理统计的方 ...
- Run SPLAHS2 under SE mode on gem5在gem5的SE模式下,运行SPLASH2程序
1. 安装相关的gem5,可以参考以前的博客. 2. 下载splash2编译好的软件 首先从gem5的官网下载已经编译成alpha指令集的splash2.下载地址:http://www.gem5. ...
- ASP.NET 程序发布详细过程
前言 ASP.NET网站的发布,无论是初学者还是高手,在程序的发布过程中或多或少会存在一些问题,譬如VS发布ASP.NET程序失败.IIS安装失败.IIS发布失败.局域网内不能访问 配置文件错误.权限 ...
- Linux实战教学笔记10:正则表达式
第十节 正则表达式 标签(空格分隔):Linux实战教学笔记 ---更多资料点我查看 第1章 什么是正则表达式 正则表达式就是为了处理大量的文本|字符串而定义的一套规则和方法 通过定义的这些特殊符号的 ...
- Android开发学习——自定义View
转载自: http://blog.csdn.net/xmxkf/article/details/51454685 本文出自:[openXu的博客]
- Kafka connect快速构建数据ETL通道
摘要: 作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 业余时间调研了一下Kafka connect的配置和使用,记录一些自己的理解和心得,欢迎 ...