LeetCode : Given a string, find the length of the longest serial substring without repeating characters.
Given a string, find the length of the longest serial substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
题目解析:意思就是找出字符串的连续递增 or 递减子串,返回该串及其长度
思路:
1 用另一个数组存储每一个char的“状态”;
2 正数表示递增,0表示与前一个char相同不增不减,负数表示递减;
3 如+3表示该char是第4个递增字符(从0开始);
4 如字符串"dkggashgt"对应的状态数组如下:
0,1,-1,0,-1,1,-1,-2,1
记录最大 or 最小值,即为最长递增 or 最长递减串长度,对应数组下标亦为最长串最后char的数组下标;
代码是js写的,如下:
var lengthOfLongestSubstring = function(str) {
if(str.length === 0) return 0;
var maxLen = 1; //maximum serial string length
var maxIdx = 0; //the array sub-index of the last char in the result string
var tmpArr = [0]; //array to save the status data
for (var i = 1, len = str.length; i < len; i++) {
var pa = str[i-1];
var pb = str[i];
var ra = tmpArr[i-1];
if(pa>pb){
if(ra<0){
tmpArr.push(ra-1);
if(-1*tmpArr[i]+1 > maxLen){
maxLen = -1*tmpArr[i]+1;
maxIdx = i;
}
}else{
tmpArr.push(-1);
if(maxLen<2){
maxLen = 2;
maxIdx = i;
}
}
}else if(pa<pb){
if(ra>0){
tmpArr.push(ra+1);
if(tmpArr[i]+1 > maxLen){
maxLen = tmpArr[i] + 1;
maxIdx = i;
}
}else{
tmpArr.push(1);
if(maxLen<2){
maxLen = 2;
maxIdx = i;
}
}
}else{
tmpArr.push(0);
}
}
var strRet = str.slice(maxIdx-maxLen+1, maxIdx+1);//result string
return [strRet,maxLen]; //result string and its length
};
LeetCode : Given a string, find the length of the longest serial substring without repeating characters.的更多相关文章
- Leetcode 3. Longest Substring Without Repeating Characters(string 用法 水题)
3. Longest Substring Without Repeating Characters Medium Given a string, find the length of the long ...
- [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(最长不重复子序列)
题目来源: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 ...
- Leetcode经典试题:Longest Substring Without Repeating Characters解析
题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
随机推荐
- 一个封装了的选项卡效果js
转载自:http://www.cnblogs.com/skyblue/archive/2008/04/26/1171968.html <!DOCTYPE HTML PUBLIC "-/ ...
- 【21.21%】【codeforces round 382D】Taxes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Less小总结
= 导航 顶部 变量 混合 继承 函数 顶部 变量 混合 继承 函数 Less 是一个Css 预编译器,意思指的是它可以扩展Css语言,添加功能如允许变量(variables),混合(mi ...
- windows远程函数不受支持
输入 regedit打开注册表编辑器, 找目录\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System ...
- Project Euler:Problem 39 Integer right triangles
If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exact ...
- hdu2083 简易版之最短距离
点A和点B之间随意一点到A的距离+到B的距离=|AB|,而AB外的一点到A的距离+到B的距离>|AB|: #include<math.h> #include<stdio.h&g ...
- Google CFO 辞职信
Google CFO 辞职信 After nearly 7 years as CFO, I will be retiring from Google to spend more time with ...
- Nginx 设置cors跨域
在我们的开发中,经常遇到跨域,这个时候,可以通过cors来解决. 解决的方法可以在服务端的代码层或者在web服务器进行设置 在web服务器上进行设置cors 跨域,这样就不必改动代码.以nginx为例 ...
- WPF特效-实现3D足球效果
原文:WPF特效-实现3D足球效果 WPF 实现 3D足球效果,效果图如下: 每个面加载不同贴图. <UserControl x:Class="MediaBalll.Model3Ds ...
- WPF Layout 系统概述——Arrange
原文:WPF Layout 系统概述--Arrange Arrange过程概述 普通基类属性对Arrange过程的影响 我们知道Measure过程是在确定DesiredSize的大小,以便Arrang ...