lintcode: 最长无重复字符的子串
题目
最长无重复字符的子串给定一个字符串,请找出其中无重复字符的最长子字符串。
例如,在"abcabcbb"
中,其无重复字符的最长子字符串是"abc"
,其长度为 3
。
对于,"bbbbb"
,其无重复字符的最长子字符串为"b"
,长度为1
。
解题
利用HashMap,map中不存在就一直加入,存在的时候,找到相同字符的位置,情况map,更改下标
public class Solution {
/**
* @param s: a string
* @return: an integer
*/
public int lengthOfLongestSubstring(String s) {
// write your code here
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
if(s == null)
return 0;
if(s.length() <=1)
return s.length();
int longest = -1;
for(int i = 0;i<s.length();i++){
char ch = s.charAt(i);
if(map.containsKey(ch)){
longest = Math.max(map.size(),longest);
i = map.get(ch);
map.clear();
}else{
map.put(ch,i);
}
}
longest = Math.max(map.size(),longest);
map.clear();
return longest;
}
}
利用数组来判断该元素是否已经存在过
public class Solution {
/**
* @param s: a string
* @return: an integer
*/
public int lengthOfLongestSubstring(String s) {
// write your code here
if(s == null)
return 0;
if(s.length() <=1)
return s.length();
int longest = -1;
int start = 0;
boolean[] flag = new boolean[256];
char[] arr = s.toCharArray();
for(int i =0;i< arr.length;i++){
char cur = arr[i];
if(flag[cur]){
longest = Math.max(longest,i - start);
for(int k = start;k<i;k++){
if( arr[k] == cur){
start = k + 1;
break;
}
flag[arr[k]] = false;
}
}else{
flag[cur] = true;
}
}
longest = Math.max(longest,arr.length - start);
return longest;
}
}
lintcode: 最长无重复字符的子串的更多相关文章
- lintcode-384-最长无重复字符的子串
384-最长无重复字符的子串 给定一个字符串,请找出其中无重复字符的最长子字符串. 样例 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc" ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- leetcode-最长无重复字符的子串
参考他的人代码:https://blog.csdn.net/littlebai07/article/details/79100081 给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: ...
- 最长无重复字符的子串 · Longest Substring Without Repeating Characters
[抄题]: 给定一个字符串,请找出其中无重复字符的最长子字符串. 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc",其长度为 3. 对于, ...
- LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)
这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...
- 3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium
Examples: Description: Given a string, find the length of the longest substring without repeating ch ...
- LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- LeetCode(3):无重复字符的最长子串
Medium! 题目描述: 给定一个字符串,找出不含有重复字符的 最长子串 的长度. 示例: 给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ...
随机推荐
- DataTemplate和ControlTemplate联系与区别
---恢复内容开始--- 正如标题中的两个拼接的单词所说,DataTemplate就是数据显示的模板,而ControlTemplate是控件自身的模板.(个人理解,错误请指出,谢谢) 我们看这二者在两 ...
- 四则运算程序扩展:将程序改为java语言,并允许用户输入,对输入结果进行验证
题目 每个同学选一个方向,把程序扩展一下:1.让程序能接受用户输入答案,并判定对错.最后给出总共对/错 的数量.2.把程序变成一个网页程序,用户通过设定参数,就可以得到各种题目.3.把程序变成一个Wi ...
- Python中的List,Tuple,Dic,Set
Python中的List,Tuple,Dic,Set List定义 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推 ...
- SASS学习笔记_01
scss两种格式 sass 大括号 scss css写法 Arguments: --no-cache –style compressed --update $FileName$:c ...
- [bzoj 2097]奶牛健美操
题目描述 对于一棵n个点的树,删除k条边,使得所有联通块直径最大值最小 题解 首先二分联通块直径最大值的最小值. 那么这个能否达成的判定变成了一个类似树形dp的东西 对于一个子树,删除一条边可以删除整 ...
- 系统集成之用户统一登录( LDAP + wso2 Identity Server)
本文场景: LDAP + wso2 Identity Server + asp.net声明感知 场景 ,假定读者已经了解过ws-*协议族,及 ws-trust 和 ws-federation. 随着开 ...
- NGP处理包
NGP处理部分(主要就是这个RunOnce函数,客户单肯定是开个线程取调用这个RunOnce的) void NGP::RunOnce() { m_spTimerFac->driveTimer() ...
- POI中设置Excel单元格格式
引用:http://apps.hi.baidu.com/share/detail/17249059 POI中可能会用到一些需要设置EXCEL单元格格式的操作小结: 先获取工作薄对象: HSSFWork ...
- Unity--截取屏幕任意区域
原地址:http://blog.csdn.net/tanmengwen/article/details/8501612 void Update () { if(Input.GetKeyDown(Key ...
- C# 对委托的BeginInvoke,EndInvoke 及Control 的BeginInvoke,EndInvoke 的理解
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...