LeetCode, leetcodeoj

Source: Internet
Author: User

LeetCode, leetcodeoj

Question:

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.

Ideas:

1) for two for loops, use the map to store the accessed letters, stop when the same is encountered, and then start from the next letter of the same letter. Timeout.

package string;import java.util.HashMap;public class LongestSubstringWithoutRepeatingCharacters {    public int lengthOfLongestSubstring(String s) {        int len;        if (s == null || (len = s.length()) == 0) return 0;        HashMap<Character, Integer> map = new HashMap<Character, Integer>();        int max = 0;        for (int i = 0; i < len; ++i) {            map.put(s.charAt(i), i);            for (int j = i + 1; j < len; ++j) {                if (!map.containsKey(s.charAt(j))) {                    map.put(s.charAt(j), j);                } else {                    i = map.get(s.charAt(j));                    break;                }            }                        int count = map.size();            if (count > max) {                max = count;            }            map.clear();        }                return max;    }        public static void main(String[] args) {        // TODO Auto-generated method stub        String s = "abcabcbb";        LongestSubstringWithoutRepeatingCharacters l = new LongestSubstringWithoutRepeatingCharacters();        System.out.println(l.lengthOfLongestSubstring(s));    }}

2) simplify method 1, keep the previous position of the repeating element, move forward, and constantly calculate

package string;import java.util.HashMap;public class LongestSubstringWithoutRepeatingCharacters {    public int lengthOfLongestSubstring(String s) {        int len;        if (s == null || (len = s.length()) == 0) return 0;        HashMap<Character, Integer> map = new HashMap<Character, Integer>();        int max = 0;        int lastIndex = -1;        for (int i = 0; i < len; ++i) {            char c = s.charAt(i);            if (map.containsKey(c) && lastIndex < map.get(c)) {                lastIndex = map.get(c);            }                        if (i - lastIndex > max)                max = i - lastIndex;            map.put(c, i);        }                return max;    }        public static void main(String[] args) {        // TODO Auto-generated method stub        String s = "abcabcbb";        LongestSubstringWithoutRepeatingCharacters l = new LongestSubstringWithoutRepeatingCharacters();        System.out.println(l.lengthOfLongestSubstring(s));    }}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.