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、JVM、JRE、JDK等组件的理解

    .java ⇒(javac) .classs ⇒ (类加载器)转换后的 .class 文件 ⇒ (解释器)可执行代码 ⇒ (JIT 编译器)⇒ 机器码 0. 虚拟机 Java 有它的虚拟机:Java ...

  2. AutoHotKey 的使用 —— 使用键盘调节 windows 声音

    AutoHotKey 下载地址 AutoHotkey Downloads 首先进行 AutoHotKey 的安装 编写如下 .ahk 文件(F10:打开关闭声音,F11:增加声音,F12:减少声音,当 ...

  3. hadoop编程技巧(8)---Unit Testing (单元测试)

    所需的环境: Hadoop相关jar包裹(下载版本的官方网站上可以): 下载junit包裹(新以及). 下载mockito包裹: 下载mrunit包裹: 下载powermock-mockito包裹: ...

  4. 使用dom4j来处理xml的一些常用方法

    要使用dom4j读写XML文档,需要先下载dom4j包,dom4j官方网站在 http://www.dom4j.org/ 解开后有两个包,仅操作XML文档的话把dom4j-1.6.1.jar加入工程就 ...

  5. win7 UAC bypass(微软已经给予了三组组件绕过UAC启动的特权)

    fireworm同学的翻译: 原文在http://www.pretentiousname.com/misc/win7_uac_whitelist2.html我只翻译了其中关于原理的一小部分,有兴趣的可 ...

  6. WPF中 PropertyPath XAML 语法

    原文:WPF中 PropertyPath XAML 语法 PropertyPath 对象支持复杂的内联XAML语法用来设置各种各样的属性,这些属性把PropertyPath类型作为它们的值.这篇文章讨 ...

  7. 堆(stack) 之 c 和 c++模板实现(空类默认成员函数 初谈引用 内联函数)

    //stack 的基本操作 #include <iostream> using namespace std; const int maxn = 3; typedef struct Stac ...

  8. 简明Python3教程 19.附录 FLOSS

    FLOSS Free/Libre and Open Source Software, in short, FLOSS is based on the concept of a community, w ...

  9. 在WPF设计工具Blend2中制作立方体图片效果

    原文:在WPF设计工具Blend2中制作立方体图片效果 ------------------------------------------------------------------------ ...

  10. [Songqw.Net 基础]WPF插件化中同步Style

    原文:[Songqw.Net 基础]WPF插件化中同步Style 版权声明:本文为博主原创文章,未经博主允许可以随意转载 https://blog.csdn.net/songqingwei1988/a ...