Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- RN새로운아키텍쳐
- RN아키텍쳐
- react
- react-native
- axios
- animation
- no-permission-handler-detected
- RN업데이트
- react-native-image-picker
- rn
- hydration mismatch
- private-access-to-photos
- react native
- Promise
- CS
- 비동기
- React-Native업데이트
- named type
- ios
- react animation
- javascript
- react-native-permissions
- motion.div
- Throttle
- async
- Hash-table
- Swift
- promise.all
- await
- debounce
Archives
- Today
- Total
하루살이 개발일지
[Algorithm] 두 정수 사이의 합 - 프로그래머스 본문
프로그래머스 주소
https://school.programmers.co.kr/learn/courses/30/lessons/12912
문제 설명
두 정수 a, b가 주어졌을 때 a와 b 사이에 속한 모든 정수의 합을 리턴하는 함수, solution을 완성하세요.
예를 들어 a = 3, b = 5인 경우, 3 + 4 + 5 = 12이므로 12를 리턴합니다.
제한 조건
- a와 b가 같은 경우는 둘 중 아무 수나 리턴하세요.
- a와 b는 -10,000,000 이상 10,000,000 이하인 정수입니다.
- a와 b의 대소관계는 정해져있지 않습니다.
입출력 예
a b return
3 | 5 | 12 |
3 | 3 | 3 |
5 | 3 | 12 |
정답
const solution = (a, b) => {
let arr = [];
let lessNum = Math.min(a, b);
let biggerNum = Math.max(a, b);
for (let i = 0; i < biggerNum - lessNum + 1; i++) {
arr.push(i + lessNum);
}
return arr.reduce((a, c) => a + c, 0);
};
'알고리즘' 카테고리의 다른 글
[Algorithm] 없는 숫자 더하기 - 프로그래머스 (0) | 2023.06.18 |
---|---|
[Algorithm] 문자열을 정수로 바꾸기 (0) | 2023.06.18 |
[Algorithm] 가운데 글자 가져오기 - 프로그래머스 (0) | 2023.06.18 |
[Algorithm] 짝수와 홀수 - 프로그래머스 (0) | 2023.06.18 |
[Algorithm] 직사각형 별찍기 - 프로그래머스 (0) | 2023.06.18 |