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的更多相关文章

  1. 【LeetCode】003. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  2. 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  3. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  4. 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters

    一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...

  5. 【LeetCode OJ】Longest Substring Without Repeating Characters

    题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...

  6. 003 Longest Substring Without Repeating Characters 最长不重复子串

    Given a string, find the length of the longest substring without repeating characters.Examples:Given ...

  7. 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...

  8. 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  9. No.003 Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters Total Accepted: 167158 Total Submissions: 735821 Diff ...

  10. 【LeetCode】3. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

随机推荐

  1. 移动端过禁止输入emoji表情实现方案

    最近手头上的项目有一个需求就是输入框不能输入表情,然后就各种在网上找资料,网上好多人给的方案是: str = str.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uD ...

  2. mongodb-MYSQL

    #encoding:utf8 import pymongoimport MySQLdbimport randomdef GetMongoData(): MyQuery = Mongo_Tab.find ...

  3. PhoenixFD插件流体模拟——UI布局【Simulation】详解

    前言 之前使用RealFlow做流体模拟,但是总得和3ds导来导去,略显麻烦,特意学习PhoenixFD插件,直接在3ds中进行流体模拟.若读者有更好的流体模拟方法,欢迎在评论区交流. 原文地址:ht ...

  4. oracle 新增并返回新增的主键

    oracle 的insert into 语句需要返回新增的主键的时候,可以使用一下insert 语法: insert into ims.t_bank_inquire_results (t_date,l ...

  5. Python开发【第五篇】:模块

    递归的案例:阶乘 1*2*3*4*5*6*7- def func(num):     if num == 1:         return 1     return num * func(num - ...

  6. Laravel5 (cli)命令行执行脚本及定时任务

    Artisan是Laravel自带的命令行接口名称,它提供了很多有用的命令想要查看所有可用的Artisan命令,可使用list命令查看: 1 php artisan list 每个命令都可以用help ...

  7. 65. Valid Number 判断字符串是不是数字

    [抄题]: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " ...

  8. list(zip(*querySet))使用

    teacher_cls_list = obj.cls.all().values_list('id', 'caption') #list(zip(*list)),将数组中的元组中的每一项取出,添加到一起 ...

  9. zookeeper 服务挂掉重启后,dubbo 服务是不会自动重新注册上的

    今天遇到一个问题: 系统初始有两个dubbo 服务 , A 和 B , 都是正常注册到zookeeper 上的, 但是zookeeper 服务机房 断电导致 服务宕机, 那就重启吧. 一切正常. 但是 ...

  10. python_day11

    一.简介 1.什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据. 2.关系型数据库 ...