leetcode03-Longest Substring Without Repeating Characters之Java版本

我的leetcode之旅,该篇章主要完成使用Java实现算法。这是第三篇Longest Substring Without Repeating Characters

全部代码下载:Github链接:github链接,点击惊喜;写文章不易,欢迎大家采我的文章,以及给出有用的评论,当然大家也可以关注一下我的github;多谢;

1.题目简介:

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.

2.我的思路:

1.使用一个字符数组记录字符是否重复

2.将没个子串的起点定义为j,终点定义为i

3.i递增当字符已经在字符数组中出现,判断max是否大于i-j;从j开始寻找找到是哪个字符相同,同时将起点j++在字符数组中去掉丢掉的字符;

4.如果字符串中最后一个字符不是重复字符,则在循环外面进行判断是否更新max

3.我的AC代码

package com.rlovep.string;
/**
 *  Longest Substring Without Repeating Characters
 *  我的思路:
 *  1.使用一个字符数组记录字符是否重复
 *  2.将没个子串的起点定义为j,终点定义为i
 *  3.i递增当字符已经在字符数组中出现,判断max是否大于i-j;从j开始寻找找到是哪个字符相同,同时将起点j++在字符数组中去掉丢掉的字符;
 *  4.如果字符串中最后一个字符不是重复字符,则在循环外面进行判断是否更新max
 * @author peace
 *
 */
public class LongSubNrep {
      public int lengthOfLongestSubstring(String s) {
        if(s==null||s.length()<=0)return 0;
        char[] a=new char[256];
        int j=0;
        int max=0;
        int i=0;
        boolean flag=false;
        for(;i<s.length();i++){
            char c=s.charAt(i);
            if(a[c]==1){
                if(max<(i-j))max=i-j;
                flag=false;
                for(int k=j;k<i;k++){
                    char dup=s.charAt(k);
                    if(c==dup){
                        j=k+1;
                        break;
                    }else{
                        a[dup]=0;
                    }
                }
            }else{
                a[c]=1;
                flag=true;
            }
        }
        if(flag==true&&max<(i-j))max=i-j;
          return max;
        }
      public static void main(String[] args) {
          char[] a=new char[256];

    }
}

好的本章介绍到这里 来自伊豚wpeace(blog.wpeace.cn)

时间: 2024-10-12 07:39:46

leetcode03-Longest Substring Without Repeating Characters之Java版本的相关文章

Longest Substring Without Repeating Characters leetcode java

题目: 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 s

leetcode longest substring without repeating characters(medium) /java

上题: 最简单粗暴的方法: 1 public class Solution { 2 public int lengthOfLongestSubstring(String s) { 3 String s1=new String(); 4 char[] c=s.toCharArray(); 5 int len=s.length(); 6 boolean[] f=new boolean[1000]; 7 int i=0; 8 for(i=0;i<1000;i++) 9 f[i]=false; 10 i

Java for LeetCode 003 Longest Substring Without Repeating Characters

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 subst

[LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++语言 java语言实现

Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: "bbbbb" Output: 1 Explana

3. Longest Substring Without Repeating Characters【leetcode】java,算法,Substring实现,子串,HashMap

3. 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", t

Java [leetcode 3] Longest Substring Without Repeating Characters

问题描述: 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

LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List

题目中文:没有重复字符的最长子串 题目难度: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 &q

【LeetCode】Longest Substring Without Repeating Characters 解题报告

[题意] 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

Leet Code Longest Substring Without Repeating Characters

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 subst