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 |
Tags
- C++
- backtracking #codetree #디버깅 #삼성코테
- 코딩
- c++ #입출력 #속도 #ios #sync_with_stdio #cin #cout #tie
- 22869
- 최소 #공배수 #최대 #공약수 #유클리드 #호제법 #lcm #gcd #c++ #boj #3343 #백준 #장미
- 진법변환 #2to10 #10to2 #이진법 #십진법 #변환 #bitset #c++
- c++ #boj #
- graph #최단경로
- 백준 #다익스트라 #dijkstra #9370 #c++
- graph
- boj #백준
- 줄어드는수
- 30870
- N번째큰수
- LIS #가장긴증가하는부분수열 #
- 레드아보
- BOJ
- 3D #Reconstruction #computer #vision #volume #metric #tsdf #kinect #fusion
- 이분탐색 #dp #11053
- 16202
- 투포인터 #백준 #boj #20922 #22862
- 3343
- hcpc
- 쌤쌤쌤
- 호반우 상인
- 사이클 없는 그래프
- 백준
- 20117
- 1174
Archives
- Today
- Total
hyunjin
[level 1] 문자열 내 맘대로 정렬하기 , string compare, pair, sort 본문
728x90
https://programmers.co.kr/learn/courses/30/lessons/12915?language=cpp
level1>연습문제>문자열 내 마음대로 정렬하기
첫 번째 방법
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
bool compare(pair <string, char> a, pair <string, char> b){
if(a.second == b.second ) return (a.first).compare(b.first)>0 ? false: true;
return a.second < b.second;
}
vector<string> solution(vector<string> strings, int n) {
vector<string> answer;
vector<pair<string,char>> temp;
for(string elem : strings){
temp.push_back( make_pair(elem , elem[n]) );
}
sort(temp.begin(),temp.end(),compare);
for(int i = 0 ; i < temp.size(); i++)
answer.push_back(temp[i].first);
return answer;
}
음 좀 더 간단하게 줄일 수 있는 방법을 다른 사람 풀이에서 찾았다.
문제 핵심 포인트인 sort compare 흐름은 같지만 temp를 사용하지 않고 바로 비교 할 수 있다.
두 번째 풀이
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
int i;
bool compare(string a, string b){
return a[i] == b[i] ? a < b: a[i] < b[i];
}
vector<string> solution(vector<string> strings, int n) {
vector<string> answer;
i = n;
answer.assign(strings.begin(), strings.end());
sort(answer.begin(),answer.end(),compare);
return answer;
}
string 자체 비교하는 것을 compare를 사용해야하는 줄 알았는데 부등호로도 바로 가능하다.
728x90
'알고리즘 연습 > 프로그래머스' 카테고리의 다른 글
[level 2] 카카오2020 문자열 압축 , substr (0) | 2020.07.20 |
---|---|
[level 1]다트 게임 (0) | 2020.07.20 |
[level 1] 같은 숫자는 싫어, iterator 선언 , unique 사용 (0) | 2020.07.13 |
[level 1] k 번째 수 , 2차 풀이 (0) | 2020.07.11 |
[level 2] 프린터 , queue, max_element=> 활용해 다시 풀어보기 (0) | 2020.07.11 |