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" ...
随机推荐
- UITableView 表视图编辑
UITableViewController(表视图控制器)继承自UIViewController,自带一个tableView self.view不是UIView而是UITableView dataso ...
- php中$this、static、final、const、self 等几个关键字的用法
<?phpclass A { public static function get_self(){ return new self(); } public static function get ...
- [转]40多个关于人脸检测/识别的API、库和软件
[转]40多个关于人脸检测/识别的API.库和软件 http://news.cnblogs.com/n/185616/ 英文原文:List of 40+ Face Detection / Recogn ...
- The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
The ObjectContext instance has been disposed and can no longer be used for operations that require a ...
- UIDatePicker倒计时 swift
// // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...
- 微软职位内部推荐-SDE II
微软近期Open的职位: Senior Software Development Engineer Job Title: Senior Development Engineer Division: V ...
- Javascript null和undefined
Javascript的数据类型包括数字.字符串.布尔值.null.undefined和对象.其中null和undefined是两种特殊的原始类型,很容易混淆.今天就来剖析一下null和undefine ...
- Log4Net学习【二】
Log4Net结构详解 当我们在描述为系统做日志这个动作的时候,实际上描述了3个点:做日志,其实就是在规定,在什么地方 用什么日志记录器 以什么样的格式做日志.把三个最重要的点抽取出来,即什么地方,日 ...
- 软件工程随堂小作业——随机四则运算Ⅱ之算法思路(C++)
1.题目避免重复: (1)利用系统时间来产生随机数,重复率会降低. (2)建立链表,逐个判断.可读取写入文件. 2.可定制(数量/打印方式): (1)格式有默认值; (2)可以选择重新设置分几列和每行 ...
- CS小分队第一阶段冲刺站立会议(5月7日)
昨日完成任务:1.完成了游戏2048退出自动保存功能,进入自动读取存档功能, 2.完成游戏失败判断函数, 3.为游戏添加了背景音乐并且可以手动开关 遇到的困难:使用数据库时由于使用的ACCESS版本比 ...