항해99 2주차 - 중복문자가 없는 가장 긴 부분 문자열
Today I learned
2022/01/19
회고록
1/19
항해 99, 알고리즘 1주차
교재 : 파이썬 알고리즘 인터뷰
10장 해시테이블
1. 이론
2. 문제
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.
Constraints:
0 <= s.length <= 5 * 10^4
s consists of English letters, digits, symbols and spaces.
https://leetcode.com/problems/longest-substring-without-repeating-characters/
3. MySol
def solution(strs):
result = {}
for i in range(len(strs)):
result[1] = strs[i]
for j in range(i+1,len(strs)):
temp= strs[i:j]
if strs[j] not in temp:
if j-i+1 not in result:
result[j-i+1] = strs[i:j+1]
else:
break
ansKey=max(result.keys())
ret=result[ansKey]
return ansKey
if __name__ == '__main__':
strs = 'abcabcbb'
result = solution(strs)
print('result : ' + str(result))
4. 배운 점
- 반복문을 이용하여 풀었으나, 해당 문제는 투 포인터를 이용하여 풀 수 있었다.
- 어찌보면 내 것도 투 포인터긴 한데.. 효율성을 위해선 투 포인터는 다른 규칙적인 로직을 더 효율적으로 활용할 수 있는 로직이 필수적이다.
5. 총평
해시 테이블 훈련
Author And Source
이 문제에 관하여(항해99 2주차 - 중복문자가 없는 가장 긴 부분 문자열), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@jsw4215/항해99-2주차-중복문자가-없는-가장-긴-부분-문자열저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)