3_Longest Substring Without Repeating Characters -- LeetCode
C++解法一:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int m[] = {},left = , res = ;
for(int index = ; index < s.size(); ++index)
{
if(m[s[index]] == || m[s[index]] < left)
{
res = max(res,index-left+);
}else{
left = m[s[index]];
}
m[s[index]] = index+;
}
return res;
}
};
算法解读:
1) 这里使用哈希的思想,把256个可能的字符都考虑在内,字符的ASCⅡ编码作为数组下标进行映射。如m[a]等价于m[97]。
2) res其实是result的缩写,表示返回的最大无重复子串的长度,left表示当前正在判断的子串的起始位置。
3) 进行一个for循环,当满足(m[s[i]] == 0 || m[s[i]] < left)条件时候,计算子串长度,并且与res比较最长并取之。
这里的m[s[i]]表示第i个字符上一次出现的位置,如果从来没有出现过即m[s[i]]==0, 则表示无重复;如果m[s[i]] < left
则表示上次出现的位置在当前判断的字串起始位置的左边,不重复。
否则,(m[s[i]] != 0 && m[s[i]] >= left),重复。此时要更新left的值为m[s[i]],即不left更新为当前字符与上一次重复
的字符位置的下一位 。这里的i+1 是因为i是从0开始的下标,而我们需要的是从1开始的序号。
4) i偏移都每一个字符都要几下当前的位置,即m[s[i]] = i + 1 。
C++解法二
复制代码
class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> m(, -);
int res = , left = -;
for (int i = ; i < s.size(); ++i) {
left = max(left, m[s[i]]);
m[s[i]] = i;
res = max(res, i - left);
}
return res;
}
};
算法分析:
1)与算法一思想一样,只是进行了精简。
2)每次循环,将left更新 max(left, m[s[i]]),如果s[i]在left的右边且
3_Longest Substring Without Repeating Characters -- LeetCode的更多相关文章
- LeetCode 3_Longest Substring Without Repeating Characters
LeetCode 3_Longest Substring Without Repeating Characters 题目描写叙述: Given a string, find the length of ...
- 3-Longest Substring Without Repeating Characters @LeetCode
3-Longest Substring Without Repeating Characters @LeetCode 题目 题目中得到的信息有: 一段字符串找出不重复子串的最大长度,只需要长度信息. ...
- LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium
题目: Given a . For . 解题思路: 这个题让找一个字符串中具有不重复单词的最长子串的长度,如:ababc,子串为abc,长度为3.有这么几个方法: 方法一: 依赖字符串本身的一些特有函 ...
- 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 ...
随机推荐
- python异步加协程获取比特币市场信息
目标 选取几个比特币交易量大的几个交易平台,查看对应的API,获取该市场下货币对的ticker和depth信息.我们从网站上选取4个交易平台:bitfinex.okex.binance.gdax.对应 ...
- Prime - 程序员的修养
求质数算法的N种境界 求质数算法的N种境界[1] - 试除法和初级筛法 过程 尽管题目并没有要我们写一个最优的算法,但是身为一个程序员,优化应该是一种习惯,在编程的过程中,随着思考进行优化. 如果你只 ...
- SQL Server比较2table字段的差异
由于项目前后用了2个数据库,需要统计数据库结构的变化,需要统计每个表的变化,由于人工核对挺浪费时间,就写了一点代码: 1.统计表的字段数量(查询表有多少列): select count(name) ...
- CSS中的选择器之html选择器和伪类选择器
1.html选择器(标签选择器) 基本语法: html标签名称{ 属性名:属性值; 属性名:属性值; } 继续在上面的代码中做修改,实例代码: <!DOCTYPE html> <ht ...
- 2.Ray-消息发布器与消息存储器
消息发布器: Ray是基于Event Sourcing设计的ES/Actor框架,ESGrain状态(State)的修改.ESGrain之间的通信默认使用RabbitMQ通信.消息的发布器主要是Rab ...
- 使用jvisualvm来远程观察Java程序及jvisualvm插件[转]
jvisualvm是JDK自带的监控JVM运行状况的工具,利用jvisualvm可以查看JVM的运行.GC情况:线程的运行情况:内存中类及实例的情况等. 使用jvisualvm来远程观察Java程序, ...
- JDBC学习笔记(四)
减少各个Dao类间的重复代码,有以下几种方式: 写一个DBConnectionManager,将公共的查询逻辑做成方法,将sql语句作为参数传递给方法. public class DBConnecti ...
- 菜鸟的it之路-起航
之前在知乎上看见怎么学习数据结构下一位答主的回答,他引用了N.Wirth(沃斯)的话:程序=数据结构+算法.(哈,菜鸟无法验证这句话的正确性有多大)但毫无疑问的是,数据结构应当是一名菜鸟程序狗要重点学 ...
- 重写equals()和hashCode()
什么时候需要重写equals()? 只有当一个实例等于它本身的时候,equals()才会返回true值.通俗地说,此时比较的是两个引用是否指向内存中的同一个对象,也可以称做是否实例相 等.而我们在使用 ...
- OpenVPN的那些坑
遇到的情形 最近遇到一种情况,当需要同时使用到多个VPN连接时,默认的openVPN连接是不支持的,但是可以通过手动配置虚拟网络适配器进行相关的设置. 具体解决方法 基本思路是:在本地的网络连接中添加 ...