자료구조 & 알고리즘/프로그래머스
Level 0. 세로 읽기
diesuki4
2023. 5. 21. 06:15
Level 0. 세로 읽기
C스타일 배열과 같이 copy() 함수를 통해 벡터에도 내부적으로 관리되는 배열에 메모리를 복사할 수 있다.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
string solution(string my_string, int m, int c)
{
string answer = "";
size_t n = my_string.length() / m;
vector<string> v(n, string(m, '\0'));
for (int i = 0; i < n; ++i)
copy(my_string.data() + i * m, my_string.data() + (i + 1) * m, &v[i][0]);
for (int i = 0; i < n; ++i)
answer += v[i][c - 1];
return answer;
}
for문을 돌 때 m씩 더하면 1행씩 순회하는 것 같은 효과를 줄 수도 있다.
#include <iostream>
using namespace std;
string solution(string my_string, int m, int c)
{
string answer = "";
for (int i = c - 1; i < my_string.length(); i += m)
answer += my_string[i];
return answer;
}