1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| package main
import ("fmt")
func lengthOfLongestSubStringBitMap(s string) (int, int, int) { if len(s) == 0 { return 0, 0, 0 } var BitMap [128]bool var target, left, right, start, end = 0, 0, 0, 0, 0 for left < len(s) { if BitMap[s[right]] { BitMap[s[left]] = false left ++ } else { BitMap[s[right]] = true right ++ } if target < right - left { target = right - left } if right >= len(s) || left + target >= len(s) { end = right start = end - target break }
} return target, start, end }
func lengthOfLongestSubStringHash(s string) (int, int, int) { if len(s) == 0 { return 0, 0, 0 } var HashMap map[string]bool = map[string]bool{} var target, left, right, start, end = 0, 0, 0, 0, 0 for left < len(s) { if HashMap[string(s[right])] { HashMap[string(s[left])] = false left ++ } else { HashMap[string(s[right])] = true right ++ } if target < right - left { target = right - left } if right >= len(s) || left + target >= len(s) { end = right start = end - target break } } return target, start, end }
func lengthOfLongestStringSplitWindows(s string) (int, int, int) { if len(s) == 0 { return 0, 0, 0 } var windows [128]int var target, left, right = 0, 0, 0 for left < len(s) { if windows[s[right] - 'a'] == 0 && right + 1 < len(s) { windows[s[right] - 'a'] ++ right ++ } else { windows[s[left] - 'a']-- left ++ } if target < right - left { target = right - left } }
return target, right - target, right }
func main() { length, start, end := lengthOfLongestSubStringBitMap("pwwkew") length1, start1, end1 := lengthOfLongestStringSplitWindows("pwwkew") fmt.Println(length, start, end) fmt.Println(length1, start1, end1) }
|