일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Python Code
- Convert Sorted List to Binary Search Tree
- data science
- Protocol
- kaggle
- Python
- Class
- shiba
- 715. Range Module
- iterator
- 파이썬
- Python Implementation
- concurrency
- 밴픽
- Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- attribute
- 운영체제
- t1
- 315. Count of Smaller Numbers After Self
- Generator
- 프로그래머스
- 109. Convert Sorted List to Binary Search Tree
- DWG
- 43. Multiply Strings
- Regular Expression
- 컴퓨터의 구조
- LeetCode
- 시바견
- Decorator
Archives
- Today
- Total
Scribbling
[Programmers] 타겟 넘버 본문
https://school.programmers.co.kr/learn/courses/30/lessons/43165
1. Python
def solution(numbers, target):
ret = 0
def dfs(i, acc):
nonlocal ret, target
if i == len(numbers):
ret += acc == target
return
dfs(i+1, acc+numbers[i])
dfs(i+1, acc-numbers[i])
dfs(0, 0)
return ret
2. C++
int ret {0};
void dfs(const vector<int>& numbers, int target, int acc, int idx) {
if (idx == numbers.size()) {
ret += target == acc;
return;
}
dfs(numbers, target, acc+numbers[idx], idx+1);
dfs(numbers, target, acc-numbers[idx], idx+1);
}
int solution(vector<int> numbers, int target) {
dfs(numbers, target, 0, 0);
return ret;
}
'Computer Science > Algorithms & Data Structures' 카테고리의 다른 글
[Programmers] 게임 맵 최단거리 (0) | 2024.06.20 |
---|---|
[Programmers] 네트워크 (0) | 2024.06.20 |
[Programmers] 도둑질 (0) | 2024.06.18 |
[Programmers] 사칙연산 (0) | 2024.06.18 |
[Programmers] 등굣길 (0) | 2024.06.17 |