Leetcode
Given a string s
, find the length of the longest substring without repeating characters.
Example 1:
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Example 4:
Input: s = ""
Output: 0
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
sub_str = ''
max_len = 0
for i in s:
while i in sub_str:
sub_str = sub_str[1:]
else:
sub_str += i
max_len = max(len(sub_str), max_len)
return max_len
PTT 上找到的解答
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
indices, j, ans = {}, 0, 0
for i, c in enumerate(s):
if c in indices:
j = max(indices[c] + 1, j)
ans = max(ans, i - j + 1)
indices[c] = i
return ans
CH8 在線廣告實現 流程 每人先對本次進行自己的總結 問題討論 2022.02.16 問題討論
Mar 16, 2022Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order. Example 1 Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16,100] Explanation: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [0,1,9,16,100]. Example 2 Input: nums = [-7,-3,2,3,11]
Jun 7, 2021You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n. Example 1 Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6] Explanation: The arrays we are merging are [1,2,3] and [2,5,6].
Jun 7, 2021Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input. example 1 Input: intervals = [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6]. example 2
Jun 4, 2021or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up