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 ...
随机推荐
- Visual Studio 2017 and Apache Cordova mobile apps | Andrés Zsögön
原文:Visual Studio 2017 and Apache Cordova mobile apps | Andrés Zsögön 以下是使用Microsoft Visual Studio 20 ...
- 开源|LightGBM:三天内收获GitHub 1000+ 星
原创 2017-01-05 LightGBM 微软研究院AI头条 [导读]不久前微软DMTK(分布式机器学习工具包)团队在GitHub上开源了性能超越其他boosting工具的LightGBM,在三天 ...
- 外部进程嵌入到Qt进程界面(使用QWindow::fromWinId)
有时候需要做框架集成的东西,需要把其他客户端像组件一样集成到一个客户端中,类似于一个软件集成的平台客户端,统一用一个中心管理的客户端做类似于控制面板一样的东西去调用不同的软件.此时就必须相应不同的点击 ...
- VS2005 编译 Qt4.8.2库,并修正源码中的错误
从昨天开始利用VS2005对Qt4.8.2库进行编译,在编译到某个文件时,总是报错,提示VS的cl.exe和nmake.exe返回致命错误.错误罗列如下: DefaultLocalizationStr ...
- 《TIME》时代周刊阅读
0. 常识 tribunal 是解决民事诉讼和民事纠纷的地方,以调解为主,使用主席制.主席是受过司法培训,具有一定法律基础的,但不能像法官一样直接给你判决,也不能强制执行. Court 一般针对严重案 ...
- Metropolis 采样与蒙特卡洛算法
Metropolis 算法又叫 Metropolis 抽样,是模拟退火算法的基础,在早期的科学计算中蒙特卡洛方法(Monte Carlo)是对大量原子在给定温度下的平衡态的随机模拟,当蒙特卡洛算法计算 ...
- TOP计划猿10最佳实践文章
本文转自:EETproject教师专辑 http://forum.eet-cn.com/FORUM_POST_10011_1200263220_0.HTM?click_from=8800111934, ...
- HDU 4414 Finding crosses(dfs)
Problem Description The Nazca Lines are a series of ancient geoglyphs located in the Nazca Desert in ...
- C# 比较不错的通用验证码
1 using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging ...
- Angular 请求另一服务的api(请求代理)
1.edit "start" of your package.json to look below 定义一个叫做start的新命令 "start": " ...