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.

我自己的代码:

public class Solution {
public int lengthOfLongestSubstring(String s) {
int slenmax=0;
for(int i=0;i<s.length();i++)
{
int j=i+1;
boolean flag=true;
while(j<s.length()&&flag)
{
for(int k=i;k<j;k++)
{ if(s.charAt(j)==s.charAt(k))
{
flag=false;
j--;
}
}
j++;
}
int slen=j-i;
if(slen>slenmax)
slenmax=slen;
}
return slenmax;
}
}

运行时可以通过,但是在提交时出现超时(思路比较简单,因而时间复杂度相应会比较高)

clean Code:

public class Solution {
public int lengthOfLongestSubstring(String s) {
boolean[] exist = new boolean[256]; //用表格处理,查找时会很快
int i = 0, maxLen = 0;
for (int j = 0; j < s.length(); j++) {
while (exist[s.charAt(j)]) { //这个while是精髓
exist[s.charAt(i)] = false;
i++;
}
exist[s.charAt(j)] = true;
maxLen = Math.max(j - i + 1, maxLen);
}
return maxLen;
}
} 

注:借用了表的形式,把字符串中的字母依次存入表中并进行相应标记(如 exist[s.charAt(i)] = false;)。解决思路是:设定两个指针(i, j),都放在左头开始,j从左到右,遇到有两个相同字母的情况,j停止,计算两相同字母间距,更新最大间距,并且把i 也逐步移到第一个相同字母的下个位置,j再继续移动。

这样时间复杂度为O(n+n)=O(2n);

clean code 2:

public int lengthOfLongestSubstring(String s) {
int[] charMap = new int[256];
Arrays.fill(charMap, -1);
int i = 0, maxLen = 0;
for (int j = 0; j < s.length(); j++) {
if (charMap[s.charAt(j)] >= i) {
i = charMap[s.charAt(j)] + 1;
}
charMap[s.charAt(j)] = j;
maxLen = Math.max(j - i + 1, maxLen);
}
return maxLen;
}

注:用整型表代替布尔型表,时间复杂度降到O(n),原因是在遇到有两个相同字母时没有上面中的 i 逐步移到第一个相同字母下一位置的过程,此处一步到位

Leetcode 详解(Substing without repeats character)的更多相关文章

  1. Leetcode 详解(ReverseWords)

    Leetcode里面关于字符串的一些问题,描述如下: Given an input string, reverse the string word by word. For example,Given ...

  2. 由Leetcode详解算法 之 动态规划(DP)

    因为最近一段时间接触了一些Leetcode上的题目,发现许多题目的解题思路相似,从中其实可以了解某类算法的一些应用场景. 这个随笔系列就是我尝试的分析总结,希望也能给大家一些启发. 动态规划的基本概念 ...

  3. Leetcode 详解(Valid Number)

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  4. Leetcode 详解(valid plindrome)

    Question: Given a string, determine if it is a palindrome, considering only alphanumeric characters ...

  5. Leetcode 详解(股票交易日)(动态规划DP)

    问题描述: 在股市的交易日中,假设最多可进行两次买卖(即买和卖的次数均小于等于2),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行).给出一天中的股票变化序列,请写一个程序计算一天可以获得 ...

  6. Leetcode详解Maximum Sum Subarray

    Question: Find the contiguous subarray within an array (containing at least one number) that has the ...

  7. Leetcode 详解(Implement strstr)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  8. The Skyline Problem leetcode 详解

    class Solution { public: vector<pair<int, int>> getSkyline(vector<vector<int>&g ...

  9. LeetCode(42.接雨水)多解法详解

    接雨水解法详解: 题目: 基本思路:从图上可以看出要想接住雨水,必须是凹字形的,也就是当前位置的左右两边必须存在高度大于它的地方,所以我们要想知道当前位置最多能存储多少水,只需找到左边最高处max_l ...

随机推荐

  1. Python爬网获取全国各地律师电话号

    [本文出自天外归云的博客园] 从64365网站获取全国各地律师电话号,用到了python的lxml库进行对html页面内容的解析,对于xpath的获取和正确性校验,需要在火狐浏览器安装firebug和 ...

  2. c++多线程の死锁与防止死锁

    如果有两把锁 lock1(mutex_gard 方式)和lock: 两者的调用顺序不同,会出现相互等待的情况,从而造成死锁: 为了避免死锁,我们可以: 1.每个线程中锁的调用顺序都相同: 2.使用st ...

  3. NSValue 类的使用

    NSValue对象是用来存储一个C或者Objective-C数据的简单容器.它可以保存任意类型的数据int,float,char等,也可以是指pointers, structures, and obj ...

  4. 【前端】提取URL中的各个GET参数

    /**************************** * 有这样一个URL:http://item.taobao.com/item.htm?a=1&b=2&c=&d=xx ...

  5. div+css 设计下拉

    css样式 <style type="text/css"> <!-- /* www.divcss5.com CSS下拉菜单实例 */ * { margin:; p ...

  6. Tips collection of iOS development

    <转>UITableView当数据很少的时候,去掉多余的cell分割线   在tableView初始化的时候 UIView *v = [[UIViewalloc] initWithFram ...

  7. C# 关于委托和事件的妙文:通过一个例子详细介绍委托和事件的作用;Observer模式简介

    委托和事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去的人每次见 ...

  8. python 最大公约数

    求解两个整数(不能是负数)的最大公约数(要求两数不能同时为0)当两数都是0时,最大公约数为0方式一:穷举法 def GCU(m, n): if not m: return n elif not n: ...

  9. Sublime Text的常用插件

    工欲善其事,必先利其器.好的插件会更多的帮助我们装逼. 最新感悟:也不要装太多的插件.有些无用的插件可以删除,有时反而臃肿.bloger下载官方最新稳定版st3 3126下载下来25M左右.安装十来个 ...

  10. js设计模式总结-代理模式

    代理模式 解决哪一类问题 从字面意思上理解,代理模式解决对一个对象的直接访问,这种直接访问可能是"不方便"的,所谓"不方便"可能是直接访问成本比较大(在前端领域 ...