LeetCode - Medium - 3

import java.util.HashSet;

import java.util.LinkedList;

import java.util.Map;

import java.util.Queue;

import java.util.Set;

public class LongestSubstringWithoutRepeatingCharacters {

//方法一:双指针+哈希表

public int lengthOfLongestSubstring1(String s) {

int max = 0, count = 0;

Map<Character, Integer> cache = new HashMap<>();

for (int leftIndex = 0, rightIndex = 0; rightIndex < s.length(); rightIndex++) {

char character = s.charAt(rightIndex);

Integer existIndex = cache.get(character);

if (existIndex != null && existIndex >= leftIndex) {

if (count > max)

max = count;

count = rightIndex - existIndex;

leftIndex = existIndex + 1;

} else {

count++;

}

cache.put(character, rightIndex);

}

return Math.max(max, count);// max()针对字符串完全无重复字符(如:“abc”)的情况

}

//方法二:比方法一精简些

public int lengthOfLongestSubstring2(String s) {

int max = 0, leftIndex = 0, rightIndex = 0;

Map<Character, Integer> cache = new HashMap<>();

for (; rightIndex < s.length(); rightIndex++) {

char character = s.charAt(rightIndex);

Integer existIndex = cache.get(character);

if (existIndex != null && existIndex >= leftIndex) {

max = Math.max(max, rightIndex - leftIndex);

leftIndex = existIndex + 1;

}

cache.put(character, rightIndex);

}

return Math.max(max, rightIndex - leftIndex);

}

//方法三:移动窗口(队列)

public int lengthOfLongestSubstring3(String s) {

Queue queue = new LinkedList<>();

int res = 0;

for (char c : s.toCharArray()) {

while (queue.contains©) {

queue.poll();

}

queue.offer©;

res = Math.max(res, queue.size());

}

return res;

}

//方法四:双指针+哈希集

public int lengthOfLongestSubstring4(String s) {

int maxLen = 0;

Set window = new HashSet<>();

int left = 0, right = 0;

while (right < s.length()) {

while (window.contains(s.charAt(right)))

window.remove(s.charAt(left++));

window.add(s.charAt(right++));

maxLen = Math.max(maxLen, right - left);

}

return maxLen;

}

}

Test


import static org.junit.Assert.*;

import org.junit.Test;

public class LongestSubstringWithoutRepeatingCharactersTest {

@Test

public void test() {

LongestSubstringWithoutRepeatingCharacters obj = new LongestSubstringWithoutRepeatingCharacters();

assertEquals(3, obj.lengthOfLongestSubstring1(“abcabcbb”));

assertEquals(1, obj.lengthOfLongestSubstring1(“bbbbb”));

assertEquals(3, obj.lengthOfLongestSubstring1(“pwwkew”));

assertEquals(0, obj.lengthOfLongestSubstring1(“”));

assertEquals(1, obj.lengthOfLongestSubstring1(" "));

assertEquals(3, obj.lengthOfLongestSubstring1(“dvdf”));

assertEquals(2, obj.lengthOfLongestSubstring1(“abba”));

assertEquals(3, obj.lengthOfLongestSubstring2(“abcabcbb”));

assertEquals(1, obj.lengthOfLongestSubstring2(“bbbbb”));

assertEquals(3, obj.lengthOfLongestSubstring2(“pwwkew”));

assertEquals(0, obj.lengthOfLongestSubstring2(“”));

assertEquals(1, obj.lengthOfLongestSubstring2(" "));

assertEquals(3, obj.lengthOfLongestSubstring2(“dvdf”));

assertEquals(2, obj.lengthOfLongestSubstring2(“abba”));

assertEquals(3, obj.lengthOfLongestSubstring3(“abcabcbb”));

assertEquals(1, obj.lengthOfLongestSubstring3(“bbbbb”));

assertEquals(3, obj.lengthOfLongestSubstring3(“pwwkew”));

assertEquals(0, obj.lengthOfLongestSubstring3(“”));

assertEquals(1, obj.lengthOfLongestSubstring3(" "));

assertEquals(3, obj.lengthOfLongestSubstring3(“dvdf”));

assertEquals(2, obj.lengthOfLongestSubstring3(“abba”));

assertEquals(3, obj.lengthOfLongestSubstring4(“abcabcbb”));

assertEquals(1, obj.lengthOfLongestSubstring4(“bbbbb”));

assertEquals(3, obj.lengthOfLongestSubstring4(“pwwkew”));

assertEquals(0, obj.lengthOfLongestSubstring4(“”));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值