原题目:
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.给定一个字符串,找出不包含重复字符的最大子串长度
算法分析:
算法1:
提取字符串中的每一个字符(s.charAt(j)),并将其放入map数组中,未出现过标记为0,出现过标记为1,找到未出现之间字符串的最大长度。用Math.max(i,j)函数判断两个值中较大的一个。
算法2:
采用两个指针i和j,首先将两个指针指向字符数组的前两位,并一次向后移动j,判断a[i]和a[j]是否相等,相等则表示字符串重复,得出重复字符串之间的距离,将i向后移动一位,j指向i后的一位,直到找到最长的字符串。这种算法缺点:可能出现第一个字符只出现一次,但后面仍有重复字符,例如"pwwkew"字符串中,p只出现一次,但w重复出现,所以,在找i和j之间长度时,要判断之间是否有重复字符。
遇到问题及解决方法:
1.string.charAt(i)函数:取出字符串string 中序号我i的字符;
2.Math.max(i,j),比较i和j的值,并取出较大的那个值;
LeetCode提交源码:
int[] map = new int[256]; // map from character's ASCII to its last occured index
int j = 0;
int i = 0;
int ans = 0;
for (i = 0; i < s.length(); i++) {
while (j < s.length() && map[s.charAt(j)]==0) {
System.out.println("map[s.charAt()]=" + map[s.charAt(j)] + ";s.charAt=" + s.charAt(j));
map[s.charAt(j)] = 1;
ans = Math.max(ans, j-i + 1); //返回两个值中较大的一个
j ++;
System.out.println("ans = " + ans);
}
map[s.charAt(i)] = 0;
System.out.println("s.charAt[i] = " + s.charAt(i));
}
return ans;
完整运行程序:
package org.GuoGuoFighting.LeetCode3;
import java.util.Scanner;
class SolutionMethod1{
public int lengthOfLongestSubstring(String s){
int[] map = new int[256]; // map from character's ASCII to its last occured index
int j = 0;
int i = 0;
int ans = 0;
for (i = 0; i < s.length(); i++) {
while (j < s.length() && map[s.charAt(j)]==0) {
System.out.println("map[s.charAt()]=" + map[s.charAt(j)] + ";s.charAt=" + s.charAt(j));
map[s.charAt(j)] = 1;
ans = Math.max(ans, j-i + 1); //返回两个值中较大的一个
j ++;
System.out.println("ans = " + ans);
}
map[s.charAt(i)] = 0;
System.out.println("s.charAt[i] = " + s.charAt(i));
}
return ans;
}
}
class SolutionMethod2{
public int lengthOfLongestSubstring(String s){
if(s == null){
return 0;
}
int tmplength = 2;
int totallength = 0;
int p1 = 0,p2 = 1;
char[] array = s.toCharArray();
System.out.println();
while(p1 != array.length - 1 && p2 != array.length){
for(int i = p1+1; i < p2; i++ ){
if(array[i] == array[p2]){
tmplength--;
p1++;
p2 = p1 + 1;
if(totallength < tmplength){
totallength = tmplength;
}
tmplength = 2;
}
}
if(array[p1] == array[p2]){
tmplength--;
p1++;
p2 = p1+1;
if(totallength < tmplength){
totallength = tmplength;
}
tmplength = 2;
}
else {
p2++;
tmplength++;
}
}
return totallength;
}
}
public class LongestSubstring {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String str = scanner.nextLine();
scanner.close();
SolutionMethod1 solution1 = new SolutionMethod1();
SolutionMethod2 solution2 = new SolutionMethod2();
System.out.println("最长不重复子串长度为:");
solution2.lengthOfLongestSubstring(str);
System.out.println(solution1.lengthOfLongestSubstring(str));
System.out.println(solution2.lengthOfLongestSubstring(str));
}
}
程序运行结果: