LeetCode-3 Longest Substring Without Repeating Characters(最长无重复子串)
来源:互联网 发布:淘宝汽车贴膜服务app 编辑:程序博客网 时间:2024/06/05 22:39
LeetCode-3 Longest Substring Without Repeating Characters
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.
JAVA:
import java.util.Arrays;public class Solution {public static int lengthOfLongestSubstring(String s) {int[] zimu = new int[1000];int max = 0;int start = 0;//记录起点位置Arrays.fill(zimu, -1);for (int i = 0; i < s.length(); i++) {if (zimu[s.charAt(i)] >= start) {max = Math.max(i - start, max);start = zimu[s.charAt(i)] + 1;//出现重复时,从重复点下一个位置作为初始点继续往下。}zimu[s.charAt(i)] = i;}return Math.max(s.length() - start, max);//结尾的情况。eg:“abcd”}}
Runtime: 277ms
分析:
比较关键的地方是记录起点位置,每次存进数组的内容都是元素出现的位置,然后在重复出现时,直接将新的位置减去数组内容,就能得到一个长度。然后将数组内存放的位置信息作为新的起点,然后继续遍历.......
0 0
- LeetCode-3 Longest Substring Without Repeating Characters(最长无重复子串)
- leetcode 3 : Longest Substring Without Repeating Characters 最长无重复子串 (C# 语言版)
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
- [Leetcode #3]Longest Substring Without Repeating Characters 无重复字符的最长子串
- LeetCode-3:Longest Substring Without Repeating Characters (最长无重复字符的子串) --medium
- leetcode:Longest Substring Without Repeating Characters (寻找最长无重复字符的子串)
- LeetCode OJ 之 Longest Substring Without Repeating Characters (无重复的最长子串)
- 【LeetCode】4. Longest Substring Without Repeating Characters 最长无重复子串
- LeetCode OJ-3.Longest Substring Without Repeating Characters(最长无重复子串)
- [LeetCode]3. Longest Substring Without Repeating Characters 最长无重复子串解析。java
- #3 Longest Substring Without Repeating Characters(最长无重复元素子串)
- [C++]LeetCode 3: Longest Substring Without Repeating Characters(最长不重复子串)
- leetcode 3. Longest Substring Without Repeating Characters 最长不重复子串和重复子串
- 寻找最长无重复字符子串 Longest Substring Without Repeating Characters
- 【字符串&最长无重复子串】Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters,最长无重复字符子串
- Longest Substring Without Repeating Characters 字符串中最长的无重复子串长度
- Longest Substring Without Repeating Characters (返回最长无字符重复的子串长度)
- An internal error occurred during: "Launching java.lang.NullPointerException
- 嵌入式软件开发培训笔记——C高级 开发环境
- java.io.Reader.read(char[] cbuf)方法实例
- setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN)设置失败
- Change the system locale
- LeetCode-3 Longest Substring Without Repeating Characters(最长无重复子串)
- C++中指针和引用的区别
- Java Web入门之JavaBean要点
- 工程国际化
- 欢迎使用CSDN-markdown编辑器
- python wsgi pep333 中文翻译
- JNI获取android wifi interface name
- 嵌入式软件开发培训笔记——C高级 指针
- 旋转后图片放大变形