[Python] 프로그래머스 - Level1 - 같은 숫자는 싫어

강주형·2022년 8월 12일
0

https://school.programmers.co.kr/learn/courses/30/lessons/12906

스택/큐

def solution(arr):
    answer = [arr.pop(0)]
    for a in arr:
        if answer[-1] != a:
            answer.append(a)   
    return answer

생각보다 너무 오래 걸렸다..
효율성 테스트에서 계속 시간 초과가 떠서 고생함


타인 코드
def no_continuous(s):
    a = []
    for i in s:
        if a[-1:] == [i]: continue
        a.append(i)
    return a

빈 리스트에 [-1:] 해도 오류가 안 뜬다는 사실을 처음 알았다.

a = []
a[-1:]
[]

  1. 빈 리스트를 감안하고 마지막 원소를 뽑고 싶을 때는 [-1:]를 사용하자
profile
Statistics & Data Science

0개의 댓글