LeetCode题解——Longest Substring Without Repeating Characters
题目:
给定一个字符串,返回其中不包含重复字符的最长子串长度。
解法:
维持两个指针,第一个指向子串开始,第二个负责遍历,当遍历到的字符出现在子串内时,应计算当前子串长度,并更新最长值;然后第一个指针更新为出现位置的下一个。
代码:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int last_pos[]; //记录每个字符最后一次出现位置
fill(last_pos, last_pos + , -); int start = , max_len = ; //start指向当前子串第一个字符
int slen = s.size(); for(int stop = ; stop < slen; ++stop) //stop用于遍历
{
char c = s[stop]; if(last_pos[c] >= start) //stop指向的字符出现在子串内部
{
max_len = max(stop - start, max_len); //更新最长值
start = last_pos[c] + ; //更新第一个字符指针
}
last_pos[c] = stop; //更新当前字符最后出现位置
} return max(max_len, slen - start); //循环停止时,最后一个子串没有参与计算,所以在这里计算其长度并对比
}
};
LeetCode题解——Longest Substring Without Repeating Characters的更多相关文章
- [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 (O(n)算法)问题
problem: Given a string, find the length of the longest substring without repeating characters. For ...
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- [Leetcode Week1]Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...
- 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] 3. Longest Substring Without Repeating Characters 解题思路
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [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】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- hdu 3544 Alice's Game 博弈论
博弈论+二分! 后一人会尽量选前一人切小的一块切!! 代码如下: #include<iostream> #include<stdio.h> #define I1(x) scan ...
- hdu 4664 Triangulation 博弈论
看到这题时,当时还不会做,也没搞懂sg函数,于是狠狠的钻研了下博弈论,渐渐的知道了sg函数…… 现在在来做这题就很容易了,1A 打表容易发现在80左右的时候就出现循环节了 代码如下: #include ...
- poj 1870 Bee Breeding
思路:首先要建立坐标,具体作法见:http://www.cnblogs.com/xin-hua/p/3237096.html 然后将得到2坐标之差x,y:如果x,y同号,则相加,否则去最大.(要取绝对 ...
- cocos2d-x3.9 NDK android 环境搭建过程中遇到的错误
编译环境:Mac OS, NDK r9d 错误:arm-linux-androideabi-gcc: error trying to exec '/media/Project/adt-bundle-l ...
- mybatis整合redis
mybatis默认缓存是PerpetualCache,可以查看一下它的源码,发现其是Cache接口的实现:那么我们的缓存只要实现该接口即可. 编写Redis需要用的2个工具类 RedisUtil. ...
- Android Spannable
ApiDemo 源码至 com.example.android.apis.text.Link 类. 首先,看一下其运行效果: 要给 TextView 加上效果,方式主要有几种: 第一种,自动应用效果, ...
- autocapticalize和autocorrect
首字母自动大写autocapitalize 在 iOS 中,用户可以手动开启「首字母自动大写」功能,这样输入英文的时候,首字母便会自动大写.但是,有些时候并不希望一直是首字母大写的.比如用户名这个字段 ...
- Annotation【转】
1.Annotation的工作原理: JDK5.0中提供了注解的功能,允许开发者定义和使用自己的注解类型.该功能由一个定义注解类型的语法和描述一个注解声明的语法,读取注解的API,一个使用注解修饰的c ...
- pylinter could not automatically determined the path to `lint.py`
先关闭Sublime Text 1) 到官网先下载pylinter,http://www.logilab.org/project/pylint,然后解压缩,拷贝到C盘,目录为C:\pylint-1.0 ...