Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.

preface

  • Leetcode algorithm problem 3 to find the largest string without repeating characters, difficulty: medium;
  • Before doing again have seen some solution ideas, there is said to use the left and right pointer method to calculate the longest length, at that time only see part of the solution ideas, did not read the end of their own hands to operate.
  • However, IF I only use left and right Pointers, I could not do it at all. Attached is a picture of submission record:

process

  • I couldn’t figure it out at the time, so I thought I could hash out the looped list algorithm.
  • Ha ha, finally through unremitting efforts to work out.
/** * @Author: ZRH * @Date: 2021/10/19 17:06 */ public class Test { public static void main (String[] args) { System.out.println(test("abcabcbb")); System.out.println(test("")); System.out.println(test(" ")); System.out.println(test("ua")); System.out.println(test("aab")); System.out.println(test("bb")); System.out.println(test("pwwkew")); System.out.println(test("abba")); System.out.println(test("dvdf")); } private static int test (String s) {final int length = s.length(); If (length <= 1) {return length; Int Max = 0, left = 0; // Define a hash table to store data Map<String, Integer> Map = new HashMap<>(length * 2); for (int i = 0; i < length; ) {// iterate over each character String a = string.valueof (s.char (I)); // i++ is put here to calculate the length of the current character. Integer b = map.get(a); if (b ! Left = math.max (b, left); left = math.max (b, left); } // Max = math.max (i-left, Max); // Put the element into the hash map.put(a, I); } return max; }}Copy the code
  • The remarks in the code are clearly described, and its time complexity is O(n).

The last

  • This problem is not difficult, but I also took a long time to solve it, just blame themselves not smart enough;
  • Frequently can compensate for clumsy, brush more questions, humble learning, common progress -_-