3-Longest Substring Without Repeating Characters @LeetCode
3-Longest Substring Without Repeating Characters @LeetCode
题目
题目中得到的信息有:
一段字符串找出不重复子串的最大长度,只需要长度信息。
思路
肯定是需要将字符串遍历一遍,在遍历过程中就需要查找前面字符串是否出现该字符,因此这是该算法的重点。若没找到,长度加一,若找到了,长度会从前面该字符位置+1处开始算起。下面以图来说明:
假如我们以begin为子串的开始,current表示当前处理的位置。1)当前位置的字符没有出现在[begin,current)的区间内,说明将该值加入到区间内满足没有重复条件,长度加一;
2)若当前值已经在该区间内,加入后肯定会出现重复,则应该将begin移动到没有该值的最左边位置,图中 new begin位置满足该条件。
在移动begin之前首先需要判断[begin, current)是否是现有的最大长度,若是则更新,然后将begin移动到该新位置。
还有一个关键点,就是如何查找当前字符是否出现在[begin, current)区间里,若在从该区间遍历一遍肯定费时间,有没有办法可以一次就能判断出来呢?
对了,可以采用hash的思想,将每个值在字符串的位置放入到以该值为下标的数组中,若数组中保存有值,并且是在[begin,current)范围内,则说明存在。
C算法
int lengthOfLongestSubstring(char* s) {
int tmp[128]; //用于保存字符的下标
int begin = 0, max = 0; //begin和最大长度默认为0
int i, index;
for (i = 0; s[i] != '\0'; i++) { //遍历字符串
if (tmp[s[i]] != 0) { //判断该值是否出现在前面的字符中
index = tmp[s[i]] - 1; //取保存的上一个该值的位置
if (index >= begin) { //判断是否在[begin, current)
if (i - begin > max) //对比是否最大
max = i - begin;
begin = index + 1;//begin新的位置,在前一个该字符的后面,即new begin位置
}
}
tmp[s[i]] = i + 1; //保存该值的最新下标
}
return i - begin > max ? i - begin : max;//最后还需要判断下
}
结果
3-Longest Substring Without Repeating Characters @LeetCode的更多相关文章
- Longest Substring Without Repeating Characters leetcode java
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- Longest Substring Without Repeating Characters ---- LeetCode 003
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Longest Substring Without Repeating Characters -- LeetCode
原题链接: http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/ 这道题用的方法是在LeetC ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- leetcode: longest substring without repeating characters
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- [LeetCode][Python]Longest Substring Without Repeating Characters
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters
一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...
随机推荐
- open还是codecs.open区别
>>> fr = open('test.txt','a')>>> line1 = "我是一道光">>> fr.write(li ...
- 提升算法——Adaboost
思路:通过改变训练样本权重,学习多个分类器,并将这些分类器进行线性组合,提高分类器性能.大多数提升方法都是改变训练数据的概率分布(数据的权值) 强可学习:存在一个多项式的学习算法能够学习他,并且正确率 ...
- 可编程逻辑控制器(PLC)漏洞挖掘思路与验证
mailto wangkai0351@gmail.com 随时记录千奇百怪的漏洞挖掘思路,主要针对STEP7 v5+西门子S7-300/400系列PLC,欢迎同行前来交流. 组态信息下载完整性攻击 思 ...
- Problem 1: Multiples of 3 and 5
小白一枚,python解法,共同学习,一起进步. Problem 1: Multiples of 3 and 5 If we list all the natural numbers below 10 ...
- Problem E: 平面上的点和线——Point类、Line类 (V)
Description 在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定,两点确定一条线段.现在我们封装一个“Point类”和“Line类”来实现平面上的点的操作. 根据“append ...
- bottle模板中的替换
line是模板中一行的内容,类似: {{x}}testinfo{{x+10}} x=10时,模板输出: 10testinfo20 x = 10 splits = re.split(r'\{\{(.*? ...
- Java编程思想 - 第11章 持有对象
· 大量笔记存放在Github Java文件中,请移步查看:https://github.com/iGuure/AndroidCodeHub/tree/master/Java%20pratice/Th ...
- 2018-计算机系机试-A
#include<stdio.h> #include<cstdio> #include<cmath> #include<cstring> #includ ...
- 2018-计算机系机试(第二批)-B-二进制输出
B. 二进制输出 单点时限: 1.0 sec 内存限制: 256 MB 输入一个十进制表示的非负整数,输出其 8 位二进制表示. 例如:输入 10 ,输出 00001010. 输入格式 一行一个非负整 ...
- GLog 初始化说明
#include <iostream> #include <glog/logging.h> int main(int argc, char* argv[]) { google: ...