LeetCode03 最长无重复子串
题目
给定一个字符串,找出不含有重复字符的最长子串的长度。
解答
刚开始以为只是一遍遍历后来的字符和前面一样便开始算新子串,给的案例都过了,但是卡在了“dvdf”
后来经过重重试验,暴力循环,不断调整变量作用域,在经过
“ohvhjdml”
“bbbbb”
“abcabcbb”
“pwwkew”
“ppkw”
“ak”
这几个字符串的测试下,暴力法终于通过了,但是用时较长
后来又尝试了哈希表缩边界的方法,相对简洁快速
var lengthOfLongestSubstring = function(s) {
var hash= new hashTable();
let maxl=0;
for(let i=0,j=0; j<s.length;j++)
{
hash.containsKey(s[j])&& ( i = Math.max(hash.getValue(s[j]),i)); //重复子串,缩小左边界
maxl = Math.max(maxl,j-i+1);
hash.add(s[j],j+1);
}
return maxl;
};
心得:
- 在网上查资料的时候发现比较优雅的条件判断,把没有else的if判断改成与或非判断的表达式,简洁优雅还省时间;
- 中间卡的时间比较长的就是“dvdf”和“aab”,在用暴力法解答的时候前者我没有考虑“vdf"的情况,字符串被分隔成了“dv”,“df”,后来又加在字符是字符串唯一的时候的条件判断,后者是判断条件不全字符串被分割成了“a”,“a”,“b”,加了第二个字母等于第一个字母时候的特殊条件判断,暴力法就稳了
- 换哈希表的时候参考官方的优化边界方法,后来发现不太理解的地方是最后的j+1,试过把j+1和上面的j-i+1的+1删掉。别的测试都没事,但是无重复的子串会少算一个。目前理解是多少个树和中间的坑的问题
LeetCode03 最长无重复子串的更多相关文章
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 最长无重复子串问题 leetcode 3
一.代码及注释 class Solution { public: int lengthOfLongestSubstring(string s) { int n = s.size(); //字符串的长度 ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- [LeetCode] Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- leetcode 3 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 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- lintcode: 最长无重复字符的子串
题目 最长无重复字符的子串给定一个字符串,请找出其中无重复字符的最长子字符串. 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc",其长度为 ...
随机推荐
- Linux下安装scikit-learn
Linux下安装scikit-learn sudo apt-get install python-pip sudo apt-get install python-pandas python-sympy ...
- raise error
raise TypeError("参数q_type 错误 ")
- cocos2dx在win10系统上的VS2017运行时报错:丢失MSVCR110.dll
如题,运行环境为cocos2dx 3.14.1,win10系统,VS2017. 编译cocos2dx的cocos2d-x-3.14.1/build/cocos2d-Win32.sln已通过,不过运行时 ...
- 【Python学习】iterator 迭代器小练习
http://anandology.com/python-practice-book/iterators.html Problem 1: Write an iterator class revers ...
- Python 写了个小程序,耗时一天,结果才100多行
from selenium import webdriver import selenium.webdriver.support.ui as ui from selenium.webdriver.co ...
- OpenStack-Neutron-Fwaas-代码【二】
上一节从代码层面来讲解了fwaas的流程,这里通过具体查看iptables规则来说下应用规则的流程: 1.首先通过命令获取当前路由中的规则 #ip netns exec qrouter-[router ...
- asp.net的<% %>特定用法
在asp.net应用程序中,在asp.net页面常用的<%@ %>.<%# %>.<%= %>.在全球化的项目中使用<%$ %>绑定资源项目,在asp. ...
- Asp.Net Core WebApi 和Asp.Net WebApi上传文件
public class UpLoadController : ControllerBase { private readonly IHostingEnvironment _hostingEnviro ...
- Qt3D Shader
--------------------------------------------------- Qt3D ShaderPrograme Qt3D GLSL 渲染器 Shader示例可参考: h ...
- 浏览器对象模型BOM总结
BOMwindows对象document对象location对象screen对象 Windows对象 1.窗口操作 移动指定的距离:window.moveBy(10,20); //向右移动10像素,向 ...