Q3:Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters
官方的链接:3. Longest Substring Without Repeating Characters
Description :
Given a string, find the length of the longest substring without repeating characters.
Example:
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.
问题描述
给定一个字符串,找出最长不重复子串的长度
思路
建立一个128的数组,存储字符的下标,min存储非重复子串的最小下标,max存储最大下标。max逐步加大,如果下一个字符的下标大于min,则增加min,同时记录最大的max - min + 1
public class Q3_LongestSubstringWithoutRepeatingCharacters {
public int lengthOfLongestSubstring(String s) {
if (null == s || s.length() < 1) {
return 0;
}
// 初始化128的数组,足够存储所有字符
int[] intArray = new int[128];
for (int i = 0; i < intArray.length; i++) {
intArray[i] = -1;
}
char num = s.charAt(0);
intArray[num] = 0;
int min = 0;
int max = 0;
int count = 1;
for (int i = 1; i < s.length(); i++) {
num = s.charAt(i);
max = i;
if (intArray[num] == (max - 1)) {
count = max - min > count ? max - min : count;
min = max;
} else if (intArray[num] >= min) {
count = max - intArray[num] > count ? max - intArray[num] : count;
min = intArray[num] + 1;
} else {
count = max - min + 1 > count ? max - min + 1 : count;
}
intArray[num] = i;
}
return count;
} public static void main(String[] args) {
Q3_LongestSubstringWithoutRepeatingCharacters longest = new Q3_LongestSubstringWithoutRepeatingCharacters();
System.out.println(longest.lengthOfLongestSubstring("nfpdmpi"));
}
}
Q3:Longest Substring Without Repeating Characters的更多相关文章
- LeetCode3:Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- No.003:Longest Substring Without Repeating Characters
问题: Given a string, find the length of the longest substring without repeating characters.Example:Gi ...
- Leetcode经典试题:Longest Substring Without Repeating Characters解析
题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...
- LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List
题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: Given a string, find the length of the longest substring without ...
- leetcode笔记:Longest Substring Without Repeating Characters
一. 题目描写叙述 Given a string, find the length of the longest substring without repeating characters. For ...
- leetcode:Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters
[Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- java虚拟机05(Java虚拟机的参数)
原文在此 (1)-Xms20M 表示设置堆容量的最小值为20M,必须以M为单位 (2)-Xmx20M 表示设置堆容量的最大值为20M,必须以M为单位.将-Xmx和-Xms设置为一样可以避免堆自动扩展. ...
- 将xml字符串的所有叶标签转换成Map集合
实际问题:对方服务器接口采用webservice方式返回xml报文,现需解析xml获取所有叶节点的标签名及携带的值 解决方案:利用dom4j解析xml并利用递归获取叶节点,将标签名及标签值封装到Map ...
- git commit -m 和 git commit -am 区别
git commit -m 和 git commit -am 通常修改一个文件 并且将文件提交到本地分支的命令是: git add . git commit -m 'update' 以上两个命令其实可 ...
- Centos 8下普通用户增加root权限
问题: 解决: 重启Centos,使用root登陆:
- Linux学习《第四章shell脚本练习一》随堂练习(重要)
- win下的常用8个命令
windows下常用的几个指令 一,ping 它是用来检查网络是否通畅或者网络连接速度的命令.作为一个生活在网络上的管理员或者黑客来说,ping命令是第一个必须掌握的DOS命令,它所利用的原理是这样的 ...
- “~" 的用法
“~" 的用法 let arr = ['weixin','qq','weibo'] console.log(arr.indexOf('aa'),~arr.indexOf('aa'),'aa' ...
- 动手实验01-----vCenter 微软AD认证配置与用户授权
环境说明: AD域-> centaline.net 阅读目录: 1. 配置与AD认证源 2.权限角色 1. 配置与AD认证源 登陆vCenter后,在 系统管理 -> 配置 -> ...
- 官网英文版学习——RabbitMQ学习笔记(七)Topic
在上一篇中使用直接交换器改进了我们的系统,使得它能够有选择的进行接收消息,但它仍然有局限性——它不能基于多个条件进行路由.本节我们就进行能够基于多个条件进行路由的topics exchange学习. ...
- idea开发springboot 的mysql数据库连接问题
今天在家用idea进行springboot开发,前面一些坑相对避免了,但是到数据库这块总是连接不上,报错主要是: Access denied for user 'root'@'localhost' ( ...