一天一道LeetCode

(一)题目

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1.

题目意思:求一字符串中最长的无重复的字符串

(二) 解题过程

一开始拿到这题,直接暴力解决,用双重循环找无重复的子字符串

结果可想而知,超时!Time Limit Exceeded!

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int max = 0 ;
        for(int i = 0 ; i < s.length();++i)
        {
            int j = i+1;
            int count = 1;
            while(j<s.length())
            {
                int z = j-1;
                while(s[j] != s[z]&&z>=i) z--;//判断是s[j]不等于s[i~j]任一个字符
                if(z+1 == i) count++;//如果不等,则count++
                j++;
            }
            if(max<count) max = count;//记录最大值
        }
        return max;
    }
};

O(n^2)的复杂度确实太大了,那么,只能另寻他径了。

降低复杂度的方式:以空间换时间,这里我们可以用到一个数组来记录字符出现过没有。

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int last[256]; //字符的ascII码0~256,记录字符最终出现的位置
        memset(last,-1,sizeof(int)*256);//初始化为-1
        int idx = -1;//用来当前字串开始的位置
        int max = 0;
        for(int i = 0; i < s.length() ; ++i)
        {
            if(last[s[i]]>idx)  //①如果这个字符出现过,则令idx等于上一次出现该字符的位置序号
            {
                idx = last[s[i]];
            } 

            if(i -idx > max){  ②//记录最大的无重复字串
                max = i-idx;
            }

            last[s[i]] = i;③
        }
        return max;
    }
};

以abca为例:

i=0:查表last[‘a’] = -1,①不执行 idx=-1, ②执行max =1,更新last表 last[‘a’] = 0;

i=1:查表last[‘b’] = -1,①不执行 idx=-1, ②执行max =2,更新last表 last[‘b’] = 1;

i=2:查表last[‘c’] = -1,①不执行 idx=-1, ②执行max =3,更新last表 last[‘c’] = 2;

i=3:查表last[‘a’] = 0,①执行,idx=0 , ②执行max =3,更新last表 last[‘a’] = 3;

…..

上述算法的时间复杂度为O(n),Accepted!

【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters的更多相关文章

  1. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

  2. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  3. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  4. [LeetCode][Python]Longest Substring Without Repeating Characters

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  5. LeetCode之Longest Substring Without Repeating Characters

    [题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...

  6. Leetcode 3. Longest Substring Without Repeating Characters (Medium)

    Description Given a string, find the length of the longest substring without repeating characters. E ...

  7. [Leetcode Week1]Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...

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

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

  9. LeetCode[3] Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...

  10. 【leetcode】Longest Substring Without Repeating Characters

    题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

随机推荐

  1. Picasso 完美兼容 OkHttp3.3,缓存优化两不误

    Tamic 专注移动开发!更多文章请关注http://www.jianshu.com/p/6241950f9daf csdn: http://blog.csdn.net/sk719887916/art ...

  2. Apache shiro集群实现 (八) web集群时session同步的3种方法

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  3. Leetcode解题-链表(2.2.0)基础类

    1 基类的作用 在开始练习LeetCode链表部分的习题之前,首先创建好一个Solution基类,其作用就是: Ø  规定好每个子Solution都要实现纯虚函数test做测试: Ø  提供了List ...

  4. [django] 利用多线程增加异步任务

    看到django异步大家的反应应该是celery这种消息队列组件,现在用的最多的最推荐的也是这种方式.然而我这需求就是请求来了,执行一个小程序,但是又不能确定这个小程序啥时候执行完,响应又要及时,丢给 ...

  5. 在windows和Linux上安装ImageMagick与jmagick,Maven配置、Java图片压缩代码(整理网上、结合自己情况、编写出来的新安装方式)

    安装过程(如图所示) .Exceptionin thread "main" java.lang.UnsatisfiedLinkError:C:\WINDOWS\system32\j ...

  6. Swift变量名的一种玩法

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 是的,Swift的变量名可以用任何合法的Unicode字符,这 ...

  7. 控制成本,控制成本知识点,挣值和实际成本、EAC,ETC.TCPI解析表

  8. SQL 2012 Always On 为 MSCRMSqlClrLogin SQL 登录名创建非对称密钥时报语法错误

    根据实施手册中的下图的SQL在为MSCRMSqlClrLogin 创建非对称秘钥时报语法错误,具体的错误在就path那,调了多次还是报错,索性就把SQL拆开执行. 先执行这条 SELECT * FRO ...

  9. python安装MySQLdb:在windows下或linux下(以及eclipse中pydev使用msqldb的配置方法)

    写的非常好,可以解决问题: windows下:http://blog.csdn.net/wklken/article/details/7253245 linux下:http://blog.csdn.n ...

  10. python读写word、excel、csv、json文件

    http://blog.csdn.net/pipisorry/article/details/50368044 python读写word文档 (include wps)将word文档转换成txt文档 ...