정렬에 있는 문제임
어떻게 풀어야 할지 몰라서 다른 사람들 풀이를 보게됨
class Solution {
fun solution(numbers: IntArray): String {
var answer = ""
numbers.sortedWith(Comparator({num1: Int, num2: Int -> "$num2$num1".compareTo("$num1$num2")})).forEach { answer += it }
if ("(0*)".toRegex().replace(answer, "").isEmpty()) {
answer = "0"
}
return answer
}
}
여기서 보면 sortedWith를 사용하면 Comparator을 사용하여 배열이 가능함.
1.sortedWith
Returns a list of all elements sorted according to the specified comparator.
2.compareTo
[Kotlin] 코틀린 연산자 오버로딩 #1 - 산술연산자, 비트연산자, equals, compareTo
이 글은 Kotlin In Action을 참고 하였습니다. 더욱 자세한 설명이나 예제는 직접 책을 구매하여 확인 하시기 바랍니다 코틀린에서는 특정 연산자의 역할을 함수로 정의할 수 있습니다. 이를 conventio
tourspace.tistory.com
a.compareTo(b) 이렇게 사용하는데 많은 타입에서 사용할 수 있다.
아래에서 확인 할 수 있듯이 output으로
-1 이면 a가 b보다 작다는 의미 (a<b)
1 이면 a가 b보다 크다는 의미 (a>b)
fun main() {
val a = intArrayOf(6, 10, 2 ,11 , 13)
println(a.sortedWith { i1, i2 -> (i1.compareTo(i2)) }) // [2, 6, 10, 11, 13]
println(a.sortedWith { i1, i2 -> ((i1%2).compareTo((i2%2))) }) // [6, 10, 2, 11, 13]
println(1.compareTo(2)) // -1
println(2.compareTo(1)) // 1
println("a".compareTo("b")) // -1
println("ab".compareTo("b")) // -1
println("ab".compareTo("ba")) // -1
println("b".compareTo("b")) // 0
println("b".compareTo("a")) // 1
}
Compares this object with the specified object for order. Returns zero if this object is equal to the specified other object, a negative number if it's less than other, or a positive number if it's greater than other.
※ String.compareTo 를 보다가 찾아낸건데
fun main() {
println("d".compareTo("e")) //-1
println("334".compareTo("339")) //-5
println("334".compareTo("339121323123123")) //-5
println("11".compareTo("11111")) //-3
println("1".compareTo("1")) //0
println("1324".compareTo("1322")) //2
}
1) 숫자 스트링 비교시에 길이가 같으면 차례대로 앞자리부터 차례대로 비교하다가 차이가 나는 자리에서 그 차이를 비교하고 값을 냄.
2) 작은 자릿수를 가진 숫자와 차례대로 비교하다가 작은 자릿수의 숫자가 끝날때까지 같은 수로 나열되었으면 그 뒤에 나타나는 수와 관계없이 몇자리 차이나는가 값을 나타냄
3. regex
ko.wikipedia.org/wiki/%EC%A0%95%EA%B7%9C_%ED%91%9C%ED%98%84%EC%8B%9D
정규 표현식 - 위키백과, 우리 모두의 백과사전
위키백과, 우리 모두의 백과사전. 노랑색 강조 부분은 다음 정규식을 사용했을 때 매치된 것이다. 정규 표현식(正規表現式, 영어: regular expression, 간단히 regexp[1] 또는 regex, rational expression)[2][3] 또
ko.wikipedia.org
이런걸 사용을 하는구나..
regex 사용하는 방법은 이걸 이용해서 연습
regexone.com/lesson/kleene_operators
RegexOne - Learn Regular Expressions - Lesson 7: Mr. Kleene, Mr. Kleene
Lesson 7: Mr. Kleene, Mr. Kleene A powerful concept in regular expressions is the ability to match an arbitrary number of characters. For example, imagine that you wrote a form that has a donation field that takes a numerical value in dollars. A wealthy us
regexone.com
4.padend // replace
fun main() {
val a = "wonjoon"
val b = "yoon"
println(a.padEnd(6,'?'))
println(b.padEnd(6,'?'))
//wonjoon
//yoon??
println(a.padEnd(6,'?').replace("?","!"))
println(b.padEnd(6,'?').replace("?","!"))
//wonjoon
//yoon!!
}
padend로 몇자리 까지 빈공간 있으면 지정해준 char로 변환 시켜준다.
그걸 replace를 하면 또 바꿀수 있음.
'코딩테스트(코틀린) 기초부터 연습' 카테고리의 다른 글
프로그래머스 디스크컨트롤러 힙(Heap) (0) | 2021.04.06 |
---|---|
프로그래머스 h-index문제 (정렬) (0) | 2021.04.06 |
코틀린에서 .map 사용.. 유용함 (0) | 2021.04.05 |
프로그래머스 다리를지나는 트럭 (0) | 2021.04.04 |
프로그래머스 해쉬문제lv3 (indices,take,flatten,forEachIndexed) (0) | 2021.04.04 |