`
vvaaiinn
  • 浏览: 21103 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

LeetCode 3 Longest substring without Repeating A

 
阅读更多

题目

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

解法1:

public class Solution {
    public int lengthOfLongestSubstring(String s) {
    int MaxLen = 1;//最大长度
		String maxString = null;//最大字符串
		if(s.length()==0||s.length()==1)//如果字符串长度为0或者1 直接返回
			return s.length();
		
		char[] sstring = s.toCharArray();//把字符串转化为字符数组
		java.util.List<Character> list=new ArrayList<Character>();    
		for(int i = 0; i <s.length(); i++)
		{	
			if(MaxLen>s.length()-i)
				return MaxLen;
			
			if(list.contains(sstring[i]))
			{
				int tlen = list.size();
				MaxLen = MaxLen>tlen?MaxLen:tlen;
				int index = list.indexOf(sstring[i]);
				list =  list.subList(index+1, list.size());
				
			}
			list.add(sstring[i]);		
		}
		if(list.size()>MaxLen)
			return list.size();		
		return MaxLen;
    }
}
初步想到的解法是这个,把最长的放在list里,如果出现重复的 就把前面的去掉。保存最大长度的值。最后当最大长度大于剩余,返回。

本地测试没有问题,结果放在上面出现


容我在想一想解决方法啊。


看了一位大大的解题思路,顿时觉得茅塞顿开。

http://blog.csdn.net/linhuanmars/article/details/19949159

采用线性的方法,就是维护一个set窗口,每次检查窗口里面有无重复,有的话进行删除,

时间复杂度空间复杂度均为O(n);不错,学习到了

public static int lengthOfLongestSubstring(String s) {
		int MaxLen = 0;//最大长度
		String maxString = null;//最大字符串
		if(s.length()==0||s.length()==1)//如果字符串长度为0或者1 直接返回
			return s.length();		
		int left = 0;
		int right = 0;
		HashSet<Character> set = new HashSet<Character>();
		while(right<s.length())
		{
			if(set.contains(s.charAt(right)))
			{
				if(MaxLen < right - left)
				{
					MaxLen = right - left;
				}
				while(s.charAt(left)!=s.charAt(right))
				{
					set.remove(s.charAt(left));
					left++;
				}
				left++;			
			}
			else {
				set.add(s.charAt(right));				
			}
			right++;						
		}
		MaxLen = Math.max(MaxLen, right-left);
	
		return MaxLen;
    }
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		String s = "abcdefghijklmnopqrstuvwxyzdefg";
		int k = lengthOfLongestSubstring(s);
		System.out.println(k+"\t\t\t");
		
	}


分享到:
评论

相关推荐

    LeetCode3 Longest Substring Without Repeating Characters

    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...

    leetcode答案-LeetCode-Longest_Substring_Without_Repeating_Characters:Leet

    答案LeetCode-Longest_Substring_Without_Repeating_Characters 这是LeetCode上“最长子串无重复字符”问题的解决方案。 问题描述:给定一个字符串,求没有重复字符的最长子串的长度。 示例 1:输入:“abcabcbb” ...

    javalruleetcode-LeetCode:LeetCode算法问题

    java lru leetcode Fighting for a job! 记录找工作期间搬运的题,全部使用Java实现,本人C++鶸 1 ...LeetCode ...LeetCode ...LeetCode ...Repeating Characters LeetCode 13 Roman to Integer LeetCode 6 Zi

    程序员面试宝典LeetCode刷题手册

    3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum 16. 3Sum Closest 17...

    leetcode分类-leetcode:leetcode问题的代码

    #3:Longest Substring Without Repeating Characters #5:Longest Palindromic Substring 链表 #2:Add Two Numbers 分而治之 #53:Maximum Subarray 队列/集 #3:Longest Substring Without Repeating Characters 优先...

    leetcode2sumc-LeetCode-3.Longest_Substring_Without_Repeating_Characters

    LeetCode-3.Longest_Substring_Without_Repeating_Characters 给定一个字符串,找出没有重复字符的最长子字符串的长度。 示例 1: 输入:“abcabcbb” 输出:3 解释:答案是“abc”,长度为 3。 解决方案 Python3:...

    leetcode卡-LeetCode:我的LeetCode解决方案

    leetcode卡 LeetCode 记录一下再LeetCode上刷的题,坚持每天刷一道吧 2017.06.12 打卡[LeetCode 2. Add Two Numbers], Linked list 2017.06.13 打卡[LeetCode 200. Number of Islands], BFS 2017.06.14 打卡...

    leetcode分类-leetcode:leetcode

    Leetcode.3 Longest Substring Without Repeating Characters Leetcode.76 Minimum Window Substring Leetcode.438 Find All Anagrams in a String Pattern: two points 双指针是这样的模式:两个指针朝着左右方向...

    leetcodepython001-leetcode:https://leetcode.com/的解决方案

    leetcode Python 001 力码 ├── Algorithms │  ├── cpp │  │  ├── 001 - Two Sum.cpp │  │  ├── 002 - Add Two Numbers.cpp │  │  ├── 003 - Longest Substring Without Repeating ...

    leetcode2sumc-leetcode:JavaScript版本leetcode中文版代码

    leetcode 2 sum c leetcode 力扣(Leetcode)编程题,JavaScript版本。 编号 中文题目 英文题目 题目描述 JavaScript 难度 1 Two Sum 简单 2 Add Two Numbers 中等 3 Longest Substring Without Repeating... 中等 5...

    lrucacheleetcode-leetcode:leetcode

    lru缓存leetcode leetcode 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 5. Longest Palindromic Substring 7. Reverse Integer 9. ...

    leetcode题库-leetcode-web:以Web形式展示LeetCode题解,基于PythonFlask

    leetcode题库 LeetCode-Web 初始化 前端库依赖 下载,并将jquery-3.x.x.min.js移动到static目录下。 下载,并将semantic.min.js、semantic.min.css、components和themes...longest-substring-without-repeating-charac

    leetcode中文版-LeetCode:LeetcodeC++/Java

    leetcode中文版 LeetCode # Title Chinese Tag Solution 1 Two Sum 两数之和 array,hash 2 Add Two Numbers 两数相加 math 3 Longest Substring Without Repeating Characters 无重复字符的最长子串 string 4 ...

    leetcode338-LeetCode:LeetCode刷题总结

    3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7.Reverse Integer 8.String to Integer (atoi) 9....

    分割数组求最大差值leetcode-Leetcode-Road:LeetCode刷题记录

    分割数组求最大差值leetcode LeetCode 学习之路 记录自己完成LeetCode的代码和结果。 序号 中文名称 英文名称 通过率 难度 1 Two Sum 47.0% 简单 2 Add Two Numbers 36.0% 中等 3 Longest Substring Without ...

    dna匹配leetcode-leetcode:leetcode刷题

    Longest Substring Without Repeating Characters 哈希表 双指针 滑动窗口 Substring with Concatenation of All Words 哈希表 注意匹配方向 Valid Sudoku 数组 遍历 Sudoku Solver 深度优先遍历 回溯 先检查后修改 ...

    leetcode双人赛-LeetCode:力扣笔记

    leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...

    javalruleetcode-Leetcode:力码解决方案

    Repeating Characters (无重复字符的最长子串) Medium Dual Pointer (Sliding Window), Hash Table 4 Median of Two Sorted Arrays (寻找两个有序数组的中位数) Hard Divide and Conquer 5 Longest Palindromic ...

    leetcode题库-LeetCode-Go:用GO回答LeetCode

    leetcode题库 LeetCode-Go 理论基础 见Note 脑图 TODO 待填充 算法题 面试高频出现,以及一些非常经典重要的算法题优先 题目列表 No Title Link Solution Acceptance Difficulty Topics Solution 0001 Two Sum 46.1%...

    Leetcode回文串拼接-leetcode_note:用于记录leetcode题目的解析

    3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring 6.ZigZag Conversion 7.Reverse Integer 8.String To Integer 9.Palindrome Number 10.String ...

Global site tag (gtag.js) - Google Analytics