Leetcode 详解(Substing without repeats character)
Given a string, find the length of the longest 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.
我自己的代码:
public class Solution {
public int lengthOfLongestSubstring(String s) {
int slenmax=0;
for(int i=0;i<s.length();i++)
{
int j=i+1;
boolean flag=true;
while(j<s.length()&&flag)
{
for(int k=i;k<j;k++)
{ if(s.charAt(j)==s.charAt(k))
{
flag=false;
j--;
}
}
j++;
}
int slen=j-i;
if(slen>slenmax)
slenmax=slen;
}
return slenmax;
}
}
运行时可以通过,但是在提交时出现超时(思路比较简单,因而时间复杂度相应会比较高)
clean Code:
public class Solution {
public int lengthOfLongestSubstring(String s) {
boolean[] exist = new boolean[256]; //用表格处理,查找时会很快
int i = 0, maxLen = 0;
for (int j = 0; j < s.length(); j++) {
while (exist[s.charAt(j)]) { //这个while是精髓
exist[s.charAt(i)] = false;
i++;
}
exist[s.charAt(j)] = true;
maxLen = Math.max(j - i + 1, maxLen);
}
return maxLen;
}
}
注:借用了表的形式,把字符串中的字母依次存入表中并进行相应标记(如 exist[s.charAt(i)] = false;)。解决思路是:设定两个指针(i, j),都放在左头开始,j从左到右,遇到有两个相同字母的情况,j停止,计算两相同字母间距,更新最大间距,并且把i 也逐步移到第一个相同字母的下个位置,j再继续移动。
这样时间复杂度为O(n+n)=O(2n);
clean code 2:
public int lengthOfLongestSubstring(String s) {
int[] charMap = new int[256];
Arrays.fill(charMap, -1);
int i = 0, maxLen = 0;
for (int j = 0; j < s.length(); j++) {
if (charMap[s.charAt(j)] >= i) {
i = charMap[s.charAt(j)] + 1;
}
charMap[s.charAt(j)] = j;
maxLen = Math.max(j - i + 1, maxLen);
}
return maxLen;
}
注:用整型表代替布尔型表,时间复杂度降到O(n),原因是在遇到有两个相同字母时没有上面中的 i 逐步移到第一个相同字母下一位置的过程,此处一步到位
Leetcode 详解(Substing without repeats character)的更多相关文章
- Leetcode 详解(ReverseWords)
Leetcode里面关于字符串的一些问题,描述如下: Given an input string, reverse the string word by word. For example,Given ...
- 由Leetcode详解算法 之 动态规划(DP)
因为最近一段时间接触了一些Leetcode上的题目,发现许多题目的解题思路相似,从中其实可以了解某类算法的一些应用场景. 这个随笔系列就是我尝试的分析总结,希望也能给大家一些启发. 动态规划的基本概念 ...
- Leetcode 详解(Valid Number)
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- Leetcode 详解(valid plindrome)
Question: Given a string, determine if it is a palindrome, considering only alphanumeric characters ...
- Leetcode 详解(股票交易日)(动态规划DP)
问题描述: 在股市的交易日中,假设最多可进行两次买卖(即买和卖的次数均小于等于2),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行).给出一天中的股票变化序列,请写一个程序计算一天可以获得 ...
- Leetcode详解Maximum Sum Subarray
Question: Find the contiguous subarray within an array (containing at least one number) that has the ...
- Leetcode 详解(Implement strstr)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- The Skyline Problem leetcode 详解
class Solution { public: vector<pair<int, int>> getSkyline(vector<vector<int>&g ...
- LeetCode(42.接雨水)多解法详解
接雨水解法详解: 题目: 基本思路:从图上可以看出要想接住雨水,必须是凹字形的,也就是当前位置的左右两边必须存在高度大于它的地方,所以我们要想知道当前位置最多能存储多少水,只需找到左边最高处max_l ...
随机推荐
- cpu主频信息
yangkunvanpersie ( yangkunvanpersie@163.com ) 通过"有道云笔记"邀请您查看以下笔记 修改CPU频率.note 打开笔记 kerne ...
- ubuntu安装
今天在win10下安装Ubuntu,结果没经验导致win10找不回来了,我再好好整理些思路 安装前要做一个ghost,万一出现问题可以用来恢复系统! 1,我使用USB Installer 在http: ...
- 移动端web开发基础概念
最近在了解移动端web开发的相关知识,有些概念总是模糊不清,这次花费了一些时间整体的梳理了一遍. 分辨率 分辨率可以从显示分辨率与图像分辨率两个方向来分类.显示分辨率(屏幕分辨率)是屏幕图像的精密度, ...
- Spring事务属性的介绍
Spring声明式事务让我们从复杂的事务处理中得到解脱.使得我们再也无需要去处理获得连接.关闭连接.事务提交和回滚等这些操作.再也无需要我们在与事务相关的方法中处理大量的try-catch-final ...
- Linux英文全称
su:Swith user 切换用户,切换到root用户cat: Concatenate 串联uname: Unix name 系统名称df: Disk free 空余硬盘du: Disk u ...
- HTML <a> download 属性,点击链接来下载图片
Html5里面的 标签的 Download 属性可以设置一个值来规定下载文件的名称.所允许的值没有限制,浏览器将自动检测正确的文件扩展名并添加到文件 (.img, .pdf, .txt, .html, ...
- EasyUI的combobox控件使用onchange 问题
在项目中几次都遇到了同样的问题,现在都不知道怎样解决了! 路过的朋友们帮我看看嘛!谢谢了! 最后我想要实现的效果是这样的. 在下拉列表中不存在值.(这里的是下拉列表中存在值的!) 但是在我输入值 ...
- poll()函数的使用
分类: LINUX poll函数用于监测多个等待事件,若事件未发生,进程睡眠,放弃CPU控制权,若监测的任何一个事件发生,poll将唤醒睡眠的进程,并判断是什么等待事件发生,执行相应的操作.poll函 ...
- Android N做了啥
Android N做了哪些改变 一. 性能改善 Doze超级省电模式 手机在关屏同时没有充电的情况,在一段时间后会进入打盹状态,第一阶段会停掉同步.作业.网络等访问,第二阶段会停掉app的位置服 ...
- Python正则式的基本用法
Python正则式的基本用法 1.1基本规则 1.2重复 1.2.1最小匹配与精确匹配 1.3前向界定与后向界定 1.4组的基本知识 2.re模块的基本函数 2.1使用compile加速 2.2 ma ...