[LeetCode]3. Longest Substring Without Repeating Characters

Athem
2 min readDec 20, 2019

--

繼續刷題!

題目網址附上:https://leetcode.com/problems/longest-substring-without-repeating-characters/

題目

Given a string, find the length of the longest substring without repeating characters.

Example 1:

Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: "pwwkew"
Output: 3
Explanation: 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.

題目解釋

要看他給的Input字(s)串中,最長的不重複字串長度。

思考方式

讀了別人的解答,整理以下思維:

  1. 宣告一個陣列all,裡面存放一個個字元拆開的s。
  2. 宣告一個暫存最長字元區ans
  3. 遍歷s,無一樣的字元就丟進ans裡,有則清空ans將重複的第一字元丟進ans裡。
程式筆記

心得

有想法卻不知道怎麼實踐,是因為對可以使用的發法不太熟悉QQ幸好有很多大神提供答案參考,程式這條路總是孤單中又帶點不孤單,踩著別人的腳印往前走也是學很多,加油吧!

--

--