Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 알고리즘
- C++
- UELOG
- moreeffectiveC++
- BFS
- UML관련
- 정렬
- stl
- 델리게이트
- UE4 커스텀로그
- unorder_map
- 정렬알고리즘
- 프로그래머스
- 애셋로드
- dataasset
- 람다
- 선택정렬
- 강참조
- 람다사용정렬
- 자료구조
- map
- 크리티컬섹션
- 언리얼가비지컬렉터
- enumasByue
- C++최적화
- 약참조
- 스마트포인터
- 구조적 바인딩
- UE_LOG
- 데이터애셋
Archives
- Today
- Total
기억을 위한 기록들
[프로그래머스 lv 2 ] - 가장 큰 수 본문
programmers.co.kr/learn/courses/30/lessons/42746?language=cpp
코딩테스트 연습 - 가장 큰 수
0 또는 양의 정수가 주어졌을 때, 정수를 이어 붙여 만들 수 있는 가장 큰 수를 알아내 주세요. 예를 들어, 주어진 정수가 [6, 10, 2]라면 [6102, 6210, 1062, 1026, 2610, 2106]를 만들 수 있고, 이중 가장 큰
programmers.co.kr
cmp 함수 사용
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
bool cmp(string a, string b) {
return a + b > b + a;
}
string solution(vector<int> numbers) {
string answer = "";
vector<string> temp;
for (auto num : numbers)
{
temp.push_back(to_string(num)); //string 변환 저장
}
sort(temp.begin(), temp.end(), cmp); //정렬하는데 더큰값이 먼저오게 ex) 3과 30이 있다면 330이 큰지 303이 큰지
if (temp.at(0) == "0")
{
return "0";
}
for (auto num : temp )
{
answer += num;
}
return answer;
}
cmp 함수대신 람다식 사용
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
string solution(vector<int> numbers) {
string answer = "";
vector<string> temp;
for (auto num : numbers)
{
temp.push_back(to_string(num)); //string 변환 저장
}
//정렬하는데 더큰값이 먼저오게
sort(temp.begin(), temp.end(), [](string a, string b){ return a + b > b + a;});
if (temp.at(0) == "0")
{
return "0";
}
for (auto num : temp)
{
answer += num;
}
return answer;
}
'Coding Test - cpp > Sort' 카테고리의 다른 글
[프로그래머스 lv 1 ] - 6주차 (0) | 2021.09.10 |
---|---|
[HackerRank/C++] Big Sorting (0) | 2021.07.01 |
[프로그래머스 lv 1 ] - K번째수 (0) | 2021.04.21 |
[백준 10867: 중복 빼고 정렬하기] - C++ (0) | 2021.03.24 |
[백준 2108: 통계학] - C++ (0) | 2021.03.15 |