Leetcode第三题《Longest Substring Without Repeating Characters》
题目:
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.
意思就是:给出一个字符串,找出不包含重复字母的最长字串,输出字符串的长度。
之前我一直是暴力破解,最近看到一个有意思的思路,记录下来。
先上代码:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> dict(, -);//ASCCI hash
int maxLen = , start = -;
for(int i = ; i < s.size(); ++i)
{
if(dict[s[i]] > start)
{
start = dict[s[i]];
}
dict[s[i]] = i;
maxLen = max(maxLen, i-start);
}
return maxLen;
}
int max(int a, int b)
{
return a > b ? a : b;
}
};
看到这么短的代码,我也被其优雅所折服,我们来看下思路:
* 对字符串进行一次遍历,用一个大小为256(ascii最多有256种字符)数组记录每个字母最后一次的下标。
* 如果当前字符在之前出现过就覆盖原来的数组元素,并且更新start值。
* 每次循环时检查当前下标i-开始下标start和一个记录当前最大串长度的max变量的关系,将max保持或更新。
* 需要注意的是,我更新start和max是在检测到重复字母时进行的,而最后一个字符不一定和前边重复,所以循环外要附加一次更新max的操作。
Leetcode第三题《Longest Substring Without Repeating Characters》的更多相关文章
- leetcode第三题--Longest Substring Without Repeating Characters
Problem:Given a string, find the length of the longest substring without repeating characters. For e ...
- leetcode第三题Longest Substring Without Repeating Characters java
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- LeetCode第三题—— Longest Substring Without Repeating Characters(最长无重复子字符串)
题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...
- LeetCode 第 3 题(Longest Substring Without Repeating Characters)
LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...
- 刷题之路第三题--Longest Substring Without Repeating Characters
问题简介:求给定字符串中最长的字符不重复的字符串的长度 问题详解: 给定一个字符串,寻找给定字符串中包含的最长的字符不重复的字符串的长度 注:答案必须是子字符串,不是子序列 是连续的字符不重复的字符串 ...
- 【LeetCode】3 、Longest Substring Without Repeating Characters
题目等级:Medium 题目描述: Given a string, find the length of the longest substring without repeating chara ...
- Leetcode经典试题:Longest Substring Without Repeating Characters解析
题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...
- LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- LeetCode(3)Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- LeetCode Hash Table 3. Longest Substring Without Repeating Characters
HashMap的应用可以提高查找的速度,键key,值value的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...
随机推荐
- seo是什么
SEO(Search Engine Optimization):汉译为搜索引擎优化.搜索引擎优化是一种利用搜索引擎的搜索规则来提高目前网站在有关搜索引擎内的自然排名的方式. SEO的目的是:为网站提供 ...
- angular2-cli 环境搭建
开发工具:windows ,Vscode, npm, 前提:安装nodejs nodejs 模块全局安装路径配置:http://www.cnblogs.com/rancho-blog/p/656792 ...
- nginx 默认配置语法和日志的format
nginx 默认配置 查看有nginx哪些默认配置文件,打开/etc/nginx/nginx.conf文件,查看尾行部分 会默认将/etc/nginx/conf.d/文件下其他以.conf结尾的配置文 ...
- unittest管理测试用例
#coding=utf-8 from selenium import webdriver from time import sleep import unittest #导入unittest库 imp ...
- MySQL增删查改语句(入门)
目录 create alter: insert delete update select 数据库定义语句: create:创建数据库及表对象 drop:删除数据库及表对象 alter:修改数据库及表对 ...
- GitLab初识以及代码迁移
目录 一.理论概述 1.什么是gitlib 2.GitLab服务构成 3.Git对比SVN 二.部署 1.简单操作GitLab 三.项目实践:SVN代码迁移至GitLab 环境 1.Linux下部署S ...
- SQL SERVER-JOB搬迁脚本
选中JOB,按F7打开对象游览器: 选中相应的JOB,生成脚本. 搬迁JOB,新实例上要有相应的DB和操作员. 脚本中有2个@enabled,一个是job enable,一个是schedule是否生效 ...
- RMAN恢复数据文件
实验之前先备份数据库 RMAN>backup database; 在操作系统中删除数据文件 5 SQL> startup ORACLE 例程已经启动. Total System Globa ...
- Markdown编辑器的使用测试
北京高校(大标题) 北京电子科技学院(小标题) 计算机技术(一级标) 研究生(二级标) 列表(三级标) 姓名 - 扎西 - 卓玛 学号 - 01 - 02 超链接 [QQ邮箱](https://www ...
- flask 杂记3
SQLAlchemy在模型之间建立关系模式: https://www.bbsmax.com/A/mo5k7gKn5w/ 一对多时:外键只能存储单一数据(标量),所以外键总是在“多”这一侧定义,多篇文 ...