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.的更多相关文章

  1. Leetcode 3. Longest Substring Without Repeating Characters(string 用法 水题)

    3. Longest Substring Without Repeating Characters Medium Given a string, find the length of the long ...

  2. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  3. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  4. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  5. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  6. [LeetCode][Python]Longest Substring Without Repeating Characters

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  7. 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters

    一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...

  8. Leetcode经典试题:Longest Substring Without Repeating Characters解析

    题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...

  9. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

随机推荐

  1. sublime 内容高级搜索

    在发展中经常需要搜索内表面的文件.更好地想找到$video->getTitle() 在该文件中使用的表面.好了,这个时候就需要使用高级搜索功能,的操作,如以下: ctrl+shif+f纽带 例如 ...

  2. React为啥很多类里的标签上事件处理函数要用bind(this)

    render() { return ( <div> <p onClick={this.clickHandler.bind(this)}>vz</p> </di ...

  3. 【21.28%】【codeforces 707D】Persistent Bookcase

    time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...

  4. Java--面试通关要点

    这里,笔者结合自己过往的面试经验,整理了一些核心的知识清单,帮助读者更好地回顾与复习 Java 服务端核心技术.本文会以引出问题为主,后面有时间的话,笔者陆续会抽些重要的知识点进行详细的剖析与解答. ...

  5. Matlab Tricks(十五) —— 圆的正确画法

    使用参数方程, phi = 0:0.01:2*pi; x = cos(phi); y = sin(phi); axis equal plot(x, y) 根据参数方程,显然,圆心在 (0, 0),半径 ...

  6. 嵌入式OS入门注意事项-同RTX案件:九.关于优先-翻转,继承和天花板

    嵌入式OS入门注意事项-同RTX案件:九.关于优先-翻转,继承和天花板 涉及当调度优先级,会有很多问题,本文中,优先调度和一些战略的主要问题,以应付. 有几个概念如下:(priority invers ...

  7. 图形的认识(curve,surface,hypersurface)

    平滑函数(smooth function): curve:曲线: 二维平面: surface:曲面: 三维空间: hypersurface:超曲面: 更高维度: 1. surface 是对平面的泛化, ...

  8. Matlab随笔之画图函数总结

    原文:Matlab随笔之画图函数总结 MATLAB函数画图 MATLAB不但擅长於矩阵相关的数值运算,也适合用在各种科学目视表示(Scientific visualization).本节将介绍MATL ...

  9. 食谱API自由和开放接口-为了发展自己的健康厨房APP应用

    什么时候健康厨房 (cook.yi18.net)上线后,基于接口的须要,就非常快就完毕的食谱API接口的开发 文档地址:http://doc.yi18.net/cookapidoc 菜谱食谱API , ...

  10. 读BeautifulSoup官方文档之html树的修改

    修改html树无非是对其中标签的改动, 改动标签的名字(也就是类型), 属性和标签里的内容... 先讲这边提供了很方便的方法来对其进行改动... soup = BeautifulSoup('<b ...