题目:

  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 最长无重复子串的更多相关文章

  1. [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  2. leetcode 3 Longest Substring Without Repeating Characters最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  3. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  4. [LeetCode] Longest Substring Without Repeating Characters最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  5. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  6. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

  7. 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)

    Given a string, find the length of the longest substring without repeating characters. Example 1:    ...

  8. LeetCode Longest Substring Without Repeating Characters 最长不重复子串

    题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离 ...

  9. 003 Longest Substring Without Repeating Characters 最长不重复子串

    Given a string, find the length of the longest substring without repeating characters.Examples:Given ...

随机推荐

  1. android学习笔记六——Spinner

    注:参考http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0105/2264.html Spinner ==> Spinner ...

  2. 共享内存是最快的一种IPC方式

    在linux进程间通信的方式中,共享内存是一种最快的IPC方式.因此,共享内存用于实现进程间大量的数据传输,共享内存的话,会在内存中单独开辟一段内存空间,这段内存空间有自己特有的数据结构,包括访问权限 ...

  3. 228. Summary Ranges

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

  4. java命令行HPROF Profiler

    The HPROF Profiler The Heap and CPU Profiling Agent (HPROF)是JAVA2 SDK自带的一个简单的profiler代理,它通过与Java Vir ...

  5. DBA_Oracle AWR Report性能监控报表(案例)

    2014-08-22 Created By BaoXinjian

  6. android 中怎么保存当前按钮的状态?就是退出后重新进入还是上一次离开的状态

    比如当前Activity中有一个按钮目前是开启,点击后按钮的text变成关闭!然后退出该Activtity,然后重新打开该Activity后当前按钮的状态还是关闭呢? 就是设置一个状态flag.fla ...

  7. 【weiphp微信开发教程】留言板插件开发详解

    基于weiphp框架的留言板插件教程: 1.功能分析 传统的留言板应该具有发布留言.查看留言.回复留言.管理留言等功能,本教程开发的是最基本的留言板,仅包含发布留言和查看留言两个功能,根据功能用boo ...

  8. PHP的require()函数可以在一行代码中多次读取

    [root@NJ232:~]$[root@NJ232:~]$more tt.php m#!/opt/php/bin/php -q<?phpwhile(1){ sleep(2); $arr = r ...

  9. DIV+CSS滑动门效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 安装程序无法复制文件 convlog.exe的解决方法

    在安装的时候出现一个错误提示“安装程序无法复制文件CONVLOG.EX_”,上网找了很多资料,都说是因为版本问题,考虑到自己的服务器安装的是2003 SP1,后来打了补丁到SP2的,也就认为是版本问题 ...