LeetCode #3 Longest Substring Without Repeating Characters C# Solution
来源:互联网 发布:php表单提交到数据库 编辑:程序博客网 时间:2024/05/10 09:24
LeetCode #3 Problem
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.
记录目前考察的子串的开头和结尾,出现过的字母、出现过的子串的字母的位置。
从头到尾扫这个数组,每往后扫一位,如果pos位置上的字母出现过,那就吧start移动到上一个这个字母出现的位置同时end++;如果没出现过那就end++。
第一次提交的时候以为这个字符串只有字母,WA了一次……看到测试数据的我是崩溃的……
C# Code public class Solution { public int LengthOfLongestSubstring(string s) { int start = 0, end = 0; bool[] exist = new bool[300]; int[] position = new int[300]; for (int i = 0; i < 200; i++) { exist[i] = false; position[i] = 0; } for (int i = 0; i < s.Length; i++) { if (exist[s[i]] == true) { for (int j = start; j <= position[s[i]]; j++) exist[s[j]] = false; start = position[s[i]] + 1; exist[s[i]] = true; position[s[i]] = i; } else { exist[s[i]] = true; position[s[i]] = i; if (end <= i - start + 1) end = i - start + 1; } } return end; } }
0 0
- LeetCode #3 Longest Substring Without Repeating Characters C# Solution
- 【LeetCode】C# 3、Longest Substring Without Repeating Characters
- solution of LeetCode 3. Longest Substring Without Repeating Characters
- [LeetCode Solution]3.Longest Substring Without Repeating Characters
- LeetCode 3 - Longest Substring Without Repeating Characters
- LeetCode 3: Longest Substring Without Repeating Characters
- leetcode-3:Longest Substring Without Repeating Characters
- LeetCode(3) Longest Substring Without Repeating Characters
- leetcode 3LONGEST SUBSTRING WITHOUT REPEATING CHARACTERS
- Leetcode【3】:Longest Substring Without Repeating Characters
- [leetcode 3] Longest Substring Without Repeating Characters
- [Leetcode] 3 - Longest Substring Without Repeating Characters
- LeetCode 3:《Longest Substring Without Repeating Characters》
- [leetcode 3] Longest Substring Without Repeating Characters
- [leetcode] 3 Longest Substring Without Repeating Characters
- [Leetcode]3Longest Substring Without Repeating Characters
- leetcode 3 Longest Substring Without Repeating Characters
- LeetCode #3 Longest Substring Without Repeating Characters
- HTML5简介&视频&音频&拖放&画布&SVG
- 处理器MMU的作用
- IT忍者神龟之MysqlDATE_FORMAT函数的作用
- 【调试】JavaWeb工程发布失败
- 单例模式
- LeetCode #3 Longest Substring Without Repeating Characters C# Solution
- 【机器学习算法2】——SVM
- C++ —— RAII编程思想
- 2016-4-13
- 【bzoj4500】【矩阵】【dfs】
- HTML5地理定位
- RN(react native)入坑指南-04,布局容器
- Android Studio 进阶配置
- SpringMVC 中HttpMessageConverter简介和Http请求415 Unsupported Media Type的问题 对比项目用的