일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
29 | 30 | 31 |
Tags
- 43. Multiply Strings
- data science
- LeetCode
- 프로그래머스
- 밴픽
- 시바견
- Python Code
- 109. Convert Sorted List to Binary Search Tree
- Regular Expression
- iterator
- Python
- Python Implementation
- concurrency
- Decorator
- Convert Sorted List to Binary Search Tree
- Substring with Concatenation of All Words
- Protocol
- kaggle
- shiba
- 컴퓨터의 구조
- 315. Count of Smaller Numbers After Self
- 715. Range Module
- 파이썬
- DWG
- t1
- attribute
- 30. Substring with Concatenation of All Words
- Class
- Generator
- 운영체제
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 |