【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目:
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.
思路:
初始字符上一次出现的位置-1,初始子串开始位置-1。对字符串中每个字符元素遍历,判断更新子串的开始位置及最大长度,循环结束后 返回最大长度max.
public class Solution {
public int lengthOfLongestSubstring(String s) {
int[] local=new int[128];
int index=-1;
int max=0; for(int a=0;a<local.length;a++){
local[a]=-1;
} for(int i=0;i<s.length();i++){ if(local[(int)s.charAt(i)]>index){
index=local[(int)s.charAt(i)];
} if(i-index>max){
max=i-index;
} local[(int)s.charAt(i)]=i;
} return max;
}
}
【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串的更多相关文章
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- [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: ...
- LeetCode Longest Substring Without Repeating Characters 最长不重复子串
题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离 ...
- 003 Longest Substring Without Repeating Characters 最长不重复子串
Given a string, find the length of the longest substring without repeating characters.Examples:Given ...
随机推荐
- Centos7和win7双系统调整默认启动
centos7之后都上grub2了,所以你要更改默认启动项什么的就不能像以前一样去改 /etc/grub.conf 当然你更不能去改/etc/grub2.conf 上了grub2之后,在设计有意规避让 ...
- 拒绝了对对象 'sp_sdidebug'(数据库 'master',所有者 'dbo')的 EXECUTE 权限。
如果在调试过程中出现异常“拒绝了对对象 'sp_sdidebug'(数据库 'master',所有者 'dbo')的 EXECUTE 权限.”则可以通过以下方式解决: 打开master数据库,打开扩展 ...
- MySQL优化—工欲善其事,必先利其器之EXPLAIN
最近慢慢接触MySQL,了解如何优化它也迫在眉睫了,话说工欲善其事,必先利其器.最近我就打算了解下几个优化MySQL中经常用到的工具.今天就简单介绍下EXPLAIN. 内容导航 id select_t ...
- PHP中的中文截取乱码问题_gb2312_utf-8
一.字符串编码为gb2312,一个中文占俩字节 public static function chinesesubstr($str, $start, $len) { // $str指字符串,$star ...
- Redis几个认识误区(转)
此文的作者是新浪微博平台架构师杨卫华(timyang)大师,如果关注了新浪一些牛人微博的同学应该知道,timyang前段时间正在对Redis进行一些研究和测试,也分享出了不少成果.下面一篇文章相信是t ...
- Java执行main方法,异常为:could not find the main class.program will exit
未解决. Java执行方法,异常为:could not find the main class.program will exitmain 原文地址:http://rogerfederer.iteye ...
- Shell_Oracle Erp和其他系统Interface资料传输通过Shell进行控制(案例)
2014-06-26 Created By BaoXinjian
- POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题
一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...
- ZOJ 3601 Unrequited Love 浙江省第九届省赛
Unrequited Love Time Limit: 16 Seconds Memory Limit: 131072 KB There are n single boys and m si ...
- 类(class)、构造函数(constructor)、原型(prototype)
类 Class 类的概念应该是面向对象语言的一个特色,但是JavaScript并不像Java,C++等高级语言那样拥有正式的类,而是多数通过构造器以及原型方式来仿造实现.在讨论构造器和原型方法前,我可 ...