알고리즘/프로그래머스
[프로그래머스] 타겟넘버 파이썬 풀이 lv2
푸딩코딩
2023. 6. 23. 21:53
728x90
반응형
https://school.programmers.co.kr/learn/courses/30/lessons/43165
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
def solution(numbers, target):
cnt=0
def dfs(idx, sum):
if idx==len(numbers):
if(sum==target):
nonlocal cnt
cnt+=1
return
else:
dfs(idx+1, sum+ numbers[idx])
dfs(idx+1, sum- numbers[idx])
dfs(0,0)
return cnt
DFS란 깊이우탐색으로, 스택을 사용!
재귀문/visited확인 을 사용. 노드를 방문하는 문제일 때 사용하자
함수안에 함수가 선언되었고, 참조하기 위해 nonlocal선언했다.
https://pudingcoding.tistory.com/36
[python] nonlocal이란
중첩문 내부에서 조건문이나 함수를 사용할 때, 해당 중첩문의 상위 변수를 참조할 수 있는 선언문이다 global은 중첩문 외부를 참조하지만, nonlocal은 중첩문 내부의 상위 변수를 참조한다. http
pudingcoding.tistory.com
nonlocal에 대한 설명
728x90
반응형