LeetCode 3. Longest Substring Without Repeating Characters--不包含重复字符的最长子串长度
来源:互联网 发布:owncloud php网盘源码 编辑:程序博客网 时间:2024/05/16 00:34
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.
package com.main;import java.util.HashMap;import java.util.Map;public class Main { //两个指针,i在前,j在后, //一开始没遇到重复元素时,i每次前进一次,j始终未0 //当后面每次遇到重复元素时,j指向前面重复元素的后一个位置,这样能保证i-j+1始终为最长的不重复子字符串的长度 public int lengthOfLongestSubstring(String s) { Map<Character, Integer> map = new HashMap<Character, Integer>(); int max = 0; Character c; for (int i = 0, j = 0; i < s.length(); i++) {//i>=j始终成立 c = s.charAt(i); if (map.containsKey(c)) { j = Math.max(j, map.get(c) + 1);//j指针不能后退 //j = map.get(s.charAt(i))+1;如果是这样则对于“abba”字符串,在i指向最后一个‘a’时j会后退,会输出3则错误,正确为2 } map.put(c, i); max = Math.max(max, i - j + 1); } return max; } public static void main(String[] args) { // write your code here Main main = new Main(); int max = main.lengthOfLongestSubstring("abcab");//3 System.out.println(max); }}
983 / 983 test cases passed.
Status: Accepted
Runtime: 51 ms
Your runtime beats 77.81 % of java submissions.
T(n) = O(n)
T(n) = O(n)
阅读全文
0 0
- LeetCode 3. Longest Substring Without Repeating Characters--不包含重复字符的最长子串长度
- 3. Longest Substring Without Repeating Characters(计算不含重复字符的最长子串的长度)
- [leetcode]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 不含重复字符的最长子串
- LeetCode 题解 3. Longest Substring Without Repeating Characters(最长不含重复字符的子字符串)
- leetcode:Longest Substring Without Repeating Characters (寻找最长无重复字符的子串)
- [Leetcode #3]Longest Substring Without Repeating Characters 无重复字符的最长子串
- LeetCode-3:Longest Substring Without Repeating Characters (最长无重复字符的子串) --medium
- LeetCode 3. Longest Substring Without Repeating Characters(最长不重复子串)
- Leetcode#3. Longest Substring Without Repeating Characters(最长不重复子串-哈希))
- Leetcode 3. Longest Substring Without Repeating Characters(最长不重复子串)
- Longest Substring Without Repeating Characters (最长不重复字符的子序列) Java-O(n)解法
- Longest Substring Without Repeating Characters 最长不重复字符的字串 @LeetCode
- LeetCode Longest Substring Without Repeating Characters 最长不重复子串查找
- 【LeetCode】Longest Substring Without Repeating Characters && 【九度】题目1530:最长不重复子串
- TUXEDO配置常见问题及解决方法
- 【过滤器】(4)过滤器的应用案例:留言板敏感词过滤技术(包装模式)
- STM32F0两路AD配置参考
- EGADS学习资料
- Uncaught ReferenceError: Invalid left-hand side expression in postfix operation
- LeetCode 3. Longest Substring Without Repeating Characters--不包含重复字符的最长子串长度
- 解决vue不相关组件之前数据传输问题
- (五)图与网络
- 关于IDEA profile点击无效的问题
- 人生在这里转弯
- js语句和函数
- 西安集训第一天----乱搞
- c#扩展方法
- 使用ActiveMQ进行C++与C#的通信4