새발블로그
[프로그래머스/C++] 수열과 구간 쿼리 본문
문제
https://school.programmers.co.kr/learn/courses/30/lessons/181924
풀이방법
swap() 함수를 사용하기 위해 algorithm 함수를 include했다
후에 범위 기반 for문을 이용해 swap 해주었다.
그런데 타입 추론이라는 게 있더라!!
c++ 타입 추론
auto
auto x = 10; //x의 타입은 int로 추론
decltype
인수로 지정한 표현식의 타입을 알아냄
double x = 10.0;
decltyple(x) y =9.0;
auto와 다르게, const 와 레퍼런스(&)를 제거하지 않는다.
for(const auto& query : queires)
해당 for 문을 이렇게 해도 무방하다는 뜻이다
풀이
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> arr, vector<vector<int>> queries) {
for (vector query : queries)
{
swap(arr[query[0]], arr[query[1]]);
}
return arr;
}
'Problem Solving > Programmers' 카테고리의 다른 글
| [프로그래머스/C++] 같은 숫자는 싫어 (0) | 2024.03.26 |
|---|---|
| [프로그래머스/C++] 폰켓몬 (0) | 2024.03.25 |
| [프로그래머스/C++] 수 조작하기2 (0) | 2024.03.07 |
| [프로그래머스/C++] 수 조작하기 1 (0) | 2024.03.06 |
| [프로그래머스/C++] 주사위 게임 2 (0) | 2024.03.05 |