-
같은 숫자는 싫어프로그래밍/알고리즘 2021. 3. 23. 14:44
나의 풀이
def solution(arr): answer = [] for i in range(len(arr)): if i == 0: answer.append(arr[i]) elif arr[i] != answer[-1]: answer.append(arr[i]) return answer
다른 사람 풀이 1
def no_continuous(s): a = [] for i in s: if a[-1:] == [i]: continue a.append(i) return a
빈 배열에 의미없은 슬라이싱(?)을 해도 인덱스 에러가 나지 않는 것을 활용해 숫자가 같으면
continue 아니면 리스트에 추가하는 방법으로 해결하였다.
728x90반응형LIST'프로그래밍 > 알고리즘' 카테고리의 다른 글
문자열 내 p와 y의 개수 (0) 2021.03.23 서울에서 김서방 찾기 (0) 2021.03.23 가운데 글자 가져오기 (0) 2021.03.23 문자열 다루기 기본 (0) 2021.03.22 2016년 (0) 2021.03.22