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. JAVA自带API的压缩与解压

    Java API中的 java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作.我们可以使用该包中的方法,结合IO中的相关知识,进行文件的压缩和解压缩相关操作. ZipFile j ...

  2. delphi之IOCP学习(一)

    困扰已久的网络通信(IOCP:完成端口),今天终于揭开她的神秘面纱了,之前百度N久还是未能理解IOCP,网络上好多博文都没有贴出源码,初学者很难正在理解IOCP并自己写出通信例子 ,经过努力,今天自己 ...

  3. LaTeX —— 特殊符号与数学字体

    1. 特殊符号 ℓ(\ell):用于和大小的 I 和 数字 1 相区分 R(\Re) ∇(\nabla):微分算子 2. 数学字体 mathbb:blackboard bold,黑板粗体 mathca ...

  4. RadioButton分组的实现

    原文:RadioButton分组的实现     XAML如下 <StackPanel> <RadioButton GroupName="colorgrp"> ...

  5. WPF中利用RadialGradient模拟放大镜效果

    原文:WPF中利用RadialGradient模拟放大镜效果 --------------------------------------------------------------------- ...

  6. Blend_Effect

    原文:Blend_Effect 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010265681/article/details/76651796 ...

  7. QPainter的坐标系系统的转换

    声明:本文原创于yafeilinux的百度博客,http://hi.baidu.com/yafeilinux 转载请注明出处. 我看了这篇文章很好很容易理解.如果看了Qt助手之后更加的形象. 前面一节 ...

  8. WPF 画线动画效果实现

    原文:WPF 画线动画效果实现 弄了将近三天才搞定的,真是艰辛的实现. 看了很多博客,都太高深了,而且想要实现的功能都太强大了,结果基础部分一直实现不了,郁闷啊~ 千辛万苦终于找到了一个Demo,打开 ...

  9. Feature extraction - sklearn文本特征提取

    http://blog.csdn.net/pipisorry/article/details/41957763 文本特征提取 词袋(Bag of Words)表征 文本分析是机器学习算法的主要应用领域 ...

  10. Android新的漏洞的应用程序中的发现!

    最近,趋势科技发现一些Android中的漏洞应用程序内存.来发动攻击.我们调查了两个受影响的应用程序,大家来感受一下: .超过一千万次安装.及在下载页面拥有数十万笔用户留言的生产力应用程序(生产力应用 ...