一天一道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. Unable to ignore resources

    摘要:分享牛,分享牛系列, Unable to ignore resources Attempted to beginRule: 异常信息处理. 出现Unable to ignore resource ...

  2. jdbc批量插入

    分享牛,分享牛原创.有这样一个需求,文本文件中的数据批量的插入mysql,怎么用jdbc方式批量插入呢? jdbc默认提供了批量插入的方法,可能用一次就忘记了,这里做笔记记录一下jdbc批量插入吧. ...

  3. CentOS6.7 下安装JDK

    第一步:从官网上下载rpm版本的jdk文件. 第二步:安装JDK 执行命令rpm    -ivh    jdk-8u73-linux-i586.rpm 至此,JDK安装完成,我们来看下JDK的安装目录 ...

  4. [tornado]websocket 最简单demo

    想法 前两天想看看django 长轮询或者是websocket的方案,发现都不太好使. tornado很适合做这个工作,于是找了些资料,参照了做了个最简单demo,以便备用. 具体的概念就不说了,to ...

  5. Hibernate缓存集成IMDG

    1 第三方缓存插件 除了Ehcache这种轻量级的缓存方案外,几乎所有IMDG产品都提供了对Hibernate二级缓存的直接支持,常用的有: Ø  Hazelcast Ø  GridGain Ø  J ...

  6. 一步步创建Qt Widget项目+TextFinder案例(摘自笔者2015年将出的《QT5权威指南》,本文为试读篇)

     创建一个基于应用的QtWidget应用程序 这个手册描述了怎样使用QtCreater创建个一个小的Qt应用程序,Text Finder.它是Qt工具Text Finder例子的简写版本.这个应用 ...

  7. Android开发小问题集

    由于安卓系统比较复杂,开发中会发中会碰见各种小问题,在此做一些记录,只要觉得有必要就会添加进来. 1.触屏鼠标模式和触屏模式 开发android4.3高通400平台时,用atmel_max 640T作 ...

  8. 最简单的基于FFmpeg的解码器-纯净版(不包含libavformat)

    ===================================================== 最简单的基于FFmpeg的视频播放器系列文章列表: 100行代码实现最简单的基于FFMPEG ...

  9. 【安卓中的缓存策略系列】安卓缓存策略之综合应用ImageLoader实现照片墙的效果

    在前面的[安卓缓存策略系列]安卓缓存之内存缓存LruCache和[安卓缓存策略系列]安卓缓存策略之磁盘缓存DiskLruCache这两篇博客中已经将安卓中的缓存策略的理论知识进行过详细讲解,还没看过这 ...

  10. Android简易实战教程--第二话《两种进度条》

    点击按钮模拟进度条下载进度,"下载"完成进度条消失. 代码如下: xml: <?xml version="1.0" encoding="utf- ...