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 ;
}
}
No.003 Longest Substring Without Repeating Characters的更多相关文章
- 【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--No.003 Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters Total Accepted: 167158 Total Submissions: 735821 Diff ...
- 【LeetCode】003. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 003 Longest Substring Without Repeating Characters 最长不重复子串
Given a string, find the length of the longest substring without repeating characters.Examples:Given ...
- LeetCode #003# Longest Substring Without Repeating Characters(js描述)
索引 思路1:分治策略 思路2:Brute Force - O(n^3) 思路3:动态规划? O(n^2)版,错解之一:420 ms O(n^2)版,错解之二:388 ms O(n)版,思路转变: 1 ...
- [Leetcode]003. Longest Substring Without Repeating Characters
https://leetcode.com/problems/longest-substring-without-repeating-characters/ public class Solution ...
- 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- No.003:Longest Substring Without Repeating Characters
问题: Given a string, find the length of the longest substring without repeating characters.Example:Gi ...
- Longest Substring Without Repeating Characters ---- LeetCode 003
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- [系统开发] Postfix 邮件管理系统
一.简介 开发时间:2012年 开发工具:Perl CGI 这是我开发的 Postfix 邮件管理系统,通过它可以安全.方便的对邮件域名.用户.权限.组.邮箱容量.安全等进行各种设置:界面样式借鉴了 ...
- c++中的peek函数
c++中 cin.peek()函数 其返回值是一个char型的字符,返回值是指针指向的当前字符, 但是只是观测,指针任停留在当前位置,并不后移.如果要访问的字符是文件结束符,则函数值是EOF(-1); ...
- 通过共享用户ID来实现多个应用程序使用同一个进程(一些情况的测试)
从很多方面来看,每个Android 应用程序都存在于它自己的世界之中:• 默认情况下,每个应用程序均运行于它自己的Linux 进程中.当应用程序中的任意代码开始执行时,Android 启动一个进程,而 ...
- 黄聪:PHP使用Simple_HTML_DOM遍历、过滤及保留指定属性
<? /* * 参考资料: * http://www.phpddt.com/manual/simplehtmldom_1_5/manual_api.htm * http://www.phpddt ...
- 创建immutable类
不可变对象(immutable objects) 那么什么是immutable objects?什么又是mutable Objects呢? immutable Objects就是那些一旦被创建,它们的 ...
- 单选按钮选中js的处理
function FinancialinfosetController($scope, $http, $timeout, $location, $rootScope, $routeParams) { ...
- uva 437 巴比伦塔(DAG上dp)
巴比伦塔 紫书P269 看完紫书,终于可以自己写一个dp了 :) [题目链接]巴比伦塔 [题目类型]DAG上dp &题意: 有n种立方体 n<=30,每种有无穷个,要求选一些立方体摞成一 ...
- listview--记录ListView滚动停止位置与设置显示位置
在项目中经常使用到listView控件,当想记录滚动停止时的记录,当点击加载新的数据,从记录的位置开始显示的操作怎么实现尼?分为如下步骤 1.记录位置代码 //声明记录停止滚动时候,可见的位置 pri ...
- [经验总结]利用xlstproc处理XSLT的makefile
转自:http://blog.csdn.net/thinkhy/article/details/5343739 # For XSLT PARSE = xsltproc SRC = main.xml S ...
- java小程序
1.水仙花数 这个数等于它的每位数的立方和 (三位数) 示例一: int i; int j; int k; for (i=1;i<=9;i++) { for (j=0;j<=9;j++){ ...