LeetCode题解(Golang实现)--Longest Substring Without Repeating Characters
来源:互联网 发布:facebook 聊天软件 编辑:程序博客网 时间:2024/05/30 23:31
题目
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.
简单的来说就是从给予的字符串中计算出没有重复字符串的最长子串的长度
解题思路
最简单的 暴力破解
第二种解题思路是:从字符串中获取一个滑动子串,长度为0,然后遍历字符串,当当前定位的字符不存在与子串中时,则将其放入子串,子串长度加一,并记录最大长度,若已存在,则子串startIndex往前滑动一个位置子串长度减一,并移除第一个字符,然后重复步骤
答案
方案1
func lengthOfLongestSubstring(s string) int { max := 0 for i := 0; i < len(s); i++ { for j := i+1; j <= len(s); j++ { substr := s[i:j-1] if !strings.Contains(substr, s[j-1:j]) { if max < j-i { max = j - i } } else { break } } } return max}
方案2
func lengthOfLongestSubstring(s string) int { var max,start,end,length = 0,0,0,len(s) for start < length && end < length{ if !strings.Contains(s[start:end],string(s[end])){ end = end +1 if end-start > max { max = end-start } }else{ start = start+1 } } return max}
阅读全文
0 0
- LeetCode题解(Golang实现)--Longest Substring Without Repeating Characters
- leetcode 3 golang实现 Longest Substring Without Repeating Characters
- LeetCode题解:Longest Substring Without Repeating Characters
- LeetCode题解:Longest Substring Without Repeating Characters
- Leetcode题解:Longest Substring Without Repeating Characters
- [LeetCode]Longest Substring Without Repeating Characters题解
- LeetCode 题解(31): Longest Substring Without Repeating Characters
- LeetCode题解——3Longest Substring Without Repeating Characters
- [Leetcode题解]003 Longest Substring Without Repeating Characters
- LeetCode题解--3. Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters : LeetCode 题解
- Leetcode题解(Python): 3.Longest Substring Without Repeating Characters
- LeetCode题解:3. Longest Substring Without Repeating Characters
- leetcode 题解 3. Longest Substring Without Repeating Characters
- Leetcode题解:4. Longest Substring Without Repeating Characters
- Leetcode题解 - 3. Longest Substring Without Repeating Characters
- LeetCode题解-3-Longest Substring Without Repeating Characters
- LeetCode Algorithms 3. Longest Substring Without Repeating Characters 题解
- 【python PDF合并】python 合并同一个文件夹下所有PDF文件
- DES加密与解密(c语言版)
- Android自定义View系列(二)——打造一个仿2K游戏摇杆
- iOS开发 PHAsset获取照片名称
- 【RMQ算法】
- LeetCode题解(Golang实现)--Longest Substring Without Repeating Characters
- 在CentOS下搭建自己的Git服务器
- Java基础学习总结(四)—— 关键字介绍
- 第一篇:JAVA获得设备信息
- BigDecimal 加,减,乘,除 详解
- camera2 opengl实现滤镜效果录制视频 三 录音
- vue中使用百度地图
- codeforces #430 (div2)
- Kotlin 第八章:接口