【leedcode】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 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.

寻找最长的不重复串,略像滑动窗口。

char[] buff;
    //used
    int used ;
    public int lastIndexOf(char ch, int endIndex) {
            int i = used;
            for (; i >= endIndex; i--) {
                if (buff[i] == ch) {
                    return i;
                }
            }
            return -1;
    }
    public int lastIndexOf(char ch) {
        return lastIndexOf(ch, 0);
    }

     public int lengthOfLongestSubstring(String s) {
             if (s == null || s.isEmpty()) {
                 return 0;
             }
            int len = s.length();
            buff = new  char[len];
            buff[0] = s.charAt(0);
            used = 1;
            char t;
            int idx = -1;
            int top = 0;
            int first = 0;
            while(used<len){
                t = s.charAt(used);
                idx = lastIndexOf(t, first);
//                System.err.println("["+top+"]s=" + new String(buff) + "," + (idx>-1 ? ( "pos=" +  idx + " find " + t ) : "" ) + ",idx="  + used + ",first=" + first);

                if (idx > -1) {
                    top = Math.max(used - first, top);
                    first = idx+1;
                }

                buff[used] = t;
                used++;
            } //            System.err.println("["+top+"]s=" + new String(buff) + "," + (idx>-1 ? ( "pos=" +  idx + " find " + t ) : "" ) + ",idx="  + used + ",first=" + first);
            return Math.max(len - first, top);
        }

上面这个粗鲁的代码,马马虎虎毕竟打败51 ~61%的code,lastIndexOf有优化的空间。

不过答案真是美妙的活动窗口实现,赞!使用了字符作为坐标,索引作为值。

 public int lengthOfLongestSubstring(String s) {
        int n = s.length(), ans = 0;
        int[] index = new int[128]; // current index of character
        // try to extend the range [i, j]
        for (int j = 0, i = 0; j < n; j++) {
            i = Math.max(index[s.charAt(j)], i);
            ans = Math.max(ans, j - i + 1);
            index[s.charAt(j)] = j + 1;
        }
        return ans;
    }
时间: 2024-08-27 03:57:55

【leedcode】longest-substring-without-repeating-characters的相关文章

【leetcode】 Longest Substring Without Repeating Characters

题目: 给定一个字符串,返回该串没有重复字符的最长子串. 分析: 1)子串:子串要求是连续的. 2)无重复,出现重复就断了,必须从新的位置开始.而新的位置就是重复字符第一次出现位置的下一个位置. 3)整个串可能没有一处重复. 那么,为了找出当前访问的字符是否出现过,要怎么做呢?当然是hash,O(1)的时间,而且既然是字符, 定义个255的hash table 就可以了,hash table 中的元素为相应字符在s中出现的位置.初始化为-1,表示都没有出现. 我们另外定义一个start 和end

【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

【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 subst

【leetcode】Longest Substring Without Repeating Characters (middle)

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

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

【Leet Code】Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters Total Accepted: 20506 Total Submissions: 92223My Submissions Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating le

【贪心算法】Longest Substring Without Repeating Characters

题目: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", whi

【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)

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 l

【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters

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

LeetCode【3】.Longest Substring Without Repeating Characters--算法图解及java实现

第三道题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", whic