-
2021-01-09] Check If Two String Arrays are Equivalent & Duplicate ZerosIT/자기계발 ( Leetcode ) 2021. 1. 9. 00:00반응형
오늘의 문제:
leetcode.com/problems/check-if-two-string-arrays-are-equivalent/
Check If Two String Arrays are Equivalent - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
leetcode.com/problems/duplicate-zeros/
Duplicate Zeros - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
왜 문제가 2개냐.. 이번 첫번째 문제가 넘 쉬워서.. 다른 쉬운 문제도 하나 더 풀어보았다~ ( 분량채우기 ㄴㄴ임 ㅋㅋ )
그럼 첫번째 문제부터 설명해보자
<첫번째 문제의 예시> 뭐가 글이 많아서 그렇지 간단히 설명하면 아래와 같다.
- 입력값으로 주어지는 2개의 List[str]이 하나의 string으로 만들었을 때 같은지 다른지 비교
첫번째 문제의 풀이를 보자
문제풀이)
class Solution(object): def arrayStringsAreEqual(self, word1, word2): """ :type word1: List[str] :type word2: List[str] :rtype: bool """ if "".join(word1) == "".join(word2): # List로 주어진 값을 join함수 모아 비교 return True return False
<첫번째 문제풀이의 성능> 단 3줄로 마무리.. Python의 좋은 점이 한 껏 발휘되는 코드이지 않나 싶다..
문자열 처리에 대해 엄청난 강점을 보이는 언어이지 않을까 한다. ( 이러니 한문제로 끝낼수가 있나.. )
다음 두번째 문제도 예시를 보며 문제를 이해해보자.
<두번째 문제의 예시> 이번 문제는 하나의 List가 주어지면, 그안에 0이 있으면 0을 하나 더 추가해주는 문제이다.
return값은 없고 입력으로 주는 arr을 변경하면 된다. 0을 추가하게 되면 당연히 출력할 값이 늘어나겠지만,
원래의 개수 만큼만 출력한다.
문제풀이)
class Solution(object): def duplicateZeros(self, arr): """ :type arr: List[int] :rtype: None Do not return anything, modify arr in-place instead. """ ans=[] for i in range(len(arr)): if len(ans) < len(arr): # ans의 크기가 arr보다 크면 더 돌필요가 없다. if arr[i]==0: ans.append(arr[i]) # arr[i]이 0이면 0을 하나 더 추가해주면 됨 ans.append(arr[i]) else: ans.append(arr[i]) # arr[i]이 0이 아니면 한 번만 추가 else: break for idx in range(len(arr)): # arr의 길이 만큼만 arr에 ans값을 적용 arr[idx]=ans[idx]
<두번째 문제풀이의 성능> 반응형'IT > 자기계발 ( Leetcode )' 카테고리의 다른 글
2021-01-11] Valid Sudoku (0) 2021.01.11 2021-01-10] Create Sorted Array through Instructions (0) 2021.01.11 2021-01-08] Longest Substring Without Repeating Characters (0) 2021.01.08 2021-01-07] Kth Missing Positive Number (0) 2021.01.07 2021-01-06] Remove Duplicates from Sorted List II (0) 2021.01.06