leetcode-Length of the longest substring without repeating characters

问题描述:得到一个字符串的最长子字符串,且该最长子字符串中的包含的字符都不相同

思想以后再补充吧

代码如下:

package com.mifeng.acm.test;

import java.util.Arrays;

import org.junit.Test;

public class SolutionDemo {
	
	@Test
	public void test(){
		//int[] array = new int[]{2, 7, 11, 15};
//		int[] array = new int[]{3,2,4};
//		int target = 6;
//		int[] index = twoSum(array, target);
//		System.out.println(Arrays.toString(index));
//		String a = "www";
//		//a.l
//		a.t
		
		String str = "abcabcbb";
		int len = lengthOfLongestSubstring(str);
		System.out.println(len);
	}
	
//	public int[] twoSum(int[] nums, int target) {
//        int[] index = new int[2];
//        for(int i = 0;i<nums.length - 1; i++){
//            int key = nums[i];
//            for(int j = i + 1; j<=nums.length - 1; j++){
//                int sum = key + nums[j];
//                if(sum == target){
//                    index[1] = j;
//                    index[0] = i;
//                    break;
//                }
//               
//            }
//        }
//        return index;
//    }
	
	public int lengthOfLongestSubstring(String s) {
        int start = 0; // current start point of substring without dup  
        int maxlen = 0; // max length of substring found  
        int [] table = new int[256] ; // hash table for index of each char appeared  
        for (int i = 0;i < 256;i++) table[i] = -1; // if char not present, index is -1  
        char[] str = s.toCharArray();
        int len = str.length;  
        for (int i = 0;i < len;i++) { 
        	int index = str[i];
            if (table[index] != -1) {  
                while (start <= table[index]) table[str[start++]] = -1;  
            }  
            if (i - start + 1 > maxlen) maxlen = i - start + 1;  
            table[index] = i;  
        }  
        return maxlen;  
    }
}




参考

1、http://www.geeksforgeeks.org/length-of-the-longest-substring-without-repeating-characters/

2、https://leetcode.com/problems/longest-substring-without-repeating-characters/#/description

3、https://discuss.leetcode.com/topic/1914/my-o-n-solution

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值