leetcode - 3、Longest Substring Without Repeating Characters
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
题目要求:求出最长的不重复子串
思路:
1、采用map,遍历字符串,将每个字符put进map
2、如果长度为1,直接输出1
3、end-begin+1(注意+1)
end为当前遍历到的位置,
begin为重复位置+1
4、在遍历字符串的时候,判断当前字符是否存在resultMap.containsKey(key),
如果不存在,将其put进map,并维护end-begin+1
如果存在,重置begin位置,计算长度
(begin的位置判断:如果当前begin位置index比重复位置大,需要begin==当前begin位置index,否则等于重复位置)
begin = begin > (resultMap.get(key)+1) ? begin : (resultMap.get(key)+1)
长度维护:
result = result > (end-begin+1) ? result : (end-begin+1);
public class Solution {
public int lengthOfLongestSubstring(String s) {
char str[] = s.toCharArray();
int len = str.length;
if(len == 1 ){
return 1;
}else {
// int sum = 0;
int result = 0;
int begin = 0;
int end = 0;
Map<String,Integer> resultMap = new HashMap<String, Integer>();
for(int i = 0; i<len; i++){
String key = str[i]+"";
end = i;
//未重复
if(!resultMap.containsKey(key)){
resultMap.put(key,i);
result = result > (end-begin+1) ? result : (end-begin+1);
}else { //遇到重复
// resultMap.clear();
begin = begin > (resultMap.get(key)+1) ? begin : (resultMap.get(key)+1);
result = result > (end-begin+1) ? result : (end-begin+1);
resultMap.put(key,i);
}
}
// System.out.println(result);
return result;
}
}
}
leetcode - 3、Longest Substring Without Repeating Characters的更多相关文章
- 【LeetCode】3 、Longest Substring Without Repeating Characters
题目等级:Medium 题目描述: Given a string, find the length of the longest substring without repeating chara ...
- 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters
一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...
- 【LeetCode OJ】Longest Substring Without Repeating Characters
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【LeetCode】3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- leetcode题解 3. Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
随机推荐
- C# datatable竖行转换的问题
这次在做项目中,遇到了这样一个问题:datable中列头的名字是不确定的,从数据库中动态查出来的,假设为typeDATA,行的数据中又包含了列头的信息,并按固定的字段分组,当查处行的数据之后用来填充每 ...
- C/S模式与B/
网络程序开发的两种计算模式--C/S模式与B/S模式.两种各有千秋,用于不同场合. C/S适用于专人使用,安全性要求较高的系统: B/S适用于交互性比较频繁的场合,容易被人们所接受,倍受用户和软件开发 ...
- DP 过河卒
棋盘上A点有一个过河卒,需要走到目标B点.卒行走的规则:可以向下.或者向右.同时在棋盘上C点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为“马拦过河卒”. 棋盘用坐标 ...
- PorterDuff.Mode
参考:http://weishu.me/2015/09/23/Xfermode-in-android/ Sa = Source alphaDa = Dest alphaSc = Source colo ...
- java之IO整理(下)
一:对象的序列化 对象序列化就是把一个对象变为二进制数据流的一种方法. 一个类要想被序列化,就行必须实现java.io.Serializable接口.虽然这个接口中没有任何方法,就如同之前的clone ...
- Centos如何设置IP地址,LINUX怎么修改IP地址
对于很多刚刚接触linux的朋友来说,如何设置linux系统的IP地址,作为第一步,下面小编以centos系统为例,给大家演示如何给centos设置IP地址,如何修改linux 系统IP地址? 步骤阅 ...
- CocoStudio创建动画帧
进入动画编辑器 选择“形体模式” 右键点击资源窗口的资源,可以进行删除,重命名的操作: 可以再资源窗口下方的预览窗口,查看选中的资源预览效果图: 右键点击“对象结构”,创建图层 选择“动画模式” 右 ...
- linux编辑器使用记录
超强大vim配置文件: wget http://files.cnblogs.com/ma6174/vimrc.zip unzip -f vimrc.zip -d ~/ 一.vim编辑器 进入 ...
- 第七篇 Flask 中路由系统
1. @app.route() 装饰器中的参数 如果不明白装饰器 点击这里 methods : 当前 url 地址,允许访问的请求方式 @app.route("/info", me ...
- 「小程序JAVA实战」小程序我的个人信息-注销功能(42)
转自:https://idig8.com/2018/09/06/xiaochengxujavashizhanxiaochengxuwodegerenxinxi-zhuxiaogongneng40/ 注 ...