LeetCod-3. Longest Substring Without Repeating Characters
2016-05-03 10:49
471 查看
Problem:
Analysis:
题意:
给定一个字符串,找到其中的一个最长的字串,使得这个子串不包含重复的字符。
方法:
利用双指针,开一个数组记录当前字符最近出现的位置,一遍算过去,更新左边界,用它计算最大值就行了。时间复杂度O(n),空间复杂度O(1).
Answer:
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. Subscribe to see which companies asked this question
Analysis:
题意:
给定一个字符串,找到其中的一个最长的字串,使得这个子串不包含重复的字符。
方法:
利用双指针,开一个数组记录当前字符最近出现的位置,一遍算过去,更新左边界,用它计算最大值就行了。时间复杂度O(n),空间复杂度O(1).
Answer:
public class Solution { public int lengthOfLongestSubstring(String s) { int res=0,left=0; int[] arr = new int[128]; for(int i=0;i<arr.length;i++){ arr[i]=-1; } for(int i=0;i<s.length();i++){ char temp = s.charAt(i); if(arr[temp]>=left) left = arr[temp]+1; arr[temp]=i; if(res< i-left+1) res = i-left+1; } return res; } }
相关文章推荐
- 在Java中如何遍历Map对象
- 深入理解Java的接口和抽象类
- Open quote is expected for attribute "{1}" associated with an element type "type".
- java.net.ConnectException: Connection refused
- JS中三目运算符和if else的区别分析与示例
- Swift —构造函数与存储属性初始化-备
- Android ListView分页功能实现方法
- 【追求进步】把字符串转换成整数
- IO管理四Usb之resource资源申请
- android设计模式
- Asp.net 企业建站CMS
- 欢迎使用CSDN-markdown编辑器
- hibernate级联
- Linux环境下段错误的产生原因及调试方法小结
- bower 教程
- linux内存源码分析 - 内存回收(整体流程)
- Java使用HTTPClient4.3开发的公众平台消息模板的推送功能
- Swift—默认构造函数-备
- 前端页面完成后如何放在IDEA项目工程的目录下能正确加载css样式
- C#访问修饰符