【leetcode】 Longest Substring Without Repeating Characters
来源:互联网 发布:linux网卡配置命令 编辑:程序博客网 时间:2024/04/29 06:03
要想出O(n)的算法来解决此题,关键是确定一个良好的扫描策略,避免重复的无谓扫描。
详细分析可见http://leetcode.com/2011/05/longest-substring-without-repeating-characters.html
下面是Java实现:
import java.util.Arrays;public class Solution { public int lengthOfLongestSubstring(String s) { // Start typing your Java solution below // DO NOT write main() function // case of empty string if(s==null || s.isEmpty()==true) return 0; int len = s.length(); int res = 1; int start = 0, cur = 1; int position[] = new int[300]; Arrays.fill(position, -1); position[s.charAt(0)] = 0; // loop and find corresponding result while(cur < len){// s[cur] has not occurred if(position[s.charAt(cur)] < start){ res = Math.max(cur-start+1, res); position[s.charAt(cur)] = cur; }// s[cur] has occurred in previous substring else{ start = position[s.charAt(cur)] + 1; res = Math.max(res, cur-start+1); position[s.charAt(cur)] = cur; } cur++; }// End while loop return res; }// End method lengthOfLongestSubstring } // End class Solution
- 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
- 浏览器的两种模式quirks mode 和strict mode
- “由于无法验证发行者,所以WINDOWS已经阻止此软件”的解决方法
- 帧、关键帧和空白关键帧的作用
- 总结php常用的正则表达式规范
- java.lang.NoClassDefFoundError: org/jaxen/JaxenException
- 【leetcode】 Longest Substring Without Repeating Characters
- 怎样解决部分网站网页中IE9无法播放flash视频的问题--硬件加速功能存在冲突
- shell中的数值计算
- 关于css中定位的使用
- 用ZJTStatusBarAlertWindow实现通知信息
- 【stanford C++】——1.概述(Introduction)
- 报错:1130-host ... is not allowed to connect to this MySql server 开放mysql远程连接 不使用localhost
- Flashing Back a Failed Primary Database into a Physical Standby Database
- 数字图像处理基础知识点