網(wǎng)站首頁 編程語言 正文
題目
給定一個字符串,請你找出其中不含有重復字符的?最長子串?的長度。
示例 1:
輸入: "abcabcbb"
輸出: 3?
解釋: 因為無重復字符的最長子串是 "abc",所以其長度為 3。
示例 2:
輸入: "bbbbb"
輸出: 1
解釋: 因為無重復字符的最長子串是 "b",所以其長度為 1。
示例 3:
輸入: "pwwkew"
輸出: 3
解釋: 因為無重復字符的最長子串是 "wke",所以其長度為 3。
要注意字符串為空、變量為null、字符串長度 Length = 1 等情況。
測試實例
輸入 " " "au" "abcabcbb" "bbbbb" "pwwkew" "aab" 預期結果分別是 1,2,3,1,3,2
代碼格式模板
public class Solution { public int LengthOfLongestSubstring(string s) { } }
筆者的代碼僅供參考
使用最笨的方式,200ms左右
public class Solution { public int LengthOfLongestSubstring(string s) { if (s == null || s == "") return 0; char[] a = s.ToCharArray(); //字符串轉為字符數(shù)組 int start = 0; //區(qū)間開始位置 int stop = 0; //區(qū)間結束位置 int newMax = 1; //當前區(qū)間數(shù) int max = 1; //區(qū)間最大個數(shù) for (stop = 1; stop < a.Length; stop++) //每次向后移動一位 { bool b = false; //是否存在重復 for (int i = start; i < stop; i++) //檢查當前元素在區(qū)間是否有相同值 { if (a[stop] == a[i]) //如果stop+1位在區(qū)間找到相同的字符 { char ls = a[stop]; if (newMax > max) max = newMax; start = i + 1; //區(qū)間開始位置重置 newMax = stop - start + 1; b = true; break; } } if (b == false) newMax += 1; } if (newMax > max) max = newMax; return max; } }
完整測試代碼(控制臺)
using System; namespace ConsoleApp1 { public class Testa { public int LengthOfLongestSubstring(string s) { if (s == null || s == "") return 0; char[] a = s.ToCharArray(); //字符串轉為字符數(shù)組 int start = 0; //區(qū)間開始位置 int stop = 0; //區(qū)間結束位置 int newMax = 1; //當前區(qū)間數(shù) int max = 1; //區(qū)間最大個數(shù) for (stop = 1; stop < a.Length; stop++) //每次向后移動一位 { bool b = false; //是否存在重復 for (int i = start; i < stop; i++) //檢查當前元素在區(qū)間是否有相同值 { if (a[stop] == a[i]) //如果stop+1位在區(qū)間找到相同的字符 { char ls = a[stop]; if (newMax > max) max = newMax; start = i + 1; //區(qū)間開始位置重置 newMax = stop - start + 1; //重新設置區(qū)間數(shù) b = true; break; } } if (b == false) ////沒有重新設置區(qū)間數(shù)時加1 newMax += 1; } if (newMax > max) max = newMax; return max; } } class Program { static void Main(string[] args) { Testa t1 = new Testa(); //正確結果 Console.WriteLine(t1.LengthOfLongestSubstring(" ")); //1 Console.WriteLine(t1.LengthOfLongestSubstring("au")); //2 Console.WriteLine(t1.LengthOfLongestSubstring("abcabcbb")); //3 Console.WriteLine(t1.LengthOfLongestSubstring("bbbbb")); //1 Console.WriteLine(t1.LengthOfLongestSubstring("pwwkew")); //3 Console.WriteLine(t1.LengthOfLongestSubstring("aab")); //2 Console.ReadKey(); } } }
使用哈希集合,速度更快,100ms-150ms
public int LengthOfLongestSubstring(string s) { int n = s.Length; HashSet<char> set = new HashSet<char>(); //集合 int ans = 0, start = 0, stop = 0; //ans為字符串長度,starp區(qū)間起點,stop區(qū)間終點 while (start < n && stop < n) { // try to extend the range [i, j] if (!set.Contains(s[stop])) { set.Add(s[stop++]); ans = Math.Max(ans, stop - start); //或者ans = ans > (stop - start) ? ans : (stop - start) } else { set.Remove(s[start++]); } } return ans; }
完整控制臺測試代碼
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp2 { public class Solution { public int LengthOfLongestSubstring(string s) { int n = s.Length; HashSet<char> set = new HashSet<char>(); //集合 int ans = 0, start = 0, stop = 0; //ans為字符串長度,starp區(qū)間起點,stop區(qū)間終點 while (start < n && stop < n) { // try to extend the range [i, j] if (!set.Contains(s[stop])) { set.Add(s[stop++]); ans = Math.Max(ans, stop - start); //或者ans = ans > (stop - start) ? ans : (stop - start) } else { set.Remove(s[start++]); } } return ans; } } class Program { static void Main(string[] args) { Solution t1 = new Solution(); //正確結果 Console.WriteLine(t1.LengthOfLongestSubstring(" ")); //1 Console.WriteLine(t1.LengthOfLongestSubstring("au")); //2 Console.WriteLine(t1.LengthOfLongestSubstring("abcabcbb")); //3 Console.WriteLine(t1.LengthOfLongestSubstring("bbbbb")); //1 Console.WriteLine(t1.LengthOfLongestSubstring("pwwkew")); //3 Console.WriteLine(t1.LengthOfLongestSubstring("aab")); //2 Console.ReadKey(); } } }
原文鏈接:https://www.cnblogs.com/whuanle/p/10342416.html
相關推薦
- 2022-05-24 Windows環(huán)境bat腳本獲取文件的創(chuàng)建時間_DOS/BAT
- 2023-05-23 numpy中的掩碼數(shù)組的使用_python
- 2023-03-29 Android?Flutter中Offstage組件的使用教程詳解_Android
- 2023-02-12 Docker創(chuàng)建MongoDB容器并添加root密碼驗證與更多用戶詳細步驟_docker
- 2022-06-17 Go語言學習之函數(shù)的定義與使用詳解_Golang
- 2024-03-06 SpringAOP基于注解方式實現(xiàn)和細節(jié)
- 2023-01-05 Kotlin?泛型邊界型變及星投影使用詳解_Android
- 2022-04-27 python技巧分享Excel創(chuàng)建和修改_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支