【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters
一天一道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的更多相关文章
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
		
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
 - LeetCode 3 Longest Substring Without Repeating Characters  解题报告
		
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
 - LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
		
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
 - [LeetCode][Python]Longest Substring Without Repeating Characters
		
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
 - LeetCode之Longest Substring Without Repeating Characters
		
[题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...
 - Leetcode 3. Longest Substring Without Repeating Characters (Medium)
		
Description Given a string, find the length of the longest substring without repeating characters. E ...
 - [Leetcode Week1]Longest Substring Without Repeating Characters
		
Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...
 - [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 exam ...
 - 【leetcode】Longest Substring Without Repeating Characters
		
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
 
随机推荐
- Android TV开发总结(四)通过RecycleView构建一个TV app列表页(仿腾讯视频TV版)
			
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52854131 前言:昨晚看锤子手 ...
 - C++语言编译系统提供的内部数据类型的自动隐式转换
			
C++语言编译系统提供的内部数据类型的自动隐式转换规则如下: 程序在执行算术运算时,低类型自动隐式转换为高类型. 在函数调用时,将实参值赋给形参,系统隐式的将实参转换为形参的类型,并赋值给形参. 函数 ...
 - Afianl加载网络图片(续)
			
上一篇已经讲了如何利用Afianl加载网络图片和下载文件,这篇文章将继续讲解使用Afinal加载网络图片的使用,主要结合listview的使用: 看效果图: listview在滑动过程中没用明显卡顿, ...
 - itoo-快捷部署脚本--提高部署开发效率
			
 本次是第一次使用批处理文件来作为批量操作的工具,代替了人工的手动的复制,粘贴的方式,使用脚本实现了项目的启动.自动化部署,打开项目根目录.等等,提高了开发和调试的效率. 说明: 当前版本:1.0 ...
 - 文档发布工具mkdocs
			
mkdocs是Python的一个对 Markdown 友好的文档生成器.,小巧精美. MkDocs is a fast, simple and downright gorgeous static si ...
 - 搜索----Android Demo
			
在前面的博客中,小编简单的介绍了,点击发现按钮,自动加载热门的相关数据,成长的脚步从不停歇,完成了发现的功能,今天我们来简单看一下如何在搜索栏中输入关键字,搜索出我们所需要的信息,今天这篇博文小编就简 ...
 - java的list几种实现方式的效率(ArrayList、LinkedList、Vector、Stack),以及 java时间戳的三种获取方式比较
			
一.list简介 List列表类,顺序存储任何对象(顺序不变),可重复. List是继承于Collection的接口,不能实例化.实例化可以用: ArrayList(实现动态数组),查询快(随意访问或 ...
 - INV 调试: 如何获取库存物料事务处理调试信息
			
 1. 按如下方式设置系统配置文件值: 系统配置文件值 地点/用户/应用/职责层配置文件值 --汇总 FND: 启用调试日志 是 FND:调试日志层级 陈述 INV: 调试跟踪: 是 IN ...
 - uploadify 3.2 java应用丢失session
			
flash中有个bug就是自身创建一个session,这样就导致与web本身的session不一致 权限验证失败的问题. 原因: 因为uploadify是不会自动传送session值的,所以当ses ...
 - 基于WAMP的Crossbario 安装入门
			
简单学习和使用WAMP协议,Router 是crossbario, Client是Autobahn, 了解运作的流程. 测试环境是Centos6 虚拟机一台 目录为 /data/wamp/ ,用的是P ...