leetcode:Longest Substring Without Repeating Characters
来源:互联网 发布:sql server设置默认值 编辑:程序博客网 时间:2024/06/18 01:01
求出串S最长的不重复子串
定义数组ans[] , ans[i]表示以第i个字符结尾的最长不重复子串的长度为ans[i],
那么可知:
ans[i] = ans[i - 1] + 1,如果第i个字符在S(i - 1 - ans[i - 1])......S(i -1)的范围内没有出现过
否则 ans[i] = i - 最后一次出现的位置;
public class Solution { public int lengthOfLongestSubstring(String s) { if (s.length() == 0) { return 0; } HashMap<Character, Integer> map = new HashMap<>(); int[] ans = new int[s.length()]; ans[0] = 1; map.put(s.charAt(0), 0); int max = 1; for (int i = 1, len = s.length(); i < len; ++i) { Integer site = map.get(s.charAt(i)); if (site == null || site < i - ans[i - 1]) { ans[i] = ans[i - 1] + 1; } else { ans[i] = i - site; } map.put(s.charAt(i), i); if (ans[i] > max) { max = ans[i]; } }// for (int k : ans) {// System.out.print(k + " ");// } return max; }}
0 0
- LeetCode: Longest Substring Without Repeating Characters
- LeetCode Longest Substring Without Repeating Characters
- LeetCode: Longest Substring Without Repeating Characters
- [Leetcode] Longest Substring Without Repeating Characters
- leetcode 23: Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- LeetCode 3 - Longest Substring Without Repeating Characters
- [LeetCode]Longest Substring Without Repeating Characters
- leetcode Longest Substring Without Repeating Characters
- 【leetcode】 Longest Substring Without Repeating Characters
- LeetCode-Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- [leetcode] 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
- leetcode之Longest Substring Without Repeating Characters
- fuelphp 学习第二天
- macos:获取c调用错误原因的描述,,,获取当前的工作目录的方法
- 【Matlab编程】Matlab高效编程技巧
- 寻找沙箱的Documents文件夹路径
- 创建表空间和用户并授权,更改用户
- leetcode:Longest Substring Without Repeating Characters
- NS2
- 尼玛梅西,贝利老马之争
- NS2机制
- python再学习4 启动cmd批处理文件
- MySql查询结果按照指定顺序排序
- adr,adrl和ldr指令的用法比较
- 单点登录CAS的超时时间的设置
- java日志功能log4j模块的使用方法