LeetCode--No.003 Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters
- Total Accepted: 167158
- Total Submissions: 735821
- Difficulty: Medium
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.
解题思路参考自: http://www.geeksforgeeks.org/length-of-the-longest-substring-without-repeating-characters/
public class Num3 {
/*
* 方法一:暴力搜索,复杂度 O(n^3)
*/
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0){
return 0 ;
}
String sub ;
for(int subLen = s.length() ; subLen > 0 ; subLen--){
for(int startIndex = 0 ; startIndex <= (s.length()-subLen) ; startIndex++){
//列出所有子串,然后判断子串是否满足有重复
if(startIndex != (s.length()-subLen)){
sub = s.substring(startIndex, startIndex+subLen) ;
}else{
sub = s.substring(startIndex) ;
}
if(!isRepeat(sub)){
return subLen ;
}
}
} return 1 ;
} private boolean isRepeat(String s){
for(int i = 1 ; i < s.length(); i++){
if(s.substring(i).contains(s.substring(i-1, i))){
return true ;
}
}
return false ;
} /*
* 方法二:用hash的方法加上动态规划求解
*/
public int lengthOfLongestSubstring2(String s) {
if(s == null || s.length() == 0){
return 0 ;
}
int cur_len = 1 ; //lenght of current substring
int max_len = 1 ;
int prev_index ; // previous index
int [] visited = new int [256] ;
char [] arr = s.toCharArray() ;
/* Initialize the visited array as -1, -1 is used to
indicate that character has not been visited yet. */
for(int i = 0 ; i < 256 ; i++){
visited[i] = -1 ;
}
/* Mark first character as visited by storing the index
of first character in visited array. */
visited[arr[0]] = 0 ; /* Start from the second character. First character is
already processed (cur_len and max_len are initialized
as 1, and visited[arr[0]] is set */
for(int i = 1 ; i < arr.length ; i++){
prev_index = visited[arr[i]] ; /* If the current character is not present in the
already processed substring or it is not part of
the current NRCS, then do cur_len++ */
if(prev_index == -1 || i - cur_len > prev_index){
cur_len++ ;
}else{
/* Also, when we are changing the NRCS, we
should also check whether length of the
previous NRCS was greater than max_len or
not.*/
if(cur_len > max_len){
max_len = cur_len ;
}
// update the index of current character
cur_len = i - prev_index ;
} visited[arr[i]] = i ;
} // Compare the length of last NRCS with max_len and
// update max_len if needed
if (cur_len > max_len){
max_len = cur_len ;
} return max_len ; } }
LeetCode--No.003 Longest Substring Without Repeating Characters的更多相关文章
- 【LeetCode】003. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【JAVA、C++】LeetCode 003 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
一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...
- 【LeetCode OJ】Longest Substring Without Repeating Characters
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...
- 003 Longest Substring Without Repeating Characters 最长不重复子串
Given a string, find the length of the longest substring without repeating characters.Examples:Given ...
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- No.003 Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters Total Accepted: 167158 Total Submissions: 735821 Diff ...
- 【LeetCode】3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
随机推荐
- EOS的发币逻辑
[EOS的发币逻辑] EOS官网的Guide中(参考[1]),描述了如何发自己的Token: 1.创建一个contract. 2.有一些create.transfer.close action. 3. ...
- 求树的重心 poj 1655
题目链接:https://vjudge.net/problem/POJ-1655 这个就是找树的重心,树的重心就是树里面找一个点,使得以这个点为树根的所有的子树中最大的子树节点数最小.题目应该讲的比较 ...
- 网络请求Adapter添加数据
一般在开发中我们都需要在listview中添加数据显示在界面上 1.首先我们会在布局中写一个listview <FrameLayout xmlns:android="http://sc ...
- mybatis中查询使用#{}和${}的区别
${}中的变量什么值,就会简单的替代变量,不会做处理 比如delete * from tb_label where name=${labelname} 如果labelname的值是 something ...
- jsp中<c:if>标签的用法
<c:if test="${(tbl.column1 eq '值') and (tbl.column2 eq 'str')}"> <table>...< ...
- 3T - A1 = ?
有如下方程:A i = (A i-1 + A i+1)/2 - C i (i = 1, 2, 3, .... n). 若给出A 0, A n+1, 和 C 1, C 2, .....C n. 请编程计 ...
- NGS NGS ngs(hisat,stringtie,ballgown)
NGS ngs(hisat,stringtie,ballgown) #HISAT (hierarchical indexing for spliced alignment of transcripts ...
- POI2015 解题报告
由于博主没有BZOJ权限号, 是在洛咕做的题~ 完成了13题(虽然有一半难题都是看题解的QAQ)剩下的题咕咕咕~~ Luogu3585 [POI2015]PIE Solution 模拟, 按顺序搜索, ...
- Java内存泄露监控工具:JVM监控工具介绍
本文将对JVM监控工具jstack, jconsole, jinfo, jmap, jdb, jstat进行详细的介绍,具体内容请看下文 Sun JDK监控和故障处理工具 名称 主要作用 jps JV ...
- spring mvc mybatis shiro构建cms系统ios android
开发语言: java.ios.android 部署平台: linux.window jdk版本:JDK1.7以上版本 开发工具: eclipse.idea等 服务器中间件:Tomcat 6.7.Jbo ...