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, ...
 
随机推荐
- 127 Word Ladder
			
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
 - iphone dev 入门实例5:Get the User Location & Address in iPhone App
			
Create the Project and Design the Interface First, create a new Xcode project using the Single View ...
 - Cpk
			
CPK:Complex Process Capability index 的缩写,是现代企业用于表示制程能力的指标.制程能力是过程性能的允许最大变化范围与过程的正常偏差的比值.制程能力研究在於确认这些 ...
 - linux命令(6)crontab的用法和解析
			
一,写入格式: * * * * * command minute hour day month week command 其中: minute: 表示分钟,可以是从0到59之间 ...
 - 设置Excel的自动筛选功能
			
单元格数字格式的问题 NPOI向Excel文件中插入数值时,可能会出现数字当作文本的情况(即左上角有个绿色三角),这样单元格的值就无法参与运算.这是因为在SetCellValue设置单元格值的时候使用 ...
 - cotex_m3内核提供的ITM串口打印调试
			
cotex_m3内核的ARM提供了ITM串口打印观测的功能,可以不用ARM单片机自己的串口就可在开发时候串口打印调试.节约了宝贵的内部资源,同时也为调试提供了方便.使用方法如下: 1 将下面的SWO_ ...
 - MYSQL批量插入数据库实现语句性能分析
			
假定我们的表结构如下 代码如下 CREATE TABLE example ( example_id INT NOT NULL, name VARCHAR( 50 ) NOT NULL, value ...
 - setcookie各个参数详解
			
定义和用法 setcookie() 函数向客户端发送一个 HTTP cookie. cookie 是由服务器发送到浏览器的变量.cookie 通常是服务器嵌入到用户计算机中的小文本文件.每当计算机通过 ...
 - oracle中的exists 和not exists 用法详解
			
有两个简单例子,以说明 “exists”和“in”的效率问题 1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ; ...
 - C++学习23 虚函数详解
			
虚函数对于多态具有决定性的作用,有虚函数才能构成多态.上节的例子中,你可能还未发现虚函数的用途,不妨来看下面的代码. #include <iostream> using namespace ...