字符串——最长无重复子串
来源:互联网 发布:mac终端如何输入密码 编辑:程序博客网 时间:2024/05/29 08:35
转自:https://discuss.leetcode.com/topic/8232/11-line-simple-java-solution-o-n-with-explanation
the basic idea is, keep a hashmap which stores the characters in string as keys and their positions as values, and keep two pointers which define the max substring. move the right pointer to scan through the string , and meanwhile update the hashmap. If the character is already in the hashmap, then move the left pointer to the right of the same character last found. Note that the two pointers can only move forward.
public int lengthOfLongestSubstring(String s) { if (s.length()==0) return 0; HashMap<Character, Integer> map = new HashMap<Character, Integer>(); int max=0; for (int i=0, j=0; i<s.length(); ++i){ if (map.containsKey(s.charAt(i))){ j = Math.max(j,map.get(s.charAt(i))+1); } map.put(s.charAt(i),i); max = Math.max(max,i-j+1); } return max; }
阅读全文
0 0
- 字符串——最长无重复子串
- 字符串算法——最长子串(无重复元素)
- 最长无重复子字符串
- 华为机试——求字符串中最长无重复子串
- 最长无重复子串
- 最长无重复子串
- 最长无重复子串
- 求字符串中最长无重复字符的子串
- 求字符串中最长无重复字符的子串
- 查找字符串中最长无重复字符的子串
- 求字符串中最长无重复字符的子串
- 求字符串中最长无重复字符的子串
- 求字符串的最长无重复字符子串(C++)
- 求字符串中最长无重复字符的子串
- 字符串中最长无重复字符的子串
- 求字符串中最长无重复字符的子串
- 求字符串中最长无重复字符的子串
- 找到字符串的最长无重复字符子串
- :-1: 错误:cannot open output file release/first.exe: Permission denied
- 【PAT】【Advanced Level】1004. Counting Leaves (30)
- 百度地图
- 录屏软件 gif
- 关于取消浮动影响的多种方法及优缺点比较
- 字符串——最长无重复子串
- iwebshop模拟秒杀
- iwebshop插件的操作
- iwebshop中的增删改查
- php锁表
- Nginx中 conf文件的内置变量
- php时间函数
- PHP和js实时倒计时
- iwebshop两表联查